Having trouble getting vertex colors to work with a procedurally generated mesh. The wrinkle is that the mesh is attached to a programatically spawned actor, which may or may not be part of the problem I don’t know.
In the actor .h file I declare the mesh and the material:
UPROPERTY(VisibleAnywhere)
UProceduralMeshComponent* SectorMesh;
UPROPERTY(VisibleAnywhere)
UMaterial* Material;
In the actor .cpp file:
SectorMesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedMesh"));
RootComponent = SectorMesh;
static ConstructorHelpers::FObjectFinder<UMaterial> MaterialObject(TEXT("Material'/Game/Meshes/M_VertexMaterial.M_VertexMaterial'"));
Material = MaterialObject.Object;
The material I am using has the vertex color node connected to the base color input:
Then I create the geometry (using as many line of code as possible ):
...
Vertices.Emplace(FVector(Chunk_X - 49.9f, Chunk_Y - 49.9f, ChunkHeight_SW));
Vertices.Emplace(FVector(Chunk_X + 49.9f, Chunk_Y - 49.9f, ChunkHeight_NW));
Vertices.Emplace(FVector(Chunk_X + 49.9f, Chunk_Y - 49.9f, Chunk_Z - 100.f));
Triangles.Emplace(TriangleIndex++);
Triangles.Emplace(TriangleIndex++);
Triangles.Emplace(TriangleIndex++);
Normals.Emplace(FVector(0.f, -1.f, 0.f));
Normals.Emplace(FVector(0.f, -1.f, 0.f));
Normals.Emplace(FVector(0.f, -1.f, 0.f));
VertexColors.Emplace(FLinearColor(21.6f, 12.5f, 6.3f, 1.f));
VertexColors.Emplace(FLinearColor(21.6f, 12.5f, 6.3f, 1.f));
VertexColors.Emplace(FLinearColor(21.6f, 12.5f, 6.3f, 1.f));
Vertices.Emplace(FVector(Chunk_X - 49.9f, Chunk_Y - 49.9f, ChunkHeight_SW));
Vertices.Emplace(FVector(Chunk_X + 49.9f, Chunk_Y - 49.9f, Chunk_Z - 100.f));
Vertices.Emplace(FVector(Chunk_X - 49.9f, Chunk_Y - 49.9f, Chunk_Z - 100.f));
Triangles.Emplace(TriangleIndex++);
Triangles.Emplace(TriangleIndex++);
Triangles.Emplace(TriangleIndex++);
Normals.Emplace(FVector(0.f, -1.f, 0.f));
Normals.Emplace(FVector(0.f, -1.f, 0.f));
Normals.Emplace(FVector(0.f, -1.f, 0.f));
VertexColors.Emplace(FLinearColor(21.6f, 12.5f, 6.3f, 1.f));
VertexColors.Emplace(FLinearColor(21.6f, 12.5f, 6.3f, 1.f));
VertexColors.Emplace(FLinearColor(21.6f, 12.5f, 6.3f, 1.f));
...
Then when the geometry arrays are filled I spawn the actor, attach the mesh to it, and apply the material to the mesh:
ABaseSector* NewBaseSector = World->SpawnActor<ABaseSector>(SectorClass, FVector::ZeroVector, FRotator::ZeroRotator);
NewBaseSector->SectorMesh->CreateMeshSection_LinearColor(Index, Vertices, Triangles, Normals, UV0, VertexColors, Tangents, false, false);
NewBaseSector->SectorMesh->SetMaterial(Index, NewBaseSector->Material);
The UV0 and Tangents arrays are empty right now.
This all works as far as the geometry appearing normal and everything spawning OK. But everything is rendered white, no vertex colors seem to be applied. If I don’t attach the material to the mesh everything gets rendered grey, which implies that the material is attached correctly since things are white when it is attached.
What am I missing?