Borrowing the GameGeneratedActor class found at the custom mesh generation wiki A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums
I was able to render a cube utilizing code below. I leverage the custommeshcomponent plug in from within a blueprint, and link the below class to it.
However, what gets rendered is an inside the box view of the cube from every perspective. Whereas I am interested in rendering the outside of the cube. Does anyone have some insight regarding why I am getting this effect? Is it the order I am adding the triangles to the array? Or do I have to include some code that addresses the “normals” of the cube? Thanks!
AGameGeneratedMesh::AGameGeneratedMesh(const class FPostConstructInitializeProperties& PCIP)
: Super(PCIP)
{
TSubobjectPtr<UCustomMeshComponent> mesh = PCIP.CreateDefaultSubobject<UCustomMeshComponent>(this, TEXT("GeneratedMesh"));
float x = 0.0;
float y = 0.0;
float z = 0.0;
TArray<FCustomMeshTriangle> triangles;
FCustomMeshTriangle triangle;
// CubeTop
triangle.Vertex0 = FVector(x, y, z + 1);
triangle.Vertex1 = FVector(x + 1, y, z + 1);
triangle.Vertex2 = FVector(x + 1, y, z);
triangles.Add(triangle);
triangle.Vertex0 = FVector(x + 1, y, z);
triangle.Vertex1 = FVector(x, y, z);
triangle.Vertex2 = FVector(x, y, z + 1);
triangles.Add(triangle);
// CubeNorth
triangle.Vertex0 = FVector(x + 1, y - 1, z + 1);
triangle.Vertex1 = FVector(x + 1, y, z + 1);
triangle.Vertex2 = FVector(x, y, z + 1);
triangles.Add(triangle);
triangle.Vertex0 = FVector(x, y, z + 1);
triangle.Vertex1 = FVector(x, y - 1, z + 1);
triangle.Vertex2 = FVector(x + 1, y - 1, z + 1);
triangles.Add(triangle);
//CubeEast
triangle.Vertex0 = FVector(x + 1, y - 1, z);
triangle.Vertex1 = FVector(x + 1, y, z);
triangle.Vertex2 = FVector(x + 1, y, z + 1);
triangles.Add(triangle);
triangle.Vertex0 = FVector(x + 1, y, z + 1);
triangle.Vertex1 = FVector(x + 1, y - 1, z + 1);
triangle.Vertex2 = FVector(x + 1, y - 1, z);
triangles.Add(triangle);
//CubeSouth
triangle.Vertex0 = FVector(x, y - 1, z);
triangle.Vertex1 = FVector(x, y, z);
triangle.Vertex2 = FVector(x + 1, y, z);
triangles.Add(triangle);
triangle.Vertex0 = FVector(x + 1, y, z);
triangle.Vertex1 = FVector(x + 1, y - 1, z);
triangle.Vertex2 = FVector(x, y - 1, z);
triangles.Add(triangle);
//CubeWest
triangle.Vertex0 = FVector(x, y - 1, z + 1);
triangle.Vertex1 = FVector(x, y, z + 1);
triangle.Vertex2 = FVector(x, y, z);
triangles.Add(triangle);
triangle.Vertex0 = FVector(x, y, z);
triangle.Vertex1 = FVector(x, y - 1, z);
triangle.Vertex2 = FVector(x, y - 1, z + 1);
triangles.Add(triangle);
//CubeBot
triangle.Vertex0 = FVector(x, y - 1, z);
triangle.Vertex1 = FVector(x + 1, y - 1, z);
triangle.Vertex2 = FVector(x + 1, y - 1, z + 1);
triangles.Add(triangle);
triangle.Vertex0 = FVector(x + 1, y - 1, z + 1);
triangle.Vertex1 = FVector(x, y - 1, z + 1);
triangle.Vertex2 = FVector(x, y - 1, z);
triangles.Add(triangle);
mesh->SetCustomMeshTriangles(triangles);
RootComponent = mesh;
}