Is there any intrinsic function in GLSL for: { return (x - a) / (b - a); }

is there any intrinsic function in GLSL for: { return (x - a) / (b - a); }
Thanks!

Check out the mix() function.

1 Like

thank!
but, mix(x, y, a) = x*(1-a) + y*a

Oh, I’m sorry! You’re right. I was skimming too fast. You asked for an “inverse lerp” and I gave you a “lerp” (mix()).

It’s not exactly what you asked for, but take a look at smoothstep():

It computes what you asked for (0…1 for a…b) internally. But then it also clamps it to 0…1, and smooths the result (so it’s not just a hard linear 0…1 ramp with gradient discontinuities at the edges). Just a few extra ALU ops so should be pretty cheap.

Failing that, I guess you could just write your own “inverse lerp” function, with or without the 0…1 clamp:

float invMix( float edge0, float edge1, float x )
{
    return ( (x - edge0) / (edge1 - edge0) );
}
1 Like

thanks :smiley:
then there seems no faster function whether intrinsic or not than this, lol