Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Unity Shader combines Projector and Rendertexture to realize Real-time Shadow

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces how Unity Shader combines Projector and Rendertexture to achieve real-time shadow. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

When it comes to the realization of real-time shadow, it is generally easy to think of using ShadowMap to render the shadow by projecting the depth map of the light space and comparing the depth on the projected object to determine whether it is in the range of the shadow. The effect of a depth map projected on an object that receives shadows is shown in the figure:

Depth map projection

What you need to do is to render a depth texture in the light space, and cast it onto the object that accepts the shadow, and compare it with the depth of the corresponding pixel position on the object that accepts the shadow (light space) to determine whether the current pixel is in a shadow. In addition, you should also consider the accuracy of the depth map and the ZFighting caused by it, of course, this is not the focus of this article.

This paper mainly introduces a method to realize shadow by directly projecting the Rendertexture of the light space camera, and will combine it with projector later. Of course, developers who are familiar with both technologies should already know that using projector to achieve shadows means that you will consume extra drawcall. In fact, objects that are projected projector and do not use "IgnoreProjector" = "true" in shader will be rendered again with projector's shader after their own shader rendering (or before rendering, depending on the order of their own rendering queue and projector's shader rendering queue).

First of all, compare this technology with shadowmap technology, in fact, I feel that to a large extent, the two techniques are actually similar, both need to use screen projection, except that shadowmap projects a depth map (depth buffer), while this article introduces direct projection of screen texture (frame buffer), so the projected texture is with Alpha channel.

Note that the Alpha of the background color of the camera in the light space is 0, otherwise the background color will also be projected onto the object that receives shadows.

Unlike shadowmap, cameras in light space should only see objects that cast shadows:

The effect of the projection is roughly as shown in the figure:

Of course, one of the inevitable problems caused by casting rendertexture in this way is that the shadow-casting object will cause shadow penetration once the shadow-casting object penetrates the shadow-receiving object because it does not cast a shadow plane.

Shadow casting objects penetrate shadows to receive objects

The main implementation code of Shader for accepting shadow objects:

The worldToCameraMatrix,projMatrix where viewMatrix is the light space camera is the projection matrix of the light space camera.

Of course, the deficiency of realizing shadows in this way is that we need to know clearly the objects that cast shadows and those that receive shadows.

Next you will try to combine it with Projector, and note that as discussed earlier, using projector means additional drawcall, especially if there are many and all separate objects in the scene, which is not recommended. Of course, if only a very small number of objects in the scene need to accept shadows, such as only the main terrain, you can try this method, because with projector, you can easily add IgnoreProjector tags to shader to ignore the role of the projector, or directly modify the layers affected by projector on projector.

From the projector shader in the unity standard resource kit, we can roughly understand that two fourth-order matrices are needed in projector shader, namely _ Projector and _ ProjectorClip, in which the latter is mainly used for fade-in and fade-out of near-and far-cut faces, but is not necessary. For the _ Projector of the former, note that this matrix should be different from the projection matrix of the camera (although the camera and projector are similar in many parameters), because the official projector shader passes directly through: o.uvShadow = mul (_ Projector, vertex) The projection texture coordinates are calculated, which means that the _ Projector matrix should simultaneously convert vertex to world space, then to the local space of projector, and finally to the projection space of projector, so its property should be similar to that of UNITY_MATRIX_MVP matrix, so to use projector to achieve the effect of projecting rendertexture, you only need to add a script that creates a camera and uses the parameters of projector. And pass the rendertexture of this camera to the material of projector. The specific implementation is as follows:

Where _ FadeTex is a map that represents shadow attenuation, and its r and g channel effects are as follows:

This is the shadow effect after implementation:

Here, the shadow quality is degraded because the RT used is the size of the original camera.

In addition, since the Rendertexture with Alpha channel is projected, it means that it is easy to use fuzzy shader to complete the blurring effect. Here is the fuzzy script effect diagram written by myself:

About how Unity Shader combines Projector and Rendertexture to achieve real-time shadow sharing here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report