THREE.js particle engine (wip)


SUBMITTED BY: Guest

DATE: Aug. 29, 2012, 7:42 a.m.

FORMAT: JavaScript

SIZE: 3.0 kB

HITS: 1682

  1. /**
  2. *
  3. * Class Name : ParticleEmitter
  4. * Created : 28/08/2012
  5. * Require : Three.js
  6. *
  7. **/
  8. GAME_ENGINE.ParticleEmitter = function(scene, picture, psize, max) {
  9. this.scene = scene;
  10. this.psize = (psize) ? psize : 20;
  11. this.maxParticleCount = (max) ? max : 20;
  12. this.emitting = false;
  13. this.timer = 0;
  14. this.delay = 0; //60 fps -> 1 particle per second
  15. this.particleAnimation = null;
  16. this.particleInitializer = null;
  17. this.particles = [];
  18. this.Material = { color: 0xFFFFFF, size: psize, map: THREE.ImageUtils.loadTexture(picture), blending: THREE.AdditiveBlending, transparent: true };
  19. this.position = new THREE.Vector3(0, 0, 0);
  20. }
  21. GAME_ENGINE.ParticleEmitter.prototype.setParticleAnimation = function(animation) {
  22. this.particleAnimation = animation;
  23. };
  24. GAME_ENGINE.ParticleEmitter.prototype.setParticleInitializer = function(initializer) {
  25. this.particleInitializer = initializer;
  26. };
  27. GAME_ENGINE.ParticleEmitter.prototype.start = function() {
  28. this.emitting = true;
  29. };
  30. GAME_ENGINE.ParticleEmitter.prototype.stop = function() {
  31. this.emitting = false;
  32. };
  33. GAME_ENGINE.ParticleEmitter.prototype.destroyParticle = function (index) {
  34. this.scene.remove( this.particles[index] );
  35. this.particles.splice(index, 1);
  36. }
  37. GAME_ENGINE.ParticleEmitter.prototype.update = function( delta ) {
  38. if (this.emitting) {
  39. if (this.timer >= this.delay) {
  40. if(this.particles.length < this.maxParticleCount) {
  41. var particle = new THREE.Sprite( this.Material );
  42. particle.useScreenCoordinates = false;
  43. particle.mergeWith3D = true;
  44. particle.affectedByDistance = true;
  45. particle.scaleByViewport = true;
  46. particle.position = new THREE.Vector3(this.position.x, this.position.y, this.position.z);
  47. particle.scale.x = particle.scale.y = this.psize;
  48. if (this.particleInitializer != null)
  49. this.particleInitializer(particle);
  50. this.particles.push( particle );
  51. this.scene.add( particle );
  52. }
  53. this.timer = 0;
  54. }
  55. this.timer += 1;
  56. }
  57. if (this.particleAnimation != null) {
  58. for (var i=0; i<this.particles.length; i++) {
  59. this.particleAnimation(this.particles[i], i);
  60. }
  61. }
  62. };
  63. // Usage :
  64. // scene : THREE.Scene
  65. var particlesEmitter = new GAME_ENGINE.ParticleEmitter(scene, "./textures/particles/01.png", 600, 35);
  66. particlesEmitter.setParticleInitializer(function(particle){
  67. // Setup each particle at creation
  68. particle.initPos = this.position;
  69. });
  70. particlesEmitter.setParticleAnimation( function(particle, index) {
  71. // update each particle on loop
  72. particle.scale.x = particle.scale.y = 0.98*particle.scale.x;
  73. particle.opacity = 0.90*particle.opacity;
  74. particle.position.y += 1;
  75. if (particle.position > 100)
  76. this.destroyParticle(index);
  77. });
  78. particlesEmitter.start();

comments powered by Disqus