using UnityEngine;
using System.Collections.Generic;
public class PointsOnSphere : MonoBehaviour {
public GameObject prefab;
public int count = 10;
public float size = 20;
[ContextMenu("Create Points")]
void Create () {
var points = UniformPointsOnSphere(count, size);
for(var i=0; i<count; i++) {
var g = Instantiate(prefab, transform.position+points[i], Quaternion.identity) as GameObject;
g.transform.parent = transform;
}
}
Vector3[] UniformPointsOnSphere(float N, float scale) {
var points = new List<Vector3>();
var i = Mathf.PI * (3  Mathf.Sqrt(5));
var o = 2 / N;
for(var k=0; k<N; k++) {
var y = k * o  1 + (o / 2);
var r = Mathf.Sqrt(1  y*y);
var phi = k * i;
points.Add(new Vector3(Mathf.Cos(phi)*r, y, Mathf.Sin(phi)*r) * scale);
}
return points.ToArray();
}
}
Tuesday, February 08, 2011
Uniform Points On a Sphere
As promised, here is my code for creating a uniform set of points on a sphere.
Subscribe to:
Post Comments (Atom)
Popular Posts

So, you've created a car prefab using WheelCollider components, and now you can apply a motorTorque to make the whole thing move along. ...

When you start working with threads in your Unity3D project, you will discover that many things need to be done in the main loop. For exampl...

Often, when building a game, you need to test if objects are colliding. The objects could be spaceships, rocks, mouse pointers, laser beams....

This script takes a screenshot and saves it to the user desktop. It has options to use an alpha background, and also upscale the image. Usef...

I decided to double check my assumptions about Stackless and the GIL on a multicore machine, using the same example provided by David Beazl...

I think ER diagrams are very handy tools for designing data requirements for a game. I don't see them given away or even talked about (i...

Summary: NodeJS wins. Test Program ab n 10000 c 5 http://localhost/ Gevent Code from gevent import wsgi class WebServer(object): ...

Possibly slightly more correct lighting. The rim light is now only applied in the direction of the sun, rather than being purely based on v...

Thank to Adrian Boeing I was inspired this morning to hack together a ripple shader for Unity3D. Thanks for the math Adrian. You can see t...

NGUI caches the transform component in a member variable called mTrans, to optimise speed of access. I think this is UGLY, so I decided to d...
4 comments:
sounds nice  an example would be neat...
This script is designed to be used from the inspector.
Simply add the component to a game object, assign a prefab, then right click the component and click "Create Points".
Wow  that is an amazing snippet, how did you come about it.
Works great!
Works nice in C#.
Iam looking for a way to do this in JS. Please help me.
var prefab : GameObject;
var count : int= 10;
var size : float= 20;
function Start () {
var points = UniformPointsOnSphere(count, size);
for(var i=0; i<count; i++) {
var g = Instantiate(prefab, transform.position+points[i], Quaternion.identity) as GameObject;
g.transform.parent = transform;
}
}
function UniformPointsOnSphere ( N : float , scale : float ) : Vector3[] {
var points = new Array (Vector3(0, 0, 0));
var i = Mathf.PI * (3  Mathf.Sqrt(5));
var o = 2 / N;
for(var k=0; k<N; k++) {
var y = k * o  1 + (o / 2);
var r = Mathf.Sqrt(1  y*y);
var phi = k * i;
points.Add(new Vector3(Mathf.Cos(phi)*r, y, Mathf.Sin(phi)*r) * scale);
}
return points.ToArray();
}
Post a Comment