[unity shader/stylized water surface rendering/basic notes] urp code version 06-simple wet bank simulation

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
insert image description here

  • Knowledge points involved
    -Depth Fade

new pass


This pass also uses GerstnerWavefunctions to transform the vertices, but the wavelength needs to be translated.
Then get the screen capture _CameraOpaqueTexturetexture and color it, the effect is as follows
insert image description here
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 RawDepthto deal with it. This chapter uses ** Depth Fade** to generate the following picture Mask, **Please note DepthFadethat 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
insert image description here

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);
            }

insert image description here

Guess you like

Origin blog.csdn.net/qq_43544518/article/details/128760443