Leaving this here for myself:
static const float PI = 3.14159265f;
// assumes p is a point on unit sphere
// https://en.wikipedia.org/wiki/File:Equirectangular_projection_SW.jpg
float2 sphere2mapUV_Equirectangular(float3 p)
{
return float2(
atan2(p.x, -p.z) / (2 * PI) + .5,
p.y * .5 + .5
);
}
// https://en.wikipedia.org/wiki/File:Lambert_cylindrical_equal-area_projection_SW.jpg
float2 sphere2mapUV_EqualArea(float3 p)
{
return float2(
(atan2(p.x, -p.z) / PI + 1) / 2,
asin(p.y) / PI + .5
);
}
I added links to example images in case you need a visual reference for checking which projection your map is made with.
Usage:
float2 uv = sphere2map(normalize(position));
That assumes position is in model space.
In case you're using this in vertex shader the position
might already be normalized, so normalization could be skipped.
Your sphere model will usually have center at float3(0,0,0)
, but if that's not the case you'll have to subtract the sphere_center_position
from position
.