I’ve just learnt about a nifty mechanism to change parameters on a material node in Unreal Engine. The niftyness lies in the fact that when we convert static values in the material editor into parameters, which them become the equivalent of public variables that are accessible from other parts of UE4. It sounds way scarier than it is, let me how you how it works in detail and step by step.
I’ve made myself a no-frills new Blueprint Actor, containing only a sphere. I’ll also create a basic material with a single node, setting the Base Color parameter to green, using a ConstantVector3 node. Here’s what it looks like:
Currently the colour can only be set via the picker. However, when we right-click on the constant vector node, I can convert this to a parameter. The colour itself will remain as is, but it now being a parameter, I can reference it from outside the material editor.
Let’s give it a name in the Details Panel, perhaps colour is appropriate (with a U).
Now we’ll apply this material to that no-frills sphere in our Blueprint and drag it into the scene. A green ball radiates at us ominously in the viewport.
Changing the colour via Blueprint
In our Sphere Blueprint, let’s hook into the Event Begin Play event. With our sphere selected, drag off and find a Create Dynamic Material Instance node. Our sphere should be connected to the target. This will give us a reference to its material.
On the output, add a Set Vector Parameter Value node. Make sure to set the Parameter Name to the same as we’ve set our material parameter earlier (Colour with a U in my case). Then fiddle with the value field and give it a new colour.
When we run the game now, our sphere should be a different colour. Excellent!
Animating Colour Values
Using a Timeline Node, we can animate colour values too. There are various ways to do this. I found that fading from one colour into another is a neat effect. That’s done using a Lerp Node. When we animate the alpha value with a timeline, we’ll see the effect shown at the top of this article, in which the sphere changes colour (click to enlarge). Neat, huh?