How cool would it be to be able to render smoke? Or fire? Everyone will need some kind of particle system eventually. In this case we will make some nice smoke. You might ask yourself what a particle system is. If you wonder about that, take a look at this wiki page. In this case, we are using a billboard particle system. A billboard is like a camera oriented plane. It will always face the camera. This makes it easier to create the textures to place on it. The particle system will spawn some billboards, apply textures on them and move them around based on some rules you can set, like gravity.
What do we need?
- First of all we need a smoke texture, which is not too hard to come by.
- Secondly, we need the particle system of TV, which is called TVParticleSystem.
How do we set things up?
- We load the texture of the smoke, you’ve seen this before.
- We create the particle system in the same way you create a mesh or actor. That is not hard to grasp, you’ve seen that before, the only thing that has changed is the function. We now call:
- The particle system will need to have an emitter to emit particles from. These are referenced by their ID, just like textures. We can create one by calling:
This creates a billboard emitter with a maximum of 1600 particles.
- Now we will set some properties to the emitter:
Particles.SetEmitterPosition(IDEmit, new TV_3DVECTOR(0, -13, 0));
Particles.SetBillboard(IDEmit, IDParticleTex, 12, 12);
The first line sets the power and lifetime of the particles of the emitter. The power is 15, the lifetime is 5 in this case.
The second line will set the position of the emitter, that’s not too hard to see.
The third line will tell the engine the emitter is spherical shaped. You can’t really see it, but it determines the position of the particles when they spawn.
The last line will set what texture should be placed on the billboard and its size. In this case the size of the billboard will be 12 by 12 units.
- Now we need to set some more advanced stuff:
Particles.SetParticleDefaultColor(IDEmit, new TV_COLOR(1, 1, 1, 1));
Particles.SetEmitterGravity(IDEmit, true, new TV_3DVECTOR(0, -10, 0));
The SetEmitterDirection function is fairly straightforward, but worth explaining, you can get cool effects with it if you know how to use it. The first parameter is of course the emitter ID. The second is a boolean to tell if there should be a main direction. The third parameter is the main direction. It tells in which direction the particles should initially move. The last parameter is the random direction factor. If you supply 1,1,1, it will be random in all directions, if you supply the values we supplied, it will only be random a bit in the x and z axis.
The SetParticleDefaultColor function tells what kind of material color there is for the billboards. In our case just plain white.
The SetEmitterGravity function tells the engine in what direction there should be gravity. In our case it’s downward with a strength of 10.
- Just to answer one of the questions you might have, you don’t have to create any billboards or particles yourself, the particle system will do that for you.
How do we render this?
- Before rendering, we need to call
to update the particle system. This will emit new particles, remove them, move them etc.
- To render the particles, we call
Things you can add/change yourself
- Try playing with all of these functions. Try to make nice shapes, big explosions, anything you want.
- Change the billboard texture to some fiery thing or some other random texture. Try to create a fire, waterfall, or any thing you want, because particle systems are just plain fun!