Render only part of a object that is inside a box

Hi,

I want to render only the part of the object that is overlapping with a box or other object.
What would be the best practice for that? Any ideas are helpful because I have no clue at all.
I tried searching for a method online and couldn’t find anything.

Use a “custom stencil buffer.”
Render into the stencil where the box is.
Render the object with a shader that clips to the stencil.

There’s a reasonable video from Epic here:

2 Likes

Hi,

Thanks for your answer, the video helped a lot with understanding the custom stencil buffer.
I’ve still got a small problem, with rendering only the part of the object.
Because currently a part of the object can also be viewed if it’s behind the box.
Are there some more documents / videos about rendering into the stencil where the box is and then rendering the object with a shader?
Thanks for the help!

The problem with wanting “non-physical” rendering modes, is that all the help that the standard graphics pipeline gives you, disappears, and you have to start emulating it yourself, which adds a bunch of restrictions.

I don’t know off-hand how to best achieve an effect where things inside the box render, but behind the box, do not. I’m going to assume you have something like a “ghost detector box,” where some kind of character (“ghost”) is visible only when it’s within the bounds of that box.

If I had to try it right now, I’d probably try something like:

  1. Make a version of the box geometry that is only the back side of it. Perhaps make this the box, but with flipped normals? Make that box black.
  2. Set up a totally separate render target. For ease, you can make this the size of the screen, but you’d probably want to reduce its size and clip the camera (off-center projection!) to tightly fit the box into the target.
  3. Render the inverted box to the depth buffer.
  4. Render your thing-that-is-made-visible to the same render target, with depth test (so it doesn’t render behind the box) and clipped to the box.
  5. Render the front of the inverted box, but invert the sense of the depth test – render only the pixels that are behind where the exposed object rendered. This means you only render parts that are inside the box. HOWEVER, this also means that you will get holes cut in the character, which may not be what you want. A real “X-ray slicer” would have to render innards of a body here.
  6. Now, map this render target as a texture on the actual box in the real scene. Set it up as unlit, and make the texture project in screen space 1:1 with how you rendered it in the render target.

This has some chance of working, assuming you can arrange the pipeline states Just So (tm,) although it may depend on resolving the offscreen render target synchronously which may end up costing GPU parallelism, maybe? Also, I haven’t messed with the depth test modes so I don’t know how easy it’d be to arrange.

An alternative to 5 might be to have a shader that clips the character to the box in a shader instead based on some kind of depth input, rather than inverting the depth test. And I still think the real challenge is actually making it look good when the “ghost” penetrates the FRONT side of the box, so they’re halfway in the box (with their backside) and halfway in front of the box.

If the box has a well-defined simple shape (like, a real box) you may be able to do math in the shader, and discard fragments that aren’t within the box, instead. That might be simpler – just render it all forwards, but with pixel discard based on a pixel-in-box calculation.