Hi everyone,
I’ve got a matrix of points, in my FixedUpdate I do some vertical displacement. I need to pass this vertex displacement to a plane mesh. Do you think is better to do via shader or just using the Mesh APIs?
In case your choice would be the shader one, can you address me with some suggestions?
It depends what you need out of it. Are you going to need to have things on the CPU side interact with this displaced mesh afterwards? If not and it’s just purely for visuals, then it will usually be better done on the GPU. Is your matrix of points a uniform grid that you can easily map to the grid of verts on the plane? Then it’s a very simple operation done in the vertex program of the shader.
If you do need to interact with the deformed plane, then quite likely it’ll be better to be done on the CPU using the newer Mesh APIs that allow you to manipulate native arrays and thus do it multi-threaded, or even use the Job System for it.
Thank you very much for your answer. I’m gonna do only visuals so I don’t need interactions. And yes, the matrix is a uniform grid that fits with the plane vertices. So can you address me in doing this operation in the vertex program?
My main question is how to make the indexing in the shader? It would be nice to have a Set(vec2, displacement_value) on C# side that sets the. single vertex in the shader.
You wouldn’t do it to each vertex from the C# side, you’d let the GPU handle it for all the vertices, which would be MUCH more efficient. To do it from C# side to GPU would require a compute shader and dispatch for each call, which would be very messy.
So, you would either bind a texture to your shader that has the deformation values, or some sort of StructuredBuffer, and use the ID to index from them.
Since my physic simulation requires a fixed time interval to be computed I thought the only way was to do it in the FixedUpdate() then after every cycle transfer the matrix to the shader. Can I do a fixed time computation shader side?
Physics simulation? Like a wave sim? You’d just update the data on the shader each fixed update and then the next time a frame renders it will use that updated data.
Or, you could use a compute shader to do your physics calculation, dispatching it every fixed update to update your vertex buffer for the mesh, which is then rendered by your shader. This avoids processing time spent transfer data to the GPU each update: GraphicsBuffer, Mesh vertices and Compute shaders
Yes, a wave sim. So how can I update the data on the shader each fixed update? Are you suggesting to make anyway the calculation in the FixedUpdate and then passing the vertex data to the shader or to move the calculation in the shader and there’s a way to make the shader update in a fixed time? I’m a bit confused
By using the material.SetBuffer command if using a StructuredBuffer bound to the shader or updating that texture that’s assigned to your material with your vertex deformation values.
You would do what I said above, in FixedUpdate.
So you calculate the displacement amount in FixedUpdate and store that info in the texture or buffer that’s assigned to the material. So it gets updated with that data each FixedUpdate. Then whenever Unity renders the object, it will have those newest values to displace the vertices with in the Vertex pass of the shader.
But, if you don’t need any interaction with your waves, like you mentioned, then it would be more ideal for you to just do the computations in a compute shader, keeping all the work/data on the GPU. You can call the computeshader.Dispatch() method in your FixedUpdate to only update the data each FixedUpdate.
Compute shaders are another discussion though which you’ll have to do some research on to convert your CPU based calculation to it.