Documentation

Unity Engine

Download Unity

Unity Engine

Stencils

Use the stencil buffer to mask parts of the screen, in a hand-coded shader or in Shader Graph.
Read time 1 minuteLast updated 5 days ago

Important
The Built-In Render Pipeline is deprecated and will be made obsolete in a future release.
It remains supported, including bug fixes and maintenance, through the full Unity 6.7 LTS lifecycle.
Use the stencil buffer to control which pixels Unity draws, to hide or reveal parts of your project content for effects such as portals, mirrors, and cutaways.
You can check or write to the stencil buffer in a hand-coded shader in any render pipeline. In the Universal Render Pipeline (URP), you can also set stencil options in Shader Graph, without writing code.

Topic

Description

Introduction to the stencil bufferUnderstand what the stencil buffer is, how the stencil test works, and what to consider when you use stencils in URP.
Choose a masking approachCompare the ways to hide or reveal parts of objects, sprites, and UI elements in Unity, and choose the approach that suits your project.
Set stencil options in Shader GraphSet stencil options in a Lit or Unlit shader graph to check or write to the stencil buffer without writing shader code.

Render pipeline information

How you work with stencils depends on the render pipeline you use.

Feature name

Built-In Render Pipeline

Universal Render Pipeline (URP)

High Definition Render Pipeline (HDRP)

Custom Scriptable Render Pipeline (SRP)

StencilYes

Use the
Stencil
command
in a hand-coded shader.
Yes

Use the
Stencil
command in a hand-coded shader, or set stencil options in Shader Graph. Shader Graph stencil options are available only in URP.
Yes

Use the
Stencil
command in a hand-coded shader.
Yes

Use the
Stencil
command in a hand-coded shader.

Additional resources