lerp() - leiget/Godot_FPC_Base GitHub Wiki

β€œ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.