去除Pancaking
这个在shadowcaster的顶点着色器中注意到:
OutputProperty vert(InputProperty inputProperty)
{
OutputProperty outputProperty;
UNITY_SETUP_INSTANCE_ID(inputProperty);
UNITY_TRANSFER_INSTANCE_ID(inputProperty, outputProperty);
float3 positionWS = TransformObjectToWorld(inputProperty.positionOS);
outputProperty.positionCS = TransformWorldToHClip(positionWS);
#if UNITY_REVERSED_Z
outputProperty.positionCS.z =
min(outputProperty.positionCS.z, outputProperty.positionCS.w * UNITY_NEAR_CLIP_VALUE);
#else
outputProperty.positionCS.z =
max(outputProperty.positionCS.z, outputProperty.positionCS.w * UNITY_NEAR_CLIP_VALUE);
#endif
outputProperty.baseUV = inputProperty.baseUV * _MainTex_ST.xy + _MainTex_ST.zw;
return outputProperty;
}
我们将顶点无条件的压到到近平面之前。防止在近平面和灯光直接有物体。
我们将物体最少移动到近平面之前,否则如果物体在绿色的体中,则会产生阴影,形成pancaking。
这是对平行光来说,需要这样的处理。但是对于点光源和聚光灯来说,不需要,因为这个两种光源都有一个位置属性。那么是允许物体出现在光源之后的。同时,对于这两种光源来说,使用透视投影,而不是正交投影,如果把所有的点都集中到近平面之前,那么则会出现过度的扭曲现象。所以对于点光源和聚光灯来说,要关闭这个处理。
参考这句话:
In the case of spot lights—which do have a position—shadow casters can end up partially behind the light’s position. As we’re using a perspective projection in this case, clamping vertices to the near plane would severely distort such shadows. So we should turn off clamping when pancaking isn’t appropriate.
又是脏的感觉 ,下面加点bias。