I try to build a static mesh in a function library called from an editor tool blueprint.
But I can’t get my static mesh to be visible. Do I miss something ?
auto actor = world->SpawnActor<AActor>();
actor->SetActorLabel(TEXT("Terrain"));
UStaticMeshComponent * mesh_component = NewObject<UStaticMeshComponent>(actor, FName(TEXT("Mesh")));
actor->SetRootComponent(mesh_component);
FMeshDescription mesh_desc;
FStaticMeshAttributes attributes(mesh_desc);
attributes.Register();
TVertexAttributesRef<FVector3f> positions = mesh_desc.GetVertexPositions();
mesh_desc.ReserveNewVertices(4);
FVertexID v0 = mesh_desc.CreateVertex();
FVertexID v1 = mesh_desc.CreateVertex();
FVertexID v2 = mesh_desc.CreateVertex();
FVertexID v3 = mesh_desc.CreateVertex();
mesh_desc.ReserveNewVertexInstances(4);
FVertexInstanceID vi0 = mesh_desc.CreateVertexInstance(v0);
FVertexInstanceID vi1 = mesh_desc.CreateVertexInstance(v1);
FVertexInstanceID vi2 = mesh_desc.CreateVertexInstance(v2);
FVertexInstanceID vi3 = mesh_desc.CreateVertexInstance(v3);
mesh_desc.ReserveNewUVs(4);
FUVID uv0 = mesh_desc.CreateUV();
FUVID uv1 = mesh_desc.CreateUV();
FUVID uv2 = mesh_desc.CreateUV();
FUVID uv3 = mesh_desc.CreateUV();
FPolygonGroupID polygon_group = mesh_desc.CreatePolygonGroup();
mesh_desc.ReserveNewPolygons(1);
mesh_desc.CreatePolygon(polygon_group, {vi0, vi1, vi2, vi3});
positions = attributes.GetVertexPositions();
positions[0] = FVector3f(-100, -100, 0);
positions[1] = FVector3f(100, -100, 0);
positions[2] = FVector3f(100, 100, 0);
positions[3] = FVector3f(-100, 100, 0);
TVertexInstanceAttributesRef<FVector3f> normals = attributes.GetVertexInstanceNormals();
normals[0] = FVector3f(0, 0, 1);
normals[1] = FVector3f(0, 0, 1);
normals[2] = FVector3f(0, 0, 1);
normals[3] = FVector3f(0, 0, 1);
TVertexInstanceAttributesRef<FVector2f> uvs = attributes.GetVertexInstanceUVs();
uvs[0] = FVector2f(0, 0);
uvs[1] = FVector2f(1, 0);
uvs[2] = FVector2f(1, 1);
uvs[3] = FVector2f(0, 1);
mesh_desc.TriangulateMesh();
// At least one material must be added
UStaticMesh* mesh = NewObject<UStaticMesh>(actor, FName(TEXT("ProceduralStaticMesh")));
mesh->GetStaticMaterials().Add(FStaticMaterial());
UStaticMesh::FBuildMeshDescriptionsParams mdParams;
mdParams.bBuildSimpleCollision = true;
//mdParams.bFastBuild = true;
// Build static mesh
mesh->BuildFromMeshDescriptions({&mesh_desc}, mdParams);
mesh_component->SetStaticMesh(mesh);