Surface Shader水效果
使用表面着色器编写一个水体效果,包含深浅采样颜色,水的光照模型,法线流动,波浪效果,抓屏扰动的折射效果,菲涅尔效果。
渲染设置
设置渲染模式和渲染队列为透明,关闭深度写入。
Tags {
"RenderType"="Transparent" "Queue"="Transparent"}
Zwrite off
设置深浅颜色:深度贴图采样
首先在顶点修改函数中获取面片的屏幕空间坐标(作为采样深度图的UV)以及深度值。
void vert(inout appdata_full v, out Input i)
{
UNITY_INITIALIZE_OUTPUT(Input, i);
i.proj = ComputeScreenPos(UnityObjectToClipPos(v.vertex));
COMPUTE_EYEDEPTH(i.proj.z);
}
表面函数中采样地面深度,水体的深度 = 地面深度-面片深度。根据深度参数DepthRange调整深度范围,结合透明度调整。
half depth = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.proj)).r);
half deltaDepth = depth - IN.proj.z;
// Albedo comes from a texture tinted by color
fixed4 c = lerp(_WaterShallowColr, _WaterDeepColr, min(_DepthRange, deltaDepth)/ _DepthRange);
.......<