Unity Shader實現(xiàn)2D水流效果
更新時間:2020年05月04日 06:47:53 作者:汐夜
這篇文章主要為大家詳細介紹了Unity Shader實現(xiàn)2D水流效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
水流的模擬主要運用了頂點變換和紋理動畫的結合;
頂點變換中,利用正弦函數(shù)模擬河流的大致形態(tài),例如波長,振幅等。
紋理動畫中,將紋理坐標朝某一方向持續(xù)滾動以形成流動的效果。
腳本如下:
Shader "MyUnlit/ScrollWater"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Color("Color Tint",color)=(1,1,1,1)
//控制水流波動的幅度,也就是三角函數(shù)中的振幅(值域范圍)
_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
{
//透明度混合:關閉深度寫入+設置混合狀態(tài)+禁用剔除(雙面渲染)
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);
//利用正弦函數(shù)模擬河流整體的形狀,最后乘以振幅
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(重復)
效果如下:

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C# Onnx CenterNet實現(xiàn)目標檢測的示例詳解
這篇文章主要為大家詳細介紹了C# Onnx CenterNet實現(xiàn)目標檢測的相關知識,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-12-12

