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
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 )
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!
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?
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?
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)
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
Discovered I was calling it Unlerp in my code, think your name is better. Nice post