Right now my code in hlsl looks like this:

static const float pi = 3.14159265f; float2 toSphere(float3 n) { float phi = 0; if((n.x>=0) && (n.z>=0)) { phi = atan(n.z/n.x); } if((n.x<0) && (n.z>=0)) { phi = atan(n.z/n.x); phi += pi; } if((n.x<0) && (n.z<0)) { phi = atan(n.z/n.x); phi += pi; } if((n.x>=0) && (n.z<0)) { phi = atan(n.z/n.x); phi += 2 * pi; } phi /= 2.f * pi; float theta = acos(n.y); theta /= pi; return float2(phi, theta); }

It works correctly and basically just an direct implementation from general math formulas of relation between cartezian and spherical coordinates.

But I don't think that this really an efficient implementation, there are almost no math but four branches and that's not good for shader in the way how it's used (it should break cashing). I hope somebody can direct me towards more effeicient implimentation, I know in past spherical mapping was used a lot. Hope somebody remembers "old tricks" that he could share.