How do I generate a UStaticMesh object from an array of vectors?

I generate an array of vectors, the vectors are the positions and are sorted so you can create triangles easily from them.

I don’t need things like UVs or normals blending between triangles. This is also just for the editor only so it doesn’t need to be saved or generated during play which means it doesn’t need to be a memory or runtime optimal solution.

What’s the best way to then create a UStaticMesh object from this data?
Otherwise what engine files/functions should I look at to get an insight on a very basic setup?

Cannot compromise on the UStaticMesh part.

Ok so I figured it out by looking at loads more posts.

A few notes on the code below:

  • There are some parts that I didn’t bother to calculate
  • MeshComponent is a UProceduralMeshComponent
  • GetSplineDistances() is my own function to do with my use case
  • GetSqaurePoly(…) is my own function that sets the Vertices and Triangles array variables which are the vertex positions and indices for triangles
MeshComponent->ClearCollisionConvexMeshes();
	MeshComponent->ClearAllMeshSections();

	const TArray<double> SplineDists = GetSplineDistances();
	
	for(int32 i = 0; i < SplineDists.Num() - 1; i++)
	{
		TArray<FVector> Vertices;
		TArray<int32> Triangles;
		GetSquarePoly(SplineDists[i], SplineDists[i + 1],Vertices, Triangles);

		const TArray<FVector2D> UVs;
		TArray<FVector> Normals;
		TArray<FProcMeshTangent> Tangents;
		UKismetProceduralMeshLibrary::CalculateTangentsForMesh(Vertices, Triangles, UVs, Normals, Tangents);

		const TArray<FLinearColor> VertexColors;
		
		MeshComponent->CreateMeshSection_LinearColor(
			MeshComponent->GetNumSections(),
			Vertices,
			Triangles,
			Normals,
			UVs,
			VertexColors,
			Tangents,
			false
		);
	}

	UStaticMesh* Mesh = NewObject<UStaticMesh>(GetOuter(), UStaticMesh::StaticClass(), FName("GenerateCliffMesh"), RF_Public | RF_Standalone);
	Mesh->InitResources();
	Mesh->SetLightingGuid();
	
	FMeshDescription MeshDescription = BuildMeshDescription(MeshComponent);

	UStaticMesh::FBuildMeshDescriptionsParams Params;
	Params.bFastBuild = true;
	
	Mesh->BuildFromMeshDescriptions({&MeshDescription}, Params);
	Mesh->CalculateExtendedBounds();
	
	Mesh->MarkPackageDirty();
	
	return Mesh;

Yes but it’s no longer a static mesh, the Procedural mesh component has it’s own mesh system so you are not storing a static mesh really.

the PMC component does not use static meshes.

You have to convert it back to static mesh, there are some workarounds but they are nasty to auto create a static mesh from this.

PMC has built in a function for the editor where you have “Convert to static mesh”
You can search in the class of unreal and find out where it is located and copy it from the class and modify it, only that you have to find a way to auto press the button, replace button with something else.

The workaround with code is various and you can find it on the forum but works and does not work depending on the version of unreal you are in.

UStaticMesh* Mesh = NewObject<UStaticMesh>(GetOuter(), 
Mesh->BuildFromMeshDescriptions({&MeshDescription}, Params);

THis I take it is the final dumping where you are trying to convert from Procedural Mesh to static ?
You are trying to convert component to mesh from what I see, that is the component of PMC not the mesh.

FMeshDescription MeshDescription = BuildMeshDescription(MeshComponent);

If it works then I guess some how it grabs what is in the component.

The procedural mesh is just a workaround so I wouldn’t have to do too much coding as it doesn’t need to do much besides create a mesh.

The end of the code, as you said is the final dumping where I convert the procedural mesh to the static mesh.

BuildMeshDescription is in ProceduralMeshComponent/ProceduralMeshConversion.h
The header contains what looks like 2 helper functions that convert it each way, from MeshDescription to ProcMesh and ProcMesh to MeshDescription which can then be used to make the static mesh

I could probably cut the procedural mesh component part out but I don’t know how and yeah, it seems to be working so far.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.