Sub topic 9
Layering patterns with Mix and Math nodes
A single Noise or Voronoi texture rarely looks like a real surface. Rock has large stains and fine grain. Rusted metal has bare patches and corroded patches. This chapter shows how to combine several patterns into one mask or one colour, using the Mix and Math nodes.
This chapter assumes you can already:
- Wire a Texture Coordinate node and a Mapping node into a texture.
- Use a Colour Ramp to remap values and make a threshold.
- Connect a result to a Principled BSDF input.
The Python example needs Blender 3.4 or newer, because it uses the unified Mix node.
Two kinds of values, two kinds of nodes
Most procedural nodes output one of two kinds of data:
- A float is a single number, usually between 0 and 1. The Noise
Facoutput and the VoronoiDistanceoutput are floats. Roughness and Metallic inputs also expect floats. - A colour holds three channels plus alpha. The Noise
Coloroutput and a Colour Ramp'sColoroutput are colours. Base Color expects a colour.
The two combining nodes have different jobs:
- The Math node works on floats. You use it to build masks: add, multiply, subtract, compare, and clamp numbers.
- The Mix node blends two inputs by a factor. Its data type can be Float, Vector, or Color, and Color mode adds blend modes such as Multiply, Overlay, and Screen.
Do not confuse the Mix node with the Mix Shader node. Mix Shader blends two complete shaders, such as a diffuse and a glossy one. The Mix node blends plain data before it reaches a shader. This chapter covers only the data node.
Using Math nodes to combine masks
Think of a mask as a grey image where white means "apply the effect here" and black means "do not". The Math node combines two masks with simple logic.
| Operation | Behaves like | Typical use |
|---|---|---|
| Multiply | AND (intersection) | Keep rust only where a large stain and a fine grain both exist |
| Add | OR (union), may exceed 1 | Join two separate sets of cracks |
| Subtract | Cut one mask out of another | Remove a pattern from inside another |
| Maximum | OR without exceeding 1 | Union that keeps the stronger value |
| Minimum | AND that keeps the weaker value | Intersection with softer results than Multiply |
| Power | Contrast curve | Push mid-greys toward black |
| Greater Than | Hard threshold | Produce a sharp black and white edge |
Multiply is the most common choice. When both inputs are between 0 and 1, the result is also between 0 and 1. It is bright only where both inputs are bright. This also means it darkens the overall result, so you often follow it with a Colour Ramp to restore contrast.
Add can produce values above 1. Many inputs ignore such values, but a later Multiply or Mix will use them as they are. Turn on the Math node's Clamp option, or use Maximum, when you need values to stay between 0 and 1.
Scale separation
The best results usually come from combining patterns at different scales. A low-scale Noise gives broad variation. A high-scale Noise or Voronoi gives fine grain. Multiply the two, and the fine detail appears only inside the broad regions.
Using the Mix node
The Mix node takes a Factor and two inputs, A and B. The output is A where the factor is 0, B where it is 1, and a linear blend in between. In effect, the factor is a mask that decides where B shows through.
This gives a standard pattern: build a mask, then plug it into the Factor of a Mix node whose A and B are two colours (or two numbers). The same mask can drive several Mix nodes at once. A rust mask can switch colour from metal to orange, roughness from 0.25 to 0.85, and Metallic from 1 to 0, so all three change together.
Blend modes in Color mode
In Color mode, the Mix node has a blend mode dropdown. With the default Mix mode, the result is a plain interpolation. Other modes combine A and B in different ways, and the factor then controls how strongly the effect applies.
- Multiply darkens. Multiplying a colour by a grey noise gives a dirty, stained version of the colour.
- Screen and Add lighten. They suit highlights, such as dust on edges.
- Overlay and Soft Light add contrast while keeping the base colour. They work well for adding fine grain on top of a colour layer.
A useful trick for adding grain is to take your base colour as A, a grey noise as B, choose Overlay, and set the factor to 0.2 to 0.4. The grain then varies the surface without changing its overall colour.
The Clamp options
The Mix node has Clamp Factor and Clamp Result checkboxes. Clamp Factor forces the factor into the 0 to 1 range. With it off, a factor above 1 extrapolates past B, which can produce negative or very bright values. Leave it on unless you have a reason to turn it off.
Building the graph with Python
The script below builds the graph in the diagram. It creates a rust-on-metal material from two noises, one Math node, two ramps, and two Mix nodes. In the unified Mix node, each data type has its own pair of sockets, so the code selects sockets by index after setting data_type. The indices are: Factor 0, float A and B 2 and 3, colour A and B 6 and 7. Outputs are float 0 and colour 2.
import bpy
mat = bpy.data.materials.new("RustLayers")
mat.use_nodes = True
nodes = mat.node_tree.nodes
links = mat.node_tree.links
bsdf = nodes["Principled BSDF"]
coord = nodes.new("ShaderNodeTexCoord")
broad = nodes.new("ShaderNodeTexNoise")
broad.inputs["Scale"].default_value = 2.0
broad.inputs["Detail"].default_value = 6.0
fine = nodes.new("ShaderNodeTexNoise")
fine.inputs["Scale"].default_value = 25.0
fine.inputs["Detail"].default_value = 10.0
# Ramp 1: turn the broad noise into stain shapes
stain = nodes.new("ShaderNodeValToRGB")
stain.color_ramp.elements[0].position = 0.45
stain.color_ramp.elements[1].position = 0.60
# Multiply: rust only where a stain and fine grain overlap
both = nodes.new("ShaderNodeMath")
both.operation = 'MULTIPLY'
both.use_clamp = True
# Ramp 2: restore contrast after multiplying
contrast = nodes.new("ShaderNodeValToRGB")
contrast.color_ramp.elements[0].position = 0.10
contrast.color_ramp.elements[1].position = 0.40
# Colour mix: metal grey to rust orange
mix_col = nodes.new("ShaderNodeMix")
mix_col.data_type = 'RGBA'
mix_col.inputs[6].default_value = (0.55, 0.55, 0.58, 1.0)
mix_col.inputs[7].default_value = (0.45, 0.16, 0.05, 1.0)
# Float mix: smooth metal to rough rust
mix_rough = nodes.new("ShaderNodeMix")
mix_rough.data_type = 'FLOAT'
mix_rough.inputs[2].default_value = 0.25
mix_rough.inputs[3].default_value = 0.85
links.new(coord.outputs["Object"], broad.inputs["Vector"])
links.new(coord.outputs["Object"], fine.inputs["Vector"])
links.new(broad.outputs["Fac"], stain.inputs["Fac"])
links.new(stain.outputs["Color"], both.inputs[0])
links.new(fine.outputs["Fac"], both.inputs[1])
links.new(both.outputs["Value"], contrast.inputs["Fac"])
links.new(contrast.outputs["Color"], mix_col.inputs[0])
links.new(contrast.outputs["Color"], mix_rough.inputs[0])
links.new(mix_col.outputs[2], bsdf.inputs["Base Color"])
links.new(mix_rough.outputs[0], bsdf.inputs["Roughness"])The two Noise nodes share the same Object coordinates but have different scales, so their patterns do not line up. If two patterns used the same scale and the same coordinates, they would be identical, and multiplying them would only square the values.
Common problems
- Both layers look the same. Two textures with the same coordinates and the same scale produce the same pattern. Change the scale, or put a separate Mapping node in front of each texture and give each a different Location offset. For the Noise node's 4D mode, you can change W instead.
- The result is almost all black. Multiplying two mid-grey masks gives values around 0.25. Add a Colour Ramp afterwards, as in the script, or raise the contrast with a Power node.
- A hard edge appears where you wanted a soft one. A ramp with stops very close together, or a Greater Than node, has no gradient. Move the stops apart. For a hard edge, this is the right tool.
- Values look wrong after Add. Sums above 1 are not clamped unless you tell the node to do so. Enable Clamp or use Maximum.
- The colour looks washed out or too dark. Roughness and Metallic are non-colour data. Feeding a colour ramp into them is fine, but the Math node converts a colour input to a single grey number by averaging its channels. Check that you meant that.
Choosing between Math and Mix
Use a Math node when you are building a mask and the result is a number. Use a Mix node when the result is a colour or a blend between two things. A mask that has to be both multiplied and later used as a Factor passes through Math first and Mix second. The Mix node's own Factor does not combine masks, so do not try to chain masks through it.
Exercise
In the rust material, you want rust to appear only where the broad stain mask is present and the fine noise is also bright, but you also want to remove rust from a thin crack pattern made with Voronoi Distance to Edge (a ramp output where cracks are white). Which Math operations do you use, and in what order?
Show answer
First use Multiply on the stain mask and the fine noise to get the intersection. Then use Subtract: the multiplied mask minus the crack mask, with Clamp turned on so values below 0 become 0. The result is rust where stain and grain overlap, with the cracks cut out of it. Pass the result through a Colour Ramp for contrast, then use it as the Factor of the Mix nodes.
Exercise
A Mix node in Color mode, set to Overlay, uses a base colour as A, a grey noise as B, and a Factor of 0.3. What happens to the result if you raise the Factor to 1.0, and why is a low value usually better for grain?
Show answer
At 1.0 the Overlay effect applies at full strength, so the noise dominates the contrast of the colour and the surface looks heavily mottled. A low Factor such as 0.3 blends the Overlay result with the plain base colour, so the grain adds subtle variation while the overall colour stays recognisable.