iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🦔

Z-Buffering, Culling, Batching, and Instancing in Unity

に公開

Introduction

Overview and Usage of Z-Buffering, Culling, Batching, and Instancing

Unity provides multiple optimization techniques for rendering 3D and 2D scenes.
I have summarized the basic concepts of each technique and how to utilize them in Unity.


Z-Buffering (Depth Buffer)

Overview

A Z-buffer is a buffer that holds depth information from the camera for each pixel. This allows the system to compare depth values during rendering and display only the pixels closest to the camera, achieving correct hidden surface removal.

How to use in Unity

  • Camera Settings
    In Unity, the active range of the Z-buffer is defined by the "Near Clip Plane" and "Far Clip Plane" of each Camera component.
  • Shader Settings
    For transparent objects, specifying "ZWrite Off" allows the draw order to rely on the camera's transparency sorting.
  • Addressing Z-Fighting
    Z-fighting can be prevented by adjusting the clipping planes or the "Z-Spacing" settings.

Culling

Overview

Culling is a technique that excludes objects or polygons that are not visible on the screen from the rendering process. The main types include:

  • Frustum Culling
    Excludes objects outside the camera's view frustum.
  • Backface Culling
    Does not render the back side of polygons.
  • Occlusion Culling
    Excludes objects from rendering that are hidden behind other objects.

How to use in Unity

  • Frustum Culling
    Unity automatically renders only the objects within the camera's view frustum every frame.
  • Backface Culling
    Use "Cull Back" in the shader settings to avoid rendering the back faces.
  • Occlusion Culling
    Enable the "Occlusion Culling" feature in Unity to prevent rendering objects that are not visible to the camera.

Batching

Overview

Batching is a technique that reduces rendering load by processing multiple draw calls together, decreasing the communication frequency between the CPU and GPU. Unity offers the following three types of batching:

  • Static Batching
    Combines meshes of stationary objects to reduce the number of draw calls.
  • Dynamic Batching
    Groups draw calls for objects with small meshes.
  • SRP Batcher
    Optimizes rendering for materials using the same shader variants in the Scriptable Render Pipeline (SRP).

How to use in Unity

1. Static Batching

  • Configuration
    • Apply by checking "Static" in the GameObject Inspector.
    • Note that this may increase memory usage because meshes are combined.

2. Dynamic Batching

  • Conditions for Application
    • Applies if the mesh does not contain more than 900 vertex attributes and more than 300 vertices.

3. SRP Batcher

  • Configuration
    • For URP (Universal Render Pipeline)
      1. Select the URP Asset.
      2. Go to the InspectorAdvanced section → Enable SRP Batcher.
    • For HDRP (High Definition Render Pipeline)
      1. Select the HDRP Asset.
      2. Switch the Inspector to Debug mode and check/enable the SRP Batcher option.

Instancing

Overview

Instancing is a technique that renders objects with identical geometry and materials all at once.

How to use in Unity

  • Enabling GPU Instancing
    • By turning on "Enable GPU Instancing" in the material, Unity automatically instances identical meshes.
  • Utilizing Scripting APIs
    • You can manually control instancing by using APIs such as Graphics.DrawMeshInstanced.

References

Reference

https://docs.unity3d.com/ja/2023.2/Manual/CamerasOverview.html
https://docs.unity3d.com/ja/2023.2/Manual/SL-ZWrite.html
https://docs.unity3d.com/ja/current/Manual/OcclusionCulling.html
https://docs.unity3d.com/ja/current/Manual/DrawCallBatching.html
https://docs.unity3d.com/ja/2023.2/Manual/SRPBatcher.html
https://docs.unity3d.com/ja/2023.2/Manual/GPUInstancing.html

Discussion