Last active
December 20, 2015 07:09
-
-
Save AngeloYazar/6091095 to your computer and use it in GitHub Desktop.
Spheres On Sphere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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