9
$\begingroup$

For the purpose of animating materials, is there a way to construct a sine wave function out of Math nodes?

A while back I asked about how to do something similar with scripted driver expressions. But drivers don't work for animating materials (yet). They also are not currently supported in Blend4Web, which is where this will ultimately be used (with the B4W_TIME node driving the incremented value by frame).

So let's start with a keyframed Value node that increments by 1 for each frame. And that is factoring a MixRGB node with two mixed colors.

Keyframed Value node factoring an RGB mix

Since 0 will be 100% the top color, and 1 will be 100% the bottom color, going from frame 0 to frame 1 instantly changes the mix completely. Inserting a Math (Multiply) node slows this process, so for example if multiplied by 0.05 it will take 20 frames to shift from red to blue, and at frame 10 it will be half/half (purple).

A single Math node added to slow the transition

So what about animating it so that it oscillates between two colors? Can this sort of sine wave animation be constructed using Math nodes?

(Note: This must be achievable using the Blender Internal Render Engine.)

Thanks

$\endgroup$
  • 1
    $\begingroup$ I must be missing something, because I just had a look at the BI shader nodes and can see a Math --> Sine option there, no? (Ver 2.74) $\endgroup$ – TLousky Jan 24 '16 at 12:32
  • 1
    $\begingroup$ No, it is I who have missed something! I somehow did not realize that, even though I've seen those options many times before. I guess I just didn't make the connection that they can be used for animation, but of course they can. Thank you! Go ahead and post your comment as an answer and I'll accept it. If you can also explain how to create triangle, sawtooth, and square waves, that would be a huge added bonus (those are not listed in the Math node settings). $\endgroup$ – Mentalist Jan 24 '16 at 13:39
13
$\begingroup$

Sine Wave

Producing a sine wave is easy as Sine is one of the math functions in the Blender Internal Converter --> Math node.

However, a normal sine is no good as a direct factor for a mix node, since a sine wave produces values between -1 and 1, and the node expects a value between 0-1. To produce sine-like oscillating values between 0 and 1 I used the equation ( sin(x) + 1 ) / 2.

The node tree looks like this: Adjusted Sine

The factor values produced by this node setup look like this (green line): Values

Square Wave

A square wave is also pretty easy, since we only need to discern between positive and negative values, which we can do using the "Greater Than" or "Less Than" nodes: Square

And the factor values: Square values

Triangle Wave

Based on this algorithm as implemented in wolfram, I used the following equation to generate a triangle wave: pi/2 * asin( sin(pi * x) )

Since there is no inverse sine (asin) in the math node, I used the identity function (that employs the inverse cosine which can be found in the math node's repertoire):

asin(x) = pi/2 - acos(x)

Also, the original triangle wave creates values between pi/2 and -pi/2 more or less, so I adapted it thus to produce values between 0-1 (more or less): ( asin( sin(pi * x) ) + pi/2 ) / pi

The node setup looks like this: Triangle node setup

This is by far the ugliest and biggest node setup, but that's the best I could manage. Here's how the factor values look: Adjusted triangle graph

Sawthooth Wave

Based on this answer, again adjusted to fit values between 0 and 1, I used this equation as basis: ( ( x + 1 ) % 2 ) / 2

Node setup: Sawtooth wave

Factor values: Factor values

$\endgroup$
  • 1
    $\begingroup$ Thank you for this high-quality answer! The clearly explained examples with visual aids are very helpful. Until you posted this I was experimenting with constructing wave patterns by trial and error using the Separate XYZ and a ColorRamp in Cycles to visualize them. This was fun, and I did have some success (here are the results), but what I was really looking for was exactly what you posted - a mathematically elegant way to represent each of these fundamental waveforms using nodes. This is going to prove useful for all sorts of projects! $\endgroup$ – Mentalist Jan 24 '16 at 17:23
  • $\begingroup$ Also, your explanation about the sine wave function needing to go between 0 and 1 (and no lower) explains why I was having so much difficulty getting my sine to pulse "right". And the solution for triangle wave... I never would have figured that one out. :-) $\endgroup$ – Mentalist Jan 24 '16 at 17:32
  • $\begingroup$ Sure, no problem :) $\endgroup$ – TLousky Jan 24 '16 at 18:30
  • 2
    $\begingroup$ This is really great answer! $\endgroup$ – Blend4Web Feb 11 '16 at 12:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.