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"

How to Create material using C++ and apply it to static mesh?

Hello Everyone,

I want to apply material to my Static mesh using C++, now i know that there is "StaticMesh->SetMaterial()" function, but the problem is this function is taking UMaterialInterface as a parameter and i could not find any way to convert my UMaterial* UnrealMaterial to UMaterialInterface ?

Another problem is, what is the difference between UMaterialInterface and UMaterial ?? And why instead of taking UMaterial in StaticMesh->SetMaterial() it is taking UMaterialInterface ?

So anyone know how to do it ?

Thanks in advance.

Product Version: UE 4.20
more ▼

asked Mar 11 '19 at 10:06 AM in C++ Programming

avatar image

249 3 4 9

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

1 answer: sort voted first

Ok to answer to each one of your questions.

  1. How to Create a material in C++.
    I Assume you mean instantiate a material here. Generally you will want to do it when constructing you object with:

    UMaterial* ExampleMaterial = nullptr;

    ConstructorHelpers::FObjectFinderMaterialAsset(TEXT("Material/MATERIAL PATH'")); ExampleMaterial = MaterialAsset.Object;

  2. UMaterialInterface vs UMaterial

You should not need to convert from UMaterial* to UMaterialinstance* since UMaterial inherits from MaterialInstance. If for some reason the code is complaintning simply cast to it Cast(YourMaterialPointer).

Material interface is an interface that allows you to pass any material type into functions that use materials as arguments (like in your case), since there are several material types (UMaterialInstance, UMaterialConstantDynamic, UMaterialInstanceDynamic, etc).

The function takes Material Interface as a value for the reason stated above (so it can receive any material type).

Hope this information helps you. If this is the information you needed please remember to mark this answer as the correct one so other people can find it. Make it a great day!

more ▼

answered Mar 11 '19 at 10:39 AM

avatar image

1k 22 13 29

avatar image Maverick_tango Mar 11 '19 at 10:46 AM

Hey @Bariudol ,

Thanks for the answer i can set the material using StaticMesh->SetMaterial(), do have any idea on how to set the color for that material which i can set to red or something which will update in editor?

avatar image Bariudol Mar 11 '19 at 10:58 AM

In that case you need to create a MaterialInstanceDynamic based on a material that has the color as a paramter. It would go something like this:

 UMaterialInstanceDynamic* ExampleMID = UMaterialInstanceDynamic::Create(ExampleMaterial);  
 ExampleStaticMesh->SetMaterial(ExampleMID, 0);  
 FLinearColor Red(FLineaerColor::Red);  
 ExampleMID->SetVectorParameter("ParameterName", Red);  

This is just a guideline for what you want to do. Look into the methods available for the MaterialInstanceDynamic class here http://api.unrealengine.com/INT/API/Runtime/Engine/Materials/UMaterialInstanceDynamic/

(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