- Home /
Buoyancy using shaders
hello! I'm currently working on a buoyancy system and I'm using shadergraph (just noise applied to a plane)to manipulate and deform my water from what I've seen it is very difficult to get (in my case) the height of waves (I'm using a floater system for my buoyancy so I only need the height of waves in the position of my floater).
I've looked into other vertex manipulation solutions, but I've found them to be too performance intensive. I've also looked into recreating the math from the shader inside a script but I've failed at that front is there (and if there is what way) a way to get the height of the waves?
additionally, does anyone know how to recreate the math from a shader inside a script and then get the height of waves under a position (I'm just using gradient noise and offsetting it and then applying it to a plane)?
Thanks for any help or advice!
Answer by Pangamini · Sep 04, 2020 at 03:56 PM
You could definitely recreate the same math in the script, but there's no definitive 'how to'. Just make sure that both functions are mathematically identical. Alternatively, you could render your waves to a texture, as a heightmap. Then download the texture to CPU using asynchronous readback and just use that value as a heightmap. Depending on how complex your wave math is and how many buoyant objects / samples you need, one or the other might perform better. I often do various non-graphics related stuff on the GPU, and the readback is really not expensive if done asynchronously (most of the cost of synchronous readbacks is waiting)
that sounds interesting! ill look into that, my wave math is only one gradient noise node and a tiling and offset node (+ some stuff to make that work correctly) and for now I'm thinking of just having one boat (so 3 - 4 buoyant objects) - I think that should be fine
if you're generating a heightmap to feed the shader then displace vertices, can't we also sample from the generated height map in C# and get the height of a certain position instead of trying to read back? I'll try that out.
Answer by BenWiller1989 · Sep 04, 2020 at 03:42 PM
Usually you do those thing with a shader. The GPU is far more suitable for that!
Your answer
![](https://koobas.hobune.stream/wayback/20220613011226im_/https://answers.unity.com/themes/thub/images/avi.jpg)