This article mainly introduces a very simple soft shadow implementation method. The main method is similar to the shadowmapping of PCF, but it only adds a step more Gaussian blur to screenspace to implement the so-called soft edge shadow. The specific method is as follows:
First, the first pass is the same as the traditional shadow ing algorithm to obtain the minimum object depth in the light source space. To put it bluntly, the camera is moved to the position of the light source and rendered again to obtain the depth map, in this way, the depth from the scene to the light source is obtained. If you are using dx, you may need to write the depth into a rendertarget using the following method:
Output. Color = float4 (Pos. Z/POS. W, POS. Z/POS. W, POS. Z/POS. W, 1.0 );
Note that the depth here is not the final value in zbuffer, because it is not transformed by the view. It ranges from-1 to 1.
If OpenGL is used, it can be conveniently obtained through the depth buffer, but the value is transformed by the viewport, and its range is between 0 and 1.
The second pass pulls camera back to its original location. Then render the scenario normally. The conventional method of Shadow Mapping is to convert each pixel in the scenario to the light source space and compare it with the value of the Depth ing. If the conversion is greater than the depth value of the point at the same position, then it must be behind this point, and the light source cannot be taken, so this point is in the shadow, and vice versa, it is under the illumination of the light source. We can simply do this. If this point is in the middle of the shadow, the final color of this point will be multiplied by a coefficient from 0 to 1 to make it darker, for example, 0.5. The problem is that the shadow generated may be distorted. :
This is due to the accuracy of the Shadow texture. At this time, PCF was born, haha. To put it bluntly, we should not only compare the current pixel points, but also compare them with the surrounding vertices. In this way, the edges won't be so stiff and there won't be obvious serrations, but the actual situation is still unsatisfactory, because in a deep texture that is not very precise, PCF will produce annoying mosaic ,:
To solve this problem, we introduce a pass, that is, the Gaussian blur of screen-space:
We place the color of the second pass generation scene (without Shadow) in a texture, and the shadow value in the second texture.
Third pass: we blur the second texture, that is, the texture of the shadow value, so that the edge of the value does not have a large gradient, so that the gradient is 0.
Last pass: overlay the color texture and the texture after Gaussian blur to form the final effect:
Well, we have achieved ideal results. However, the shadow of screen-space has its own weakness, that is, it is always the screen-space technology and has nothing to do with the distance of the scene. That is to say, regardless of the vision, the Blur scale is so big, this is obviously not correct, but if you want to avoid it a little, it will have a good effect.
Hope this article is useful to you, if you have any questions, please mail to my mailbox: xingjinanmo@163.com, or add my QQ: 182322035