Is it possible to read the custom stencil buffer in C++?

Hello, guys. I’m making some sort of Fez’s physics, as you can see here [video]Vine.

What I’m doing right now is making a SceneCapture2D follow the main camera and render the custom stencil buffer with a postprocess shader. Then I read the texture, project the sphere’s position to screen coordinates and compare it to that texture to check for collisions, but, even if I use a half-size render target, it feels kinda stupid rerendering a buffer that the engine is already rendering. I mean, I’m following the camera with another camera to render a buffer that’s already been rendered just so I can read it, but I have not been able to find how to read the main camera stencil buffer.

So… Is this possible? Could somebody help me find how to read this buffer?

Thank you.

Not sure what you mean but there’s no such ‘Stencil Buffer’ for objects AFAIK, everything goes straight to the deferred renderer which renders the GBuffer’s, and the final image is composited from those.

You can render the sphere in a separate pass, known as Custom Depth in Unreal, which is kind of like a stencil. That might be what you want to look into?

No, sorry, that’s not what I mean. If you watch the video you’ll see I already have it working, but to read the custom stencil pass I need to use a rendertarget, so I’m rendering everything twice. What I want to know is if there is any way to read the G-Buffer so I can use the main rendertarget for this.

Thank you.

There’s no separate “stencil” buffer, it is “depthstencil” buffer. It is only accessible if main camera renderer has requested depth stencil format (like D24S8 or something).

Frankly, I wouldn’t do it. Reading stuff from buffer on cpu and especially accessing primary buffer won’t be very quick (because you’ll need to transfer data from video memory to system memory first), and while it may not result in noticeable bottleneck on modern system, I wouldn’t do it.

Instead I would try to modify colliders on surfaces. It is not like you need to to be able to collide with objects at arbitrary degrees/angles. You have side, front/back, top/bottom views, so you pretty much could just work with collision code or just enable/disable collision on separate platforms based on current scene angle.