AcademySoftwareFoundation / OpenShadingLanguage

Advanced shading language for production GI renderers
BSD 3-Clause "New" or "Revised" License
2.11k stars 361 forks source link

remap function included in the std library #1899

Open emkkla opened 1 week ago

emkkla commented 1 week ago

The need for a remap() function (also called efit() in VEX) is very common in shading, and it would be appreciated if the standard library included it natively.

So far, of course, we can remap vectors or floats by simply defining the function manually:

vector vectorRemap(vector input, vector minIn, vector maxIn, vector minOut, vector maxOut) {
    return minOut + (input - minIn) * (maxOut - minOut) / (maxIn - minIn);
}
float floatRemap(float input, float minIn, float maxIn, float minOut, float maxOut) {
    return minOut + (input - minIn) * (maxOut - minOut) / (maxIn - minIn);
}

But, having it part of the std would simplify code and ensure a proper implementation:

type remap(type input, type minIn, type maxIn, type minOut, type maxOut)
lgritz commented 6 days ago

That's a good idea, I will do so.