To add linear dunes to a scene we again need an algorithm.
The idealised cross section of a dune looks like this...
It has a straight line rise and then a concave fall.
The formula for a straight line is y = mx + c where m is the gradient and c is the position
on the y-axis where the line crosses the axis.
Lot of different algorithms could give us the concave part of the equation, but I want a simple one,
plus my maths is not that hot this days, so its back to my 'O'-level Maths.
The first thing that came to mind was a quadratic equation, most school kids probably know these, but as a refresher its the
+ bx + c type of equation that you get to try an resolve into (dx + e)(fx + g)
+ 5x + 2 = (2x + 1)(x + 2) . Plotting one gives a line that looks like a U or a V.
The equations I picked where y = 0.2x and y = x2 - 2x + 1 which conveniently resolves to (x-1)(x-1)
If you plot these two equations you get.
If we just use the lowest value of the two plot lines for any value of x we can see something which looks like our cross section
between x = 0 and x = 1. You may notice that I've gone for a quadratic equations of the form
(ax - a)(ax - a) as I know that at x = 1 the y value will be 0. If you increase 'a' from 1.0 to 2.0 you get a steeper concave slope.
So using (2x-2)(2x-2) instead you get this...
I'll add a way of adjusting this easily in the node tree so you can play with the values
later and pick a values that gives the nicest looking dunes.
So the algorithm we end up with is y = min(0.2x, (x-1)(x-1)) which looks like this...
As you can see the sand dune cross section sits between x = 0 and x = 1, outside of those
values we basically have a big slope, so we'll need to restrict the x values somehow.
Now we need to use that algorithm in TG2. First we need the x co-ordinate and we've been there before.
- Add a Get Position node and X to Scalar node.
To restrict the x value to between 0 and 1 we could use clamp, but there's a more interesting option
the Modulo Scalar node (Get Function/Divide/Modulo Scalar). Modulo return the reminder from a division
so 3.5 modulo 3.0 returns 0.5 . If we use 1.0 then we restrict the value between 0 and 1. It also has the
added bonus of repeating, so 0.5 mod 1 is 0.5, 1.5 mod 1 is 0.5, so we get a linear dune for every unit where as
Clamp Scalar would have given just one dune.
Add a Modulo Scalar and a Constant Scalar set to 1.0.
Connect the X to Scalar node to the Modulo Scalar left hand side input (Input node) and the Constant Scalar to the right hand side input (Input 2).
The rest is simple maths, first we need to multiple x by 0.2. The next new node in the tutorial is
the Multiply Scalar node (Get Function/Multiply/Multiply Scalar) it multiplies two scalar values.
- Add a Multiply Scalar
- Feed the Modulo Scalar into one input.
- Add a Constant Scalar node set to 0.2 to the other multiply input node.
- Name this 'Linear' so we can refer to it later.
(x-1)(x-1) breaks down to two steps,subtract 1 from x, then multiply the result by itself. However
I'm going to use three steps, multiply x by 1, subtract 1, then multiply the result by itself.
- Add a Multiply scalar node and a Constant Scalar Node set to 1.0 .
The next new node is the Subtract Scalar node (Get Function/Subtract/Subtract Scalar), like the multiply node does exactly
what it sounds like and subtracts two scalars.
- Add a Subtract Node.
- Connect the Modulo Scalar node to the Multiply Scalar node.
- Connect the Multiply Scalar node to the left hand input on the Subtract node.
- Connect the Constant Scalar Node to the multiply AND the right hand input Subtract node.
By connecting the Constant Scalar Node to the multiply and the Subtract node it's easy to change the steepness of the
concave slope by changing the value of the Constant Scalar. The Constant Scalar is the a in (ax - a).
TG2 provides us with a Square Scalar node (Get Function/Power/Square Scalar) which takes a single input
and squares it, which will does the next step nicely.
- Add a Square Scalar Node.
- Connect the Subtract node to its input node, rename the node 'Quadratic'.
Finally we need a min function which, as you may be able to guess if you've read Part 1 of this Tutorial, is the Conditional Scalar Node.
The condition we want is 'if Linear is Less than Quadratic then Linear else Quadratic' so the feed from
- Add a Conditional Scalar Node.
- Attach 'Linear' to Input Node and If result, 'Quadratic' to Comparison value and Else result.
- The Condition is set to 'Less than'.
- Attach the Conditional Scalar Node to a Displacement Node.
- Attach the Displacement Node to the Compute Terrain Node.
- Set the Camera 'Y' co-ordinate to 2.0
If you followed all that you should end up with this node tree.
Render the image to find a row and rows of perfectly linear sand dunes that run as far as the eye can see.
To make them less linear and a lot more dune like see the last two parts of the tutorial.