I'm currently writing a program to randomly generate low reoslution planets. I already have a way to render flat 2D planets but now I'd like to add an option to give them the illusion of being 3D as well.
To achive this I had the Idea to create a 3D noise Sphere. First, I Translate (x,y) coordinates to the corresponding (x,y,z) coordinates on the Sphere. Then I rotate the Sphere to get the coordinates at animationFrame i and use those to sample the noise at these coordinates.
For this I wrote the following code:
for (let x = -radius; x < radius; x++)
for (let y = -radius; y < radius; y++)
for (let z = 0; z < animationLength; z++) {
//rotation for this frame
//map re-maps a number from one range to another.
const angle = map(z, 0, animationLength, 0, 2 * Math.PI);
//translate 2D to 3D point
const longitude = (x * Math.PI) / 180;
const latitude = (y * Math.PI) / 180;
const SphereX = radius * Math.cos(latitude) * Math.cos(longitude);
const SphereY = radius * Math.cos(latitude) * Math.sin(longitude);
const SphereZ = radius * Math.sin(latitude);
//Rotate Sphere on Y Axis depending on animationFrame
const rotX = (SphereX * Math.cos(angle) + SphereZ * -Math.sin(angle));
const rotZ = (SphereX * Math.sin(angle) + SphereZ * Math.cos(angle));
const noiseVal = noise3D(rotX , SphereY, rotZ );
let val = noiseVal / (1.5);
val = Math.pow(val * 1.15, 0.9);
texture[x + radius][y + radius][z] = val
}
The porblem is that (I think) the Sphere is rendered from the origin at (0,0), making it look like this:
To solve this, I think I would have to look at the sphere from a fixed position outside it, but I don't know how I would go about that.
If anyone knows how this can be achived I'd greatly appreciate that.