The triangle wave function it’s a cheaper alternative to a sine wave, it can be very useful as the alpha input for a lerp node when working with materials or FXs.
In some cases, it might be exactly what you’re after since it allows you to blend between two inputs in a linear way.
This is what a remapped 0 to 1 sine wave looks like:
and here’s the triangle wave function:
The function is
w(t) = 1 − |1 − 2t|
where | | is the absolute value.
So in code, it would be:
w = 1 - abs(1 - 2 * time);
In Unreal Engine you can create a Material Function asset, which would look like this:
(just make sure you frac time)
You can find the snippet here on Epic Developer Community.
Below you can see a comparison between the two functions, they’re both lerping at the same speed between black and their respective color:
Cyan is Triangle function, Yellow is sine.
You can notice how the sine smoothly approaches the value of 0 and 1, while the triangle function ping pongs between the two values.
Here’s the comparison is more evident, using the two functions to move some spheres up and down.
And again, in the gif below I’m visualizing the two functions one on top of the other.
While we’re on the subject, you can also easily make a Sawtooth Wave function just using time as the input of a frac node:
Leave a Reply