I would like to obtain the vertex normals from a cube. I am raycasting downward from a Pawn onto a cube, getting the face, getting the vertices for the face, then fetching VertexTangentZ
for each vertex. My understanding is that this should return the vertex normals. However, I am getting [0, 0, 1]
for all vertices, which corresponds to the face normal vector. Why is this the case?
TL;DR - Why does VertexTangentZ
return the face normal vector and not the vertex normals?
The log shows the values returned from VertexTangentZ
for all three vertices of the hit face. The C++ code snippet is below.
// 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));