-
Notifications
You must be signed in to change notification settings - Fork 8
lerp()
“lerp()” is the linear interpolation function. The first argument is the “from” variable and the second is the “to” variable. The third argument is the “weight”, using a normalized value, that you want to interpolate between the two.
As an example, let’s say we have a “lerp()” function that looks like this:
Result = lerp( 0.5 , 1.5 , 0.5)
“Result” will equal “1.0”. This is because the third argument, called the “weight”, is “0.5”. And since this is a normalized value, what it really means is “50%”.
Since “50%” is halfway between “0%” and “100%”, that means that we want to find what’s halfway through “0.5” and “1.5”. As you probably already figured out it’s “1.0”.
Another way of looking at it is by this formula:
Result = ( from + ( weight * ( to – from ) ) )
And in real numbers, according to our example:
Result = ( 0.5 + ( 0.5 * (1.5 – 0.5) ) ) = 1.0
And if we were to have a weight of “0.25”:
Result = ( 0.5 + ( 0.25 * (1.5 – 0.5) ) ) = 0.75
And if this was visualized:
It would look like this. Of course, you can just use “lerp()” without all the manual math, but it’s helpful to have an idea of what’s going on under the hood.
You can also use a “pow()” function, explained in the next chapter, as the weight argument to get a non-linear result.