How to get vertex positions of spline mesh component in world space?

Figured out how to convert vertex poistions from asset (static mesh) to spline. The following method is used in StaticMesh.cpp, in function static void GetStaticLightingVertex (https://github.com/EpicGames/UnrealEngine/blob/76085d1106078d8988e4404391428252ba1eb9a7/Engine/Source/Programs/UnrealLightmass/Private/Lighting/StaticMesh.cpp#L151) and also in vertex shader for spline meshes

this function transforms position in static mesh space to spline mesh space:



FVector StaticMeshToSplineMeshVertexPosition(const FVector& StaticMeshVertexPosition, USplineMeshComponent* SplineMeshComponent)
{
	const float VertexPositionAlongSpline = StaticMeshVertexPosition[SplineMeshComponent->ForwardAxis];
	const FTransform StaticMeshToSplineMeshTransform = SplineMeshComponent->CalcSliceTransform(VertexPositionAlongSpline);
	FVector SlicePos = StaticMeshVertexPosition;
	SlicePos[SplineMeshComponent->ForwardAxis] = 0;
	const FVector SplineMeshSpaceVector = StaticMeshToSplineMeshTransform.TransformPosition(SlicePos);
	FVector ResultPos = SplineMeshSpaceVector;
	return SplineMeshSpaceVector;
}


this code gets positions of all vertices of USplineMeshComponent in world space:



UStaticMesh* StaticMesh = SplineMeshComponent->GetStaticMesh();

const FStaticMeshLODResources& RenderData = StaticMesh->RenderData->LODResources[0];
for (uint32 Index = 0; Index < RenderData.PositionVertexBuffer.GetNumVertices(); Index++)
{
	const FVector StaticMeshSpacePosition = RenderData.PositionVertexBuffer.VertexPosition(Index);
	const FVector SplineMeshSpacePosition = StaticMeshToSplineMeshVertexPosition(StaticMeshSpacePosition, SplineMeshComponent);
	const FVector WorldSpacePosition = SplineMeshComponent->GetComponentLocation() + SplineMeshComponent->GetComponentTransform().TransformVector(SplineMeshSpacePosition);

}


I still have no idea why you have to do this


 SlicePos[SplineMeshComponent->ForwardAxis] = 0; 

but it works

3 Likes