Converting a Cube Map to a Spherical/Equirectangular Map

Cubemap to Spherical
Ever want to convert from a cubemap (6 textures of a cube) to a spherical/equirectangular map? I randomly decided to fiddle around with generating simple planet textures and thought it might come in handy since planets are often just sphere meshes with a spherical texture mapped on em. Sure, they compress at the top and bottom, but they’re easy to apply and look decent most of the time.
About the following code, I haven’t 100% confirmed this mapping is correct for all cubemaps, but I exported a debug cubemap from Spacescape and ran it through this GLSL pixel shader to convert from the cubemap to spherical and it appears to work.

uniform samplerCube cubemapTexture;
varying vec3 vertexPos;
varying vec2 outUV;
vec4 cubeToLatLon(samplerCube cubemap, vec2 inUV)
 const float PI = 3.141592653589793238462643383;
 vec3 cubmapTexCoords;
 cubmapTexCoords.x = -sin(inUV.x * PI * 2.0) * sin(inUV.y * PI);
 cubmapTexCoords.y = cos(inUV.y * PI);
 cubmapTexCoords.z = -cos(inUV.x * PI * 2.0) * sin(inUV.y * PI);
 return textureCube(cubemap, cubmapTexCoords);
void main( void )
 gl_FragColor = cubeToLatLon(cubemapTexture, outUV);

The uvCoords for x and y range from 0 to 1.
For some reason the recommended algorithm on StackOverflow did not work for me and so I just pulled up Wolfram Alpha to generate the graphs and did it myself. I love Wolfram Alpha.

, ,