最终效果
代码
新建一个Plane或Quad放置在湖面上
Shader "Custom/RiverWaveShader"
{
Properties
{
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_NoiseTex("噪声贴图",2D) = "black"{}
}
SubShader
{
Tags { "Queue" = "TRANSPARENT+3" }
Pass{
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
fixed4 _Color;
sampler2D _MainTex;
sampler2D _NoiseTex;
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
struct a2v {
float4 vertex:POSITION;
float2 texcoord:TEXCOORD0;
};
struct v2f {
float4 pos:SV_POSITION;
float2 uv:TEXCOORD0;
};
v2f vert(a2v v) {
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
return o;
}
fixed4 frag(v2f i) :SV_Target{
//彩色噪声贴图用来改变边缘的造型(可以不用,不用的话就是圆形波纹)
fixed4 noiseTex = tex2D(_NoiseTex, i.uv*0.5 + fixed2(1,1)*_Time.x);
//将波纹的rgb分别提取出来,在时间上设置不同的偏移量,用于同时产生3种波纹
//我图片用的是个黑白图,可以根据需要换成彩色图,可以有更多波纹特性,也可以多次采样颜色通道来产生更多波纹
float waveT = _Time.y*0.8;
float2 clipOffSet_GB = float2(1.6, 3.2);//g和b分量的时间偏移
fixed clipR = 1 - frac(waveT);//动态生成1-0的值,用于跟颜色通道值比较,生成波纹圆圈用的
fixed clipG = 1 - frac(waveT+ clipOffSet_GB.x);
fixed clipB = 1 - frac(waveT+ clipOffSet_GB.y);
fixed moveRange = 0.15;//波纹移动速度
//三个通道的贴图采样,公式看着长,其实就是用噪声贴图和波纹移动来改变波纹的形态和位置
fixed4 tex_r = tex2D(_MainTex, i.uv + noiseTex.rg * 0.03 - fixed2((frac(waveT*0.25)-0.5)*moveRange, (frac(waveT*0.5)-0.5)*moveRange));
fixed4 tex_g = tex2D(_MainTex, i.uv + noiseTex.rg * 0.03 - fixed2((frac((waveT+ clipOffSet_GB.x)*0.25)-0.5)*moveRange, (frac((waveT + clipOffSet_GB.x)*0.5)-0.5)*moveRange));
fixed4 tex_b = tex2D(_MainTex, i.uv + noiseTex.rg * 0.03 - fixed2((frac((waveT+ clipOffSet_GB.y)*0.25)-0.5)*moveRange, (frac((waveT + clipOffSet_GB.y)*0.5)-0.5)*moveRange));
//计算每个通道的alpha值
fixed alpha_r = lerp(0, 1, pow(1 - abs(clipR - tex_r.r),15))*tex_r.r;
fixed alpha_g = lerp(0, 1, pow(1 - abs(clipG - tex_g.g),15))*tex_g.g;
fixed alpha_b = lerp(0, 1, pow(1 - abs(clipB - tex_b.b),15))*tex_b.b;
//输出最终效果
fixed4 c;
c.rgb = _Color.rgb;
c.a = (alpha_r + alpha_g + alpha_b)*_Color.a;
return c;
}
ENDCG
}
}
FallBack Off
}