CreateMeshSection_LinearColor: vertex coloring doesn't always work

Hello. Working a bit on procedural meshes, I get hits and misses on coloring the vertices. I made a nice voxel model in full color in another part of my code. Then I try to create some additional meshes and get only grey instead of red.
Sometimes, it’s about changing the section index from 0 to 1. But that doesn’t always work.

Below is a simple square mesh that I am trying to color. It was tested on the Basic Code template for C++. Can anyone see what’s wrong ?

ProceduralMeshComponent module is set in the build.cs file.

Mesh object is set in .h file like so:

UPROPERTY(VisibleAnywhere, Category = ProcMesh)
UProceduralMeshComponent* mesh;

Constructor in .cpp goes like this:

AMyActor::AMyActor()
{
	mesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
	RootComponent = mesh;

	TArray<FVector> vertices;
	TArray<int32> triangles;
	TArray<FVector> normals;
	TArray<FLinearColor> colors;

	vertices.Add(FVector(0.f, 0.f, 0.f));
	vertices.Add(FVector(0.f, 100.f, 0.f));
	vertices.Add(FVector(100.f, 0.f, 0.f));
	vertices.Add(FVector(100.f, 100.f, 0.f));

	triangles.Add(0);
	triangles.Add(1);
	triangles.Add(2);
	triangles.Add(3);
	triangles.Add(2);
	triangles.Add(1);

	for (int32 i = 0; i < vertices.Num(); i++)
	{
		normals.Add(FVector(0.f, 0.f, 1.f));
		colors.Add(FLinearColor::Red);
	}

	// Optional arrays
	TArray<FVector2D> UV0;
	TArray<FProcMeshTangent> tangents;

	mesh->CreateMeshSection_LinearColor(0, vertices, triangles, normals, UV0, colors, tangents, true);
}

Found it.

I have to attach it to a material.

*.h:

UPROPERTY(VisibleAnywhere, Category = ProcMesh)
UMaterial* Material;

*cpp:

in contructor:

static ConstructorHelpers::FObjectFinder<UMaterial> MaterialOb(TEXT("Material'/Game/VertexMat.VertexMat'"));
Material = MaterialOb.Object;

when done with vertices:

mesh->CreateMeshSection_LinearColor(0, vertices, triangles, normals, UV0, colors, tangents, true);
mesh->SetMaterial(0, Material);

in which case I don’t understand why it actually worked without it, with SectionIndex 1, on a large model I did recently. But I’ll take this.

Happy meshing.

1 Like

Will this work when real color data are added to the
TArray colors
?
What kind of material do you have?