ShadowOnlyMateria Z-Fighting


I would like to avoid z-Fighting of the ShadowOnlyMaterial with another WebGL context.

My first option was to use zOffset on a ShadowOnlyMaterial, but it seems like it is not part of the shader.
At least it is not working in this playground:

Now I hoped I could just copy the shadow material into my repo and try working with the shader, but it seems it needs to be compiled first.

Would this be easy to add?

The other option I thought of, was setting autoClearDepthAndStencil to true and then set it per Rendering Group. Unfortunately, this won`t work. Not sure if it would help, if I could only clear stencil and not depth. I think that this is the better approach… (Babylon.js/packages/dev/core/src/scene.ts at 4a2533cd7d6c5810723e62a8eeeed95d8ab8dba0 · BabylonJS/Babylon.js · GitHub)

Kind regards!

Addind @sebavan but I see no issue to add support for zOffset

z offset is correctly apply as you can see here

This wont help with another context though cause it would be in a different canvas ???

I am not sure to fully understand if you render in the same canvas or layer canvases ???

Is z-offset working for you in the provided playground?
I am rendering into the same canvas onto a maplibre background and would like to see shadows on it.
But my ground is mixing with the maplibre ground. And offsetting does not lead to good results so far.

I am seeing this in the playground

and the spector capture is from there.

So it should be ok.

1 Like

Oh wow… you are right… sorry for wasting time! I am a bit confused, because I thought yesterday it was not working in my app. Probably I mistook the orange are as a z-fighting issue. Green is z-fighting. But orange is probably an area where the shadows is not being projected on.
Would you have an idea to deal with the green area besides using zOffset? Since I have to set it around -10 to avoid it.

Looks like I cannot open a PG at this moment for some reason but since seb figured zOffset is applied and working &…


My question would be: What did you set for the zOffset (units or factor?). Like what happens if you get closer or further with the cam?
You will eventually want to set both (units + factor) depending on the distance, size of scene and distance from cam.

Thanks for the hint!
It just does not seem to be easy to control the zOffset properly. Though zOffsetUnit might be better suitable if you look at the following playground. But both options change when camera is moving away from object. I guess that is because of the depth render precision?

More on zOffset: ZOffset explanation
Calculation is: zOffset x DZ + r x zOffsetUnits “where DZ is a measurement of the change in depth relative to the screen area of the polygon, and r is the smallest value that is guaranteed to produce a resolvable offset for a given implementation.”

1 Like

Ok using zOffsetUnits seems to be the way to go! Thanks a lot, learned a lot again! The other zoffset explanation thread also gave some nice insights, that zOffsetUnits are better for decals. And my shadow is like one.