Painting Vertex Colours From Code


I have moved this thread to the Rendering forum, as I think it’s a better place for this question. The new thread:

I’m trying to write some code that traverses vertices within a certain distance from a given vert, and changes their vertex colour.

Here’s a quick illustration, the green vert in the centre “spreads” to the vertices around it, making them green:

vertex-stage-1.png vertex-stage-2.png

The code that finds the verts within the distance is fairly simple, but I’m having trouble actually changing the colour of vertices that I have found.

The UStaticMeshComponent->StaticMesh member has the GetVertexColorData and SetVertexColorData methods, which work, but aren’t a good solution (it is to be used in-game, so won’t actually affect the mesh, just instances of the mesh).

The only example of vertex painting I can find in the engine source is the mesh paint tool in the editor: MeshPaintEdMode.cpp

I have been trying to pick that apart, but I’m a little stuck (I’m not sure which portion of the code applies to what I am trying to do). Here’s what I have:

ColorSpreadComponent->SetLODDataCount(0 + 1, ColorSpreadComponent->LODData.Num());

FStaticMeshComponentLODInfo* InstanceMeshLODInfo = &ColorSpreadComponent->LODData[0];

InstanceMeshLODInfo->OverrideVertexColors = new FColorVertexBuffer;

FStaticMeshLODResources& LODModel = ColorSpreadComponent->StaticMesh->RenderData->LODResources[0];

// Init all colours to green as a test
if ((int32)LODModel.ColorVertexBuffer.GetNumVertices() >= LODModel.GetNumVertices())
	InstanceMeshLODInfo->OverrideVertexColors->InitFromColorArray(&LODModel.ColorVertexBuffer.VertexColor(0), LODModel.GetNumVertices());
	InstanceMeshLODInfo->OverrideVertexColors->InitFromSingleColor(FColor::Green, LODModel.GetNumVertices());

// Loop all of the verts and set their colour to green (this would be the "spread" code, and it would conditionally pick out verts)
for (uint32 i = 0; i < InstanceMeshLODInfo->OverrideVertexColors->GetNumVertices(); i++)
	InstanceMeshLODInfo->OverrideVertexColors->VertexColor(i) = FColor::Green;

It happily runs, but doesn’t do anything :slight_smile:

ColorSpreadComponent is a UStaticMeshComponent, and for now I’m just trying to colour all vertices green as a place to start. The mesh in question already has vertex colours set up.

I’m also running this inside an AActor derived class, inside the Tick method, which is probably the wrong place to be doing this stuff.

Can anyone point me in the right direction?