Unity real-time shadow technology (shadowMap)

real-time shadow technology

1.Planar Shadow

2.Projector Shadow

3.ShadowMap(CSM ,PCF)

1.Planar Shadow

The principle is to flatten the model and draw it on the object that needs to receive shadows. This method is very efficient and consumes very little. For the specific implementation process, refer to Unity Shader - Planar Shadow - Planar Shadow . According to my own understanding, it is actually to calculate the projection position of the fragment on the plane receiving the shadow according to the light direction, and then draw it. This method is only suitable for drawing shadows on the plane.

2.Projector Shadow

For the implementation principle of Projector Shadow, please refer to the article Unity - Projector - Real-time Shadow . Here is a brief summary.
Projector Shadow is a common way to implement real-time shadows. Its basic principle is to render objects that need to display shadows to a RenderTexture (RT) through the camera, record the color value of the object (can be set to a custom color), and RT is associated with the material of the Projector component; then the object that needs to receive the shadow is rendered again with the material of the Projector component through the Projector component to realize the display of the shadow.

3. Shadow mapping texture - ShadowMap (CSM, PCF)

To describe the principle of ShadowMap in one sentence, the place where the light source cannot see is the shadow.
When rendering the scene, use LightMode to render a ShadowMap (essentially a depth map) for the Pass of ShadowCaster. The rendering target of this Pass is not the frame buffer, but the ShadowMap.

ShadowMap of the light source
ShadowMap of the light source

  • In the traditional ShadowMap implementation, the vertex position is transformed into the light source space in the normal rendering Pass, and its xy component is used to sample the ShadowMap, and the obtained depth is compared with the depth information of the vertex (obtained from the z component). , if less than , the point is in the shadow.

  • Unity5 uses the screen-space shadow mapping technology (ScreenSpace ShadowMap). In the pass where the Light Mode is ShadowCaster, the depth texture of the camera and the ShadowMap of the light source are obtained respectively, so as to obtain a shadow map of the screen space. Through this shadow map Sampling, you can get the corresponding shadow effect. (Specifically, the world coordinates of the corresponding fragment are obtained from the depth value in the camera depth texture, and then its coordinates are converted from the world space to the light source space, compared with the depth in the light source's ShadowMap, and then whether the fragment is located in the shadows.)

  • Disadvantage 1. In a larger scene, using the same precision ShadowMap, the near-end and far-end objects of the camera have the same sampling accuracy for the shadow map, which will lead to insufficient sampling accuracy of near-end objects and waste of sampling accuracy of far-end objects. 2. Since the algorithm of ShadowMap only compares the depth value very violently, the shadow effect obtained is relatively blunt, with blunt edges and jagged edges.

  • CSM Cascaded Shadow Mapping (Cascaded Shadow Mapping)
    CSM divides the viewing cone of the camera into several levels according to the distance, and the depth information of objects at each level is drawn into the shadow maps of each level, and the corresponding shadow maps are sampled when displaying. . For specific implementation, please refer to Unity real-time shadow implementation——Cascaded Shadow Mapping

  • PCF soft shadows In hard shadows, it is mentioned that the depth of the point is compared with the depth of the depth map of the light source. If there is a mismatch, it means that there is occlusion (a shadow area), otherwise it is a visible area. Here we not only find the depth of the point, but also find the depth of the points around the point on the shadow map.
    1. Assume that a 3X3 area is taken here.
    2. Compare the depth of each point in the area with the depth of the point (center point/projected point) (the value after each comparison is 1 or 0, 1 is less than the depth of the point (visible), 0 is a depth greater than that point (not visible)).
    3. After the comparison, the average of the compared values ​​is 0.667.
    For more implementations of PCF soft shadows, please refer to Soft Shadows (PCF, PCSS)

  • Unique Character Shadows This technology is mainly used for the shadow rendering of the character's bangs, clothes, etc. These shadows have high quality requirements, and generally a ShadowMap is rendered separately for the character.insert image description here

  • Static per-object shadow (Per Object ShadowMap) is used for objects that do not need to change the shadow in real time, and a ShadowMap is stored separately for the object. Implemented in Unity's HDRP pipeline.

Supongo que te gusta

Origin blog.csdn.net/qq_53057269/article/details/127439110
Recomendado
Clasificación