Clear depth buffer of prior render group for a specific mesh

Hi,

i have a mapping application where i render babylonjs ontop of maplibre. Maplibre gives me the depthbuffer so i can hide i.e. objects behind a hill.

However, now I have a gltf model that should actually overwrite the depthbuffer since otherwise z-buffer issues appear. I am wondering if i could somehow clear the depthbuffer for a mesh in those places that are behind the current render group.

I have created a playground that simulates the issue.

The ground is on rendering group -1 (our map). And the spheres are on rendering group 0. Depth and Stencil clearing are turned off. This is what we want for the green mesh.

Now the red and blue meshes should be rendered ontop of the ground. I could do this by setting the depth function to ALWAYS, however then they overlap themselves. So now blue would appear infront of red:

What i would like to achieve though would look like this:

I faked this screenshot by setting depthFunction for blue to Default.

Now I guess using depthFunction is not suitable for my case. I am not even sure if there is anything suitable for my case.

On thought i had: a depthbuffer that would be Default + overwrite prior renderingGroup depth buffer. Would that make sense and would it be suitable?

Kind regards!

Here is a glimpse of my issue:

this might be a suitable approach:

introduce another renderingGroup that has clear depth stencil to false.
then first render with depthFunc GREATER for the engine and not writing to depth buffer. and then do the opposite with a normal render.

1 Like

Note that in your latest post you linked to the same PG as in your first post.

I tried to do it with a frame graph:

Graph: https://nrge.babylonjs.com/#NAON7K#1

Order of operations:

  • It renders the plane + green sphere normally
  • It renders the blue and red spheres over the previous texture, but with another depth/stencil texture attached. Stencil is enabled and 1 is written where the spheres are drawn
  • It renders the green sphere again over the previous texture, but with stencil check enabled: the green sphere is only rendered where the stencil is 1.

I don’t know if this is what you are after, but at least it seems to render as your reference screenshot!

1 Like

now it should be the correct playground. did not save properly.

wow frame graph is something new to me! the stencil approach seems very promising! the result looks correct to me. but i need to wrap my head around it…

bit of a different question:
is it possible to use this frame graph to render also shadow on a depth buffer? currently i am creating a transparent mesh over my maplibre map to render shadows. but it would be much nicer to use the depthbuffer to render the shadow on it.

I’m not sure what you mean? To generate shadows, a shadow map is created by a shadow generator, and when an object is rendered, we check against this shadow map to know if the current pixel is in shadow, and modulate the color accordingly.

yes this makes sense. however for maplibre i normally do not have a babylon object. so this would mean:

  • maplibre draws to the stencil and depth buffer, we can see the map
  • babylon continues drawing with this information. however shadows will not be drawn onto maplibre.

i get around this by creating a transparent terrain mesh, so that i have an object that i can render on.
but it would be nice, if i could just render onto the terrain itself. i guess that would be more a screenspace rendering process or having a quad in screenspace that is not rendering onto the depth buffer but onto colors.

anyways, i should probably rather open a separate topic for this.

In your case, I think it’s the best solution. You could also try Screen Space Shadows, but it’s limited to small range shadows, and it looks like there may be some artifacts if not coupled with a standard shadow mapping technique…

1 Like

maybe i stick to my mesh approach for now. it works so far. but the screen space approach seems interesting! :slight_smile: