Sunday 13 January 2019

shadow

shadow map theory

shadow map is used to calculate the shadow in real time. 
There are two passes used in this technique

pass 1 , in vertex shader, a z depth is rendered through light view. Pixel shader returns 0. Normally this use render to texture technique, Unity will do it for us here.
pass 2 ,  find out if a pixel is in shader by comparing the distance between the pixel and light and the z depth rendered in 1st pass. Normally shadowmap created from 1st pass will be a sampled here.

shadow acne


one problem of shadow map is shadow acne. 
to calculate if a point is in shadow, it compare its distance to light and the light view depth map.
As the resolution of the light map is limited the camera render pixel can not fully align with lightmap pixel.  When the position request from camera is slightly in front of the light view depth (in fact they should be the same), it will create shadow in the middle of the lit area.


To fix it set the shadow bias

shadow render in Unity

  • shadow casting pass need to be add
LightMode need to be ShadowCaster

Include file "Shadow.cginc" and add the shadow case code in it.
vertex shader is all it needs for shadow caster pass, pixel shader will return 0

a basic process needed is transfer the vertex into clip space.
UnityClipSpaceShadowCasterPos(pos,nor); is used to support normal map
UnityApplyLinearShadowBias(pos); is used for fixing shadow acne
  • Shadow receive pass
multi compile the shadow mode in the light pass that cast shadow:
allocate the memory for shadow map in vertex shadow
SHADOW_COORDS(coordinate number)
try to get the UV coordinate for the shadow map in vertex shader
REANSFER_SHADOW(OUT)

the shadow will be add into the light automatically when using 
UNITY_LIGHT_ATTENUATION

No comments:

Post a Comment