API

Animating Particles

Creating Animated Particles

Here we are talking about the animation of the texture of a particle rather than any movement of the particle itself. This feature uses a similar system to that of sprite animation and was added to Babylon.js v3.1. As with sprite animation, it requires a uniform spritesheet.

You must set the property isAnimationSheetEnabled to true for animated particles either on or after construction (fourth parameter):

import { ParticleSystem } from "@babylonjs/core/Particles/particleSystem";
particleSystem = new ParticleSystem("particles", capacity, scene, null, true); // on construction
particleSystem = new ParticleSystem("particles", capacity);
particleSystem.isAnimationSheetEnabled = true; // after construction

As an example, we use the same spritesheet as in the sprite manager section of the documentation.

sheet

This spritesheet is used as the particleTexture:

import { Texture } from "@babylonjs/core/Materials/Textures/texture";
particleSystem.particleTexture = new Texture("textures/player.png", scene, true, false);

In this case, the third parameter noMipMaps is set to true and the fourth, invertY, to false to read the texture data from top to bottom.

particleSystem.spriteCellHeight = 64;
particleSystem.spriteCellWidth = 64;
particleSystem.startSpriteCellID = 0;
particleSystem.endSpriteCellID = 9;
particleSystem.spriteCellChangeSpeed = 4; // default is one

The spriteCellHeight and spriteCellWidth are the cell height and width for each sprite in the animation sheet.

Cell Dimensions

We are only going to use the first 10 cells of the spritesheet, and so startSpriteCellID is 0 and endSpriteCellID is 9. The top-left sprite is in cell 0, and you count from left to right from the top row downwards.

Starting with Babylon.js v3.3, you can also use

particleSystem.spriteRandomStartCell = true;

to randomly pick the start cell ID of each particle between startSpriteCellID and endSpriteCellID.

To control the speed of animation, you can experiment with adjusting direction, emitPower, and updateSpeed, though doing so adjusts the whole particle animation and not just the sprite animation frame rate.

To adjust the sprite animation frame rate, you set the property spriteCellChangeSpeed. A value of 1 matches the animation rate to the lifetime of a particle. The particle will be emitted with the sprite at startSpriteCellID and will die displaying the sprite at endSpriteCellID. As values increase above 1, the animation frame rate speeds up. Although the particle will be emitted with the sprite at startSpriteCellID, the sprite on display when the particle dies will depend on the value of the speed and the lifetime of the particle.

Examples

Adjust emit power and update speed: Adjust Emit Power And Update Speed
Cell Change Speed: Cell Change Speed