前言
上一节生成了岸边的泡沫。本节主要梳理水表面的上色过程,这部分涉及半透明渲染和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;
}