If anybody should need a script (UnityScript) for evenly distributing points on a surface of a sphere then here it is.
Save as PointsOnSphere.js
Edit: Would be interesting to see the difference in execution time between this UnityScript version and in pure c# (if anybody wants to do it )
/*
Points on a sphere
Adaptation from Patrick Boucher: [url]http://www.xsi-blog.com/archives/115[/url]
Description:
If you need to distribute objects evenly on the surface of a sphere then this script could be helpful.
Usage:
Simply attach this script to a GameObject, edit the scaling value and the numbers of spheres you want created.
The spheres are then created as children.
Philipp Smolka
07.07.09
*/
function Start() {
var scaling : float = 10;
var pts : Vector3[] = PointsOnSphere(1024);
var uspheres = new Array();
var i : int = 0;
for (var value : Vector3 in pts){
uspheres.Push(GameObject.CreatePrimitive(PrimitiveType.Sphere));
uspheres[i].transform.parent = transform;
uspheres[i].transform.position = value * scaling;
i++;
}
}
function PointsOnSphere(n) {
var upts = new Array();
var inc : float = Mathf.PI * (3 - Mathf.Sqrt(5));
var off : float = 2.0 / n;
var x : float;
var y : float;
var z : float;
var r : float;
var phi : float;
for (var k = 0; k < n; k++){
y = k * off - 1 + (off /2);
r = Mathf.Sqrt(1 - y * y);
phi = k * inc;
x = Mathf.Cos(phi) * r;
z = Mathf.Sin(phi) * r;
upts.Push(Vector3(x, y, z));
}
var pts : Vector3[] = upts.ToBuiltin(Vector3);
return pts;
}
function Update () {
}
This is years later, but since this is one of the places that Google took me when researching the problem I thought I’d share here. I took a different approach to solving this for my own game, and since there wasn’t any recent scripts that I had any luck with. I ended up taking the final script and posting it in the Asset Store if anyone in the future is interested: ShapeCover | Modeling | Unity Asset Store
When I use this the points appear to be evenly distributed, however they are not on the sphere. They are off and down to the right completely off the sphere.
FIXED
I just changed line 18 to uspheres.transform.localposition = value * scaling; Edit #2: If previous doesnt fix the issue, instead of applying scaling on line 18 do this inside of pointsOnSphere and pass scaling to the method ```csharp *Vector3 b = new Vector3(x,y,z);
Vector3 a = new Vector3(0,0,0);
Vector3 correction = (b - a).normalized * scaling/2;
If you need a spherical mesh, then why not simply create a sphere gameobject? This here is rather helpful if you have a sphere (or sphere-like) object and want to place something on or around it - like for example trees on a planet.
Mostly because I like the challenge! But for practical reasons, I want to simulate each point on the sphere separately to create a blob-like shape. Each point is connected to a center point and to it’s neighbors via springs.