Hey guys, I figured this one out finally, initially inspired with the help of this guy’s post: Overlay camera not rendering plane with custom shader - Unity Engine - Unity Discussions
The meat of the solution is based around this tutorial: Impossible Geometry with Stencils in Unity URP
For example, let’s say we have a minimap as shown below
A bunch of stuff is flowing outside of it that we want to crop out. So what I do is I create a huge quad and place it in the sky over my entire game map so it covers the overhead camera’s view precisely where I would want it cropped to:
Then apply the following shader to the quad:
Shader "Custom/Mask"
{
Properties
{
[IntRange] _StencilID ("Stencil ID", Range(0, 255)) = 0
[Enum(Depth Only, 0, Show Color, 15)] _ColorMask ("Color Mask", Float) = 0
}
SubShader
{
Tags
{
"RenderType" = "Opaque"
"RenderPipeline" = "UniversalPipeline"
"Queue" = "Geometry"
}
Pass
{
ZWrite Off
Stencil
{
Ref [_StencilID]
Comp Always
Pass Replace
Fail Keep
}
ColorMask [_ColorMask]
}
}
}
Set its stencil ID to some value that isn’t 0, for example 1
Put the quad on its own Layer, for example a new layer called “StencilMask”
Next, go to your URP Asset and add a new item to the “Renderer List”, for example called “MinimapRenderer”
Now carefully configure it so the Layer Mask is only your Stencil, and then create a new “Render Objects”
feature that renders anything with a stencil value of 1.
Go to your overhead camera and set the “Renderer” to this new MinimapRenderer. You’re all done and you have a perfectly cropped overlay camera!
This works because the Stencil Value for everything is 0 by default. You are telling the renderer “Only show things with a Stencil Value of 1” which makes everything disappear. Then the shader on your Quad says “Set the stencil value of this entire region to 1”, so your renderer is able to see everything in the line of sight of that shader quad.
NOTE:
If you are rendering sprites that you also want to be affected by this, you need to duplicate this “Render Objects” feature and set the Filters → Queue to “Transparent” because it would seem sprite renderers are not considered part of the Opaque queue
– FYI
If you’re actually using this for a minimap like I am, then you will want to add further optimizations to improve the performance of your overlay camera, such as only rendering certain minimap-specific layers (e.g. some sprite layer and lower resolution map) rather than rendering everything. But that is unrelated to this discussion here.