Posts

1. Introduction

Image
Is this workshop for me? This workshop is initially designed for Game Developers at HKU, who want to explore Technical Art. But anybody with an interest in technical art is welcome to follow this workshop. What do I need? Within this workshop you are expected to have a core understanding of programming with C++ or C#. Also a minimum understanding of Unreal Engine 4 (UE4) is required.  It is advised to have a working PC or laptop, that can handle UE4, to participate in the workshop and its challenges. This workshop will be making use of version 4.26 of UE4. Unreal Engine 4 is a free engine, which you can download here: https://www.unrealengine.com/en-US/download . Make sure that you choose C++ at “Choose whether to create a Blueprint or C++ project”, when creating your own project. Img. 1, choosing for a C++ project At the end of this workshop When you have finished this workshop, you will be able to create your own effects using dynamic materials. This workshop will teach you abou...

2. Background information

What is a dynamic material? A dynamic material is a material that can be altered during runtime. Through C++ you can edit the parameters of a dynamic material, which allows you to create wonderful visual effects. A dynamic material is a term that is used by Unreal Engine 4. Back to the previous lesson | Continue to the next lesson

3. Preparation

Image
Creating a C++ script in UE4 Before we can start with the fun stuff, we need to create a script where we can work on. We need to create one script for this workshop, where we will put all of our code in.  In order to create a script, we need to access the “C++ Classes” folder in the content browser. Click on the folder icon left of “Content” in the content browser. A window will open where you can at least choose between “Content” and “C++ Classes”. It’s possible that you see extra folders that are related to the engine. You can hide and unhide these folders in your settings. Click on “C++ Classes” to access the C++ Classes folder. In this folder you can find all of your C++ scripts in the project. Img 2, Accessing the “C++ Classes” folder Right click in the content browser. A window will appear like in image 3. Click on “New C++ Class”. Img 3, Clicking on “New C++ Class” to create a new script A window will ask you to choose a parent class. UE4 has different parent classes for di...

4. Setting up a material for the dynamic material

Image
Before we start programming our dynamic material, we need to create a static material. This material will be used to create our dynamic material. You can create a new material by going to the “Content” folder in the Content Browser. Right click in the content browser and click on “Material”. Img 10, Creating a material By double clicking on our material, a new window will open where we can edit our material. First let’s create a vector parameter. Right click on the grid and type “Vector Parameter”. Img 11, Searching and selecting the vector parameter node Click on the “Vector Parameter” option. A new node will appear. By clicking on the node, you can edit the details of the node. For us, it’s important to set a clear parameter name. The parameter name will be used for our code later. I’ve called my node “EmissionColor”, because this parameter is going to determine what color our dynamic material is going to emit. Img 12, Setting the vector parameter name Let’s create a scalar parameter...

5. Constructing a blueprint

Image
We need a blueprint where we can combine our assets. A blueprint will allow us to create multiple instances that we can place in our level. You can create a blueprint in the same way as creating a material. Go to the Content Browser, right click in the content and select “Blueprint Class”. Img 17, Creating a blueprint class A new window opens. A blueprint is by UE4 seen as a type of class. We need our blueprint to inherit our class “DMObject” in order for our blueprint to have our code. Open “All Classes” at the bottom of the window and type in your class, I used “DMObject”. Click on your option to continue. Img 18, Creating a BP that inherits “DMObject” Open the newly created blueprint. We need to add a component that can hold a mesh with our material. Click on “+Add Component” in the left upper corner. A window will pop-up with a list of predefined components. Click on “Static Mesh” to create a static mesh component. Img 19, Adding a static mesh component Let’s choose a mesh that can...

6. Creating and editing the dynamic material

Image
Before we can create our dynamic material, we need to find our static mesh component that contains our material. Img 23, Code of finding our static mesh component and creating and setting our dynamic mesh With the “GetComponentByClass()” function we are asking UE4 to return to us the first component object defined within the round brackets. We want to receive our static mesh component, so we use “UStaticMeshComponent::StaticClass()”. The function returns a “UComponent*”. With the “Cast<T>()” function we can convert the returned object between the round brackets to our class defined in position of “T”.  We use the “IsValid()” function as a security to make sure that the cast has been executed successfully. If the cast has failed and we were to try to access an empty pointer, our game would instantly crash. Within the if-statement we put the rest of our code. Line 25 on image 23 creates a dynamic material and assigns it to our pointer. Within the function we use “SMC->GetMat...

7. Testing the dynamic material

Image
Do not forget to compile the code. Else the system will not recognize the changes that have been made in the code, which will cause the new code to not be executed. Press on compile to compile your code. Img 26, Pressing on compile to compile new code In order to test our dynamic material, we need to place our blueprint in the level. You can do this by dragging the blueprint into the level. I’ve placed multiple instances of my blueprint to see if every object is different at start up. Img 27, Placing multiple blueprint instances into the level Now we can start our level to see if our code works. Press on play to start the level. Img 28, Pressing on play to start the game We should be seeing three spheres, with each a different emission color and emission strength. Just like in the image below. Img 29, Three spheres with different emission colors and strengths Back to the previous lesson | Continue to the next lesson

8. Reading parameters of the dynamic material

Image
For creating different types of effects it can be very useful to read the parameters of a dynamic material. Let's read and output our emission color right after the effect we created earlier. Img 30, Reading and outputting emission color For our reading function, we need to create a variable placeholder that can contain the information of our color. You can see on line 38 of image 30 that I’ve created a FLinearColor placeholder called “ReadVector”. To read a vector parameter, we need to use the “GetVectorParameterValue()” function contained in our dynamic material. The function needs the parameter name, which we declare in the first parameter slot. In the second parameter slot we put our “ReadVector” placeholder. Next, we want to output our emission color to the output log of UE4. That way we can test if our code works. On line 40 of image 30 you can see how to do that. I am outputting the name of the object, following the read color. Reading the scalar parameter value is similar. ...

9. Challenge

I challenge you to add an extra effect to your dynamic material, using the knowledge you have learned in this workshop. If you don’t know what effect to add you can use this assignment: Creating a Disco Sphere Create a dynamic material that changes color every 0,3 seconds. Every time the dynamic material has a color with at least 70% red, increase the emission strength. When the emission strength is above 5.0f, reset the emission strength to 0.0f Share your assignment through social media by using the #DMChallenge! Back to the previous lesson