I am creating a static mesh in Blender and importing into UE5. For using the mesh in my prototype game, it is important that the vertex normals be accessible once the asset is in the Unreal editor.
While in Blender the vertex normals display as expected (see screenshot). However, in Unreal when I access the vertex normals (VertexTangentZ
) I simply get the face normal at each vertex position I query. The C++ code for getting the vertex normals is below.
TL;DR - In Unreal, why am I unable to fetch vertex normals from a static mesh created in Blender?
// Get face index from raycast downward from Pawn to surface
int FaceIndex = HitResult.FaceIndex;
FTransform ComponentTransform = StaticMeshComp->GetComponentTransform();
FStaticMeshVertexBuffers* VertexBuffers = &StaticMesh->GetRenderData()->LODResources[0].VertexBuffers;
// Get buffer to query vertex position
FPositionVertexBuffer* PositionVertexBuffer = &VertexBuffers->PositionVertexBuffer;
// Get buffer to query vertex normals
FStaticMeshVertexBuffer* StaticMeshVertexBuffer = &VertexBuffers->StaticMeshVertexBuffer;
FIndexArrayView IndexBuffer = StaticMesh->GetRenderData()->LODResources[0].IndexBuffer.GetArrayView();
// Get position and vertex normal from vertex 1 of the triangle
uint32 index0 = IndexBuffer[FaceIndex * 3 + 0];
VertexPositions[0] = FVector(PositionVertexBuffer->VertexPosition(index0));
VertexNormals[0] = FVector(StaticMeshVertexBuffer->VertexTangentZ(index0));
// Get position and vertex normal from vertex 2 of the hit triangle
uint32 index1 = IndexBuffer[FaceIndex * 3 + 1];
VertexPositions[1] = FVector(PositionVertexBuffer->VertexPosition(index1));
VertexNormals[1] = FVector(StaticMeshVertexBuffer->VertexTangentZ(index1));
// Get position and vertex normal from vertex 3 of the hit triangle
uint32 index2 = IndexBuffer[FaceIndex * 3 + 2];
VertexPositions[2] = FVector(PositionVertexBuffer->VertexPosition(index2));
VertexNormals[2] = FVector(StaticMeshVertexBuffer->VertexTangentZ(index2));