Unity Shader實現2D水流效果
阿新 • • 發佈:2020-05-06
水流的模擬主要運用了頂點變換和紋理動畫的結合;
頂點變換中,利用正弦函式模擬河流的大致形態,例如波長,振幅等。
紋理動畫中,將紋理座標朝某一方向持續滾動以形成流動的效果。
指令碼如下:
Shader "MyUnlit/ScrollWater" { Properties { _MainTex ("Texture",2D) = "white" {} _Color("Color Tint",color)=(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); //利用正弦函式模擬河流整體的形狀,最後乘以振幅 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(重複)
效果如下:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。