Getting vertex from FaceIndex on SkeletalMeshComponent?

Hi all,

I’m trying to retrieve the vertices of a triangle on a skeletal mesh. I’ve managed to get the FaceIndex using a LineTrace, but I couldn’t find out how to convert this FaceIndex to the actual vertices indexes.

Here is my current attempt to retrieve the vertexes :



bool GetTriangleVertex(const USkeletalMeshComponent* component, const int& faceIndex, FVector& v0, FVector& v1, FVector& v2)
{
	if (!component || !component->SkeletalMesh)
		return false;

	auto* skeletalResource = component->SkeletalMesh->GetImportedResource();

	if (!skeletalResource || skeletalResource->LODModels.Num() <= 0)
		return false;

	int vertexIndex = faceIndex * 3;

	v0 = component->GetSkinnedVertexPosition(vertexIndex);
	v1 = component->GetSkinnedVertexPosition(vertexIndex + 1);
	v2 = component->GetSkinnedVertexPosition(vertexIndex + 2);

	DrawDebugPoint(GetWorld(), component->ComponentToWorld.TransformPosition(v0), 30, FColor(100, 100, 0), false);
	DrawDebugPoint(GetWorld(), component->ComponentToWorld.TransformPosition(v1), 30, FColor(100, 100, 0), false);
	DrawDebugPoint(GetWorld(), component->ComponentToWorld.TransformPosition(v2), 30, FColor(100, 100, 0), false);

	return true;
}


Using the code above, I get three vertices but they are not at all at the position of the LineTrace’s hit. My guess is that FaceIndex cannot be convert directly to the vertex index.

So if it’s the case, how do I convert FaceIndex to the actual vertex index of the skeletal mesh ? Is it even possible?

Thank you for your help!

this works for me:


		const FSkeletalMeshResource* SkelRes = skelMeshComp->GetSkeletalMeshResource();
		const FStaticLODModel& LODModel = SkelRes->LODModels 0 ];

		const FRawStaticIndexBuffer16or32Interface* modelIndexBuffer = LODModel.MultiSizeIndexContainer.GetIndexBuffer();

		FVector ComponentLocation = skelMeshComp->GetComponentLocation();
		FRotator ComponentRotation = skelMeshComp->GetComponentRotation();
		FVector ComponentScale = skelMeshComp->GetComponentScale();

		uint32 HitVertIndex0 = modelIndexBuffer->Get( ( uint32 )hitResult.FaceIndex * 3 );
		uint32 HitVertIndex1 = modelIndexBuffer->Get( ( uint32 )hitResult.FaceIndex * 3 + 1 );
		uint32 HitVertIndex2 = modelIndexBuffer->Get( ( uint32 )hitResult.FaceIndex * 3 + 2 );

		FVector Vert0Pos = LODModel.VertexBufferGPUSkin.GetVertexPositionSlow( HitVertIndex0 );
		FVector Vert1Pos = LODModel.VertexBufferGPUSkin.GetVertexPositionSlow( HitVertIndex1 );
		FVector Vert2Pos = LODModel.VertexBufferGPUSkin.GetVertexPositionSlow( HitVertIndex2 );

the only difference I see is the way we get the skeletal mesh. perhaps yours is not taking the animated position

cheers

1 Like

Thanks to you, I’ve managed to get it to work! :slight_smile:

The probelm was that I was not getting the FRawStaticIndexBuffer16or32Interface from the skeletal mesh. Instead, I was using the FaceIndex directly.

Thank you!!!

any chance you can post how you get FaceIndex for skeletal mesh? For me it’s alwasy -1

make sure you have per poly collision enabled on the sk mesh