水流的模拟主要运用了顶点变换和纹理动画的结合;
顶点变换中,利用正弦函数模拟河流的大致形态,例如波长,振幅等。
纹理动画中,将纹理坐标朝某一方向持续滚动以形成流动的效果。
脚本如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
Shader "MyUnlit/ScrollWater" { Properties { _MainTex ( "Texture" , 2D) = "white" {} _Color( "Color Tint" ,color)=(1,1,1,1) //控制水流波动的幅度,也就是三角函数中的振幅(值域范围) _Magnitude( "Distortion Magnitude" , float )=0.3 //控制周期的长度,值越大,周期越短,频率越高 _InvWaveLength( "Distortion Inserve Wave Length" , float )=1 //流动速度,用于纹理变换 _Speed( "Speed" , float )=0.1 } SubShader { //顶点动画需要禁用合P处理 Tags { "Queue" = "Transparent" "RenderType" = "Transparent" "IgnoreProjector" = "true" "DisableBatching" = "True" } Pass { //透明度混合:关闭深度写入+设置混合状态+禁用剔除(双面渲染) Tags{ "lightmode" = "forwardbase" } ZWrite off Blend SrcAlpha OneMinusSrcAlpha Cull off CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fog #include "UnityCG.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; }; struct v2f { float2 uv : TEXCOORD0; UNITY_FOG_COORDS(1) float4 vertex : SV_POSITION; }; sampler2D _MainTex; float4 _MainTex_ST; fixed4 _Color; float _Magnitude; float _InvWaveLength; float _Speed; v2f vert (appdata v) { v2f o; float4 offset; //这里的方向可以自己选择,这里选择偏移x方向,其他方向的偏移保持不变 offset.yzw = float3(0, 0, 0); //利用正弦函数模拟河流整体的形状,最后乘以振幅 offset.x = sin((v.vertex.x + v.vertex.y + v.vertex.z)*_InvWaveLength)*_Magnitude; o.vertex = UnityObjectToClipPos(v.vertex+offset); //对uv进行某一方向的滚动以模拟水流,这里选择v向 o.uv = TRANSFORM_TEX(v.uv, _MainTex); o.uv += float2(0.0, _Time.y*_Speed); UNITY_TRANSFER_FOG(o,o.vertex); return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); col.rgb *= _Color.rgb; UNITY_APPLY_FOG(i.fogCoord, col); return col; } ENDCG } } FallBack "Transparent/VertexLit" } |
P.S.需要把纹理的导入设置改为Repeat(重复)
效果如下:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://www.cnblogs.com/koshio0219/p/11121969.html