Float on Water Material

Well, there’s a big hole in the boat, so it’s expected that water comes in :stuck_out_tongue:

To answer more seriously, you can use the stencil buffer but you would need to split the boat into two mesh parts, the “outside” and the “inside”.

https://playground.babylonjs.com/#MJSK67#3

This PG does not work fully as expected because the stencil buffer is set so that the water can’t be drawn over the boat. For it to work as expected, only the “inside” of the board should write into the stencil buffer, not the “outside”. Thay way, the water around the boat would correctly hide the boat when it is below the surface.

1 Like

Thank you so very much for your help. Would you mind explaining the purpose behind the stencil buffer ? Functions like “setRenderingAutoClearDepthStencil(0, true, true, true);” and “engine.setStencilFunction(BABYLON.Engine.NOTEQUAL);” sound like giberrish to me? I am still unsure on the significance of your approach.

Here’s a link for an explanation about the stencil buffer and its usage: LearnOpenGL - Stencil testing

After reading this you should be able to understand the code as the Babylonjs stencil functions are a direct mapping over the opengl stencil functions.

Don’t hesitate to ask questions if it’s still not clear, though.

Regarding the usage I make from the stencil buffer:

  1. render first the boat and write “1” in the stencil buffer where the boat is rendered
  2. render the water and setup the stencil buffer so that the water is not rendered when the stencil buffer is 1 for that pixel

As for setRenderingAutoClearDepthStencil(0, true, true, true), it means to clear the depth/stencil/color buffers before handling the objects with renderingGroupdId = 0 and scene.setRenderingAutoClearDepthStencil(1, false, false, false); means to NOT clear the depth/stencil/color buffers before handling the objects with renderingGroupdId = 1 (because I want to use the same buffers when handling objects with renderingGroupdId = 1). That’s because I set renderingGroupdId = 1 for the water, to be sure it is rendered after the boat (which has renderingGroupdId = 0). You could also make sure the water mesh is created after the boat mesh to have the boat being rendered first and avoid using different renderingGroupdId values.

That makes a little more sense. Thank you! :smiley:

hi master, when i scaling root mesh, water still fill the boat , Have I ignore something like update bounds ?

The problem is not with the scaling but with the fact that the boat can go below the sand ground because you changed the value 35 (line 96) to 38.

So, when the boat is rendered like this (before the water is rendered):
image

The part of the boat that is below the ground does not write to the stencil buffer because it is culled by the ground, so the water is allowed to show in this part.

1 Like

thanks, I have found the direction of modification

1 Like