效果图:(贴图类似于泥石流)
代码:
1 Shader "CookbookShaders/Chapter02/ScrollingUVs" 2 { 3 Properties 4 { 5 _MainTint("Diffuse Tint", Color) = (1,1,1,1) 6 _MainTex("Base (RGB)", 2D) = "white" {} 7 _ScrollXSpeed("X Scroll Speed", Range(0, 10)) = 2 8 _ScrollYSpeed("Y Scroll Speed", Range(0, 10)) = 2 9 } 10 11 SubShader 12 { 13 Tags{ "RenderType" = "Opaque" } 14 LOD 200 15 16 CGPROGRAM 17 #pragma surface surf Lambert 18 19 fixed4 _MainTint; 20 fixed _ScrollXSpeed; 21 fixed _ScrollYSpeed; 22 sampler2D _MainTex; 23 24 struct Input 25 { 26 float2 uv_MainTex; 27 }; 28 29 void surf(Input IN, inout SurfaceOutput o) 30 { 31 //Create a seperate variable to store our uvs 32 //before we pass them to the tex2D() function 33 fixed2 scrolledUV = IN.uv_MainTex; 34 35 //Create variables that store the individual x and y 36 //components for the uv‘s scaled by time 37 fixed xScrollValue = _ScrollXSpeed * _Time; 38 fixed yScrollValue = _ScrollYSpeed * _Time; 39 40 //Apply the final uv offset 41 scrolledUV += fixed2(xScrollValue, yScrollValue); 42 43 //Apply textures and tint 44 half4 c = tex2D(_MainTex, scrolledUV); 45 o.Albedo = c.rgb * _MainTint; 46 o.Alpha = c.a; 47 } 48 ENDCG 49 } 50 FallBack "Diffuse" 51 }
注意:
内置方法 _Time
是个4维向量,跟Unity3D中的deltaTime(这是个一维的,数值)不同。
- float4 _Time : Time (t/20, t, t*2, t*3), use to animate things inside the shaders
时间: 2024-11-08 18:15:04