# How to make a shader that applies this effect to each face of a cube?

I'm very new at shaders, so in order to learn what they're capable of I wanted to try and create an effect that is a fair distance away from anything you can do with a single principled BSDF node. I don't quite now how to express this in 3D, so I coded a 2D animation:

basically the idea is to draw a series of squares along a line and have them grow each frame until the part of the "screen" behind them is filled with their color. in effect it is a fancy color wipe.

my question is, is there a way to apply this kind of effect to faces using a shader? if so, how would I go about that?

• Do you specifically want to use a shader? Or can you use other techniques? Jan 20, 2021 at 8:59
• With the objective of making the tree as simple as possible for the job you want done.. do you want this, 1 square per face? Or a 2D UV texture? When you say 3D, could you give an idea of the way you would like the effect to be spread across a 3D surface? Jan 20, 2021 at 10:01

I feel like the easiest way to think of this is to have a grid of square gradients and when you use a greater than, you can change the size of the square. Think of it as changing the height that you cut through a pyramid, at the top the cross section would be small but at the bottom, it'll be large.

To make your grid of squares we can do a little bit of vector maths. I'll be using the UV coordinates as this will let you unwrap your cube so each face gets the whole effect. I am scaling the UV coordinates 20x so that we get 20 squares and then breaking it into a grid with Fraction. I then centre each space by subtracting 0.5 and use absolute to make each space symmetrical. This is the easiest way to get a square grid. I then separateXYZ and use a maximum node in order to get our little pyramids for each square.

If we add a math node set to Less Than at the end, you'll see the threshold will change the size of all the squares. To make this wipe down like your animation, we need the threshold value to be a gradient.

Branch off from the Scale node and add a separateXYZ followed by a math node set to snap. Take the Y socket into the math node and set the increment to 1 (this is to match the fraction node which also repeats at 1).

We now have a gradient and that we can manipulate to control the animation. To change the position of the gradient, add a math node set to subtract, and then follow that with another math node, set to multiply. The multiply will act as the falloff control. Use a small number like 0.1 for now. If we plug this into the threshold of the Less Than node, you'll see the square gradient how you want. You can change the subtract value to move the transition up and down.

Finally, to animate the sequence, we need to bring the frame number into the shader. Add a value node and type #frame into the value field. This will create a driver that takes the frame number. We want to have a loop so we're going to add a math node set to modulo. We need to use the loop length twice so add another value node that we can use and plug this into the bottom of the modulo. Set the loop length value to something like 80. Follow the modulo with a map range and plug the modulo into the first value and the loop length value node into the From Max socket. Set the from Min socket to 1. Our To Min and To Max socket slightly depends on a few things such as what scale value we've used and what the falloff is set to and the loop length so the settings for a scale of 20 and falloff of 0.1 are: To Min: 20 To Max: -5 Plug this map range into the bottom of the subtract node to control the position of the wipe.

• I'm still working my way through this, but it's very helpful! I'm new to visualizing how UV coordinates work so this exercise is proving quite educational in that regard :) Jan 21, 2021 at 0:19

My basic theory was that if we could do it once we could scale it up and give it any number of controls, and if it's based on UVs then it should be able to be applied to anything.

I have just a plane here, I've separated out the UV coordinates and set up some Math -> Compare nodes with a Value node that we can drag back and forth to fill up the square. The square is completely filled at .5, so we're using a Math node to normalize this value between 0 and 1. This is important for later.

So now we can divide up our coordinate space with a Vector Math node set to Modulo, but this actually creates a dependency between the value we divide our plane by, and the values coming out of our separate XYZ node.

We can solve for this by using one value node to drive the whole operation. We divide by this value to do the Modulo operation, and multiply the values from our Separate node to bring back up to the 0 - 1.0 range. At the end our node graph looks like this.

We can make changes to either of these value nodes and see that they are working, the square expands from the center of each box. We also see that whatever maps we send into these Epsilon values are what creates the visual pattern.

Since we've been careful to normalize for values between 0 and 1, we can use the output from almost any of the procedural textures. I've made a few examples here. I recommend trying these inputs and sliders but the possibilities are endless.

The subtract node does exactly what you asked for in the question, I think.

Here's the .blend file.

• thank you, this is incredibly useful! if I could mark both answers thus far as correct, I would have ;_; Jan 21, 2021 at 0:17
• It was a well formed question, and a good exercise for me, so I thank you for that. Jan 21, 2021 at 0:34