Dynamic Materials: Random Colours

Written For: 4.17 Estimated Duration: 30 mins Difficulty: Medium

A problem I have often come up against whilst set-dressing, is placing random coloured meshes with-in a predefined palette. There are different ways to address this problem; you can spawn them on play and define the colour on spawn; you can turn each mesh into it’s own Blueprint and randomise each colour per Blueprint; but today I came across the scenario where I wanted to place meshes by hand (flowers), inside another Blueprint (which was then spawned into the world) and have them randomly coloured. Here’s my solution.

dynamicColours1

To begin, create a new Material. Create a vector 3 Parameter (hold down 3 whilst clicking then right-click and convert to parameter) and name it something memorable in the Details Panel, I called mine ‘flowerHead’. Hook it up to Base Colour/Emissive and save and close.

dynamicColours2

Next open up the Blueprint you want to position the meshes inside of and add them. Once you are happy with your placement, select all of the meshes inside the Components Panel and type ‘tag’ into the Details Panel search bar. Click the + to add an element and type in the tag name, I called mine ‘flower’. Compile and save.

dynamicColours3

Open up the Construction Graph as it’s here we’ll write the code. The first part collects the meshes with the tag we defined earlier using a Get Components by Tag node. Set the Component Class to Static Mesh Components and the Tag to ‘flower’. We then loop through each mesh creating a Dynamic Material Instance.

dynamicColours6

The Source Material is the name of the Material you created and the Element Index represents which Material slot you want to modify. If you aren’t sure which one you are changing, open up the Static Mesh Editor and look in the Details Panel.

dynamicColours4

The second part of the code creates an array of pre-defined colours. To make an array, add a Make Array node and then right click on the small square grid and promote to variable, I called mine ColourArray. For choosing the palette colours, the easiest way I found to do this was to create a nice set in the Material Editor and then copy the numbers across into each vector 3 variable. You can add as many as you want by clicking on Add Pin.

dynamicColours5

From there a random colour is chosen by using a Random Integer in Range node (remember arrays start at 0 and for scalability I used the array’s Last Index as the high bounds so more values can be added easily).

The final part is to set the colour via a Set Vector Parameter Value. The Target is the Return Value of the Create Dynamic Material Instance, the Parameter Name is the name of the parameter inside of your Material (mine was flowerHead)  and the value is the result of the Get from your array.

dynamicColours4

Compile and Save and go back to the viewport. Either by compiling or moving a component in the editor you cause the construction script to run and the colour to change.

flowersDynamic

A couple of notes: you can’t alter the colour of just one mesh (all of them change every-time), but because of this on spawning the Blueprint, the colours will always be different. If you want the initial placement to be random, but then remain the same you can replace the Random Integer in Range with a Random Stream. You can also go to the Class Defaults of your Blueprint and disable Run Construction Script on Drag.

As shown in the above .gif you can also extend this script for more than one colour. You could do it two ways, by having specified each coloured area on the mesh as a separate Material slot (easy, but not necessarily recommended as it increases draw calls) and making each dynamic as before.

dynamicColours7

Or, by having a texture mask that corresponds to the UV layout and using that to lerp between two colour parameters.

dynamicColours8dynamicColours9