♪♪♪
Zak Parrish: Welcome back. In the last video, we
set up a basic Blueprint that we can use to show off dynamic
Material instances, and now we are ready to start constructing the graph for it.
Before we do that, you can see I have my RandomChair Blueprint
in the Content Browser. This is what we created in the last video. I am going to
drag-and-drop that right onto the scene. I will press the E key
to rotate it so it is kind of facing the Player Start. We will
also face it into the light so we can really make out the change in the Material.
With that done, we can now edit
the graph for this. I am going to do this in the Details panel.
You will see there is a tiny blue hyperlink we can click on that says
Edit Random Chair. When I click on that, it opens the graph.
There are two (2) parts to making a dynamic
Material Instance. The first part is actually specifying
within the Blueprint that you want the Material that is currently applied
to this object to be dynamic.
Then, you will set up whatever behavior you want to make that Material change in your
Event Graph. The first part of that takes place in the Construction Script.
Here in the Construction Script, we will determine what Material we are going to be using
and then make it dynamic. This is extremely easy to do. I am going to bring in
our Static Mesh. I suppose if I wanted to be nice and
concise about everything, I could rename the Static Mesh. We can jump over to
the Components tab. Here you can see StaticMesh1
up here at the top. Right-click on StaticMesh1 and choose Rename.
I will call this "Chair". Now when I click on the
Graph tab, you see the Static Mesh is now called Chair. I will
drag-and-drop this onto the graph and choose Get because we want to get
some information from this. Specifically, we want to get its Material
and make a dynamic version of it. Watch how easy this is. Drag a wire
out into open space and let go. Let's say we don't know what node we need.
We just kind of know what we want to do, which is make a dynamic Material.
Type in "Dynamic". At the top, select Create Dynamic Material Instance.
All we need to do is take the Construction Script
and connect it to the Create Dynamic Material Instance.
We are essentially done at this point. This node will automatically
grab the Material that has been applied to the Chair at Element 0,
or the first material that has been applied. It will create
a dynamic version of it where we can talk to all of those parameters
in the Material, such as the parameter named ColorSeats that we looked at
in the last video. There is one more thing I want to do that will make our job a lot
easier over in the Event Graph. I want to take this dynamic Material
and put it into a variable, so we can call it by name and talk
to it in our Event Graph. This is also very easy.
I am going to right-click on the Return Value output pin, and choose Promote
to Variable. That automatically creates the exact type of variable I need.
It is already set up and connected. It is just waiting for me to type in a name.
We will call this "Dynamic Material".
We are done. Click Compile and Save,
just to be on the safe side. Over in the
Event Graph, we can start creating the system by which
we will change color. We will start very simple, and then we will add some
randomization. Let's begin by selecting the actual
Box1 that surrounds the chair. This is going to be kind of like a trigger volume.
When the player enters this area, we are going to change the color
of the chair cushions. With Box1 selected,
right-click in the graph. You will see
Add Event for Box 1. Expand that.
Expand Collision, and choose
Add OnComponentBeginOverlap.
It is very easy. Next, we want
to talk to that Material parameter
that affects the seat color. It was actually called ColorSeats.
To do that, we need to get our reference to the dynamic Material,
which is that variable we created earlier. Hold down the CTRL key
and drag OnComponentBeginOverlap (Box 1) into the graph. That automatically creates a
getter or a get version of this variable. We know we want to get some
information from it.
Drag a wire out from Dynamic Material, type and select
"Set Vector Parameter Value".
All we need to do is specify the name of the property we want to control.
We looked that up in the last video. If you need a quick refresher, we can
do that in the Components tab. Click on the Components tab.
Select our Chair.
Double-click on the Static Mesh. You will see we can double-click on
the Static Mesh in the Static Mesh Group and that opens up the Static Mesh Editor.
At the very top, you can see the Material. Double-click the Material. In this way, it is
very easy for you to go through all of the different references that make up your object.
Again, here is the ColorSeats VectorParameter
that is driving the color of the actual chair cushion.
Go back to our Blueprint and return to the Graph tab.
Select the Set Vector Parameter Value node, and
we will set the Parameter Name to
"ColorSeats".
For starters, we are not going to worry about randomization. We are just going to set this
to a value of our choice. The seats are currently orange.
Let's set it to something on the opposite of the spectrum. The opposite
would be something like blue. Let's choose green. I don't know why.
I just want to see a green chair. Saturate it. Now, all we need to do is
connect the OnComponentBeginOverlap to the Set Vector Parameter Value.
When we detect an overlap with that Box component, we are going to change
the value within ColorSeats to green. That is exactly what this network says.
Click Compile and click Save. I am going to slide this
window offscreen, so I can bring it back later.
Click Play to give this a quick play. It looks like it already triggered.
I think my Player Start might be colliding with that Box.
You can see that is. If I look down from above, it is just starting to encroach.
Let's move the Player Start back. We can take the chair and slide it away.
Put a little bit of space in between them.
When we click Play again, you will the chair is orange. As we get close,
it changes color to green. That is exactly what we wanted
to see happen. With a successful test, let's augment
our graph just slightly so we are creating a
random value. We can do that in a few different ways.
The color Value input is currently a linear color.
If I drag off Value and type in "Random",
you notice we don't get any results. There is no
random color node. There won't always be a node for each and every
little thing you want to do by default. The trick is thinking back to
what we talked about in the materials lessons where we discussed
every color is really just a collection of numbers. We have numbers for Red,
for Green, and for Blue. We are going to choose three (3)
random numbers between 0 and 1,
and we will use those to make a color.
Right-click in the graph, and type "Random".
Select Random Float in Range.
We will leave the Min at 0.0. Set the Max to 1.
I am going to duplicate this node three (3) separate times because we want
three (3) random numbers: one for Red, one for Green, and one for Blue.
Drag a wire out from the Return Value of the first node.
We are going to use this to make a color.
Type and select, "Make Color".
Plug the second Return Value into Green.
Plug the third Return Value into Blue. There we go. We have constructed a color.
We can take the Return Value and plug it into the Value on the Set Vector Parameter Value.
We are done.
Click Compile and click Save. Let's give this a quick test.
Click Play.
This time we get kind of a blue color. Back out of the Box and come back in.
Now, we get green. Move away, come back in, and
now we get pink. We are getting a different random color each time we enter the Box.
To show this off, I am going try to make the most screen space.
I am going to slide the viewport over so I can see.
Let's do this a different way.
I am going to take the Event Graph and pull off
the actual tab for the graph itself.
Move the rest of the editor out of the way. Press the Windows key and tap the right arrow,
which will put the window on one side of the view.
This is just for fun to show you we can do cool things with the interface.
We will actually rip off the viewport. Hold down the Windows key and press
the left arrow. Now, I have a view on one side and I have
my graph on the other side. Press Alt+P to start playing.
Now whenever I enter that Box, you can actually
see the code firing and see the color change at the same time.
That is a quick look at making a dynamic
Material Instance. It is an instanced version of the Material that has parameters
that can change at run-time, or during gameplay. Again, there are a lot
of different ways to apply this. This is a very simple example. As you get deeper into UE4,
I am sure you will think of different ways you can change your materials during gameplay.
That is going to wrap things up for this video. Thanks a lot.
♪♪♪