MATHS FUNCTIONS

MATHS FUNCTIONS: AN INTRODUCTION

They might not appear to be the most impressive nodes in the material room, but the humble maths functions actually lie at the heart of the vast majority of clever shader tree tricks. They allow you to combine or modify any number of other nodes to produce effects not possible with single nodes.

 


Fig 1: Maths_Functions node

Each maths_function node has up to five controls, 3 attributes and 2 inputs, although not all maths functions use the Value_2 attribute or input. Figure 1 shows the Maths_Function node as it first appears, adding 1+0 to produce 1.

The Math_Arguement attribute controls which maths function your node will be using. There are twenty four different maths functions available, ranging from simple addition through advanced maths to specific graphic effects such as bias or gain.

Value_1 and Value_2 are used to provide the input for the maths function you have chosen. In each case, the input node you apply is multiplied by the Value attribute, allowing you to do quite complex things with just a single node. Maths functions can work without any inputs, but there is little or no point doing so with the majority of functions. It is the ability to perform maths on the output of other nodes that makes the maths functions so powerful.


Fig 2: Add node in action

Figure 2 shows an example Maths_Function that uses all five controls. This shader tree adds half the value of the Tile node to half the value of the fBm node to produce a rough surfaced tile that can be used as a bump map, a displacement map or just to control a colour. You will not always need all five controls - many useful effects can be achieved when one input node is altered by the same value.

You can plug colour inputs into your maths node, but the output will always be in shades of gray.

When to use normal maths

At a first glance, the fact that the output of the maths functions is in shades of gray would appear to limit its usefulness, and suggest that we should be using the colour maths nodes instead. However, in many (or possibly even most) cases, the grayscale output is what we actually require.

Displacement and Bump Maps

Maths functions can produce some amazing displacement and bump maps. In each case, black is no displacement or bump, and white is the maximum displacement or bump.

Controlling the strength of specular or diffuse effects.

Our maths nodes can be used to control the strength of the specular, ambient, diffuse and highlights. White is fully on, black is fully off. This can be especially useful when combined with transparency, as it allows you to turn off the highlights and specular gleam in the areas that are meant to be transparent.

Controlling the Blender and Colour Ramp nodes.

The output from maths functions can be used to merge other more colourful nodes using the Blender or Color Ramp nodes.

On the blender node, a black input produces Input 1 and white produces Input 2, with shades of gray producing a mix of the two.

On the color ramp, black produces Color 1, dark grey Color 2, light grey Color 3 and white Color 4, with intermediate shades blending the two nearest colors. With the correct maths functions, you can convert a smooth grayscale picture into the four required shades to split the output directly into the four inputs.

Transparency

Transparency maps also need to be in shades of black and white. Here, white is solid and black is transparent entirely transparent, with shades of grey semi-transparent. Our maths nodes can be used to turn a complex colour texture into a suitable black and white transparency map, allowing for some interesting effects.

How to use normal maths?

So we know how to plug things into the maths nodes, and have looked at some of the places we can use the results, but how do we decide what maths functions to use?

Random experimentation.

First of all, you need to get to know the results you can expect from different functions. The best way to do this is to experiment. Set up something like figure 2, then experiment with different maths_functions, settings for the values and input nodes. You'll get all sorts of interesting results that will be worth saving to the material library, as well as realising that some nodes don't appear to make much sense.

You will also find links to tutorials on using the different maths functions at the bottom of this article (in progress..). Work through these to get an idea of what each maths function can do. Remember, each one can do plenty of things I haven't thought of!

Think about what you want to achieve.

Once you've got used to the effects of the maths nodes, you can move on to achieving deliberate effects. First, think about what you want to achieve. As an example, we will work through the process of designing a shader tree to produce clumps of snow on grass.

To produce this, we will need to produce a node that displays smooth areas of black where we want our grass, and clumps of grays and whites for our snow. We will then use this node to control a displacement map, and a blender node.

Our starting point is going to be a fBm node. This produces a suitably varied pattern for our snow, but doesn't produce our black areas for the grass. We need to modify our fBm node to create these flat areas.

This will need two maths nodes. Our fBm node produces output between 0 (black) and 1 (white). We are going to alter this so that the output goes from -1 to 1. To do this, we need to multiply the fBm node by 2, then subtract 1 from the result. We can do this with a single subtract node. Plug the output from our fBm node into the input for the Value_1 attribute. Set the attribute for Value_1 to 2. This will multiply the fBm node by two. Finally, set the Value_2 attribute to 1. This will produce an output of -1 to 1. We now have large black areas.

Having got our black patches, we would rather they were all set to 0, rather than ranging down to -1, otherwise we will be unable to use this for our displacement map. To do this, we will plug the output from our subtract node into a clamp node. This forces our results into the range 0 to 1 ready for the displacement map.

The result from our clamp node can be plugged directly into the displacement node. However, we need to do some more work on it before it can control the blender node. Here, we want a simple input with just black and white. To do this, we are going to use the Ceil maths functions. This rounds numbers up to the nearest round number, so our flat 0s will remain at 0, while everything else will become 1. This output can be plugged straight into the blender node, as shown in figure 3. We have set Input 1 to green, Input 2 to white and the Blending value to 1, then plugged our Ceil output into the Blending input.

Finally, plug the output from the Blender into the Diffuse_Color node. Render this, (remembering to turn displacement on!). The result can be improved by working on the grass and snow effects produced by the blender, but this gets you your basic material.

 


Fig 3: Example Maths effect

Individual Tutorials

Chart Showing maths functions

Add function
Subtract function
Multiply function
Divide function
Sine function
Cosine function
Mod function
Min function
Max function
Clamp function
Floor Function
Round function
Step function
Bias function
Gain function


PoserWorld