unity版本:2019.4.12f
urp版本:7.5.1
前言
Unity URP有一个很好的官方示例项目Boat Attack,我最近参考该项目实现了下水体渲染,所以水篇文章记录下。
波形
我是直接用了标准3d模型Plane作为水面,不过有用上曲面细分,关于曲面细分我有另写过文章(链接)。
波形的实现则使用了Gerstner波来进行叠加。如上图所示,Gerstner相较于正弦波波峰更尖锐而波谷更宽阔。关于Gerstner的相关文章很多,比如这一篇,这里就不详细展开了。
散射和吸收
对于水体的着色,则是生成两张Ramp图,表示不同深度下的散射(Scattering)和吸收(Absorption)的颜色。这里的深度指的是深度图在水面下的部分。
float UnderwaterDepthDistance(float2 screenPos, float3 positionVS)
{
float depthTex = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, sampler_ScreenTextures_linear_clamp, screenPos);
float depthCamera2End = LinearEyeDepth(depthTex, _ZBufferParams);
return depthCamera2End + positionVS.z;
}
half3 Scattering(half depth)
{
return SAMPLE_TEXTURE2D(_WaterRampMap, sampler_WaterRampMap, half2(depth, 0.375h)).rgb;
}
half3 Absorption(half depth)
{
return SAMPLE_TEXTURE2D(_WaterRampMap, sampler_WaterRampMap, half2(depth, 0.0h)).rgb;