Creating Dynamic Materials
In this tutorial, we will be creating a Dynamic Material Instance on blueprints to change a texture dynamically. We will be utilizing a ZD Actor component and expose it to the Reality Hub as a node.
Steps
- Go to the Content Browser tab, click on the ADD/IMPORT button and select the Blueprint Class.
- Select the Actor from the Pick Parent Class popup menu.
- Rename your Actor as DynamicMaterialActor, and double click on it. This action leads you to the Blueprint UI.
- Go to the Components tab, click on the + ADD COMPONENT button.
- In the Search Component bar, type ZD Actor and click on it.
- Click on the + ADD COMPONENT button again, find and add the Static Mesh component
- Go to the Details tab, change the Static Mesh property from None to Cube
- Go to the My Blueprint tab, click on the + ADD NEW button, and select the Variable. This action adds a Boolean type variable by default
- Select the new variable you added and change its Variable Name to MyTexture
- Click on the Variable Type property
- Find and select the Texture Render Target 2D
- Click on the Instance Editable checkbox in the Details Tab
- Click on the COMPILE and then the SAVE button
- Switch to the main Editor interface
- Go to the Content Browser tab, click on the ADD/IMPORT button and select the Material
- Rename the material you added as MyMaterial and double-click on it. This action opens the Material Editor UI.
- Right-click on the Material Editor Canvas, type TextureSampleParameter2D in the search bar
- Select the TextureSampleParameter2D from Parameters group
- Change the name of TextureSampleParameter2D to MyTextureParameter
- Connect the RGB output pin MyTextureParameter to the Base Color input pin of the MyMaterial
- Click on the APPLY and then the SAVE button
- Switch to the main Editor UI
- Find the MyMaterial inside the Content Browser
- Right-click on the MyMaterial and choose the Create Material Instance option
- Double click on the DynamicMaterialActor and switch to the Construct Script tab inside the Blueprint UI
- Right-click on the Blueprint Canvas
- Find and add the Create Dynamic Material Instance (Static Mesh)
- Select the Create Dynamic Material Instance (Static Mesh) function
- Connect the event output pin of the Construction Script into Create Dynamic Material Instance’s event input pin
- Go to Source Material > Select Asset and choose MyMaterial_Inst from the search bar
- Right-click on the Return Value output pin of the Create Dynamic Material Instance and select the Prompt to Variable.
- Your last action creates a new variable called NewVar_0. Change the variable name to MaterialInstance
- Go to the My Blueprint tab, click on the + ADD NEW button
- Select the Function, rename it as OnChanged_MyTexture, and declare it as a Public
- Right-click on the canvas, find and add the Set Texture Parameter Value from the search menu under Rendering > Material
- Connect the event output pin of the On Changed My Texture into Set Texture Parameter Value’s event input pin
- Drag and drop the MyTexture variable to the canvas and select Get MyTexture in the popup menu
- Connect the MyTexture’s output pin to the Value input of the Set Texture Parameter Value
- Drag and drop the MaterialInstance variable inside the canvas and select Get MaterialInstance in the popup menu
- Connect the Material Instance’s output pin to Target input of Set Texture Parameter Value
- Change the Parameter Name of the Set Texture Parameter Value to MyTextureParameter
- Click on the COMPILE and then the SAVE buttons
- Switch to the main Editor UI
- Drag the drop DynamicMaterialActor into your scene
- Click on the PLAY button
- Switch to the Reality Hub and activate the Nodegraph/Actions module
As soon as you click the PLAY button on the Reality Editor interface, your Dynamic Material Actor get exposed to the Reality Hub, as shown in the image above
- Create the following node tree inside the Reality Hub Nodegraph
As the picture above shows, as soon as you connect the OUTPUT pin of the CHECKERBOARD node to the MY TEXTURE input pin of the DYNAMIC MATERIAL ACTOR node, the texture gets changed on the fly.