tldr: I wish to modify a small mesh to match a prespecified FVector Array, but the FPositionVertexBuffer.VertexPosition(Index) returns a very weird FVector3f.
So I have made a triangle terrain mesh inside of blender. This mesh fits within the coordinates: (0,0) (1,0) (0,1) excluding the z axis. In other words, the mesh is made to be in natural coordinates for easy transformation/deformation calculations.
I use the following code to try and deform the mesh:
void AStaticMeshDeformer::DeformStaticMesh(const TArray<FVector>& NewVertices)
{
if (MeshComponent and NewVertices.Num() == 3)
{
UE_LOG(LogTemp, Warning, TEXT("IF gate meshcomponent == True"));
UStaticMesh* StaticMesh = MeshComponent->GetStaticMesh();
if (StaticMesh)
{
UE_LOG(LogTemp, Warning, TEXT("IF gate StaticMesh == True"));
FStaticMeshRenderData* RenderData = StaticMesh->GetRenderData();
if (RenderData)
{
UE_LOG(LogTemp, Warning, TEXT("IF gate RenderData == True"));
FPositionVertexBuffer* VertexBuffer = &RenderData->LODResources[0].VertexBuffers.PositionVertexBuffer;
if (VertexBuffer)
{
UE_LOG(LogTemp, Warning, TEXT("IF gate VertexBuffer == True"));
const int32 VertexCount = VertexBuffer->GetNumVertices();
UE_LOG(LogTemp, Warning, TEXT("Receiving Vertex count: %d"), VertexCount);
for (int32 Index = 0; Index < VertexCount; Index++)
{
UE_LOG(LogTemp, Warning, TEXT("Updating Vertex"));
// get the original vertex position
const FVector3f& VertexPosition3f = VertexBuffer->VertexPosition(Index);
FVector OriginalVertex(VertexPosition3f.X, VertexPosition3f.Y, VertexPosition3f.Z);
UE_LOG(LogTemp, Warning, TEXT("Index: %d, Vertex Position: X=%f, Y=%f, Z=%f"), Index, OriginalVertex.X, OriginalVertex.Y, OriginalVertex.Z);
FVector NewLocation = OriginalVertex.X * NewVertices[0] + OriginalVertex.Y * NewVertices[1] + OriginalVertex.Z * NewVertices[2];
VertexBuffer->VertexPosition(Index).Set(NewLocation.X, NewLocation.Y, NewLocation.Z);
}
// Mark the render state as dirty to ensure the changes take effect
MeshComponent->MarkRenderStateDirty();
}
}
}
}
}
Now the weird thing is that the VertexBuffer->VertexPosition(Index) returns numbers like:
Vertex Position: X=8435231681478389675059699138441510912.000000, Y=2938254050440756938707419071281889280.000000, Z=0.000000
Whats even weirder is that not all my Z coordinates are/should be actually zero, while it does show in unreal like that. I have a slight elevation within the mesh halfway through. Here is an image of the mesh.
Additionally, the mesh remains unmodified after the execution of the code. no errors, no changes. I suspect my issue is related to how FVector3f is normally stored. as: VertexBuffer->VertexPosition(Index) returns an FVector3f. I also assume my .Set() method is incorrect. If anyone knows how to help me out, I would love to know!