【问题标题】:How do I offset a Normal UV in a Unity shader?如何在 Unity 着色器中偏移法线 UV?
【发布时间】:2016-07-31 21:49:15
【问题描述】:

我有这个着色器,它以给定的速度偏移纹理(模拟水流、熔岩、通常的东西)。它工作正常,除了正常的纹理,由于某种原因,即使我使用相同的代码也不会偏移。谁能明白为什么?应用于对象时,主纹理会滚动,但法线保持在同一位置。

 Shader "mrpmorris/Scrolling shader" {
     Properties{
         _MainTint("Diffuse tint", Color) = (1,1,1,1)
         _MainTex("Base (RGB)", 2D) = "white" {}
         _NormalTex("Normap map", 2D) = "bump" {}
         _ScrollXSpeed("X scroll speed", Range(-10, 10)) = 0
         _ScrollYSpeed("Y scroll speed", Range(-10, 10)) = -0.4
     }
     SubShader{
         Tags { "RenderType" = "Opaque" }
         LOD 200

         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows

         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0

         fixed4 _MainTint;
         fixed _ScrollXSpeed;
         fixed _ScrollYSpeed;
         sampler2D _MainTex;
         sampler2D _NormalTex;

         struct Input {
             float2 uv_MainTex;
             float2 uv_NormalTex;
         };

         void surf(Input IN, inout SurfaceOutputStandard o) {
             fixed offsetX = _ScrollXSpeed * _Time;
             fixed offsetY = _ScrollYSpeed * _Time;
             fixed2 offsetUV = fixed2(offsetX, offsetY);

             fixed2 normalUV = IN.uv_NormalTex + offsetUV;
             fixed2 mainUV = IN.uv_MainTex + offsetUV;

             float4 normalPixel = tex2D(_NormalTex, normalUV);
             float3 n = UnpackNormal(normalPixel);
             float4 c = tex2D(_MainTex, mainUV);

             o.Albedo = c.rgb * _MainTint;
             o.Normal = n.xyz;
             o.Alpha = c.a;
         }
         ENDCG
     }
     FallBack "Diffuse"
 }

【问题讨论】:

    标签: opengl unity3d shader


    【解决方案1】:

    看起来法线贴图实际上是在旋转,但是因为我在法线上设置的材质平铺与主纹理上的平铺不同,所以它移动得非常缓慢,看起来是静止的。

    【讨论】:

    • 嗯...我没有注意到您的自我回答,所以我按要求为您制作了视频...请参阅下面对我的回答的评论。
    【解决方案2】:

    这对我有用,不知道为什么对你来说它不...顺便说一句,我会使用基本来获得更多控制,如下所示:

    Shader "Custom/ScrollingShader" {
        Properties{
            _Color("Color", Color) = (1,1,1,1)
            _MainTex("Albedo (RGB)", 2D) = "white" {}
            _NormalTex("Normap map", 2D) = "bump" {}
            _Glossiness("Smoothness", Range(0,1)) = 0.5
            _Metallic("Metallic", Range(0,1)) = 0.0
            _ScrollXSpeed("X scroll speed", Range(-10, 10)) = 0
            _ScrollYSpeed("Y scroll speed", Range(-10, 10)) = -0.4 }
            SubShader{
                Tags { "RenderType" = "Opaque" }
                LOD 200
    
                CGPROGRAM
            // Physically based Standard lighting model, and enable shadows on all light types
            #pragma surface surf Standard fullforwardshadows
    
            // Use shader model 3.0 target, to get nicer looking lighting
            #pragma target 3.0
    
            sampler2D _MainTex;
            sampler2D _NormalTex;
            fixed _ScrollXSpeed;
            fixed _ScrollYSpeed;
    
            struct Input {
                float2 uv_MainTex;
                float2 uv_NormalTex;
            };
    
            half _Glossiness;
            half _Metallic;
            fixed4 _Color;
    
            void surf(Input IN, inout SurfaceOutputStandard o) {
                fixed offsetX = _ScrollXSpeed * _Time;
                fixed offsetY = _ScrollYSpeed * _Time;
                fixed2 offsetUV = fixed2(offsetX, offsetY);
    
                fixed2 normalUV = IN.uv_NormalTex + offsetUV;
                fixed2 mainUV = IN.uv_MainTex + offsetUV;
    
                // Albedo comes from a texture tinted by color
                fixed4 c = tex2D(_MainTex, mainUV) * _Color;
                o.Albedo = c.rgb;
    
                float4 normalPixel = tex2D(_NormalTex, normalUV);
                float3 n = UnpackNormal(normalPixel);
                o.Normal = n.xyz;
    
                // Metallic and smoothness come from slider variables
                o.Metallic = _Metallic;
                o.Smoothness = _Glossiness;
                o.Alpha = c.a;
            }
            ENDCG
        }
            FallBack "Diffuse"
    }
    

    【讨论】:

    • 我复制粘贴了您的代码,当纹理贴图滚动时,我的法线(凹凸)贴图保持静止。你确定凹凸贴图也在移动吗?您使用的是哪个版本的 Unity?
    • 我使用的是 Unity 5.4.0f3(64 位)专业许可证,但我认为这不是问题所在,可能是您的设置或视频驱动程序有问题。请检查这些,因为代码确实会按预期滚动两种纹理。如果你愿意,我可以为你制作一个视频,以防你认为我在开玩笑:)
    • 我不认为你在开玩笑 :)
    • 其实我希望能看到视频,谢谢!
    猜你喜欢
    • 2019-02-10
    • 2015-06-27
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2020-09-14
    • 1970-01-01
    • 2018-06-23
    • 1970-01-01
    相关资源
    最近更新 更多