You cannot create a 3D model for delicate things like fire, smoke, or explosions. Particle Emitters are quite an efficient solution to create these kinds of effects: The emitter renders a series of flat orthogonal images and manipulates them in a way that creates the illusion of a anything from a delicate smoke cloud to individual flames, etc. Creating an effect involves some trial and error to get the settings just right, and it’s worth exploring the expressiveness of the options described below.
Create an Emitter
Create one emitter for each effect:
ParticleEmitter explosion = new ParticleEmitter( "My explosion effect", Type.Triangle, 30);
Attach the emitter to the rootNode and position it in the scene:
Trigger the effect by calling
End the effect by calling
Choose one of the following mesh shapes
Not all of these parameters are required for all kinds of effects. If you don’t specify one of them, a default value will be used.
The maximum number of particles visible at the same time. Specified by user in constructor.
Density of the effect, how many new particles are emitted per second.
The radius of the scaled sprite image. Set both to same value for constant size effect.
Controls how the opaque (non-black) parts of the texture are colorized.
A vector specifying the initial direction and speed of particles. The longer the vector, the faster.
How much the direction (
true = Flying particles pitch in the direction they’re flying (e.g. missiles).
Vector3f = Flying particles face in the given direction (e.g. horizontal shockwave faces up = Vector3f.UNIT_Y).
The time period before a particle fades is set to a random value between minimum and maximum; minimum must be smaller than maximum. A minimum < 1f makes the effect more busy, a higher minimum looks more steady. Use a maximum < 1f for short bursts, and higher maxima for long lasting swarms or smoke. Set maximum and minimum to similar values to create an evenly spaced effect (e.g. fountain), set the to very different values to create a distorted effect (e.g. fire with individual long flames).
0 = Flying particles don’t spin while flying (e.g. smoke, insects, controlled projectiles).
true = The particle sprite is rotated at a random angle when it is emitted (e.g. explosion, debris).
Particles fall in the direction of the vector (e.g. debris, sparks).
By default, particles are emitted from the emitters location (a point). You can increase the emitter shape to occupy a sphere, so that the start point of new particles can be anywhere inside the sphere, which makes the effect a bit more irregular.
Build up your effect by specifying one parameter after the other. If you change several parameters at the same time, it’s difficult to tell which of the values caused which outcome.
Create an Effect Material
Use the common Particle.j3md Material Definition and a texture to specify the shape of the particles. The shape is defined by the texture you provide and can be anything – debris, flames, smoke, mosquitoes, leaves, butterflies… be creative.
Material flash_mat = new Material( assetManager, "Common/MatDefs/Misc/Particle.j3md"); flash_mat.setTexture("Texture", assetManager.loadTexture("Effects/Explosion/flash.png")); flash.setMaterial(flash_mat); flash.setImagesX(2); // columns flash.setImagesY(2); // rows flash.setSelectRandomImage(true);
The effect texture can be one image, or contain a sprite animation – a series of slightly different pictures in equally spaced rows and columns. If you choose the sprite animation:
Specify the number of rows and columns using setImagesX(2) and setImagesY().
Specify whether you want to play the sprite series in order (animation), or at random (explosion, flame), by setting setSelectRandomImage() true or false.
Examples: Have a look at the following default textures and you will see how you can create your own sprite textures after the same fashion.
Default Particle Textures
The Material is used together with grayscale texture: The black parts will be transparent and the white parts will be opaque (colored).
The following effect textures are available by default from
test-data.jar. You can also load your own textures from your assets directory.
ParticleEmitter fire = new ParticleEmitter("Emitter", Type.Triangle, 30); Material mat_red = new Material(assetManager, "Common/MatDefs/Misc/Particle.j3md"); mat_red.setTexture("Texture", assetManager.loadTexture("Effects/Explosion/flame.png")); fire.setMaterial(mat_red); fire.setImagesX(2); fire.setImagesY(2); // 2x2 texture animation fire.setEndColor( new ColorRGBA(1f, 0f, 0f, 1f)); // red fire.setStartColor(new ColorRGBA(1f, 1f, 0f, 0.5f)); // yellow fire.getParticleInfluencer().setInitialVelocity(new Vector3f(0,2,0)); fire.setStartSize(1.5f); fire.setEndSize(0.1f); fire.setGravity(0,0,0); fire.setLowLife(0.5f); fire.setHighLife(3f); fire.getParticleInfluencer().setVelocityVariation(0.3f); rootNode.attachChild(fire);
Browse the full source code of all effect examples here.
See also: Effects Overview