【unity shader/风格化水表面渲染/基础笔记】urp代码版03-水表面颜色

前言


上一节生成了岸边的泡沫。本节主要梳理水表面的上色过程,这部分涉及半透明渲染和fresnel颜色

颜色


  • 首先,岸边_ShallowCol和远离岸边_DeepCol的颜色应该有所区分。这里依旧利用上一节的RawDepth,稍加处理后变为WaterDepth
float waterDepth = _getNewDepthMask(RawDepth, _DepthRange);

在这里插入图片描述
fig1
图1所示,白色为浅水部分,黑色为深水部分。用lerp后的结果如下
在这里插入图片描述

  • 其次,加入fresnel的颜色
    在这里插入图片描述

这段代码为

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

半透明


将RenderType从不透明模式改为半透明模式,之后,颜色的alpha通道能够改变mesh的透明度。同样透明度有depth相关的变量控制

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

地面换上了之前所做的沙漠纹理
在这里插入图片描述

加入上一节的岸边泡沫
在这里插入图片描述


目前的片元部分的代码

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

猜你喜欢

转载自blog.csdn.net/qq_43544518/article/details/128757669