Skip to content

Instantly share code, notes, and snippets.

@AngeloYazar
Last active December 20, 2015 07:09
Show Gist options
  • Save AngeloYazar/6091095 to your computer and use it in GitHub Desktop.
Save AngeloYazar/6091095 to your computer and use it in GitHub Desktop.
Spheres On Sphere
//based on: http://www.softimageblog.com/archives/115
void SpheresOnSphere( int n ) {
float increment = Mathf.PI * ( 3 - Mathf.Sqrt(5) );
float offset = 2 / (float)n;
for(int i = 0; i < n; i++) {
float y = i * offset - 1 + (offset / 2);
float r = Mathf.Sqrt(1 - y*y);
float currentAngle = i * increment;
Vector3 position = new Vector3(Mathf.Cos(currentAngle)*r, y, Mathf.Sin(currentAngle)*r) * 5; // 5 is the radius of the sphere
//Instantiate a cube:
GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = position;
// Get the rotation and apply it to the cube:
Quaternion rotation = Quaternion.LookRotation(position);
cube.transform.rotation = rotation;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment