Help with Stencil Buffer to crop objects

Hi there, on a high level, I’m trying to crop objects - I initially tried doing this with CSG, however the objects I’m loading are very complex so crash the browser, or produce artefacts due to some weird geometry. Unfortunately I won’t control the input models, so I can’t fix my models ahead of time.

So I’ve been trying to come up with a way of doing it with stencil buffers instead, but I’m not getting very far. Here’s a link to a playground i’ve been working on https://www.babylonjs-playground.com/#M3RNVC#3

The general idea is that I want to cut a segment out of the sphere using the cube (and dispose of the rest), however I also want it to obey the existing depth information from the ground map on renderGrouping=0.

So far I’ve managed to get it to cut the segment out, but that’s actually the bit I want to keep! I’ve also managed to retain the segment, but it doesn’t obey the depth information.

Does anyone have any ideas or can suggest a new direction for me to try?

Thanks!
Martin

Pinging @sebavan

This sounds easy but actually is not as much :slight_smile: I am still looking into it.

1 Like

Here is an easier way to do it :slight_smile: https://www.babylonjs-playground.com/#M3RNVC#8

1 Like

Thanks for taking a look - and I’m kinda glad that you didn’t find it straight forward either :sweat_smile:

Unfortunately your proposed solution won’t work for me in the final version - the actual stencil/masking object can be an arbitrary shape and the object to be masked may overlap on all sides - I’d just presented it as a cube to simplify it in the playground - sorry about that.

I’ll keep working at it in the meantime and try to explore using the onBeforeRenderObservable method you’ve used in your example

actually i’ve just had a look through your iterations (the playground really has some amazing features) and I think for my purposes #6 might work - or at least give me a good starting position. Will get back to you. https://www.babylonjs-playground.com/#M3RNVC#6

1 Like