Introduction to Unreal Engine Part 8
In chapter 8 of Rob Redman's comprehensive video introduction to Unreal Engine, he takes a closer look at adding random assets to your environment
My original plan was to cover dynamic physical objects in this part of the series but a few people have come to me with a similar question, so I will cover that instead. The question is about adding many assets to a scene that are similar but have some variation to them. I'm a big fan of natural chaos in a scene so I feel strongly about this and in UE4 it's actually quite simple.
You could simply add numerous assets by dragging them into the scene but then you would have to add all the materials and create all the variations manually but there are far easier ways we can do this. We have already taken a look at blueprints and they can help too. We will make a single blueprint which will contain a mesh and a material with a neat little setup that will ensure that every time it's added to a level it will make the random selection for us.
For simplicity I'll do this in the color channel of a simple object but the same technique applies to more subtle uses, such as rock color or tones of grasses and the like.
Part 1: Setting up a new project
Part 2: Introducing materials and landscapes
Part 3: Adding foliage and rocks
Part 4: Particle smoke
Part 5: Introducing the skybox
Part 6: Setting up cameras and post-effects
Part 7: Teleporters
Step 1: Static Mesh
To start off we will need base geometry that will be the underlying shape for our blueprint. You can use any object really but I've made a very simple, slightly misshapen sphere which I imported as an.FBX file. It's included for you here if you want to follow along exactly. I imported directly into my Draghorn Vale folder of the content manager. You can drag from there directly into your level but you will see it has the default material applied, which we don't want.
Step 2: Material basics
We've looked at certain material aspects in previous tutorials so I won't cover it too much here. What you need to do is make a new material and give it a name. I'm calling mine Diffuse. Double click to open it up and add a VectorParameter node, hooking it up to the color. Of course you link up more nodes here to create different materials. Name your new node someth9ing memorable in the details tab then save and close the editor.
Step 3: Applying materials
In the intro I talked about why it might be useful to take this approach and now it should become clear. If you have the mesh in the level you can drag the material onto it but you still have to add the material. There are a few more steps to take and the first is making our new Diffuse material the default for that object, so open up the Rock and drag the material into the appropriate slot in the details tab. Save and close. Now you can drag in as many of the rocks to the scene as you like and they will all have the diffuse material applied. We are one step closer.
Step 4: Graphing
Right click in the content manager and choose new blueprint class and from the menu choose actor. Rename the blueprint to rock_001_BP and open it. Drag your rock object into the components list and it should appear in your viewport. Move to the event graph view the drag it from the component list into the graph view and now we can set up the randomness.
Add an event BeginPlay node, so that our variations take effect at the start of the level. Add a dynamic material instance and hook up the event and rock (to the target) then on the return value add a vector parameter. Be sure to choose Diffuse from the Source Material dropdown of the node. This is telling UE4 which aspect of the rock we want to be affected by the setup. In our case Color, so type that into the Name field.
Add a make color node and hook it up to the value input of the parameter node. Finally add a Random Float in Range and use values from 0-1 for a full range. Hook this up to the R, G and B inputs of the Make Color. Save and Compile.
Step 5: More than gray
As I was setting this up I wanted the rocks to be a shade of gray, so I used one Random Float to control all the RGB inputs at once. This meant the single random figure would be the same for each, giving a 0-100% variation of gray. If you hooked up a separate random node for each channel you would get the possibility of any color imaginable. Of course you could limit this by choosing a range smaller than 0-1.
Also notice there is an alpha channel controllable in the same way, so you could have a map for your rocks with a second layer controlled by the alpha, meaning you could have a single texture and add some natural chaos with an alpha controlled color on top. This same set of tools will work on any detail of your material too, so you could have variations in Specular or roughness etc.
Top tip: Change over life
Rather than using Even Begin Play to set when the variation takes place you could use other events, meaning you can have the color adapt to pretty much anything that happens in your level, from simple time passing to contact with a player, projectile or other action. The tools open up all kinds of opportunities.