قالب وردپرس قالب وردپرس قالب فروشگاهی وردپرس وردپرس آموزش وردپرس

Command Buffers Explained in Unity

How Physics.autoSyncTransforms Works in Unity
How Physics.autoSyncTransforms Works in Unity
February 23, 2026
Scriptable Render Pipeline Basics
February 23, 2026

Command Buffers Explained in Unity

Command Buffers Explained in Unity

Most Unity developers use cameras, lights, and materials without ever touching Command Buffers. But under the hood, Unity’s rendering pipeline is built around commands sent to the GPU. Command Buffers allow you to inject your own rendering instructions directly into that pipeline.

If you want custom rendering effects, advanced post-processing, or fine control over when something is drawn, Command Buffers are a powerful tool.

What Is a Command Buffer?

A Command Buffer is a list of rendering commands that Unity executes at a specific point in the render pipeline.

Think of it as a custom set of GPU instructions you prepare in advance and tell Unity when to run.

You can:

  • Draw meshes manually
  • Blit textures
  • Render to custom RenderTextures
  • Override materials
  • Inject effects before or after certain camera events

Why Use Command Buffers?

Command Buffers are useful when:

  • You want custom outlines or glow effects
  • You need off-screen rendering
  • You are building custom post-processing
  • You need rendering that does not fit standard pipelines

They give you precise control over rendering order.

Basic Example: Creating a Command Buffer

Here’s a simple example that draws a mesh manually.

using UnityEngine;
using UnityEngine.Rendering;

public class SimpleCommandBufferExample : MonoBehaviour
{
    public Camera targetCamera;
    public Mesh mesh;
    public Material material;

    private CommandBuffer commandBuffer;

    void Start()
    {
        commandBuffer = new CommandBuffer();
        commandBuffer.name = "Draw Custom Mesh";

        commandBuffer.DrawMesh(
            mesh,
            Matrix4x4.identity,
            material
        );

        targetCamera.AddCommandBuffer(
            CameraEvent.AfterForwardOpaque,
            commandBuffer
        );
    }

    void OnDisable()
    {
        if (targetCamera != null && commandBuffer != null)
        {
            targetCamera.RemoveCommandBuffer(
                CameraEvent.AfterForwardOpaque,
                commandBuffer
            );
        }
    }
}

This script injects drawing instructions after opaque objects are rendered.

Understanding Camera Events

When attaching a Command Buffer to a camera, you must choose when it runs. Some common events include:

  • BeforeDepthTexture
  • BeforeForwardOpaque
  • AfterForwardOpaque
  • BeforeImageEffects
  • AfterImageEffects

Choosing the correct event determines how your effect interacts with other objects.

Example: Blitting a Texture

Blit copies a texture into another render target. This is common in post-processing.

commandBuffer.Blit(
    BuiltinRenderTextureType.CameraTarget,
    BuiltinRenderTextureType.CameraTarget,
    material
);

This applies a material effect to the camera output.

Rendering to a Custom RenderTexture

You can render to a separate texture for portals, mirrors, or special effects.

RenderTexture rt = new RenderTexture(512, 512, 16);

commandBuffer.SetRenderTarget(rt);
commandBuffer.ClearRenderTarget(true, true, Color.black);
commandBuffer.DrawMesh(mesh, Matrix4x4.identity, material);

This gives you full control over off-screen rendering.

Important: Clean Up Properly

If you forget to remove Command Buffers, they can stack or persist unexpectedly.

Always remove them in OnDisable or OnDestroy.

Command Buffers in SRP (URP/HDRP)

In Scriptable Render Pipeline (URP or HDRP), Command Buffers are used internally through ScriptableRenderPass.

Instead of attaching to CameraEvent directly, you extend the render pipeline.

In built-in pipeline, you attach directly to Camera or Light.

Performance Considerations

  • Every command adds GPU workload
  • Excessive custom rendering can reduce performance
  • Keep buffers minimal and efficient
  • Reuse buffers instead of creating new ones every frame

Common Mistakes

  • Not removing Command Buffers when disabling objects
  • Using wrong CameraEvent order
  • Creating new buffers every frame
  • Forgetting depth considerations

When Should You Use Command Buffers?

Use them when you need rendering control that standard materials and cameras cannot provide.

If a feature can be built using normal shaders and materials, prefer that first. Command Buffers are powerful but add complexity.

Final Thoughts

Command Buffers give you low-level access to Unity’s rendering pipeline without writing a full custom render pipeline. They are perfect for advanced visual effects, special rendering passes, and performance-optimized custom solutions.

Used carefully, they open the door to professional-grade rendering techniques inside Unity.

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar