Documentation

Unity Engine

Download Unity

Unity Engine

RTClearFlags

Flags that determine which render targets Unity clears when you use CommandBuffer.ClearRenderTarget.
Read time 2 minutesLast updated 3 hours ago

Definition

[Flags]public enum RTClearFlags

Remarks

You can combine flags by using the bitwise OR operator.
The following example clears a render texture with the selected flags, and displays the render texture on the material of the GameObject. Because the example runs the command buffer outside camera rendering, it works the same way in every render pipeline. Attach the script to a GameObject that has a Renderer component, for example a quad. In the Inspector window, select a Clear Mode, then enter Play mode.

Examples

using UnityEngine;using UnityEngine.Rendering;[RequireComponent(typeof(Renderer))]public class MyClearScript : MonoBehaviour{ public enum ClearMode { All, ColorStencil } public ClearMode m_ClearMode; RenderTexture m_RenderTexture; void Start() { // Create a render texture with a color buffer and a 24-bit depth and stencil buffer. m_RenderTexture = new RenderTexture(256, 256, 24); // Display the render texture on the GameObject's material. GetComponent<Renderer>().material.mainTexture = m_RenderTexture; CommandBuffer buffer = new CommandBuffer(); buffer.SetRenderTarget(m_RenderTexture); switch (m_ClearMode) { case ClearMode.All: // Clear the color, depth, and stencil buffers. Clear the stencil buffer to 0xF0. buffer.ClearRenderTarget(RTClearFlags.All, Color.red, 1.0f, 0xF0); break; case ClearMode.ColorStencil: // Clear only the color and stencil buffers. Clear the stencil buffer to 0xF0. buffer.ClearRenderTarget(RTClearFlags.Color | RTClearFlags.Stencil, Color.green, 1.0f, 0xF0); break; } // Run the command buffer immediately, then release it. Graphics.ExecuteCommandBuffer(buffer); buffer.Release(); } void OnDestroy() { if (m_RenderTexture != null) Destroy(m_RenderTexture); }}

Fields

Value

Description

AllClear all color render targets, the depth buffer, and the stencil buffer. This is equivalent to combining RTClearFlags.Color, RTClearFlags.Depth and RTClearFlags.Stencil.
ColorClear all color render targets.
Color0Clear color target 0.
Color1Clear color target 1.
Color2Clear color target 2.
Color3Clear color target 3.
Color4Clear color target 4.
Color5Clear color target 5.
Color6Clear color target 6.
Color7Clear color target 7.
ColorDepthClear both the color and the depth buffer. This is equivalent to combining RTClearFlags.Color and RTClearFlags.Depth.
ColorStencilClear both the color and the stencil buffer. This is equivalent to combining RTClearFlags.Color and RTClearFlags.Stencil.
DepthClear the depth buffer.
DepthStencilClear both the depth and the stencil buffer. This is equivalent to combining RTClearFlags.Depth and RTClearFlags.Stencil.
NoneDo not clear any render target.
StencilClear the stencil buffer.