Entropy Particle-Effects Tutorial

From LagoonWiki

Jump to: navigation, search

This tutorial introduces the creation of particle effects in Galaga/Entropy/Ogre3D.

Particles are used in game engines to create dynamic effects such as sparks, smoke, expolsions, erupting volcanos, and the like. They are created by rendering small textures onto invisible "billboards" in your scene. I.e., they are composed of flat images rather than 3D models, though they can have a 3D look.

Contents

The Purple Fountain Example

This example is taken from the ogre3d.org manual.

For the texture, create a new PNG image with The GIMP. This does not have to be very big, so use 128x128 or so. In the pop-up that asks you for the image size, also expand the "Advanced Options" area and set "Fill with" to "Transparency". This creates an image with a single transparent layer. (You don't want to fill the background for a particle image, or else each particle will be a square when it appears in your environment.) Create a "spark" in the image by painting a smallish white dot in the center, and then applying the Filters-->Light and Shadow-->Sparkle menu option.

Create a material named Spark that uses your new image, and save the image and the script where you have been putting your other materials. Due to the transparency the material needs an attribute we havent used before:

scene_blend alpha_blend

Which you can put near the top with the other attributes.

Now create a new directory galaga/data/particle-effects, and create a script called Fountain.particle in that directory, with the following content:

Fountain
# Stolen from http://www.ogre3d.org/docs/manual/manual_31.html#SEC159
{
   material Spark
   particle_width 5
   particle_height 5
   cull_each false
   quota 10000
   billboard_type oriented_self

   emitter Point
   {
       angle 15
       emission_rate 75
       time_to_live 3
       direction 0 1 0
       velocity_min 250
       velocity_max 300
       colour_range_start 1 0 0
       colour_range_end 0 0 1
   }

   affector LinearForce
   {
       force_vector 0 -100 0
       force_application add
   }

   affector ColourFader
   {
       red -0.25
       green -0.25
       blue -0.25
   }
}

This particle script is similar in structure to the material scripts you have made previously. It starts with the effect's name outside the brackets, and has several internal sections. First come some general attributes, then there is the definition of the emitter that generates the sparks in your environment. Then there are two (optional) affector definitions that act on the individual particles after they are emitted.

Attributes

material Spark
particle_width 5
particle_height 5
cull_each false
quota 10000
billboard_type oriented_self

These tell the engine what material to use when drawing the particles, how big to draw them, how many are allowed to exist at one time, plus a couple of other attributes that are not covered in this tutorial. Per above, the material is almost always defined with a transparent background so that the particles will not look like little squares with pictures on them. The quota is important because an excessive number of particles will overload the engine's renderer and slow down the display.

Additional attributes are described on the ogre3d.org manual manual page.

The Emitter

emitter Point
{
    angle 15
    emission_rate 75
    time_to_live 3
    direction 0 1 0
    velocity_min 250
    velocity_max 300
    colour_range_start 1 0 0
    colour_range_end 0 0 1
}

This example uses a Point emitter, which spews the particles from a single point. Other options include Ring, Cylinder, Ellipsoid, HollowEllipsoid, and Box.

The angle tells what range of directions to emit the particles in. Each particle gets a random direction within this angle.

The emission_rate tells how fast to emit the particles.

The time_to_live is how long each particle survives after being emitted. It is automatically deleted when this time expires.

The direction is the orientation of the emitter in the game world, in the familiar x,y,z coordinates. So this emitter points upward, and the individual particles are emitted within angle of its axis.

The velocity ranges let particles be emitted at different speeds. The speed will be randomly selected between the min and the max. If you want them to all travel at the same speed, use velocity without the suffixes to specify it.

The colour ranges (notice the spelling!) are specified with the familiar RGB values. The start and end values delimit a range of random colors that will be assigned to each particle. If you want them to all be the same color, use colour without the suffixes to specify it. (To make semi-transparent particles you can use an RGBA value, where the fourth number is the alpha used for rendering the color.)

  • Apparently the random colors are chosen by picking a random number in the available range for each color channel independently. Thus this is a "purple" fountain because the red and purple can range between 0 and 1, but the green is limited to 0 only.

Additional emitter attributes are described on the ogre3d manual page.

The LinearForce Affector

affector LinearForce
{
   force_vector 0 -100 0
   force_application add
}

Affectors operate on individual particles after they have been emitted. The LinearForce affector applies a force that accelerates the particles. This example applies a strong downward force to simulate the effect of gravity. (Notice that 0 -10 0 would also be a downward force, but less strong because it is a shorter vector.)

You could add a horizontal component to this force to make the particles drift in the wind as well as falling.

The ColourFader Affector

N.B. -- Notice the British spelling of "colour".

affector ColourFader
{
   red -0.25
   green -0.25
   blue -0.25
}

The ColourFader affector causes particles' colors to change with time. The numbers give an independent rate of change for each color channel. Since these are all negative, the sparks will fade to black. The numbers do not have to be negative, nor do all three have to be the same. (How would you make all the particles turn bright green?)

Other Affectors

See the ogre3d.org manual page for more predefine affectors.

Putting Particle Effects into the Environment

Add the galaga/data/particle-effects directory to your resources.cfg to make sure your definitions are loaded when the engine starts.

Modify ogreApplication.cc to put the fountain into your environment. Put the following code right after the code that added your skyplane:

ParticleSystem *fountain = sceneManager->createParticleSystem("myfountain","Fountain");
SceneNode *fountainNode = sceneManager->getRootSceneNode()->createChildSceneNode("myfountainnode",  Vector3( 600, 0, 600 ));
fountainNode->attachObject( fountain );

The first line loads the definitions from your Fountain script into an object whose variable name is fountain, and assigns it the name "myfountain" as a side effect. The second line creates a scene node object whose variable name is fountainNode, places it at a specific location in your environment, and assigns it the name "myfountainnode" as a side effect. (This example places it at elevation 0, but you may have to raise it if your terrain is very high at that point.) The third line attaches the fountain object to the scene node.

Recompile and start the engine to see the effect.

Now get creative. Put three very different particle effects into you demo. Make new materials and scripts for each, and consider trying some of the features descibed at the linked manual pages. (What happens if you put two effects at the same location? What if you put one in mid-air?)