How to access the Stencil Buffer using C++? [Portals]

Hi guys,

I want to recreate this portal rendering in Unreal:

They made use of the stencil buffer to achieve the visuals.

I got a working C++ code which shows the concept with OpenGL: (in the description)
I studied and debugged the code to understand what’s happening.

Now I have to translate it into Unreal, but I don’t know where to start, since the Renderer in Unreal is really complex.

Example of what I want to achieve:

Video Example: (done in Unreal by GalaxyMan2015)

Please help me.

All the best

Any help would be apreciated. :slight_smile:

I’ve found out that you could use something called SetDepthStencilState via the RHI.
Could someone explain how to use it?
The documentation is non-existent… at least i couldn’t find one.

You don’t need a stencil buffer, you just need to use ScreenAlignedUVs on the portal’s material.

Check out my example here:

I go a step further, using a SceneCapture2D that matches the main viewport resolution and then mapping that RenderTarget to the portal using the Pixel-Perfect variant of the ScreenAlignedUVs node.

If your post process quality is turned all the way down (don’t know why it interferes in 4.9), you shouldn’t be able to detect the transition (well, I couldn’t using the stock FPS character controllers).

Thanks for pointing me to this thread.
I actually experimented with CustomDepth and CustomStencil but I never had the idea to put it on the actual mesh.

Also thanks for uploading your solution.
I’ll try it later and report back then!

Still interested in a stencil buffer solution.


Still looking for a solution :slight_smile:

Necro, looking to do this for vr