A function only Landscape : Part One - A Pyramid

Started by David Burnett, March 25, 2007, 01:37:48 PM

Previous topic - Next topic

David Burnett

This tutorial was originally released in the file sharing forum at [url]http://forums.planetside.co.uk/index.php?topic=843.0[url]

To make a pyramid using a function, we need an algorithm that represents a pyramid.

So lets look at a rough plan of an upside down pyramid.

[attachimg=2]

The 'height' of the upside down pyramid at any point is highest distance between the x or y co-ordinate
and the y or x axis repectively, i.e. z = max(x, y).
We then limit the size of the pyramid by stopping the measurements at a
maximum value of x and y, oh and we need to turn it the right way around.

Right let fire up TGTP and don't forget to save often.

So lets see how we make a pyramid. First we need to extract the 'x' and 'y' values.
In TG2 the co-ordinate system has X and Z as the co-ordinates on the plane and Y is up - down.
so what we really need is the X and Z co-ordinates.

To get the current co-ordinates we use the Get Position Function (Create Function/Get/Get Position).
To extract the individual ordinates there are three functions in Create Function/Convert

  • X to scalar
  • Y to scalar
  • Z to scalar

These take the in a vertex, and output the individual ordinates as scalar values.
We will use 'X to scalar' and 'Z to scalar' feeding Get Position into both.

We want the distance between the ordinate and the adjacent axis, which effectively mean we want to
throw away the ordinates sign. This is called the absolute value, and the function that gets the absolute value
for a scalar is Create Function/Clamp/Abs scalar. There also a Create Function/Clamp/Abs vector that we could use
on the output from Get Position, but I'm going to change things up there later.  So add two Create Function/Clamp/Abs Scalar nodes and attach one to 'X to scalar' and the other to 'Z to scalar', call them Absolute X and Absolute Z

So now I know the distances, I need to pick the maximum distance. There is no function called max though, what this is is the conditional scalar (Create Function/Conditional scalar).  This function asks if...then...else.... If has 4 inputs and a setting.


  • The first input (Input node) is the left hand side of the condition.
  • The setting is the condition (is greater than, is less than etc.).
  • The second input (Comparison value) is the right hand side of the condition.
  • The third input (If result) is the value output if the condition is true.
  • The final input (Else result) is the value output is the the condition is false.

The condition we want is 'if x is greater than z then x else z' so the feed from Absolute X goes in Input Node and If result, Absolute Z goes in Comparison value and Else result. The Condition is set to greater than.

Our upside down pyramid currently goes on forever, so we need to clamp it.
There are a lot of clamping functions, you can find them in Create Function/Clamp
We've already used Abs scalar. Now we are going to use a clamping functions,


  • Clamp 0   means that any value less than 0 is forced to zero
  • Clamp 0 1 means that any value less than 0 is forced to zero, values greater than 1 are forced to 1
  • Clamp     clamps between the two input values

We need to clamp between 0 and our max pyramid height. So we need two constants
(Create Function/Constant/Constant scalar) one set to Zero and the other to our max pyramid height, which I've called max pyramid radius.
Add a 'Clamp scalar' function and connect the zero constant to min and the max pyramid radius to max.
Connect the Conditional Scalar to the Clamp scalar input.  

Finally we need to turn our upside pyramid the right way around, this is easy. Our pyramid goes from 0 to x, we need it to go from x to 0., and we do this by subtracting the height so far from the max height.

Add a subtract function (Create Function/Subtract/Subtract scalar)  attach the max pyramid radius node we connected to the clamp node to the left hand input node, and then connect the clamp node to the right hand input node  (Input 2).

We now have a pyramid, but we need to use it to change the landscape. To do this add a Displacement shader, connect it to the compute terrain node in the Terrain Group. Finally attach the Subtraction Function to the Displacement Shader Function Input Node.

You should have something like this....

[attachimg=1]

Now, depending on how big your pyramid is, the camera is probably in the middle of it, so to 'find' it set the camera to 0,2xmax pyramid radius,0 and the rotation to -90,0,0 and your pyramid should be right below you. This top down view looks like this.

[attachimg=3]

Moving the camera to position 250, 235, 800 rotation -15, 200, 0 give's you a nice view of the pyramid.

[attachimg=4]




Oshyan

Glad to see this show up in the tutorial section now. It makes a very good stand-alone write-up. The only thing I think is missing would be a rendered example of the terrain this produces. :)

- Oshyan

David Burnett

I've added a couple of images now, showing the end result of this part of the tutorial, and I've done the same for the second part too.

Dave

Oshyan


rcallicotte

Thanks David.  This is an excellent way to an understanding of the TG2 functions.
So this is Disney World.  Can we live here?

Ironshirt

I'm sorry, but since english isn't my native language I couldn't figure out which math. formula you used for the pyramid...?

In other words - before you start with things like the pyramid you have a mathmatical formula which you use to convert into TG2...
which one was it in the case of the pyramid? It would be a great help if you could tell me!

thanks ahead!
Michael


David Burnett

#6
The maths formula would be

y = r - clamp( maximum( absolute(x), absolute(z) ), 0, r)

Where r is the half the length of one side of the pyramid base.

Depending on how good your english is you may need to know that...

Clamp restricts the result of the maximum function to between 0 and r. Effectively min (max (x, 0), min(x, r))

Absolute gives the value without the sign so absolute(-5) is 5 and absolute(5) is 5

Dave

Ironshirt


lukedesmith

there is way too much math involved in this program, which should be simple and intuitive to us artist types. why cant i just drop a primitve shape in the scene with one click?

Alpha Centurion

I don't think this tutorial is written in a useful way. There should have been waaay more graphic illustrations, since what is described doesn't match what the image of the node network shows. 

David Burnett

#10
Quote from: Alpha Centurion on April 28, 2010, 06:30:28 PM
I don't think this tutorial is written in a useful way. There should have been waaay more graphic illustrations, since what is described doesn't match what the image of the node network shows.  

Yes it does, I've just run though the tutorial again and I get exactly what I've described, bar the name of a node or two which I've altered the node that might be confusing (mac pyramid height -> mac pyramid radius) in the the tutorial to match the network node picture.

nethskie

#11
Dave,

Thank you for the function tutorial because of this tutorial i was able to create this:



Full View: http://nethskie.deviantart.com/art/The-Great-Pyramids-of-Kaiser-1-174843418

Thanks and Best Regards,

Ken

yangin0

It took a while for me to get the math, but I enjoyed it very much. Thank you!

bellker

Hello!
Can someone look into my pyramid?? I don't know what is wrong... the link is attached!
Thank you very much!!

Dune

Here you go. It was there alright. If you make a surface shader at the end of the line before planet, you can test where things are with the color.