前言
前5章完成了岸边泡沫、水表面上色、波浪、焦散等功能。本章新建一个pass来模拟简单的湿岸效果,也就是退潮时,岸的潮湿现象
- 涉及的知识点
-Depth Fade
新建pass
这个pass同样使用GerstnerWave
函数来改变顶点,但波长需要平移。
然后获取抓屏_CameraOpaqueTexture
贴图,并调色,效果如下
然后会发现衔接处会非常生硬,本章主要对这个做出改进
可以使用第一章所得出的RawDepth
来处理,本章则使用**Depth Fade
**来生成下图的Mask,**请注意DepthFade
一般在深度写入关闭的Transparent模式起作用,因为非透明物体会遮挡,从而无法计算被物体遮挡下的顶点数据。**由于水面就是半透明的mesh,所以可以用这个方法
获得观察空间的深度差
在我们采样原来的_CameraDepthTexture
后,使用**LinearEyeDepth
**函数,其实就是通过深度反推出视图空间下z
float4 screenPos = i.scrPos / i.scrPos.w;
float sceneRawDepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_CameraDepthTexture,screenPos.xy);
sceneRawDepth = LinearEyeDepth(sceneRawDepth, _ZBufferParams);
被遮挡的顶点z与当前mesh的z相减,然后做以下处理
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);
应用深度差
然后,我们将DepthInteraction应用于alpha,会得到如下效果
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);
}