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.
In my C++ class I can access to the material with this code:
Then, what I would like to do in the C++ class is something like
But there is no such function... There is only a
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!
ollowing Roel advices, I created a MaterialInstanceDynamic, and called
In the .f file I added:
In the .cc file I added:
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.
answered Jan 21 '19 at 01:37 PM
Have you tried with
Only downside of that method on the
Hope this helps.
answered Jan 21 '19 at 01:39 PM
Maybe it´s just enough to replace the
answered Jan 21 '19 at 01:45 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here