This isn’t strictly a shader question but I think you guys have the math skills for this.
I’ve got 3d perlin noise along the surface of a sphere I am producing, and have the X,Y,Z coordinates and spherical polar coordinates of that noise. I’m know this code is good. (these coordinates do produce a sphere with nice perlin noise on the surface, for instance, verified by spawning a bunch of spheres )
for (float alpha = 0; alpha <= twopi; alpha = alpha + .002f)
{
for (float gamma = 0; gamma <= pi; gamma = gamma + .002f)
{
float x = Mathf.Sin(gamma) * Mathf.Cos(alpha);
float y = Mathf.Sin(gamma) * Mathf.Sin(alpha);
float z = Mathf.Cos(gamma);
//the above are all [0,1] as expected
float color = noise.noise3(x*10f, y*10f, z*10f); //simplex noise
Now I want to take that and map it onto a texture which will later be put onto a sphere. So I believe if I do the exact same process as UV mapping, I should get produce a texture that will wrap nicely around the sphere, like this kind of image with the increasing smearing as you get near the poles:
So, looking up how UV mapping on a sphere works I come up with this:
float x2d = Mathf.Atan2(z, x) / twopi + .5f;
float y2d = Mathf.Asin(y) / pi + .5f;
// both are 0 to 1 as expected
x2d = x2d * (float)planetTex.width;
y2d = y2d * (float)planetTex.height;
//range of the above is 0 to texture size, as expected
I feel like I am close because I’m getting at least the expected range of values, but the resulting texture is a mess: