Shadow ing has become the standard for 3D games. Traditional SM + PCF can achieve hard shadow in the daylight environment, but if you want to implement soft shadow generated by dim light, either the effect is too stiff (the number of samples is small) or the efficiency is low (the number of samples is large ). Therefore, more and more games are beginning to use the Soft Shadows that make full use of hardware features.Algorithm.
This section mainly summarizes the ideas and derivation processes of various soft shadow ing algorithms, and does not mention implementation details and details.CodeAnd try to write the demo when you are free.
This article is original content. For more information, see the source.
Standard shadow maps
The basic idea of shadow map: light view draws depth once, and then converts pixel coordinates to light space during camera view preview scenario, compare depth (depth of pixel is greater than depth of shadow map in the shadow area ). You can also directly perform post-processing on the depth map using screen space.
The traditional method can use percentage closer filtering to solve the problem of sawtooth. It is also very simple. After the pixel coordinates correspond to the past, compare the surrounding points and calculate the percentage of the shadow area. In addition, you can also use bilinear filter to smooth the shadow, which is similar to PCF. Compare the adjacent four points, and then perform interpolation based on the offset of Texel. It is worth noting that the current GPU supports the hardware shadow map. You can directly use the tex2dproj (u, v, Z, p) command to implement 2x2pcf + BF.
Perspective shadow maps & trapezoidal shadow maps
The two methods follow the basic idea of shadow map. To solve the shadow map precision problem, do some work on the transformation matrix of the Depth pass.
Variance shadow maps
VSM has a great idea. It can be said that soft shadow has been developing from VSM to hardware filter. First, it is also the depth pass of light view, but this time two values are written, one channel writes depth, and the other channel writes dep22. Then perform blur on the texture of the [depth, dep22. Intuitively, the result of blur is to take the average value (expectation value) of the points within a certain range, that is, after blur is complete, get a [E (depth), E (dep22.)] texture. With this texture, the variance can be calculated based on the variance formula V (depth) = E (depth1)-E (depth) 2.
Finally, according to Chebychev's inequality (Chebychev inequality)
P (x ≥ t) ≤ σ 2/(σ 2 + (t-μ) 2)In formula, σ 2 is the variance V (depth), and μ is the expected E (depth)
After the pixel coordinate is transformed to the depth after the light space, it is used to get the ratio of the points with a certain range greater than the depth t around a certain point (note that the cut-by-sleep inequality only produces a range ≤, the result Pmax on the right of the inequality is similar here ). It is actually a differential-level PCF value, so smooth shadows can be obtained very efficiently.
Unfortunately, in the region where the depth changes significantly (with a large variance), VSM has a defect (light bleeding ). Because according to the cherbihov inequality, in the case of a large variance, (t-μ) must be large enough to make the right side of the inequality tend to 0. So we have lvsm to solve this problem.
Layered variance shadow maps
In order to solve the light bleeding of VSM, the shadow map of VSM is layered, and the depth interval in each layer is controlled within a certain range, that is, the possible difference is reduced.
convolution shadow maps
CSM proposed a very good idea of graphics algorithms. The derivation process is gorgeous and involves several mathematical concepts: convolution and Fourier series. (Note that there is another CSM-cascaded shadow maps, which has different functions)
first, assume x cursor r 3 is the world coordinate of a point, D ( x ) is the distance from the point to the light source (that is, the depth of the light space ). P direction r 2 is the coordinate of the point corresponding to the shadow map, Z ( P ) it is the depth on the shadow map corresponding to this point. You can define a shadow test function:
S ( x ) = f (D ( x ), Z ( P ) when D> Z, S is 0, and vice versa is 1
in order to obtain soft shadow, we want to perform convolution of S, that is, we want to get:
S F ( x ) = Sigma W ( q ) f (D ( x ), Z ( P - q ) = [w * F (D ( x ), z)] ( P ) (Highlight 1)
The convolution theorem is used to obtain the result of the following formula, indicating that Soft Shadows can be obtained through convolution of Z ( P . (That is, you can use blur, hardware bilinear filter, and mipmap to achieve Soft Shadows)
[w * F (D ( x ), Z)] ( P ) = f (D ( x ), [W * z] ( P )
unfortunately, F (D ( x ), Z ( P ) is not a linear function (but a step function ), convolutional theorem cannot be applied to it. For this reason, CSM uses the numerical analysis method and Fourier series to approach the function f. (Highlight 2)
Although the derivation process of CSM is quite gorgeous, it has too many disadvantages (for example, the memory overhead is large, the amount of data to be output by shadow map pass is too large, and the bandwidth memory is too tight ), as a result, the actual use value is very low.
Exponential shadow maps
After understanding the principles of CSM, ESM is quite simple.