What is range_lerp() ? What are it's function ?
What is range_lerp() ? What are it's function ?
- Edited
from http://docs.godotengine.org/ko/latest/classes/class@gdscript.html#class-gdscript-range-lerp_ Maps a value from range [istart, istop] to [ostart, ostop].
range_lerp(75, 0, 100, -1, 1) # returns 0.5
And it was added in this commit: https://github.com/godotengine/godot/commit/d28da86f9ff5a70284e4a2078fa08867d4858a57
OK, so lets look at the above example again:
range_lerp(75, 0, 100, -1, 1) # returns 0.5
The 75 is the integer value plugged into it, and then two ranges are given to it. If we look at the ranges given, 0 to 100 and -1 to 1 here are stated to be equivalent so the input of 75 is effectively then interpreted as 0.5(since that is three fourths, or 75 percent of the way linearly from -1 to 1) which is the value returned by the function.
As for the term lerp
, it is short for linear interpolation.
- Edited
Would it be too much to ask for someone to share what range_lerp would look like in mathematical notation?
Edit: range_lerp(value, min1, min2, max1, max2):
min2 + (max2 - min2) * ((value - min1) / (max1 - min1))
- Edited
@Erich_L said: Would it be too much to ask for someone to share what range_lerp would look like in mathematical notation?
Imho, much better name for range_lerp would be remap. Because it remaps the value from one min/max range to another.
Math is rather simple:
func range_lerp(value, min1, max1, min2, max2):
var value_norm = inverse_lerp(min1, max1, value)
return lerp( min2, max2, value_norm)
while lerp and inverse_lerp are:
func lerp(min, max, parameter):
return min + (max-min) * parameter
func inverse_lerp( min, max, value):
return (value-min) / (max-min)
Thankyou @xyz, I think by
var value_norm = inverse_lerp(min1, min2, value)
you meant
var value_norm = inverse_lerp(min1, max1, value)
@Erich_L Yep, good catch! Corrected it now.
- Edited
@xyz It really isn't that complicated but it was a bit much for my brain to chew on, so thanks again this helped me a lot. Also, interestingly, even though I have min1 and max2 set to zero and therefore the equation can be simplified significantly, I found that continuing to use range_lerp()
was five percent faster than the simplified version.
- Edited
Sure. It's always better to let native code do the grunt work. Adding two numbers in native code is much faster than doing so in the script.
I stand corrected, I had a range lerp with: min1 and min2 as zero and max 2 as one. Simplification in the math saved me 139 milliseconds over 22,500 computations. :p
- Edited
Always good to hear when leaps and bounds are made in the noble field of premature optimization :joy:
I've been range_lerping for lines and lines- for my terrain noise algorithm that I (you) have just about finished. It's about done so any tiny speed increases that I can put in my research results are... good? You're right remap would be a better name but I'm so used to range_lerp() now I'm over it. It's almost a little fun trying to convert code to math.
@Erich_L said: I've been range_lerping for lines and lines- for my terrain noise algorithm that I (you) have just about finished. It's about done so any tiny speed increases that I can put in my research results are... good? You're right remap would be a better name but I'm so used to range_lerp() now I'm over it. It's almost a little fun trying to convert code to math.
have you tried turning it to binary numbers yet?
@OpinionatedGamer I have not. Please tell me more. =)
- Edited
Tbh, the term lerp irritates me :) And when I see inverse_lerp in code, my brain just freezes and I cannot comprehend what's going on. Not joking at all. I start scratching my head and reach for pen and paper to try to resolve it step by step. It gets me every time.
When working in C I tend to use my own functions. The names are: interpolate = lerp normalize = inverse_lerp remap = range_lerp With these names I know exactly what is going on.
~ Lerping is one thing, but don't mess with binary at home on your own, fellow kids. It's dangerous. People you've seen doing it online were trained professionals.