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.
Tip: Use the Scene Editor in the jMonkeyPlatform to design and preview effects.
ParticleEmitter explosion = new ParticleEmitter( "My explosion effect", ParticleMesh.Type.Triangle, 30);
rootNode.attachChild(explosion); explosion.setLocalTranslation(bomb.getLocalTranslation());
explosion.emitAllParticles()
explosion.killAllParticles()
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.
Parameter | Method | Default | Description |
---|---|---|---|
number | setNumParticles() | The maximum number of particles visible at the same time. Specified by user in constructor. | |
emission rate | setParticlesPerSec() | 20 | Density of the effect, how many new particles are emitted per second. Set to zero to control the start/end of the effect. Set to a number for a constantly running effect. |
size | setStartSize() , setEndSize() | 0.2f, 2f | The radius of the scaled sprite image. Set both to same value for constant size effect. Set to different values for shrink/grow effect. |
color | setStartColor() , setEndColor() | gray | Controls how the opaque (non-black) parts of the texture are colorized. Set both to the same color for single-colored effects (e.g. fog, debris). Set both to different colors for a gradient effect (e.g. fire). |
direction/velocity | getParticleInfluencer(). setInitialVelocity(initialVelocity) | Vector3f(0,0,0) | A vector specifying the initial direction and speed of particles. The longer the vector, the faster. |
fanning out | getParticleInfluencer(). setVelocityVariation(variation) | 0.2f | How much the direction (setInitialVelocity() ) can vary among particles. Use a value between 1 and 0 to create a directed swarm-like cloud of particles. 1 = Maximum variation, particles emit in random 360° directions (e.g. explosion, butterflies). 0.5f = particles are emitted within 180° of the initial direction. 0 = No variation, particles fly in a straight line in direction of start velocity (e.g. lasergun blasts). |
direction (pick one) | setFacingVelocity() | false | true = Flying particles pitch in the direction they're flying (e.g. missiles). false = Particles keep flying rotated the way they started (e.g. debris). |
direction (pick one) | setFaceNormal() | Vector3f.NAN | Vector3f = Flying particles face in the given direction (e.g. horizontal shockwave faces up = Vector3f.UNIT_Y). Vector3f.NAN = Flying particles face the camera. |
lifetime | setLowLife() , setHighLife() | 3f, 7f | 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). |
spinning | setRotateSpeed() | 0f | 0 = Flying particles don't spin while flying (e.g. smoke, insects, controlled projectiles). > 0 = How fast particle spins while flying (e.g. debris, shuriken, missiles out of control). |
rotation | setRandomAngle() | false | true = The particle sprite is rotated at a random angle when it is emitted (e.g. explosion, debris). false = Particles fly straight like you drew them in the sprite texture (e.g. insects). |
gravity | setGravity() | Vector3f(0.0f,0.1f,0.0f) | Particles fall in the direction of the vector (e.g. debris, sparks). (0,0,0) = Particles keep flying in start direction (e.g. flames, zero-gravity explosion.) |
start area | setShape(new EmitterSphereShape( Vector3f.ZERO, 2f)); | EmitterPointShape() | 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 you 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.
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:
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.
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.
Texture Path | Dimension | Preview |
---|---|---|
Effects/Explosion/Debris.png | 3*3 | |
Effects/Explosion/flame.png | 2*2 | |
Effects/Explosion/flash.png | 2*2 | |
Effects/Explosion/roundspark.png | 1*1 | |
Effects/Explosion/shockwave.png | 1*1 | |
Effects/Explosion/smoketrail.png | 1*3 | |
Effects/Explosion/spark.png | 1*1 | |
Effects/Smoke/Smoke.png | 1*15 | |
Tip: Use the setStartColor()
/setEndColor()
settings described above to colorize the white and gray parts of textures.
ParticleEmitter fire = new ParticleEmitter("Emitter", ParticleMesh.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 here.
See also: Effects Overview