In this quick-tips tutorial we're going to answer a common question, "how can I randomise items so that adjacent segments and materials IDs are not repeated?". It is the nature of true random generators that they will occasionally output a sequence with repeating values. This can be undesirable when it results in repeating material IDs or geometry. Unfortunately neither RailClone's built in Random node nor its expressions features have an option to output a non-repeating sequence, but there is a nifty cheat we can use to create a similar effect.
To illustrate, we'll create a play mat style that features 10 numbered segments. Each segment uses two material IDs: ID 1 is used for the colour of the tile, and ID 2 is for the number.
If we distribute the segments along a single spline using a L1S Generator via a Randomise Operator you will see that occasionally adjacent segments are the same.
To fix this divide the number of segments in half so (in this example) you have 2 groups of 5 segments each. These two groups are placed alternately to avoid repetition. To do this:
You now have no repetition because odd items are randomising between segments 1 - 5 and even items are randomising between segments 6-10. Repetition on adjacent segments is impossible!
This works great for 1D arrays, but what about 2D arrays? If we wire the same setup to an A2S generator you'll see that there is no repetition horizontally along each row, but an awful lot of repetition vertically, in columns.
Fortunately this too is quite easy to fix. Remember odd items use segments 1-5 and even items use segments 6-10. If we switch the items being used in odd and even segments on alternate rows we will create a grid of randomised segments that doesn't repeat horizontally or vertically. The table below makes this clearer.
... | 1 (odd) | 2 | 3 (odd) | 4 |
---|---|---|---|---|
4 (even) | 6 - 10 | 1 - 5 | 6 - 10 | 1 - 5 |
3 (odd) | 1 - 5 | 6 - 10 | 1 - 5 | 6 - 10 |
2 (even) | 6 - 10 | 1 - 5 | 6 - 10 | 1 - 5 |
1 (odd) | 1 - 5 | 6 - 10 | 1 - 5 | 6 - 10 |
To create this setup:
You will no longer see any repetition between adjacent horizontal and vertical segments!
Exactly the same technique can be used to randomise non-repeating material IDs. To make this a little easier, recent versions of RailClone include a macro to do all the hard work for you. To use the macro:
In the scene file that accompanies this tutorial there's a macro called NonRepeatRandomInt. This will also be included in our next update to the built-in macros. It works using exactly the same principle as the examples shown above, except it outputs non repeating integers based on a segment's position in the 2D grid. This means you can use it with anything that is controlled using an index. For example, to randomise non-repeating geometry using this macro instead of using random and sequence operators as we saw earlier, we can simply do the following:
Though this is a bit of a hack, it's quite effective for preventing the repetition of the same geometry, materials and more on adjacent segments. In this tutorial we also explored a couple of recent macros, in future releases we'll look in more depth at several other macros that can be used to speed up and simplify your work, in addition to extending the plugin's core features.