x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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++?

Product Version: UE 4.11
Tags:
more ▼

asked May 16 '16 at 08:03 AM in C++ Programming

avatar image

MarcoPari
11 1 2 3

avatar image Zarkopafilis May 16 '16 at 12:20 PM

Try using material instance / dynamic material instance instead

avatar image MarcoPari May 16 '16 at 12:35 PM

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.

avatar image WHSolv Feb 28 '17 at 03:47 AM

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

avatar image MarcoPari May 18 '16 at 08:34 AM

Now I've created a fake MyMaterial in the editor and acted,inside my custom actor, as suggested here:https://wiki.unrealengine.com/Dynamic_Load_Object . 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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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.

more ▼

answered May 22 '18 at 01:24 PM

avatar image

MrMcKvarz
231 1 6 15

avatar image Fish-1028 Sep 07 '18 at 09:01 AM

Hi, Can that work on Ingame?

avatar image MrMcKvarz Sep 07 '18 at 04:58 PM

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).

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question