[Unity - basic principles of shadow implementation]

foreword

This article is a brief introduction to the principle of shadow implementation in Unity, without involving the details of Shader implementation.

Article directory

1. Shadow realization principle
1.1 Simple explanation
1.2 Realization principle 2.
Shadow in Unity
2.1 Introduction to shadow mapping texture, depth texture, shadow texture
2.2 Shadow projection and reception

1. Implementation principle

1.1 Simple explanation

Assume that the image below is the content presented by the current camera without any shadows.
figure 1

The principle of shadow mapping is: sample the pixel in the image above, obtain the position of the pixel in the world space (corresponding to a point in the world space), and judge whether this point can be "seen" by the light source.

If the line between this point and the light source is not blocked by other objects, then this point is in the light.
If occluded, the point is in shadow. This point (pixel) needs to be shaded.
The result after traversing the pixels in the above figure and applying shadows is shown in the figure below.
insert image description here

1.2 Implementation principle

Regarding "whether there is an object occlusion between the point and the light source", we use a technology similar to the depth buffer to achieve it.
We set up an imaginary camera at the light position, and render the depth of the scene seen from the light,
and save these results to get the position of the shadow map texture (shadowmap)
light source, and the shadow map texture (shadowmap) obtained by the imaginary camera See image below
insert image description here
Each pixel of the shadowmap texture (shadowmap) can be remapped in world space and will tell you which pixel the light sees and how far that point is from the light source itself. In the above picture, if we sample the pixel P3/P2 in the shadow map texture (shadowmap), what we can actually get is the distance from the point P3 in the world space to the light source.

Let's sort out the whole process:
For a pixel in the camera perspective of P1 in the above picture, we sample this pixel to obtain its position in the world space (which can be regarded as a point), and then map this point to the shadow map texture ( shadowmap), get the corresponding pixel P1 in the shadow map texture (shadowmap), and then sample the point P1 in the shadow map texture (shadowmap), get the pixel seen by the light and the distance between the point and the light source itself. Both are equal and point P1 is not in the shade.
For the pixel P2 in the camera's perspective, the pixel P3/P2 is obtained after being mapped to the shadow map texture (shadowmap), and then the value obtained by sampling the pixel P3/P2 in the shadow map texture (shadowmap) is from P3 to the light source in world coordinates The distance is not equal to the distance from P2 to the light source in the world coordinate system. P2 is in the shadows.

2. Shadows in Unity

2.1 Introduction to Shadow Mapping Textures, Depth Textures, and Shadow Textures

In Chapter 9 of "Introduction to Unity Shader Essentials" about shadows, the author mentioned the following three textures:
1. The shadow mapping texture of the light source
2. The depth texture of the screen space
3. The shadow texture of the screen space
can be intuitively understood by pictures These three textures are conducive to subsequent understanding
First, the following is the experimental scene and final rendering effect in the book
insert image description here

1. The shadow map texture of the light source, that is, the shadowmap just mentioned
insert image description here
2. The depth texture of the screen space
already exists in this depth map (texture) in deferred rendering. If it is forward rendering, the entire scene needs to be rendered again. Render depth into a depth map (texture).
insert image description here
3. Screen space shadow texture
The left picture is the shadow texture, and the right picture is the final rendering effect.
The author explained this texture on github: The main idea of ​​​​obtaining this texture is to sample the fragment from the depth texture of the camera. Depth value, then use the matrix transformation to calculate the world coordinates of the world space corresponding to the point (using the CameraToWorld matrix), and then transform to the coordinates in the light source space (using the WorldToShadow matrix), and finally use this coordinate to calculate the shadow of the ShadowMap of the light source . Finally store all the results in a shadow texture.
The general idea of ​​calculating and obtaining the shadow texture is the same as that described in the first section. The difference is that the distance from the point to the light source in the world space is not used in the comparison, but the distance from the point to the light source in the light source space is used.

insert image description here

2.2 Shadow projection and reception

Obviously, shadow generation requires a light source, a shadow caster, and an object that receives shadows.
In the example in this section, the cube both casts shadows to the ground and receives shadows from the facade.
In Unity, we can control the casting/receiving shadows of objects through related options. As shown in the figure below, insert image description here
the author’s explanation of cast shadows/receive shadows in P198 in the book is roughly as follows: If the object casts shadow properties, Unity will add the object to the calculation of the shadow mapping texture of the light source. Other objects are affected by this object when they sample the shadow map texture. Take the picture in the first section as an example: if the sphere where P3 is located does not enable the cast shadow property, then the shadow circle shown
in the red box in the shadow map texture on the right will not exist , and the information stored in P3/P2 in the shadow map texture will be P2 The distance to the light source. After P2 is mapped to the shadow map texture during rendering, what is obtained by sampling the shadow map texture is no longer the distance from P3 to the light source, but the distance from P2 to the light source. Both are equal, and P2 will be judged to be out of shadow. There will no longer be shadows at point P2 in the screen space shadow texture. The image below demonstrates the effect of turning on/off the Cube's Cast Shadows option on the screen-space shadow texture in the Frame Debugger. On: Off: Turn off the shadow casting of the cube Final rendering result: For the reception of shadows, whether the object is turned on or not, it will not affect the calculation result of the shadow texture in the screen space. No more examples here.insert image description here

insert image description here

insert image description here

insert image description here

Reference Article 1: Supplementary Notes on Unity Shadows and Depth Textures (Important)
Reference Article 2: Delayed Rendering Shadow Mapping

Guess you like

Origin blog.csdn.net/m0_62316271/article/details/128515034