I made a simple shader to make plane function as water. The plane I used has many faces. When I tried to implement buoyancy by checking the position of the nearest vertex of the plane (Ocean). The shader displaces the plane correctly, but when I access it through my script each vertex is still at the same position as the object.
for eg= if my plane’s y position is 114, each vertex’s y position is also 114.
I get the mesh by: meshFilter.mesh.
Do I need to displace my Plane through script? or is there a better way.
I am new to unity, Thanks in advance
This is expected behaviour. Vertex shaders don’t change the mesh, they only change how it is drawn.
I recommend replicating the vertex shader in C# but don’t update the mesh since that’s bad for performance (if your mesh is big). Instead, keep using a vertex shader and do the same in C# on a separate vertex array, not on the mesh itself.
I’d try to calculate the displacement in a compute shader and feed it to the shader responsible with displacement through a structuredbuffer. And depends on your implementation do other calculations on the compute shader as well. I am not sure how you displace your boat but if it involves a shader, overall process has a potential to improve your speed.
If compute shader approach is not available, it will be a good excuse to do cpu calculations with the jobs system.