Labels

Saturday, September 6, 2014

How to Change Parameters in Dynamic Materials at Runtime in UE4 using Substance Designer and Blueprints


Substance Designer is an all-around great tool for texturing 3D models, but one of the features that I'm most excited about in my game development is Substance integration in UE4. Allegorithmic had already integrated with Unreal back in the UDK and UE3 days (I guess technically you can still use UDK and UE3), but they've only recently released a plugin for UE4 that doesn't require recompiling the engine for it to work.

The Substance integration is huge because not only does it mean that you can save time importing textures and creating materials by simply dragging and dropping an SBSAR file into your game, it also means that you can dynamically change any values that you've "exposed" in your Substance graph at runtime.

This is a tutorial on how to manipulate your exposed Substance variables at runtime in your game using a key press.

(Skip to step four if you want to cut straight to the blueprints)

1. To begin, import your Substance SBSAR into UE4. For this tutorial, I chose the "shutter" substance that came with SD when I purchased it. (Note: If you are getting an error on import, make sure that you have the plugin installed from the Allegorithmic Website).



2. Create and import the mesh you want to assign to your Substance. Because the Substance I'm going to use is meant to be placed flat on the face of an object, I made a simple box in Blender, created seams on the wide faces on both sides, and made a seam on one of the corners. I assigned the same material to the wide faces (blue), and a different material to the outer rim of the box (green). (This isn't necessarily the best practice for making a game asset, but for simplicity in this tutorial, this is how I'm going to do it)



If you are following along with the same Substance, in the material editor, set both the U and V tiling to 4 so that the material will be centered on the mesh. I also set the Blend Mode to "Masked" and plugged the opacity texture into the "Opacity Mask" node so that you can see through the open slits in the vent.


3. Next place your object into your level.



Now it's time to set this thing up so that we can dynamically change the Substance to make it look like we're opening and closing the vents.

(Note: the next several steps only provide a text reminder of which button to press and aren't necessary. If you want to skip adding the the text reminder, skip to step 9)

4. In the Content Browser, right click your mesh and select the option to "Create Blueprint Using." Choose where you want to save the blueprint, and hit "OK."



5. Under the Components section of the blueprint, add a Box and a Text Render Component. Make sure the box is large enough so that your player will be able to walk inside of it in your level.



6. Resize your text so that it will fit in front of your vent. Under the "Text" heading in the Details Tab, type your message. I used "Press E to toggle vent." Under the "Rendering" heading, uncheck the box next to "Visible."



7. Compile your blueprint and switch to Graph Editing Mode. Click to select your box in under the "Variables" section of the My Blueprint Tab. Right click on the event graph and under the context menu, expand the selection for "Add Event" > "Collision"  and then click "Add OnComponentBeginOverlap" to add that node to your graph.



8. Drag your "Press E to Toggle Vent" text variable onto the graph and choose to "get" it. Drag a wire from that node and use the context menu to add a "Toggle visibility" node. Connect that to the node from step 7, as shown.



9. Now switch to your level blueprint. Create a float variable and name it "Aperture Value" (pictured as "test value") and create a boolean and name it "ShutterIsOpen."

Right-click in your graph and press the "E" key and select the "E" key under input from the context menu.



10. Drag a wire from the "pressed" pin in the "E" node and use the context menu to create a branch. Drag a "ShutterIsOpen" boolean into the graph and connect it to the condition in the branch.



11. Drag two "Aperture Value" nodes (again, pictured as "Test Value") onto the graph and choose to "set" them. Connect the True value to one of the "Aperture Value" nodes and connect the False value to the other. 

Type in 0.1 for the "Set" value on the "Aperture Value" node connected to the True branch. Type 0.9 for the False branch.



12. Create a variable and set the type to "SubstanceGraphInstance" and name it SubstanceGraphInstance (hereinafter "SGI"). Drag the SGI onto the graph. Drag a wire from the pin and use the context menu to create a "Set Input Float" node. 



13. Connect the pins from both of the "Set" nodes to the "Set Input Float" node. Delete the Substance Graph Instance node from your graph.

Create two "Make Array" nodes and plug one into the "Target" pin and the other into the "Input Value" pin of the "Set Input Float" node.  In the "Identifier" box, type "Aperture" (or whatever the variable you exposed in your Substance is named in the Substance Graph Instance)



14.  In the "Make Array" node you connected to the "Target" pin, use the dropdown box to choose the Substance Graph you plan to modify at runtime.

Get another "Aperture Value" node and plug it into the "Make Array" node that is connected to "Input Values"



15. Set a "ShutterIsOpen" variable on the graph and connect the "Set Input Float" node to it. Get an "Aperture Value" node and drag a pin from it and create a "Float is > = Float " node. Connect the boolean pin from that node to the boolean pin of the "Set Input Float" node.



Compile and save your blueprints and you should now have a working material that toggles when you press the "E" key.



Note: if you have trouble getting this to work, make sure you correctly type in the "Identifier" from your Substance Graph.

I plan on making more tutorials on manipulating Substances at run time as ideas come to me. If you have any suggestions for tutorials on this topic, feel free to leave a comment.

2 comments:

  1. hey thank you for this tutorial. one question. when having multiple of these vent in one level. would they then open all at once, when opening one of them, or is the substance created at runtime one for each blueprint instance? greetings! sascha

    ReplyDelete
    Replies
    1. Sorry I didn't see your comment earlier. I believe you are right. I made this tutorial very early on in my learning of UE4 Blueprints (mostly because I couldn't find any literature on it), so it should definitely be used more as a loose reference to get started. Hopefully, if time permits, I'll be able to make an updated tutorial that will be more useful for actually using Substances in a game.

      Delete