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"

How to lerp material color in C++ ?

I created a Blueprint from a C++ class. In that Blueprint, I added a static mesh component (a plane). This plane uses a simple material, only a color.

I want to be able to change the color of this plane, progressively in the C++ class.

I investigated two strategies: Creating 2 materials and switching from one to the other, or creating a material with 2 colors, and using a lerp function. The second strategy seems more accessible, but I still need help on that.

So here is how I set up the material: 2 colors, 1 lerp function to blend the colors, and a scalar parameter that I called "Blend" to input the proportion of each color in the blend. alt text

In my C++ class I can access to the material with this code:

 auto Plane = FindComponentByClass<UStaticMeshComponent>();
 auto material = Plane->GetMaterial(0);

Then, what I would like to do in the C++ class is something like

SetScalarParameterValue(TEXT("Blend"), value);

But there is no such function... There is only a

GetScalarParameterValue().

Any idea how I could modify the scalar parameter from the C++ class? I would also be interested in another way to change the color from the C++ class if there is better strategy. Thanks!

EDIT: SOLUTION

ollowing Roel advices, I created a MaterialInstanceDynamic, and called SetScalarParameterValue() on this MaterialInstanceDynamic.

In the .f file I added:

 UMaterialInstanceDynamic* DynamicMaterial;

In the .cc file I added:

 void AMyPlane::BeginPlay()
 {
     Super::BeginPlay();
 
     auto Plane = FindComponentByClass<UStaticMeshComponent>();
     auto material = Plane->GetMaterial(0);
     DynamicMaterial = UMaterialInstanceDynamic::Create(material, NULL);
     Plane->SetMaterial(0, DynamicMaterial);
 }

 void AMyPlanel::Tick(float DeltaTime)
 {
     Super::Tick(DeltaTime);
     // Testing the color switch at runtime
     float blend = 0.5f + FMath::Cos(GetWorld()->TimeSeconds)/2;
     DynamicMaterial->SetScalarParameterValue(TEXT("Blend"), blend);
 }

Result

alt text

Product Version: UE 4.21
Tags:
material.png (543.9 kB)
more ▼

asked Jan 22 '19 at 12:18 PM in C++ Programming

avatar image

vdlmrc
7 1 1 2

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

3 answers: sort voted first

Only a material instance dynamic has the functionality of setting parameters at run time. So the first step would to to create a material instance dynamic from the required material and apply it to a the primitive component. Luckily, Epic did the work for you and included a function in the primitive component class. Any primitive component like your static mesh component can call this function: virtual class UMaterialInstanceDynamic* CreateDynamicMaterialInstance(int32 ElementIndex, class UMaterialInterface* SourceMaterial = NULL, FName OptionalName = NAME_None);

Once you have the material instance dynamic you can call the set 'x' parameter value functions on it.

more ▼

answered Jan 21 '19 at 01:37 PM

avatar image

Roel
2.4k 69 42 141

avatar image vdlmrc Jan 22 '19 at 12:41 AM

Thank you! Material instance dynamic was exactly what I needed. I updated my question with the solution.

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

Have you tried with UMeshComponent::SetScalarParameterValueOnMaterials? you can call that on your Plane, or there is UKismetMaterialLibrary::SetScalarParameterValue but I don't really know what that @param: UMaterialParameterCollection is or how you get that.

Only downside of that method on the UMeshComponent would be that if you have the same parameter on more than one material on the mesh looks like it will change the scalar as well, so be careful.

Hope this helps.

more ▼

answered Jan 21 '19 at 01:39 PM

avatar image

virtual_nomak
356 15 16 25

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

Maybe it´s just enough to replace the auto and cast the return to a UMaterial, because the GetMaterial returns a UMaterialInterface. So perhabs try something like this:

 UMaterial* material = Cast<UMaterial>(plane->GetMaterial(0));
 
more ▼

answered Jan 21 '19 at 01:45 PM

avatar image

DaKenpachi
79 4 6 10

(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