Dynamically create mesh from points in space

Hello there

I would like to create a Mesh from several points (world coordinates if possible), by clicking on the ground to draw a shape for instance.

I did some researches beforehand, and roughly found two ways I can explore :

	const TSharedPtr<TArray<FVector>> sharedVertices = MakeShared<TArray<FVector>>(vertices); 
	FGeometryScriptVectorList verticesList = FGeometryScriptVectorList { sharedVertices }; 

	const TSharedPtr<TArray<int>> indexes = MakeShared<TArray<int>>();
	FGeometryScriptIndexList indexesList = FGeometryScriptIndexList { EGeometryScriptIndexType::Vertex ,indexes };
	
	UDynamicMesh* myDynMesh = this->DynamicMeshComponent->GetDynamicMesh()->Reset();
	UGeometryScriptLibrary_MeshBasicEditFunctions::AddVerticesToMesh(myDynMesh, std::move(verticesList), indexesList);

Any advice ?
Thank you ^^

1 Like

I dig on the source code of this Demo GitHub - gradientspace/UE5RuntimeToolsFrameworkDemo: Sample project/code that uses the UE5 InteractiveToolsFramework to provide a small modeling app at Runtime and managed to cook something.

Starting with a TArray<FVector> of your vertices, append them to a FPolygon2d. The coordinates of the vertices are local to the Polygon.

FPolygon2d OuterPolygon;
for (FVector& Vertex : Vertices)
{
	OuterPolygon.AppendVertex(TVector2(Vertex));
}

Then, ensure that your vertices order is counter-clockwise (otherwise your mesh will be reversed)

if(OuterPolygon.IsClockwise())
{
	OuterPolygon.Reverse();
}

NB : If you’re sure about the correct order of your vertices, you don’t need those 2 steps. But you will still need your vertices as FVector2D (and not FVector).

Then, use AppendSimpleExtrudePolygon function and tada. No need to compute triangles.

// Just in case, resetting all data for the Dynamic Mesh
UDynamicMesh* MyDynMesh = this->DynamicMeshComponent->GetDynamicMesh()->Reset();

UGeometryScriptLibrary_MeshPrimitiveFunctions::AppendSimpleExtrudePolygon(MyDynMesh,
	FGeometryScriptPrimitiveOptions(),
	FTransform(),
	OuterPolygon.GetVertices(), //Vertices2D,
	100, // Height
	5);  // HeightSteps, dunno what it does

Also, you can add this line, otherwise the normals are weird (but I can’t explain why)

UGeometryScriptLibrary_MeshNormalsFunctions::ComputeSplitNormals(MyDynMesh, FGeometryScriptSplitNormalsOptions(), FGeometryScriptCalculateNormalsOptions());

Note that it uses some experimental functions and AppendSimpleExtrudePolygon seems to not be available in any namespace ? Hence, I needed to do an import with relative path like that (UE 5.0.3)
#include "../Plugins/Experimental/GeometryScripting/Source/GeometryScriptingCore/Public/GeometryScript/MeshPrimitiveFunctions.h"
(same thing for ComputeSplitNormals)

This may not be a clean way to do it, but for now it’s the only way I managed to find with my (restricted) knowledge.

1 Like

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