[unity shader/stylized water surface rendering/basic notes] urp code version 03-water surface color

foreword


The previous section generated the foam on the shore. This section mainly sorts out the coloring process of the water surface, which involves translucent rendering and fresnel color

color


  • First of all, there should be a distinction between the colors on the shore _ShallowColand away from the shore _DeepCol. The previous section is still used here RawDepth, and after a little processing, it becomesWaterDepth
float waterDepth = _getNewDepthMask(RawDepth, _DepthRange);

insert image description here
As shown in Fig.
1, white is the shallow water part, and black is the deep water part. The result after using lerp is as follows
insert image description here

  • Second, add the color of fresnel
    insert image description here

This code is

                // =========Fresnel=========== //
                real4 base_col = lerp(_DeepCol, _ShallowCol, waterDepth);
                float F = _getfresnelMask(normalW, viewW);
                base_col = lerp(base_col, _FresnelCol, F);

translucent


Change the RenderType from opaque to translucent, after which the alpha channel of the color can change the transparency of the mesh. The same transparency is controlled by variables related to depth

        Tags {
    
    "RenderPipeline"="UniversalRenderPipeline"
            "RenderType"="Transparent" 
            "Queue" = "Transparent"
            "IgnoreProjector"="True"}
            
           Tags {
    
    "LightMode"="UniversalForward"}
           Blend SrcAlpha OneMinusSrcAlpha
           ZWrite Off

The ground was replaced with the desert texture that was done before
insert image description here

Join the shore foam from the previous section
insert image description here


The code of the current fragment part :

           real4 frag(v2f i) : SV_TARGET
           {
    
    
                // =========Get necessity=========== //
                float3 PosW = i.posW;
                float3 normalW = i.normalW;
                float3 viewW = i.viewDirW;

                float4 screenPos = i.scrPos / i.scrPos.w;
                // screenPos.z = (UNITY_NEAR_CLIP_VALUE >=0)?screenPos.z:screenPos.z* 0.5 + 0.5;
                float sceneRawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture,screenPos.xy);
                float3 worldPos = ComputeWorldSpacePosition(screenPos.xy, sceneRawDepth, UNITY_MATRIX_I_VP);
                float RawDepth = PosW.y-worldPos.y;

                float waterDepth = _getNewDepthMask(RawDepth, _DepthRange);

                // =========Foam=========== //
                float FoamDepth = smoothstep(_FoamBlend, 1, RawDepth);
                float wave_noise = SAMPLE_TEXTURE2D(_FoamNoiseMap, sampler_FoamNoiseMap, i.uv.xy).r
                                    *  _FoamNoiseStr;
               
                real4 sinWave = _getFoamWave(RawDepth, wave_noise) * _FoamCol;

                // =========Fresnel=========== //
                real4 base_col = lerp(_DeepCol, _ShallowCol, waterDepth);
                float F = _getfresnelMask(normalW, viewW);
                base_col = lerp(base_col, _FresnelCol, F);

                // =========Mix Results=========== //
                base_col = lerp(base_col, base_col+sinWave, sinWave.a);

                // return float4(base_col.xyz, 1);
                return base_col;
        
           }

Guess you like

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