= Particle Emitter Settings
:revnumber: 2.2
:revdate: 2020/07/24
:keywords: documentation, effect
:uri-jmonkeyengine: https://github.com/jMonkeyEngine/jmonkeyengine/tree/master/
:uri-forum: https://hub.jmonkeyengine.org/


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 xref:sdk:scene_explorer.adoc[Scene Explorer] in the xref:sdk:sdk.adoc[SDK] to design and preview effects.
====


image:effect/explosion-5.png[explosion-5.png,width="150",height="100"]
image:effect/particle.png[particle.png,width="150",height="100"]
image:tutorials:beginner/beginner-effect-fire.png[beginner-effect-fire.png,width="150",height="100"]
image:effect/butterfly-particle-emitter.png[butterfly-particle-emitter.png,width="150",height="100"]


== Create an Emitter

.  Create one emitter for each effect:
+
[source,java]
----
ParticleEmitter explosion = new ParticleEmitter(
"My explosion effect", Type.Triangle, 30);
----

.  Attach the emitter to the rootNode and position it in the scene:
+
[source,java]
----
rootNode.attachChild(explosion);
explosion.setLocalTranslation(bomb.getLocalTranslation());
----

.  Trigger the effect by calling
+
[source,java]
----
explosion.emitAllParticles()
----

.  End the effect by calling
+
[source,java]
----
explosion.killAllParticles()
----


Choose one of the following mesh shapes

*  Type.Triangle
*  Type.Point


== Configure Parameters

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.
[cols="15,25,20,40", options="header"]
|===

<a| Parameter
<a| Method
a| Default
a| Description

<a| number
a| `setNumParticles()`
<a|
a| The maximum number of particles visible at the same time. Specified by user in constructor.

<a| emission rate
a| `setParticlesPerSec()`
a| 20
a| 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.

<a| size
a| `setStartSize()`, `setEndSize()`
a| 0.2f, 2f
a| The radius of the scaled sprite image. Set both to same value for constant size effect. +
Set to different values for shrink/grow effect.

<a| color
a| `setStartColor()`, `setEndColor()`
a| gray
a| 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).

<a| direction/velocity
a| `getParticleInfluencer(). setInitialVelocity(initialVelocity)`
a| Vector3f(0,0,0)
a| A vector specifying the initial direction and speed of particles. The longer the vector, the faster.

<a| fanning out
a| `getParticleInfluencer().setVelocityVariation(variation)`
a| 0.2f
a| 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).

a| direction +
(pick one)
a| `setFacingVelocity()`
a| false
a| 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).

a| direction +
(pick one)
a| `setFaceNormal()`
a| Vector3f.NAN
a| Vector3f = Flying particles face in the given direction (e.g. horizontal shockwave faces up = Vector3f.UNIT_Y). +
Vector3f.NAN = Flying particles face the camera.

<a| lifetime
a| `setLowLife()`, `setHighLife()`
a| 3f, 7f
a| 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).

<a| spinning
a| `setRotateSpeed()`
a| 0f
a| 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).

<a| rotation
a| `setRandomAngle()`
a| false
a| 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).

<a| gravity
a| `setGravity()`
a| Vector3f(0.0f,0.1f,0.0f)
a| 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.)

<a| start area
a|`setShape(new EmitterSphereShape( Vector3f.ZERO, 2f));`
a|EmitterPointShape()
a|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


image:tutorials:beginner/flash.png[flash.png,width="128",height="128"]


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.

[source,java]
----

    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.

include::tutorials:partial$default-particles-table.adoc[]

[TIP]
====
Use the `setStartColor()`/`setEndColor()` settings described above to colorize the white and gray parts of textures.
====


== Usage Example

[source,java]
----

    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 link:https://github.com/jMonkeyEngine/jmonkeyengine/tree/master/jme3-examples/src/main/java/jme3test/effect[effect examples] here.

'''

See also: xref:effect/effects_overview.adoc[Effects Overview]
