Using Stencils

The existing stencil sample is cool but I want to do something much simpler.

I want to have a monitor in my scene and I want the monitor screen to be a stencil mask into another world.

I can create the other world that exists inside the monitor and the stuff I put on that layer only shows inside the monitor.

But everything outside the monitor in the regular scene is also seen through the monitor as well.

How can I hide all the ‘regular’ scenery so it doesn’t show in the stencil area? In other words, how can I prevent everything rendered through the stencil mask from showing the default layer objects?

If I set the regular scenery to stencil cut or stencil outside, it doesn’t render at all.

Original Post on Discord

by user 563549194137239552

Maybe I can clarify my question a little. How does the needle engine interpret/translate the ‘Cut’ and ‘Content’ layers in the Unity project into GL stencil buffer states? Is there any magic to the ‘cut’ and ‘content’ layer names? How does needle engine know when you are drawing a stencil layer vs drawing the content that goes into that layer? Is there a specific .ts source file to look at to see how the render pipeline in the engine works with regard to stencils?

by user 563549194137239552

Hey, there’s no magic - do you have stencil layers setup as RenderObjects in your URP asset? That’s what gets exported and set appropriately on the threejs objects. The base Renderer class (in Renderer.ts) calls a method in the core that knows about the exported stencil settings and sets the correct flags.

I’ll take a look in the Renderer class

by user 563549194137239552

I was basically using the existing sample and it worked fine, so I assume they are set up as RenderObjects. I think i need to modify the render flags to not clear the stencil buffer when it draws the stencil objects

by user 563549194137239552

Yes exactly. I think all you would need to do is assign your objects to the correct layers in Unity (and maybe remove the stencil RenderObject features that you dont need since you only want one window in another world instead of 3)

I found the settings in the URP settings object. I need to find the right settings that do what i want in unity and then they will probably work in Needle engine… thanks!

by user 563549194137239552

@VRSpatialist yes that’s right. If it doesnt look right in Unity it wont look right on export. Have you tried to use the stencil sample URP settings? They define some layers and you would just need to put your objects in the right layers

I found the URP settings with the Render objects. I just need to spend some time messing with them to get the effect i want.

by user 563549194137239552