Skip to content

Instantly share code, notes, and snippets.

@hariedo
Created March 29, 2024 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hariedo/fa60a3360ee7a00eec35f760c7b9c434 to your computer and use it in GitHub Desktop.
Save hariedo/fa60a3360ee7a00eec35f760c7b9c434 to your computer and use it in GitHub Desktop.
public static Vector3[] DistributePointsOnUnitSphere(int count)
{
Assert.IsTrue(count > 0);
Vector3[] points = new Vector3[count];
// The Fibonacci Sphere algorithm.
float phi = Mathf.PI * (Mathf.Sqrt(5f) - 1f);
float descent = 2f / (float)(count-1);
for (int i = 0; i < count; i++)
{
// calculate Y between 1 and -1
float y = 1f - i * descent;
float radius = Mathf.Sqrt(1f - y * y);
float theta = phi * (float)i;
float x = radius * Mathf.Cos(theta);
float z = radius * Mathf.Sin(theta);
points[i] = new Vector3(x, y, z);
}
return points;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment