What I came up with is this. Any thoughts?
float Remap( float value, float inMin, float inMax, float outMin, float outMax )
{
float mappedValue = value;
if ( ( inMax - inMin ) == 0.0f )
{
mappedValue = ( outMax + outMin ) * 0.5f;
}
else
{
mappedValue = outMin + ( ( ( mappedValue - inMin ) / ( inMax - inMin ) ) * ( outMax - outMin ) );
}
return mappedValue;
}
float2 RangeMapFloat2( float2 value, float2 inMin, float2 inMax, float2 outMin, float2 outMax )
{
float2 mappedValue = value;
if ( ( inMax - inMin ).x == 0 && ( inMax - inMin ).y == 0)
{
mappedValue = ( outMax + outMin ) * 0.5f;
}
else
{
mappedValue = outMin + ( ( ( mappedValue - inMin ) / ( inMax - inMin ) ) * ( outMax - outMin ) );
}
return mappedValue;
}
//inMinMaxCloudLayer = (InnerRadius+1500, InnerRadius+4000)
float3 GetSampleUVW(float3 pos)
{
float3 uvw;
float radius = length(pos);
float halfArcLength = 2*(inMinMaxCloudLayer.y-inMinMaxCloudLayer.x);
float xRadius = length(float2(pos.x, pos.y));
float xArcThetaRadians = halfArcLength / xRadius;
float xMax = xRadius * sin(xArcThetaRadians);
float zRadius = length(float2(pos.z, pos.y));
float zArcThetaRadians = halfArcLength / zRadius;
float zMax = zRadius * sin(zArcThetaRadians);
float2 uvMappedXZ = RangeMapFloat2(pos.xz, float2(-xMax, -zMax), float2(xMax, zMax), float2(0, 0), float2(1, 1));
uvw.x = uvMappedXZ.x;
uvw.y = Remap(radius, (radius-inMinMaxCloudLayer.x), (inMinMaxCloudLayer.y-radius), 0.0f, 1.0f);
uvw.z = uvMappedXZ.y;
return uvw;
}
Is above code ok?