GPU Particles

Heaps supports rendering particles on the GPU. This allows for an extremely high amount of particles to be rendered on screen with very little performance impact.

Setting up your GPU particles involves first creating a particle system.

  1. //Create a particle system and pass it our 3d scene
  2. var particles = new h3d.parts.GpuParticles(s3d);

From there you add particle groups to the system. Each group is it’s own separate bundle of particles that work independently. A particle system can support multiple groups.

  1. //Create a particle group with a reference to our particle system
  2. var particleGroup = new h3d.parts.GpuParticles.GpuPartGroup(particles);

When you are ready to see your particle group on screen you just need to add it to the system.

  1. particles.addGroup(particleGroup);

Customizing Particles

GPU Particles have a lot of properties that can be modified to give your particles different behavior. See the API docs for a full list.

  1. //The shape at which the particles will be emitted each ar of type GPUEmitMode
  2. particleGroup.emitMode = Cone;
  3. //The angle which the paricles will be emitted
  4. particleGroup.emitAngle = 0.5;
  5. //The distance from the initial spawn location of each particle
  6. particleGroup.emitDist = 0;
  7. //Fade in and fade out time for each particle
  8. particleGroup.fadeIn = 0.8;
  9. particleGroup.fadeOut = 0.8;
  10. particleGroup.fadePower = 10;
  11. //How much the particles are effective by gravity
  12. particleGroup.gravity = 5;
  13. //The initial size of each particle
  14. particleGroup.size = 0.1;
  15. //Random size offset of each particle
  16. particleGroup.sizeRand = 0.5;
  17. //How fast the particles will rotate
  18. particleGroup.rotSpeed = 10;
  19. //The speed of each particle
  20. particleGroup.speed = 2;
  21. //Random variation in speed for each particle
  22. particleGroup.speedRand = 0.5;
  23. //The lifespan of the particle in seconds
  24. particleGroup.life = 2;
  25. //Random variance in lifespan
  26. particleGroup.lifeRand = 0.5;
  27. //The number of particles in a group - these all get uploaded to the GPU
  28. particleGroup.nparts = 10000;
  29. //You can assign a texture to the particle group
  30. //Every particle will have the texture appllied.
  31. particleGroup.texture = hxd.Res.hxlogo.toTexture();
  32. //Use Texture.fromColor to set the color of each particle.
  33. particleGroup.texture = h3d.mat.Texture.fromColor(0xEA8220);

10000 textured particles running on the GPU Haxe Logo particle group

Looping

By default the particle groups will loop. You can disable this with the following

  1. particleGroup.emitLoop = false;

If your particle groups are not set to looping you can listen for a completion event to know when the particle system is done emitting. Note that

  1. particles.onEnd = function(){
  2. //If none of the groups in this particle system are set to looping
  3. //this method will fire once the system is done emitting particles
  4. }