Join Seb Lee-Delisle for an in-depth discussion in this video Arranging the particles, part of Papervision3D 2 Essential Training.
So now we're going to learn how to arrange our particles in several different ways. So here is our exercise file. Pretty much the same as it was before, just putting our particles in at random positions. Let's delete this line, and uncomment this line now and what this line of code does is just like before, it creates a new particle with the particlematerial we're giving it. It gives the scale of 0, and I'm going to just make that a little bit larger. Let's make it 1 so that we know that our pixels will match our units, but of course, then it's divided by 5 with this scale factor here from the material.
Now these two parameters here define the X and Y position of our particle. We're actually using some math here which takes our I value and converts it into a grid of X and Y. So as I increments, we'll find that our X position increments by 50 every time, but then because we're guessing i%10 this means that this value be reset back to 0 once we get above 10. So this means that our X value will be going up to 10 and then back to 0 and then up to 10 again.
So then this next part actually increases the Y position of our particle every 10 particles. So this has the effect of arranging our particles in a grid. There is some fairly complex stuff going on there. So I recommend that you take the time to just study that in more detail, if arranging things using math is something that you're interested in. So let's take a look at that. So now you can see our particles are arranged in a grid. So let's look at more interesting ways to arrange our particles. I'm going to delete this For loop and uncomment this one. Currently, this is doesn't actually do anything except add 1000 particles all at the same position.
What I'm trying to do here is arrange the particles randomly around the outside of this sphere, and I'll show you exactly how we're doing this. So, the first thing. We want to know how wide that sphere should be. In this case, our sphere is going to have a radius of 200 units. So then we iterate amount of 1000 times this bit of code here. The first we do is we're going to create a Number3D. A Number3D is a three-dimensional point, and it's one of that built-in Papervision objects. So using the Number3D constructively we are setting the X value of this Number3D to be the radius and the Y and the Z values to be 0.
This means that this position is over to the right. Now the next thing we're going to do is rotate that value around the Y axis. There is a built-in method of the Number3D, which is rotateY. So that will rotate our Number3D around the Y axis. We're going to do that by a random around between 0 and 360. Let's just take a look at that. So now you can see we've 1000 particles all arranged in a huge ring. That's probably too many particles to actually have within that small space, but this is just the first step of where we're going.
So now we're rotating it randomly around the Y axis. We're subsequently then going to rotate randomly around the X axis. But we're not going to go all the way from 0-360. We're only going to go from between 0-180. So if you imagine what this is doing is it's rotating a random amount around the Y axis between 0 and 360. So that's a full circle. And then it's rotating a random amount around 180 around the X axis, which is half a circle.
And if you can visualize that, that should produce an entire sphere of particles. Let's just take a look, and here you have it. So by using a Number3D we can actually reposition our particles around the surface of this sphere. Now let's move on to the next file, scrolling down to take a look at this. It's almost identical to the one before, but this time instead of randomly positioning our particles, we're firstly creating a sphere. So, this is the same as the spheres as we've used before.
So we're not actually going to place this sphere inside our scene. We're just going to use the points within the sphere to arrange our particles. So now we're actually to iterate through every vertex within this sphere, find the geometry for all of the DisplayObjects3D are stored inside a geometry object. Inside of that geometry object is an array of vertices. These are all the points that are within our DisplayObject3D. So using this loop we can iterate through each one of the points in the sphere. So then when we create our particle, we assign it a position that is relative to that vertex position.
So we give that an X value that not just the vertex's x value, and so on with the Y and Z. This means that we're arranging a single particle at every point where there is a vertex within the sphere. Let's take a look at that. So you can see what we have our sphere full of particles and of course in this example we've also added a reflection. So now you know how you can manually alter the position of our particles to make some really interesting and creative shapes.
Users should have a basic knowledge of Flash Builder and Actionscript 3.
- Creating 3D primitive shapes
- Controlling rotation
- Moving the camera in 3D space
- Creating a particle field with depth-of-field blur
- Importing models from external applications
- Making a simple 3D game
- Using ViewportLayers to selectively render and sort layers
- Placing a custom 3D model into an augmented reality scene
- Creating interactive carousals
- Creating interactive grids
- Working with particles and billboards