If you create a tree or a sign, you might want to apply an effect to the material that the changes the color of the mesh, depending on its location in the world.
To apply a color-changing texture to the leaves of a tree mesh, for example, you would use the Sine node to target the world position of the mesh and modify the color as it changes position along the Z-axis.
To create this effect:
-
Add the following nodes to the configuration above in the Material Editor:
-
Linear Interpolate node
-
Sine node
-
Divide node
-
Component Mask node
-
Absolute World Position node
-
2X Constant 3Vector expression node
-
-
Drag off the Absolute World Position node and plug it into the Component Mask node.
-
Select the Component Mask node, then check only the B option in the Material Expression Component Mask on the Details panel.
-
Drag off the Component Mask node and plug it into the Sine node.
-
Select the Sine node and change the Material Expression Sine value to 1200 in the Details panel.
-
Drag off the Sine node and plug it into the Divide node A input.
-
Change the Divide node B input numerical value to 3.0.
-
Drag off the Divide node and plug it into the Lerp node Alpha input.
Sine node values can produce less than desirable results. If you find the sine wave does not add value to your material, you can get away with losing the sine output and saturating the divide output to keep everything within nominal range.
-
Select the first Constant 3Vector expression node and add a color.
-
Select the second Constant 3Vector expression node and add a color.
-
Drag off the first Constant 3Vector expression node and plug it into the Lerp node A input.
-
Drag off the second Constant 3Vector expression node and plug it into the Lerp node B input.
-
Drag off the Lerp node and plug it into the Basic Color input of the Main Material node.
Click image to enlarge.
-
Click Apply in the toolbar.
-
Add the new material to a mesh in the viewport.
-
Move the mesh up and down along the Z-axis. Notice how the color changes from one to the other, depending on where the mesh is in the world.
The Mask node determines where the color change is happening along the sine wave. The Divide node then determines how frequently the color change repeats in the world position.
The two Constant 3Vector expression nodes determine the colors that remain constant in the world position, while the Lerp node interpolates between the colors in the world position, creating a gradient at times, depending on where the mesh is located.
Play around with the different node values to get a feeling for where the color change should happen in the world position.
You do not have to create a new material every time you want to change the color values for your material. If you familiarize yourself with the Material Instance Editor, you can change your material instance by changing material parameters.