Unreal Engine 4 Materials Tutorial

In this Unreal Engine 4 materials tutorial, you will learn how to modify textures in the material editor, create material instances and update material instances during gameplay. By Tommy Tran.

Leave a rating/review
Save for later
Share
You are currently viewing page 3 of 4 of this article. Click here to view the first page.

About Dynamic Material Instances

Unlike a regular instance, you can edit a dynamic material instance during gameplay. You can do this using either Blueprints or C++.

You can use dynamic instances in a variety of ways such as changing an object’s opacity to make it invisible. Or, you can increase an object’s specularity as it gets wet.

Another good thing about dynamic material instances is that you can individually edit them.

Below is an example of updating individual instances to mask out areas of an object.

Let’s start by creating a dynamic material instance.

Creating a Dynamic Material Instance

You can only create dynamic material instances during gameplay. You can use Blueprints (or C++) to do this.

In the Content Browser, go to the Blueprints folder and double-click on BP_Player to open it.

The first thing you will do is create a new dynamic material instance and then apply it to the cube mesh. It is a good idea to do this when Unreal spawns the actor, which is the purpose of the Event BeginPlay node.

Make sure you are in the Event Graph and then locate the Event BeginPlay node.

Now, add a Create Dynamic Material Instance (StaticMesh) node. This node will simultaneously create and apply a new dynamic material instance to the cube mesh.

Next, you need to specify which material the cube should use. Click the drop-down under Source Material and select M_Cube.

To easily reference the material later, it’s a good idea to store it in a variable. An easy way to do this is by right-clicking the Return Value pin on the Create Dynamic Material Instance node. Afterwards, select Promote to Variable.

If you look at the My Blueprint tab, you’ll notice you have a new variable. Rename it to CubeMaterial. You can quickly do this by pressing the F2 key.

Finally, link the Event BeginPlay node to the Create Dynamic Material Instance node.

Summary: once Unreal spawns BP_Player, it will create a new dynamic material instance and apply it to the StaticMesh component. It will then store the material into a variable named CubeMaterial.

The next step is to create a counter to keep track of the amount of bananas collected.

Creating the Banana Counter

If you pan down a little bit from the Event BeginPlay node, you will see the setup below. This is where you will update the banana counter and material.

The On Component Begin Overlap node will execute when the cube overlaps another actor. Next, the Cast to BP_Banana node checks if the overlapping actor is a banana. If the actor is a banana, the DestroyActor node will destroy it so it disappears from the game.

The first thing to do is to create a variable to store the amount of bananas collected. Afterwards, you will increment the variable by one every time the cube overlaps a banana.

Create a new Float variable and name it BananaCounter. Drag-click the BananaCounter variable into the Event Graph and select Get.

To increment the counter by one, add an IncrementFloat node. Once created, connect BananaCounter to it.

Next, connect the DestroyActor node to the IncrementFloat node.

Now, whenever the player collects a banana, the BananaCounter variable will increment by one.

If you were to use BananaCounter as the alpha right now, you would get unexpected results. This is because the LinearInterpolation node expects a value in the range of 0 to 1. You can use normalization to convert the counter to a range of 0 to 1.

To normalize, simply divide BananaCounter by a max value. This value is how many bananas the player needs to collect before the cube is completely red.

Add a float / float node and connect its top pin to the remaining pin of the IncrementFloat node.

Set the bottom input of the float / float node to 6. This means the cube will be completely red once the player has collected 6 bananas.

There is a small problem. When the player collects more than 6 bananas, you will get an alpha greater than 1. To fix this, use the Clamp (float) node to keep the alpha in the range of 0 to 1.

Add a Clamp (float) node and connect the Value pin to the right pin of the float / float node.

Now, that you have an alpha, it’s time to send it to the material.

Updating the Material

Drag-click the CubeMaterial variable into Event Graph and select Get.

Next, drag-click the pin of the CubeMaterial variable onto an empty space and then release left-click. This will bring up a list of nodes that can use this type of variable. Any node selected will automatically link with the variable. Add a Set Scalar Parameter Value node. This node will set a specified parameter to the supplied value.

Now, you need to specify which parameter to update. Set the Parameter Name field to ColorAlpha. This is the parameter you created in the cube material.

Link the result of the Clamp (float) node to the Value pin of the Set Scalar Parameter Value node.

Finally, link the IncrementFloat node to the Set Scalar Parameter Value node.

Here is the order of execution:

  1. On Component Begin Overlap (StaticMesh): Executes when the cube mesh overlaps with another actor
  2. Cast to BP_Banana: Checks if the overlapping actor is a banana
  3. DestroyActor: If the overlapping actor is a banana, destroy it so it disappears
  4. IncrementFloat: Increments the BananaCounter variable by one
  5. float / float: Divides the counter by a specified number to normalize it
  6. Clamp (float): Clamps the result of the division so that you don’t get a value higher than 1
  7. Set Scalar Parameter Value: Sets the ColorAlpha parameter of the cube material to the supplied value. In this case, the value is the normalized and clamped version of BananaCounter

It’s time to test it out! Click Compile and then close the Blueprint editor.

Click Play and start collecting bananas. The cube will start off as white and progressively become more red as you collect bananas. Once you collect 6 bananas, it will be completely red.

Tommy Tran

Contributors

Tommy Tran

Author

Over 300 content creators. Join our team.