Edit the color of each polygon of a mesh

Hello. I’m developing a program that has as entry a list of polygons identifiers with a color associated to each one. This list is the output of another independent program that solves illumination problems and calculates the color of each polygon in a static mesh. The goal is to apply the colors to the static mesh and show the result.

Then, my question is if is there a way to edit the color of each polygon of a static mesh in Unreal Engine at runtime, or some c++ plugin that I could use to solve this.

Thanks.

Off hand depending on complexity of the mesh you could use modeling software like Maya or blender to give different material IDs to each face then unreal would pick that up and you could make a blueprint that would assign a material at runtime.

But, how I do it? How I access to all individual polygons? Is there some function to divide the mesh, and set the color of each polygon?

I have never tried it in unreal alone, I know there was something on here a while back about this but i cannot find the question. My way would be to model lets say a cube in a 3d modeling program. Assign 6 different materials, one to each face, they dont have to be permanent because you can override them in unreal. Then in unreal, in the mesh editor you will have 6 material slots to either assign a material or in blueprint you can you assign a material. I will keep looking for the question posted earlier about this, i think it was 4 months ago. I know its possible, I will try to answer back today unless someone beats me to it.

Yeah it’s fairly easy to do. Get FRawMesh from your UStaticMesh, it has WedgeColors member (FColor), this is where vertex colors per index are. Change them, then you can rebuild your UStaticMesh from the changed FRawMesh.

Thanks for the reply, but be aware that I need to perform these actions from code/blueprint at runtime, can not be done manually

Thanks for your reply. I am trying to do what you recommend me but the compiler throws the following error:

IntelliSense: class "FRawMeshBulkData" has no member "LoadRawMesh"	

This is the function:

void ACreadorLuces::PintarModelo(UStaticMesh* modelo){
	if (!modelo->IsValidLowLevel())
		return;
	if (&modelo->SourceModels[0] == nullptr)
		return;
	FStaticMeshSourceModel* sourceModel = &modelo->SourceModels[0];
	FRawMesh rawMesh;
	sourceModel->RawMeshBulkData->LoadRawMesh(rawMesh);
}

Any idea to solve this?

// Grab base LOD level.
FStaticMeshSourceModel& SrcModel = StaticMesh->SourceModels[0];

// Load the existing raw mesh.
FRawMesh RawMesh;
SrcModel.RawMeshBulkData->LoadRawMesh(RawMesh);

// Change RawMesh.WedgeColors ...

// Store the new raw mesh.
SrcModel->RawMeshBulkData->SaveRawMesh(RawMesh);

while(StaticMesh->SourceModels.Num() < NumLODs)
					{
						new(StaticMesh->SourceModels) FStaticMeshSourceModel();
					}

					for(int32 ModelLODIndex = 0; ModelLODIndex < NumLODs; ++ModelLODIndex)
					{
						StaticMesh->SourceModels[ModelLODIndex].ReductionSettings =
							LODGroup.GetDefaultSettings(ModelLODIndex);

						for(int32 MaterialIndex = 0; MaterialIndex < StaticMesh->Materials.Num(); ++MaterialIndex)
						{
							FMeshSectionInfo Info = StaticMesh->SectionInfoMap.Get(ModelLODIndex, MaterialIndex);
							Info.MaterialIndex = MaterialIndex;
							Info.bEnableCollision = true;
							Info.bCastShadow = true;
							StaticMesh->SectionInfoMap.Set(ModelLODIndex, MaterialIndex, Info);
						}
					}

// Free any RHI resources.
StaticMesh->PreEditChange(nullptr);

TArray<FText> BuildErrors;
StaticMesh->Build(true, &BuildErrors);
StaticMesh->MarkPackageDirty();

Something along these lines.

I am still having the same compilation error in this line:

 SrcModel.RawMeshBulkData->LoadRawMesh(RawMesh);

I found in this thread other people having the same issue. However is not clear to me how to solve that.

Are you linking in RawMesh module?

Yes, I did that.

	PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore"});

	PrivateDependencyModuleNames.AddRange(new string[] { "UnrealEd", "RawMesh"});

Something wrong in these lines?

I have RawMesh added in PublicDependencyModuleNames, but that shouldn’t make a difference. I am assuming you are building in Editor mode as well.

Looks like you are not building in Editor mode. RawMesh will only work in Editor, since that particular code block is in WITH_EDITORONLY_DATA ifdef. You are also linking in UnrealEd, which is also an Editor only module I believe, so you should be building Editor.

Well, that can be the problem, because I don’t know what is the “Editor mode”. Please, could you explain it?

UE4 configuration, you are looking for Debug Editor or Development Editor. In Visual Studio → Menu → Build → Configuration Manager

Select either Debug Editor or Development Editor as active. Build.

DebugGame Editor will also work.

I switched to DebugGame_editor and the problem was solved immediately! I really thank you for your time and your fast answers! :smiley: Now I will try to paint those triangles!

Hey! It’s me again, I’m having a lot of problems trying to set the colors. The wedgecolors array is empty, and when I insert colors in this array the engine crashes instantly, and the same happens when I do many other actions using the model, and when this happens the project gets broken and keeps broken when I undo changes, it’s really annoying.

Some idea to solve this? Is there a problem or conflict with materials associated with faces? The rawMesh.FaceMaterialIndices array has information, all the faces have the same material associated.

I tried loading a material with a parameter in the basecolor too, but when I instantiate the dynamic material the project crashes.