Coloring Procedural Mesh in C++ code

Hi. I have a procedural Mesh component. All it is is a triangle. I would like to make the whole thing red.

So…

    JtMesh = CreateDefaultSubobject<UProceduralMeshComponent>(TEXT("GeneratedJtMesh"));
    JtMesh->SetMobility(EComponentMobility::Movable);
    
    // Make a triangle
        TArray<FVector> vertices;
        
        vertices.Add(FVector(0, 0, 0));
        vertices.Add(FVector(0, 100, 0));
        vertices.Add(FVector(0, 0, 100));
    
        TArray<int32> Triangles;
        Triangles.Add(0);
        Triangles.Add(2);
        Triangles.Add(1);
        
        TArray<FVector> normals;
        normals.Add(FVector(1, 0, 0));
        normals.Add(FVector(1, 0, 0));
        normals.Add(FVector(1, 0, 0));
        
        TArray<FVector2D> UV0;
        UV0.Add(FVector2D(0, 0));
        UV0.Add(FVector2D(0, 10));
        UV0.Add(FVector2D(10, 10));
        
        TArray<FColor> vertexColors;
        //vertexColors.Add(FColor(200, 0, 0, 1));
        //vertexColors.Add(FColor(200, 0, 0, 1));
        //vertexColors.Add(FColor(200, 0, 0, 1));
        
        
        TArray<FProcMeshTangent> tangents;
        tangents.Add(FProcMeshTangent(1, 1, 1));
        tangents.Add(FProcMeshTangent(1, 1, 1));
        tangents.Add(FProcMeshTangent(1, 1, 1));
    // Make the mesh section
        JtMesh->CreateMeshSection(0, vertices, Triangles, normals, UV0, vertexColors, tangents, false);
        LeMaterial = UMaterial::GetDefaultMaterial(MD_Surface);
        UMaterialInstanceDynamic * InstanceMaterial = UMaterialInstanceDynamic::Create(LeMaterial, NULL);
    // Add a vector parameter for the color
        InstanceMaterial->SetVectorParameterValue(FName(TEXT("Base Color")), FLinearColor(100.0f, 0.0f, 0.0f));
    // Set the material
        JtMesh->SetMaterial(numMeshSections, InstanceMaterial);

Upon doing this, I have a vector parameter for the color. I can see this in the designer…

But it does not change the color of the rendered object. How do I apply this parameter as the material of my mesh?

1 Like

Well I notice that your Material Instance does not have a parent material. So then the parameter you are setting to red, has no hookup within it I would assume. Can you verify that the parent material is valid and has a proper vector parameter needed to change it’s color?

I have changed the line UMaterialInstanceDynamic * InstanceMaterial = UMaterialInstanceDynamic::Create(LeMaterial, NULL);
to
UMaterialInstanceDynamic * InstanceMaterial = JtMesh->CreateAndSetMaterialInstanceDynamicFromMaterial(numMeshSections, LeMaterial);

So now I have a parent material.

How can I check if that material has a proper vector parameter needed to change it’s color?

So world grid does not. you will want to create your own material. Then open the material editor and add a constant3 which will give you a vector node. Right click it, convert it to a parameter, give it the parameter name, then hook it up to the base color node. It should work lovely. You just need to pull that material in.