2016-07-31 44 views
1

我有這個着色器以給定的速度偏移紋理(模擬水流,熔岩,通常的東西)。它工作正常,除了正常的紋理,由於某些原因,即使我使用相同的代碼也不會抵消。任何人都能看到爲什麼當應用於對象時,主紋理會滾動,但法線保持在同一位置。如何抵消Unity着色器中的正常UV?

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

回答

1

它看起來法線貼圖實際上是旋轉的,但是因爲我已經將法線上的材質上的貼圖設置爲與主紋理上的貼圖不同,所以它移動得非常緩慢以至於看起來不動。

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

我複製粘貼你的代碼,而我的正常(凹凸)貼圖保持靜止,而紋理貼圖滾動。你確定凹凸貼圖也在移動嗎?你使用的是什麼版本的Unity? –

+0

我使用的是Unity 5.4.0f3(64位)專業版許可證,但我認爲這不是問題,可能是您的設置或視頻驅動程序有問題。請檢查這些代碼,因爲代碼確實會滾動兩個紋理。如果你想我可以爲你創建一個視頻,如果你認爲我在開玩笑:) –

+0

我不認爲你在開玩笑:) –

相關問題