现实生活中看到的水面或者雾气都是存在交错起伏效果的。
像这样:
以上效果的关键在于屏幕特效Shader中,提取水面的法线贴图时,将uv的移动方向交叉移动,然后将采样得到的两个法线值叠加。
如下:
float2 speed = _Time.y * float2(_WaveXSpeed, _WaveYSpeed);
// Get the normal in tangent space
fixed3 bump1 = UnpackNormal(tex2D(_WaveMap, i.uv.zw + speed)).rgb;
fixed3 bump2 = UnpackNormal(tex2D(_WaveMap, i.uv.zw - speed)).rgb;
fixed3 bump = normalize(bump1+bump2);
// Compute the offset in tangent space
float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy;
i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy;
fixed3 refrCol = tex2D( _RefractionTex, i.scrPos.xy/i.scrPos.w).rgb;
如果是雾效,使用的是一张噪声图作为影响雾气密度。在提取时,不要使用normalize()。否则噪声效果就没了。
float2 speed = _Time.y * float2(_FogXSpeed, _FogYSpeed);
fixed3 noiseCol1 = tex2D(_NoiseTex, i.uv + speed ).rgb;
fixed3 noiseCol2 = tex2D(_NoiseTex, i.uv - speed ).rgb;
fixed3 noiseCol = (noiseCol1+noiseCol2); // normalize()了之后就没有效果了
float noise = (noiseCol.r - 0.5) * _NoiseAmount;
float fogDensity = (_FogEnd - worldPos.y) / (_FogEnd - _FogStart);
fogDensity = saturate(fogDensity * _FogDensity * (1 + noise));
如果不采取以上的方式,就只有法线和噪声单方向移动的效果,如下图所示:
以上项目和代码来自冯乐乐的《Unity Shader 入门精要》。
实现水面波纹或者雾的交错起伏效果




