Skip to content
Swifter edited this page Oct 19, 2024 · 2 revisions

What is lerp?

Lerp (or linear interpolation) is a mathematical concept which can be used to blend between 2 values.

It is defined as lerp(a, b, t) = (b - a) * t + a where:

  • a is the start point.
  • b is the end point.
  • t is the position between a and b.

When t is:

  • 0, the output will be a
  • 1, the output will be b
  • somewhere in between, it will be a mix of a and b.

alt text

Here's a demonstration of various values of a, b, and t:

lerp(2, 4, 0) = 2
lerp(2, 4, 0.5) = 3 (halfway between 2 and 4)
lerp(2, 4, 1) = 4

Utilities

  • rm.lerp will take in numbers a, b, and t and return lerp(a,b,t).
rm.lerp(2, 4, 0) // 2
rm.lerp(2, 4, 0.5) // 3
rm.lerp(2, 4, 1) // 4
  • rm.inverseLerp will take in numbers a, b, and x (lerp(a,b,t)) and find t.

You can imagine it as doing the reverse operation of lerp, where it finds the t value that generates the number you pass in.

rm.lerp(2, 4, 2) // 0
rm.lerp(2, 4, 3) // 0.5
rm.lerp(2, 4, 4) // 1
  • rm.remap will take in a position in the range of a1 to b1 and convert it to a range from a2 to b2.

The parameters are ordered like: (x, a1, b1, a2, b2).

rm.remap(2, 2, 4, 1, 2) // 1
rm.remap(3, 2, 4, 1, 2) // 1.5
rm.remap(4, 2, 4, 1, 2) // 2
  • rm.slerp1D works like rm.lerp but assumes that values above 1 wrap around to 0. That is to say, it interpolates spherically.
rm.slerp1D(0.2, 0.8, 0.5) // 0

You can imagine these values as angles on a sphere and you're finding an angle in between.

alt text

Clone this wiki locally