Skip to content

Instantly share code, notes, and snippets.

@kayk5654
Created February 13, 2022 02:33
Show Gist options
  • Save kayk5654/3b56c9fa8df61b7f75affb4b8096ca7d to your computer and use it in GitHub Desktop.
Save kayk5654/3b56c9fa8df61b7f75affb4b8096ca7d to your computer and use it in GitHub Desktop.
// fit a value in a specific range to another specific range
float fitRange(float value, float inMin, float inMax, float outMin, float outMax)
{
float lerpFactor = lerp(0, 1, (clamp(value, inMin, inMax) - inMin) / (inMax - inMin));
return lerp(outMin, outMax, lerpFactor);
}
// get reflection vector for box projection
float3 boxProjection(half3 worldRefl, float3 worldPos, float3 cubemapCenter, float3 boxMin, float3 boxMax)
{
// normalize reflection vector
half3 normalizedDir = normalize(worldRefl);
// get magnitude of a vector from world position to the closest surface of a box
half3 magnitudeMax = (boxMax - worldPos) / normalizedDir;
half3 magnitudeMin = (boxMin - worldPos) / normalizedDir;
half3 magnitude = (normalizedDir > 0) ? magnitudeMax : magnitudeMin;
half smallestMagnitude = min(min(magnitude.x, magnitude.y), magnitude.z);
// adjust reflection vector considering position offset of world position from cubemap center
return (worldPos - cubemapCenter) + normalizedDir * smallestMagnitude;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment