- Home /
Point list rendering in Unity3d
Hi all,
I'm trying to see if I can make Unity my next project engine and I'm trying to port some code I created on Xna to create a randomly generated Starfield using Points uniformally distributed on a sphere rendered using PointList primitive type.
However, I didn't find any way for me to do such thing. Would anyone see how I could achieve that?
Here is the original generation code:
Random rand = new Random(seed); VertexPositionColor[] verts = new VertexPositionColor[MAX_NUMBER_OF_STARS];
if (numberOfStars > MAX_NUMBER_OF_STARS)
numberOfStars = MAX_NUMBER_OF_STARS;
for (int i = 0; i < numberOfStars; i++)
{
double x, y, z, w, t;
z = 2.0 * rand.NextDouble() - 1.0;
t = 2.0 * MathHelper.Pi * rand.NextDouble();
w = Math.Sqrt(1 - z * z);
x = w * Math.Cos(t);
y = w * Math.Sin(t);
float color = (float)rand.NextDouble();
verts[i] = new VertexPositionColor(new Vector3((float)x * 1000, (float)y * 1000, (float)z * 1000), new Color(color, color, color, color));
}
__SmallStarVertices = new Vertices<VertexPositionColor>(verts);
And the Rendering code:
__SmallStarVertices.Draw(state, null, PrimitiveType.PointList);
Note that __SmallStarVertices is just a class that abstracts some inner render states helper calls from my previous graphics lib.
Answer by duck · Jan 18, 2010 at 06:24 PM
Well, in terms of porting your "random points on a sphere" code, there's a function in unity to do just that:
For the actual rendering of the stars, I'm not aware of a way of rendering vertices as point on the screen, but perhaps you might consider using a particle system.
You could start by using this basic setup for creating a nebula, and then assign a small point-like texture for your star particles. Make sure the particle animator is deleted, so that the particles don't move. Then instead of allowing the particle emitter itself to emit the particles, you could use the particle system API to set a list of particles in the positions selected by your "Random.onUnitSphere" loop.