My problem is that i try to “UV map” an unwrapped jupiter onto points on a sphere and i did that but the texture doesnt rotate (just occasionaly flips 180, to be exact i see the poles of the planet) with the sphere, And i know i have a wierd system but i like the look and vibe of this. My code is janky and some parts are obsolete but for now i dont need to change that
using UnityEngine;
using UnityEngine.UI;
public class MakeSemiSphere : MonoBehaviour
{
public int numPoints = 100; // Number of points to generate
public int RedPointPrc = 10;
public float radius = 1.0f; // Radius of the sphere
public float angle1;
public float angle2;
public float angle3;
public Texture2D PlanetTexture;
public RawImage image;
private Texture2D OutputTexture;
private void MakeTextureBlack()
{
OutputTexture = new Texture2D(100, 100);
OutputTexture.filterMode = FilterMode.Point;
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100; j++)
{
OutputTexture.SetPixel(i, j, Color.black);
}
}
OutputTexture.Apply();
}
void Update()
{
if (Input.GetKey(KeyCode.Space))
{
MakeTextureBlack();
for (int i = 0; i < numPoints; i++)
{
Vector3 v = GeneratePointOnSphere(i, numPoints, radius);
Vector3 rotatedV = RotatePoint(v, angle1, angle2, angle3);
// Scale and center the point on the 2D plane
Vector2 FlatPoint = new Vector2(rotatedV.x, rotatedV.y);
// Calculate spherical coordinates
float theta = Mathf.Atan2(rotatedV.y, rotatedV.x);
if (theta < 0)
theta += 2 * Mathf.PI;
float phi = Mathf.Acos(rotatedV.z / radius);
// Normalize Theta and phi to [0, 1] range
float u = theta / (2 * Mathf.PI);
float vNorm = phi / Mathf.PI;
// Map to texture coordinates
int x = Mathf.RoundToInt(u * (PlanetTexture.width - 1));
int y = Mathf.RoundToInt((1 - vNorm) * (PlanetTexture.height - 1));
// Clamp coordinates
x = Mathf.Clamp(x, 0, PlanetTexture.width - 1);
y = Mathf.Clamp(y, 0, PlanetTexture.height - 1);
// Sample pixel color from PlanetTexture
Color pixelColor = PlanetTexture.GetPixel(x, y);
// Determine color intensity based on distance from center
float colorIntensity = GetHowFarFromCenter(FlatPoint);
// Blend colors based on RedPointPrc threshold
if (i < numPoints * RedPointPrc / 100)
{
// Red point
OutputTexture.SetPixel(Mathf.RoundToInt(FlatPoint.x + 50), Mathf.RoundToInt(FlatPoint.y + 50), Color.red * colorIntensity);
}
else
{
// Sampled texture color
OutputTexture.SetPixel(Mathf.RoundToInt(FlatPoint.x + 50), Mathf.RoundToInt(FlatPoint.y + 50), pixelColor);
}
}
// Apply changes to OutputTexture and update RawImage
OutputTexture.Apply();
image.texture = OutputTexture;
angle1++;
angle2 += 3;
angle3 += 2;
}
}
Vector3 RotatePoint(Vector3 point, float angleX, float angleY, float angleZ)
{
// Convert angles to radians
float angleXRad = Mathf.Deg2Rad * angleX;
float angleYRad = Mathf.Deg2Rad * angleY;
float angleZRad = Mathf.Deg2Rad * angleZ;
// Rotate around X axis
Vector3 rotated = new Vector3(
point.x,
point.y * Mathf.Cos(angleXRad) - point.z * Mathf.Sin(angleXRad),
point.y * Mathf.Sin(angleXRad) + point.z * Mathf.Cos(angleXRad)
);
// Rotate around Y axis
rotated = new Vector3(
rotated.x * Mathf.Cos(angleYRad) + rotated.z * Mathf.Sin(angleYRad),
rotated.y,
-rotated.x * Mathf.Sin(angleYRad) + rotated.z * Mathf.Cos(angleYRad)
);
// Rotate around Z axis
rotated = new Vector3(
rotated.x * Mathf.Cos(angleZRad) - rotated.y * Mathf.Sin(angleZRad),
rotated.x * Mathf.Sin(angleZRad) + rotated.y * Mathf.Cos(angleZRad),
rotated.z
);
return rotated;
}
float GetHowFarFromCenter(Vector2 vector)
{
Vector2 HowFar = new Vector2(50 - vector.x, 50 - vector.y);
HowFar /= 50;
return HowFar.magnitude;
}
Vector3 GeneratePointOnSphere(int index, int totalPoints, float radius)
{
// Golden ratio
float phi = (1 + Mathf.Sqrt(5)) / 2;
// Calculate spherical coordinates
float theta = 2 * Mathf.PI * index / phi;
float z = 1 - (2 * index + 1) / (float)totalPoints;
float radiusXY = Mathf.Sqrt(1 - z * z);
float x = radiusXY * Mathf.Cos(theta);
float y = radiusXY * Mathf.Sin(theta);
// Convert to Cartesian coordinates
return new Vector3(x, y, z) * radius;
}
}
I tried most of the things that would come to mind but im 100% sure that i just mist one line or didnt change a variable but im going blind from looking at the code
what i tried:
changing the rotation matrix,
making sure i writen the phi and theta projections right
changing the sample method