Recently I’ve managed to deform a mesh through a compute shader. Although there are many approaches, I used the VertexBuffer of a StaticMesh to “directly” deform the mesh.
This is how I get the vertex buffer with the positions:
UStaticMeshComponent* mesh_comp = (UStaticMeshComponent*)RootComponent->GetChildComponent(0); FStaticMeshRenderData* render_data = mesh_comp->StaticMesh->RenderData; FVertexBufferRHIRef vertex_buffer = render_data->LODResources.PositionVertexBuffer.VertexBufferRHI;
I can then use the RHI vertex buffer to do whatever. I successfully managed to deform a mesh.
My next challenge would be to update the deformed mesh normals.
I’ve read that it’s possible to recalculate the mesh normals with some engine functions, but it seemed by looking at the ource that it only worked within the editor, which would not do for me.
Since that approach didn’t work, I was confident that I could also place the buffer with the mesh normals in the compute shader and process it there. The process I used is basically the same I had used for the vertices’ positions, only in this case the buffer I’d have to use is simply called VertexBuffer.
FVertexBufferRHIRef vertex_buffer_nrml = render_data->LODResources.VertexBuffer.VertexBufferRHI;
Problem is that, somehow, the normals are weirdly codified into its buffer, because the same buffer supposedly has both TangentZ (normals) and TangentX. The values that shown up (if I check them directly) are nonsense (stuff like huuuge numbers or #QNANO).
Is there a way to only get the normals from the static mesh, assuming I’m not getting this all wrong? I was thinking that it would have something to do with the struct offset in the buffer. but I fiddled with it and got to no conclusion (talking about using STRUCT_OFFSET macro in RHILockVertexBuffer, for example).
I can be a bit more specific and post a little more code if necessary.
In any case if there’s a really simple way to recalculate a mesh’s normals dynamically that I haven’t heard of, please do let me know!
Many thanks in advance!