Last active
June 9, 2016 14:26
-
-
Save DaveAtDog/4c94a28e685e70e5eeb6ae18fbcb205d to your computer and use it in GitHub Desktop.
My preferred way of positioning 3D objects on the surface of a sphere.
This file contains 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 example C# code from http://forum.unity3d.com/threads/evenly-distributed-points-on-a-surface-of-a-sphere.26138/#post-2031195 | |
// objects is an array of THREE.js 3D Objects | |
function positionObjectsOnSphereSurface(objects) { | |
var radius = 900; | |
var n = objects.length; | |
var inc = Math.PI * (3 - Math.sqrt(5)); | |
var off = 2 / n; | |
var x = 0; | |
var y = 0; | |
var z = 0; | |
var r = 0; | |
var phi = 0; | |
for (var k = 0; k < n; k++) { | |
var object = objects[k]; | |
y = k * off - 1 + (off / 2); | |
r = Math.sqrt(1 - y * y); | |
phi = k * inc; | |
x = Math.cos(phi) * r; | |
z = Math.sin(phi) * r; | |
x *= radius; | |
y *= radius; | |
z *= radius; | |
object.position.x = x; | |
object.position.y = y; | |
object.position.z = z; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment