如何在Unity着色器中偏移Normal UV?

时间:2016-07-31 21:49:16

标签: opengl unity3d shader

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

 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"
 }

2 个答案:

答案 0 :(得分:1)

看起来普通的地图实际上是在旋转,但是因为我在Normal上的材质上设置了与主要纹理上的平铺不同的平铺,所以它移动的速度非常慢,看起来是静止的。

答案 1 :(得分:0)

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

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"
}