Creating a new Material in C++

Hi everyone,
I’m trying to build a program that, given a project path, would load at runtime a 3D scene. I’ve already done it using Unity 5, and I wanted to recreate the same product under Unreal to compare pros and cons. Right now I’m struggling creating a new material starting from some input parameters (i.e. the program reads a bool isReflective variable set true, and creates a new reflective material instance). Under Unity was quite easy because all I had to do was to load the proper shader, but I can’t find any parallelism under Unreal.

//Unity example
    Material mat = null;
    if (_transparency)
    {
        mat = new Material(Shader.Find("Transparent/VertexLit"));
    }

Is there any easy way to create a new material instance from Engine/Shaders or any other already existing resource using C++?

Try using material instance / dynamic material instance instead

Thank you for the quick answer! My current approach is this one:

UMaterial* TheMaterial = new UMaterial();
		UMaterialInstanceDynamic* mat;

		static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT("Material'/Game/Materials/MyMaterial.MyMaterial'"));

		if (Material.Object != NULL)
		{
			TheMaterial = (UMaterial*)Material.Object;
		}
		mat = UMaterialInstanceDynamic::Create(TheMaterial, this);

.
but running it gives me:
FObjectInitializer::Get() can only be used inside of UObject-derived class constructor.

Now I’ve created a fake MyMaterial in the editor and acted,inside my custom actor, as suggested here:A new, community-hosted Unreal Engine Wiki - Announcements - Epic Developer Community Forums . This is more like a stopgap than a solution since I always load the same material for all my meshes… I’m still not able to create a new one from code.

I doubt if using new is a good idea. Try built-in constructors instead.

If anyone else needs to create material in c++ you can do it like this:

 UMaterial* UnrealMaterial = NewObject<UMaterial>();

you can also specify outer and a name and all that stuff. But what more interesting is that you can actually create material nodes in c++ and compile them. For example:

		UMaterialExpressionConstant3Vector * BaseColorFactorNode = NewObject<UMaterialExpressionConstant3Vector>(NewMaterial);
		BaseColorFactorNode->Constant = FColor::Red; //you can specify any color here;
            UnrealMaterial->BaseColor.Connect(0, BaseColorFactorNode);

This will create a material with a const vector3d parameter in your material. I don’t know the limits of it, I only had to work with constants, textures and multiply nodes. Anyway, hope this help somebody.

Hi, Can that work on Ingame?

Hi, no, I afraid it won’t. This code is used by the editor when it created material from export files. It can’t be done runtime. The way I did it was creating a material with parameters and some custom HLSL code to define behavior based on them(since standard material expressions don’t have too many logical nodes).

Hello, Could you be a little more specific on how the material can be created?

2 Likes

Hi Alex, thanks for an interesting pointer. But the question is does it work at runtime?

1 Like