Adding textures to a ProceduralMeshComponent (C++)

I’m new to Unreal and I’ve been playing with the ProceduralMeshComponent the last few days, using only C++.

I wanted to import meshes at runtime, and I managed to do that with the help of the Assimp library. So, right now I can load any mesh at runtime and it works fine: the thing missing are the textures.

Suppose I have a .obj mesh, with a .mtl file and a single .png texture. Suppose that I have no idea of what mesh I’m going to create a runtime, so I don’t know how many materials there are and so on. How can I create the materials at runtime for this?

I’ve been looking all day on the internet for this kind of situation, but I couldn’t find anything related. People always use materials that are made from within the editor, they don’t create it via C++ code, but that’s what I need.

I’ve looked into instanced materials and tried to do something like this (it’s just an example in which some properties of the material get changed):

// This code is executed everytime a section is added to the procedural mesh
UMaterialInstanceDynamic* dynamic = UMaterialInstanceDynamic::Create(mMesh->GetMaterial(mSectionsCurrentlyProcessed), this);

FLinearColor RandomColor;
RandomColor.R = FMath::RandRange(0, 1);
RandomColor.G = FMath::RandRange(0, 1);
RandomColor.B = FMath::RandRange(0, 1);
RandomColor.A = FMath::RandRange(0, 1);

if (dynamic)
{
	dynamic->SetVectorParameterValue(FName("Base_Color"), RandomColor);
	dynamic->SetScalarParameterValue(FName("MetalParam"), FMath::RandRange(0, 1));
	dynamic->SetScalarParameterValue("Roughness", 1.0f);
}

The problem is that there is no starting material in my case, and the parameters I’m “modifying” in the example aren’t present since they weren’t defined in the editor.

So, to sum up, I’m running out of ideas. Is there a way to create an empty material from scratch in C++ and then fill it, in particular with a texture?

This is a good question. I liked your idea. How much you have move forwarded from this part ? Can you share me your process ?

I ended up doing pretty much what I was (indirectly) suggesting.

I set up a “BaseMaterial”, which contained a param for all the standard properties (“Color”, “Metallic”, “Roughness”, etc.).
At runtime, whenever I load info regarding a material I create an instance of that material and set the properties, as I was hinting in the question.