Download

Give a constant color to each StaticMesh in final rendered image

Hi, everyone!
My final purpose is to give a constant color to each StaticMesh in the rendered image. Just like below.
![73GL715TJFUK}8D)21N2IM.jpg|830x323
As the beginning,I built a really simple scene contains only a floor and a cylinder as StaticMashActor. Then a create a c++ class derived from USceneCaptureComponent2D. Add this c++ class to the cylinder through Add Component button. Then set an offset so the UNewSceneCaptureComponent2D can see the cylinder.
KBNO05A{T$UP(@@4J~(9THP.jpg
Then I try to render the paint the cylinder with a black color. The key lines of code are below. These codes are in TickComponent function



ShowFlags.SetMaterials(false);
ShowFlags.SetLighting(false);
ShowFlags.SetBSPTriangles(true);
ShowFlags.SetVertexColors(true);
ShowFlags.SetPostProcessing(false);
ShowFlags.SetHMDDistortion(false);
ShowFlags.SetTonemapper(false);

FColor NewColor = FColor(0, 0, 0, 255);

UStaticMeshComponent* StaticMeshComponent = Cast<UStaticMeshComponent>(actor->GetStaticMeshComponent());
UStaticMesh* StaticMesh = StaticMeshComponent->GetStaticMesh();

FStaticMeshLODResources& LODModel = StaticMesh->RenderData->LODResources[0];
FStaticMeshComponentLODInfo* InstanceMeshLODInfo = NULL;

StaticMeshComponent->SetLODDataCount(0 + 1, StaticMeshComponent->LODData.Num());
InstanceMeshLODInfo = &StaticMeshComponent->LODData[0];

InstanceMeshLODInfo->ReleaseOverrideVertexColorsAndBlock();

InstanceMeshLODInfo->OverrideVertexColors = new FColorVertexBuffer;
InstanceMeshLODInfo->OverrideVertexColors->InitFromSingleColor(FColor::White, LODModel.GetNumVertices());

uint32 NumVertices = LODModel.GetNumVertices();
check(InstanceMeshLODInfo->OverrideVertexColors);
check(NumVertices <= InstanceMeshLODInfo->OverrideVertexColors->GetNumVertices());

for (uint32 ColorIndex = 0; ColorIndex < NumVertices; ++ColorIndex)
{
uint32 NumOverrideVertexColors = InstanceMeshLODInfo->OverrideVertexColors->GetNumVertices();
uint32 NumPaintedVertices = InstanceMeshLODInfo->PaintedVertices.Num();
InstanceMeshLODInfo->OverrideVertexColors->VertexColor(ColorIndex) = NewColor;
}
BeginInitResource(InstanceMeshLODInfo->OverrideVertexColors);

StaticMeshComponent->MarkRenderStateDirty();


I think the cylinder should be shaded totally black after this procedure. But result is not correct. Like below
image_1.jpg
Does anyone have any idea about this phenomenon? Did I miss something all do something wrong in my codes. Thanks for any suggestion!