foreword
The first 5 chapters have completed functions such as shore foam, water surface coloring, waves, and caustics. In this chapter, create a new pass to simulate a simple wet bank effect, that is, when the tide is low, the wet phenomenon on the bank
- Knowledge points involved
-Depth Fade
new pass
This pass also uses GerstnerWave
functions to transform the vertices, but the wavelength needs to be translated.
Then get the screen capture _CameraOpaqueTexture
texture and color it, the effect is as follows
Then you will find that the connection will be very stiff. This chapter mainly improves this. You
can use the results obtained in the first chapter RawDepth
to deal with it. This chapter uses ** Depth Fade
** to generate the following picture Mask, **Please note DepthFade
that it generally works in the Transparent mode where the depth write is turned off, because non-transparent objects will be occluded, so the vertex data under the occlusion of the object cannot be calculated. **Since the water surface is a translucent mesh, this method can be used
Obtain the depth difference of the observation space
After we sample the original _CameraDepthTexture
, use the ** LinearEyeDepth
** function, in fact, through the depth inversion to deduce z in the view space
float4 screenPos = i.scrPos / i.scrPos.w;
float sceneRawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture,screenPos.xy);
sceneRawDepth = LinearEyeDepth(sceneRawDepth, _ZBufferParams);
The occluded vertex z is subtracted from the z of the current mesh, and then the following processing is performed
float distanceDepth = abs((sceneRawDepth - LinearEyeDepth(screenPos.z , _ZBufferParams)) / ((_FadeDistance * 0.01 )));
distanceDepth = max((1.0 - distanceDepth), 0.0001);
float DepthInteraction = smoothstep(0, _InterSoftness, saturate(pow(distanceDepth, (_FadeSoftness * 0.1))));
DepthInteraction = 1- saturate( DepthInteraction);
Applied Depth Difference
Then, we apply DepthInteraction to alpha, and we get the following effect
real4 frag(v2f i) : SV_TARGET
{
float4 screenPos = i.scrPos / i.scrPos.w;
float sceneRawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture,screenPos.xy);
sceneRawDepth = LinearEyeDepth(sceneRawDepth, _ZBufferParams);
float distanceDepth = abs((sceneRawDepth - LinearEyeDepth(screenPos.z , _ZBufferParams)) / ((_FadeDistance * 0.01)));
distanceDepth = max((1.0 - distanceDepth), 0.0001);
float DepthInteraction = smoothstep(0, _InterSoftness, saturate( pow(distanceDepth , (_FadeSoftness * 0.1 ))));
DepthInteraction = 1- saturate( DepthInteraction);
real4 grab_col = SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, screenPos.xy) * _Wetness;
float alpha = lerp(0, 1, DepthInteraction);
return real4( grab_col.xyz, alpha);
}