Unreal Engine 4.24
Material Functions encapsulate shading flow graphs (material blueprints) into reusable shading nodes that have their own inputs and outputs.
This allows development of custom shading nodes, and saving the time it takes to recreate the same flow graphs multiple times or even copy and paste material flow graphs.
Common shading processes and operations that have to be performed in many different materials, and even multiple times in a single material can be defined as a Material Function for quick and easy re-usability.
Material functions can also be used to encapsulate a full material blueprint with a Material Attributes output. this provides a convenient workflow for blending different materials together.
In this post I’ll detail the steps needed to create and use a Material Function.
The Material Function example we’ll create, called “ColorAngleBlend” performs a commonly needed shading operation of blending 2 colors or textures according to the surface viewing angle (facing ratio).
The ColorAngleBlend Material Function will have the following inputs:
- color a:
The color or texture appearing when viewing the surface at perpendicular angle.
- color b:
The color or texture appearing when viewing the surface at grazing view angle.
- curve exponent:
The steepness of the blend curve between the colors, 1 being a linear blend and higher values displaying color a at more angles “pushing” color b to be seen only at grazing angle.
- base color blend:
The percent of color b seen at perpendicular view angle.
bump normals input.
The final “ColorAngleBlend” Material Function Blueprint:
* The internals of the “ColorAngleBlend” Material Function
An example of the “ColorAngleBlend” Material Function node used to create a reach view-angle dependent color blend for a steampunk metal material:
An example of the “ColorAngleBlend” Material Function node used to create a reach color for a car-paint material:
An example of the “ColorAngleBlend” Material Function node used to create a washed-out effect for a cloth material:
Steps for creating the “ColorAngleBlend” Material Function:
- In the content browser, create a Material Function Object and name it “ColorAngleBlend”:
- Double click the ColorAngleBlend Material Function to open it for editing:
- Click the background of the work space to deselect the Output Result Node,
So that the Details panel on the left will display the Material Functions‘s properties.
Type a description into the Description field, check the Expose to Library option so that the new Material Function will be available to all materials in the Palate and node search, and define in which node categories it should appear:
- Select the Output Result node and in the Details panel on the left set its output name to “color”:
- Add a Linear Interpolate (Lerp) node, a Fresnel node and a Transform Vector (Tangent space to World space) node to the Blueprint and connect the nodes like this:
* The Lerp node will blend the 2 color inputs with the Fresnel providing view angle data as the alpha for the Lerp.
The Transform Vector node is needed to convert normal (bump) input to world space for the Fresnel node.
- Adding function inputs:
Create 2 Function Input nodes, in their Details panel, name them “color a” and “color b”, leave their Input Type as default Vector3D, check the option Use Preview Value as Default, number their Sort Priority parameters 0 and 1 to make them appear as the first inputs of the ColorAngleBlend node as it will appear when used in a material, and connect them to the Lerp node’s A and B inputs:
- Adding function inputs:
Create 2 new Function Input nodes, name them “curve exponent” and “base color blend”, this time set their Input Type to Scalar, check the option Use Preview Value as Default, set their Sort Priority parameters to 2 and 3 and connect their outputs to the Fresnel node’s ExpoentIn and BaseReflectFractionIn inputs:
- Adding function inputs:
Create the final Function Input node, name it “normal“, set its Input Type to Vector3D, check its Preview Value as Default option, set its Sort Priority to 4, and connect its output to the Fresnel node’s Normal input:
- Adding default inputs:
Finally, add constant nodes to serve as default input values for the Material Function.
A pure white Constant3Vector (color) constant as the default value for “color a” input,
A pure black Constant3Vector (color) constant as the default value for “color a” input,
A Constant with value 1.0 as the default value for “curve exponent” input,
A Constant with value 0.0 as the default value for”base color blend” input,
A pure blue Constant3Vector (color) constant as the default value for “normal” input.
> Tip for quick creation of constant value nodes
- Save the new Material Function.
To use the new ColorAngleBlend Material Function create a new material, in the node search start typing color… to locate the ColorAngleBlend node and create it, and connect it to the desired material input.
> Material Functions can also be used by dragging them from the Content Browser into the Material Blueprint.
UE4 Material Instances
UE4 Fresnel node
Unreal Engine 4.24
To create animations and trigger them to play on game start:
First create a Level Sequence containing the animation:
- Create a new Level Sequence actor:
- Name the new Level Sequence and drag it to into the level:
- Select the actor you want to animate in the level and double click the Level Sequence in the Content Browser to open it in the Sequencer window:
- In the Sequencer window, press the +Track button to add a sequence track, choose the upper most option Actor To Sequence, the option to Add the selected actor will automatically appear first on the menu that will open on the right:
- Add the selected actor as a sequence track, expand the track’s Transform channels to reveal the Transform property you would like to animate, and click the + button for that channel to create the first key-frame:
- Activate the Create when channels/properties change option button:
- Move the time slider to a desired time for the motion and move/change the actors transform to create a new key-frame:
The Level Sequence now contains animation for the Actor, but when we play the game, the animation doesn’t play.
For the animation to play in game, we must trigger it fro a Blueprint, in this case the Level Blueprint:
- From the Editor Blueprints menu, choose Open Level Blueprint:
- In the Level Blueprint, drag the Event BeginPlay execution graph and create CreateLevelSequencePlayer node that will follow it:
- Drag the CreateLevelSequencePlayer node’s Return Value output and create a Play node that will be executed after it and receive it’s output:
- The Level Blueprint now has instructions to play a Level Sequence,
but it’s not yet specified which Level Sequence to play:
- In the Variables list on the left, press the +Variable button to create a new variable and name it:
- With the new variable selected, in it’s details on the right, press the Variable Type button, and locate Level Sequence – Object Reference type:
- The Level Blueprint now contains a variable named seq of type: Level Sequence – Object Reference:
- Drag the new variable to the Blueprint and choose Get when placing it:
- Connect the variable’s output to the Level Sequence input of the CreateLevelSequencePlayer node:
- With the variable selected, in the details panel on the right, select the Level Sequence object it will be referencing:
- Press Compile and save the Level Blueprint:
The Level Blueprint now has instructions to play the desired Level Sequence when the level begins playing so a the animation we created plays when we hit play game in the editor:
UE4 Camera Animation
Unreal Engine 4.18
The Delay node serves as a timer that will execute the connected blueprint after a predefined time in seconds.
An example of usage could be triggering a game event at a set time after the player entered a location.
You can use random values or other expressions connected to the Duration input to create a richer and less predictable interaction.
Unreal Engine 4.18
A direct reference to another actor instance can’t be created from within the blueprint because it can only be provided at run-time after the instances have been created, both the current actor and the other actor we want to refer to.
In the Actor Blueprint:
- Add a new variable of type Actor > Object Reference
- Make the variable public and editable in the editor (the eye button..)
- Hit Compile and Save.
In the Map Editor:
- Place both actors in the map and save it.
- Select the actor in which you created the reference variable.
- In the Details panel, set the Actor property you created to the wanted other Actor instance.