Stencil Buffer is a technical term that is extremely important in rendering and optimizing game graphics (technical art). Explanation of crop shader technology that writes an arbitrary integer (mask value) for each pixel on the screen and draws objects only within the range of a specific shape.
Real-world analogy: Masking a ``cutout pattern (stencil board)'' used in spray art
A stencil buffer is a ``pattern (mask sheet)'' that is applied to the wall to draw a star shape in spray art. First, place the pattern against the wall and expose only the star-shaped part of the wall (writing to the stencil buffer). No matter what color spray (drawing object) you spray on top of it, the paint will only get on the star-shaped part of the pattern (pixels with matching stencil values), and the paint will never spill outside the frame. This allows you to draw and cut out extremely complex shapes.
Illustration: Stencil Buffer Infographic that clearly illustrates the basic processing flow and mechanism of (stencil buffer) in Japanese.
Detailed mechanism and operating principle
Write a specific ID (e.g. 1) to the stencil buffer with the portal's "frame (mask board)" shader, and set up a comparison test that "allows drawing only pixels with a stencil value of 1" in the portal's "contents" shader.