Inverse Lerp - a super useful yet often overlooked function
A brief description of the oft-overlooked inverse lerp function.
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
Related Tutorials
Balancing Game Development and Creative Direction in Indie Production
A practical look at how indie developers can balance creative direction with hands-on game development. This article co…
My Unreal Engine Development Process: From Core Idea to Playable Build
A practical overview of my Unreal Engine development process, covering how I move from a core game idea to a playable b…
Introducing LaneGraph: The Ultimate Road Network Solution for Unity
Discover the power of LaneGraph, a lightweight and flexible lane-based navigation system for Unity. LaneGraph makes it…
Retargeting Mixamo Characters with Root Motion In Unreal Engine 5.4.
I have always found Retargeting Mixamo Characters To have Root Motion is a serious lengthy Tast, Recently I stumbled up…
How To Make A SIMPLE Main Menu In Unity
In this tutorial for unity, i go over how to make a simple main menu for unity, it's an unlisted video because i do not…
Guide to Gameplay Balance
A perspective on competitive gameplay balance, from a background of "shooter" sandbox design.
Discussion
More from Freya Holmér
New World Interactive: How IMS Game Server Operations Help Enable Greater Cost Management
Discover how New World Interactive migrated to Improbable Multiplayer Services and cut their monthly recurring server o…
GameDev.net
How to update your game without impacting player experience (or revenue)
How much is a minute worth? What about thirty? When it comes to free-to-play titles that rely on micro-transactions and…
GameDev.net
Adopting CI/CD: How Midwinter Entertainment iterates at speed with the help of IMS
Game development was once like one long sprint: a huge effort until you reached the finish line — at which point you co…
GameDev.net
Discussion