Unity shadow (Shadow), Shadowmap

Unity shadow (Shadow)

In Unity, Shadow is a feature used to simulate mutual occlusion and lighting effects between objects in the scene. Shadows can add to the realism of a scene and visually provide a sense of depth and space.

Unity provides several shadow casting and receiving methods and techniques, including:

  1. Real-time Shadows: Use real-time calculations to generate shadow effects at runtime. There are two main types of real-time shadows in Unity:

    • Real-time Shadow Maps (Real-time Shadow Maps): By rendering the perspective of the light source into a shadow map, and then applying the map to objects in the scene, real-time shadow effects are achieved.
    • Real-time Shadow Projection: Calculate the shadow effect directly in the rendering process by calculating the intersection relationship between light and objects.
  2. Precomputed Shadows: Precomputed and stored shadow information in the editor, and then applied to objects in the scene at runtime. This method is suitable for static or less changing scenes, and can provide higher quality and more efficient shadow effects.

  3. Shadow Masks: Use shadow masking technology to optimize the performance of real-time shadows by creating specific mask objects in the scene to limit the casting range of shadows.

  4. Cascade Shadow Maps (Cascade Shadow Maps): Used in scenes with different close-range and long-range shadow casting effects, the shadows of distant objects are divided into multiple cascaded shadow maps to improve the quality and details of shadows.

When using the shadow effect, you need to make corresponding settings on the light source and the object receiving the shadow. Parameters such as intensity, softness, and resolution of the shadow can be adjusted to obtain the desired shadow effect.

It should be noted that the calculation and rendering of shadows are relatively resource-intensive operations. For scenes with high performance requirements, appropriate optimization and trade-offs need to be made, such as reducing the resolution of shadows, limiting the projection range of shadows, or using static pre-calculation methods.

All in all, Unity provides a variety of shadow technologies and options, and developers can choose the appropriate shadow solution according to project needs and performance requirements to enhance the realism and visual effects of the scene.

Unity Shadowmap

Shadowmap (shadow map) is a technique used in Unity to achieve real-time shadow effects.

The basic principle of Shadowmap is to render the scene from the perspective of the light source into a texture called Shadowmap. During rendering, record the depth value of each pixel from the light source. Then, during the rendering of the scene, the depth information in the Shadowmap is used to determine whether a pixel is in shadow. According to the depth comparison between the pixel and the light source, if the depth of the pixel is greater than the depth of the corresponding position in the Shadowmap, it means that the pixel is in the shadow.

Using Shadowmap requires the following steps:

  1. Set the light source: In Unity, you need to place an appropriate light source (such as Directional Light) in the scene and set its shadow-generating properties.

  2. Render Shadowmap: Under the position and direction of the light source, render the scene into the Shadowmap. This is usually done in a special Pass, only rendering depth information without color rendering. During rendering, it is necessary to bind the Shadowmap to the corresponding render target and use appropriate camera parameters (such as orthographic projection) to ensure accurate shadow projection.

  3. Shadow projection: During the rendering of the scene, the depth information in the Shadowmap is used to determine whether a pixel is in the shadow. By converting the world coordinates of the current pixel into Shadowmap texture coordinates, sampling the depth value from the Shadowmap, and comparing it with the depth value of the current pixel, it can be determined whether the pixel is in the shadow.

  4. Shadow sampling and filtering: To improve shadow quality and reduce jagged edges, shadow sampling and filtering are often required. This can be achieved by bilinear filtering, PCF (Percentage-Closer Filtering) or other advanced filtering techniques on the Shadowmap texture.

Shadowmap is a commonly used real-time shadow technology, widely used in game development, virtual reality and architectural visualization and other fields. It provides realistic shadow effects and has high performance on modern graphics hardware. However, it should be noted that in some cases, Shadowmap may have problems such as shadow distortion, jagged edges, or insufficient shadow map resolution, and developers need to tune and optimize according to specific situations.

Guess you like

Origin blog.csdn.net/qq_60125117/article/details/130723444