Update mesh vertices from Water Material

Hello,

Does anyone have any pointers as to how I could update mesh vertices from the water material?

I need to be able to find the water height at any given position.

There is a thread from a year ago - but the solution suggested there only seems to work at 0,0 position?

The water material is not changing the vertex data, it is a customized shader.

I guess you could see the calculation the water shader does and implement it in JS? I have no better suggestion, but maybe the wonderful souls in this forum have a smarter suggestion

1 Like

That appears to be the way it has been solved before using the equation from the shader in JS - which works when looking at the water height at position 0,0 but seems to break elsewhere…

https://playground.babylonjs.com/#SBG0X8
In this example, if you move the sphere position, it will not be at the correct height of the water

The formula is correct if the sphere is located at one of the vertex of the water mesh.

As the water mesh is 512x512 and the subdivision is 32, then the formula is correct when x % 16 = 0 and z % 16 = 0 (% = modulus - 16x32=512).

For other positions, you will have to interpolate between the vertices surroundings your (x,z) location to get the right coordinates.

1 Like
object y = 
  map object x z to watermesh space .. P
  then find nearest 4 vertices (in watermesh space) of P
  then bilinear interp that 4 vertices

https://www.babylonjs-playground.com/#L76FB1#120

3 Likes

Very nice!

Amazing! Thank you all