Skip to main content
GameDev.net gamedev.net

PRO Tired of ads? Read GameDev.net ad-free and help keep the community independent with GameDev Pro — $3/month.

Inverse Lerp - a super useful yet often overlooked function
Article Featured

Inverse Lerp - a super useful yet often overlooked function

A brief description of the oft-overlooked inverse lerp function.

Freya Holmér
Freya Holmér
October 23, 2019 101.3k views

Lerp( a, b, t ) = value
InvLerp( a, b, value ) = t
  • lerp returns a blend between a and b, based on a fraction t
  • inverse lerp returns a fraction t, based on a value between a and b

1.gif

Use case! ?

Say you want to control audio source volume based on distance

  • at 10 meters, you want volume 1
  • at 20 meters, you want volume 0

Then the volume is then given by


volume = InvLerp( 20, 10, distance )

2.gif

If you've ever used photoshop's levels tool, then you've used both lerp and inverse lerp! ?

The input values use inverse lerp, the output values use lerp!

3.png

When used with images, inverse lerp can be used to increase value contrast! for example, here's a selfie before and after an inverse lerp?

4.png 4-2.png

Also, note that some Lerp/InvLerp functions also extrapolate (such as in shaders), while others clamp the values within your given range (such as Unity's Mathf.Lerp/InverseLerp functions)

Make sure you clamp unless you want to extrapolate ?

Here's an interesting use case!

An inv lerp where a and b are colors and the value parameter is the depth of this water, you can achieve hue-shifting for a color elimination effect by depth?

water.mp4

Addendum - another useful function is Remap!

Remap takes a value within a given input range into a given output range, which is basically a combined inverse lerp and lerp!

Here's the code for all three!

(Also, none of these are clamped - they can all extrapolate)

code.png

Enjoyed this quick lesson? Check out Freya's work via the following social platforms:

? Patreon ❱ https://patreon.com/acegikmo
? Twitch ❱ https://twitch.tv/acegikmo
? YouTube ❱ https://youtube.com/c/acegikmo
? Twitter ❱ https://twitter.com/FreyaHolmer
? Discord ❱ https://discord.gg/v5VWuga
? Instagram ❱ https://instagram.com/freya_holmer

Want to read more about Lerp? Freya posted a thread here:

Adapted from the original Twitter thread with kind permission of the original author.

Further reading on GameDev.net: A Brief Introduction to Lerp, by Matt DesLauriers

License: gdol

Related Tutorials

Discussion

Discussion

Loading comments...

More from Freya Holmér