babylon.particleSystem.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. var appendParticleVertex = function (particle, vertices, offsetX, offsetY) {
  4. vertices.push(particle.position.x);
  5. vertices.push(particle.position.y);
  6. vertices.push(particle.position.z);
  7. vertices.push(particle.color.r);
  8. vertices.push(particle.color.g);
  9. vertices.push(particle.color.b);
  10. vertices.push(particle.color.a);
  11. vertices.push(particle.angle);
  12. vertices.push(particle.size);
  13. vertices.push(offsetX);
  14. vertices.push(offsetY);
  15. };
  16. var randomNumber = function (min, max) {
  17. if (min == max) {
  18. return (min);
  19. }
  20. var random = Math.random();
  21. return ((random * (max - min)) + min);
  22. };
  23. BABYLON.ParticleSystem = function (name, capacity, scene) {
  24. this.name = name;
  25. this.id = name;
  26. this._capacity = capacity;
  27. this._scene = scene;
  28. scene.particleSystems.push(this);
  29. // Vectors and colors
  30. this.gravity = BABYLON.Vector3.Zero();
  31. this.direction1 = new BABYLON.Vector3(0, 1.0, 0);
  32. this.direction2 = new BABYLON.Vector3(0, 1.0, 0);
  33. this.minEmitBox = new BABYLON.Vector3(-0.5, -0.5, -0.5);
  34. this.maxEmitBox = new BABYLON.Vector3(0.5, 0.5, 0.5);
  35. this.color1 = new BABYLON.Color4(1.0, 1.0, 1.0, 1.0);
  36. this.color2 = new BABYLON.Color4(1.0, 1.0, 1.0, 1.0);
  37. this.colorDead = new BABYLON.Color4(0, 0, 0, 1.0);
  38. this.deadAlpha = 0;
  39. this.textureMask = new BABYLON.Color4(1.0, 1.0, 1.0, 1.0);
  40. // Particles
  41. this.particles = [];
  42. this._newPartsExcess = 0;
  43. // VBO
  44. this._vertexDeclaration = [3, 4, 4];
  45. this._vertexStrideSize = 11 * 4; // 10 floats per particle (x, y, z, r, g, b, a, angle, size, offsetX, offsetY)
  46. this._vertexBuffer = scene.getEngine().createDynamicVertexBuffer(capacity * this._vertexStrideSize * 4);
  47. var indices = [];
  48. var index = 0;
  49. for (var count = 0; count < capacity; count++) {
  50. indices.push(index);
  51. indices.push(index + 1);
  52. indices.push(index + 2);
  53. indices.push(index);
  54. indices.push(index + 2);
  55. indices.push(index + 3);
  56. index += 4;
  57. }
  58. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  59. // Effects
  60. this._baseEffect = this._scene.getEngine().createEffect("particles",
  61. ["position", "color", "options"],
  62. ["view", "projection", "textureMask"],
  63. ["diffuseSampler"], "");
  64. this._clippedEffect = this._scene.getEngine().createEffect("particles",
  65. ["position", "color", "options"],
  66. ["invView", "view", "projection", "vClipPlane", "textureMask"],
  67. ["diffuseSampler"], "#define CLIPPLANE");
  68. };
  69. // Members
  70. BABYLON.ParticleSystem.prototype.emitter = null;
  71. BABYLON.ParticleSystem.prototype.emitRate = 10;
  72. BABYLON.ParticleSystem.prototype.manualEmitCount = -1;
  73. BABYLON.ParticleSystem.prototype.updateSpeed = 0.01;
  74. BABYLON.ParticleSystem.prototype.targetStopDuration = 0;
  75. BABYLON.ParticleSystem.prototype.disposeOnStop = false;
  76. BABYLON.ParticleSystem.prototype.minEmitPower = 1;
  77. BABYLON.ParticleSystem.prototype.maxEmitPower = 1;
  78. BABYLON.ParticleSystem.prototype.minLifeTime = 1;
  79. BABYLON.ParticleSystem.prototype.maxLifeTime = 1;
  80. BABYLON.ParticleSystem.prototype.minSize = 1;
  81. BABYLON.ParticleSystem.prototype.maxSize = 1;
  82. BABYLON.ParticleSystem.prototype.minAngularSpeed = 0;
  83. BABYLON.ParticleSystem.prototype.maxAngularSpeed = 0;
  84. BABYLON.ParticleSystem.prototype.particleTexture = null;
  85. BABYLON.ParticleSystem.prototype.onDispose = null;
  86. BABYLON.ParticleSystem.prototype.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
  87. // Methods
  88. BABYLON.ParticleSystem.prototype.isAlive = function () {
  89. return this._alive;
  90. };
  91. BABYLON.ParticleSystem.prototype.start = function () {
  92. this._started = true;
  93. this._stopped = false;
  94. this._actualFrame = 0;
  95. };
  96. BABYLON.ParticleSystem.prototype.stop = function () {
  97. this._stopped = true;
  98. };
  99. BABYLON.ParticleSystem.prototype._update = function (newParticles) {
  100. // Update current
  101. this._alive = this.particles.length > 0;
  102. for (var index = 0; index < this.particles.length; index++) {
  103. var particle = this.particles[index];
  104. particle.age += this._scaledUpdateSpeed;
  105. if (particle.age >= particle.lifeTime) {
  106. this.particles.splice(index, 1);
  107. index--;
  108. continue;
  109. }
  110. else {
  111. particle.color = particle.color.add(particle.colorStep.scale(this._scaledUpdateSpeed));
  112. if (particle.color.a < 0)
  113. particle.color.a = 0;
  114. particle.position = particle.position.add(particle.direction.scale(this._scaledUpdateSpeed));
  115. particle.angle += particle.angularSpeed * this._scaledUpdateSpeed;
  116. particle.direction = particle.direction.add(this.gravity.scale(this._scaledUpdateSpeed));
  117. }
  118. }
  119. // Add new ones
  120. var worldMatrix;
  121. if (this.emitter.position) {
  122. worldMatrix = this.emitter.getWorldMatrix();
  123. } else {
  124. worldMatrix = BABYLON.Matrix.Translation(this.emitter.x, this.emitter.y, this.emitter.z);
  125. }
  126. for (var index = 0; index < newParticles; index++) {
  127. if (this.particles.length == this._capacity) {
  128. break;
  129. }
  130. var particle = new BABYLON.Particle();
  131. this.particles.push(particle);
  132. var emitPower = randomNumber(this.minEmitPower, this.maxEmitPower);
  133. var randX = randomNumber(this.direction1.x, this.direction2.x);
  134. var randY = randomNumber(this.direction1.y, this.direction2.y);
  135. var randZ = randomNumber(this.direction1.z, this.direction2.z);
  136. particle.direction = BABYLON.Vector3.TransformNormal(new BABYLON.Vector3(randX, randY, randZ).scale(emitPower), worldMatrix);
  137. particle.lifeTime = randomNumber(this.minLifeTime, this.maxLifeTime);
  138. particle.size = randomNumber(this.minSize, this.maxSize);
  139. particle.angularSpeed = randomNumber(this.minAngularSpeed, this.maxAngularSpeed);
  140. randX = randomNumber(this.minEmitBox.x, this.maxEmitBox.x);
  141. randY = randomNumber(this.minEmitBox.y, this.maxEmitBox.y);
  142. randZ = randomNumber(this.minEmitBox.z, this.maxEmitBox.z);
  143. var dispatch = new BABYLON.Vector3(randX, randY, randZ);
  144. particle.position = BABYLON.Vector3.TransformCoordinates(dispatch, worldMatrix);
  145. var step = randomNumber(0, 1.0);
  146. var startColor = BABYLON.Color4.Lerp(this.color1, this.color2, step);
  147. var deadColor = this.colorDead;
  148. startColor.a = 1.0;
  149. deadColor.a = this.deadAlpha;
  150. particle.color = startColor;
  151. var diff = deadColor.subtract(startColor);
  152. particle.colorStep = diff.scale(1.0 / particle.lifeTime);
  153. }
  154. };
  155. BABYLON.ParticleSystem.prototype.animate = function () {
  156. if (!this._started)
  157. return;
  158. // Check
  159. if (!this.emitter || !this._baseEffect.isReady() || !this._clippedEffect.isReady() || !this.particleTexture || !this.particleTexture.isReady())
  160. return;
  161. this._scaledUpdateSpeed = this.updateSpeed * this._scene.getAnimationRatio();
  162. // determine the number of particles we need to create
  163. var emitCout;
  164. if (this.manualEmitCount > -1) {
  165. emitCout = this.manualEmitCount;
  166. this.manualEmitCount = 0;
  167. } else {
  168. emitCout = this.emitRate;
  169. }
  170. var newParticles = ((emitCout * this._scaledUpdateSpeed) >> 0);
  171. this._newPartsExcess += emitCout * this._scaledUpdateSpeed - newParticles;
  172. if (this._newPartsExcess > 1.0) {
  173. newParticles += this._newPartsExcess >> 0;
  174. this._newPartsExcess -= this._newPartsExcess >> 0;
  175. }
  176. this._alive = false;
  177. if (!this._stopped) {
  178. this._actualFrame += this._scaledUpdateSpeed;
  179. if (this.targetStopDuration && this._actualFrame >= this.targetStopDuration)
  180. this.stop();
  181. } else {
  182. newParticles = 0;
  183. }
  184. this._update(newParticles);
  185. // Stopped?
  186. if (this._stopped) {
  187. if (!this._alive) {
  188. this._started = false;
  189. if (this.disposeOnStop) {
  190. this._scene._toBeDisposed.push(this);
  191. }
  192. }
  193. }
  194. // Update VBO
  195. var vertices = [];
  196. for (var index = 0; index < this.particles.length; index++) {
  197. var particle = this.particles[index];
  198. appendParticleVertex(particle, vertices, 0, 0);
  199. appendParticleVertex(particle, vertices, 1, 0);
  200. appendParticleVertex(particle, vertices, 1, 1);
  201. appendParticleVertex(particle, vertices, 0, 1);
  202. }
  203. var engine = this._scene.getEngine();
  204. engine.updateDynamicVertexBuffer(this._vertexBuffer, vertices);
  205. };
  206. BABYLON.ParticleSystem.prototype.render = function () {
  207. // Effect
  208. if (BABYLON.clipPlane) {
  209. this._effect = this._clippedEffect;
  210. } else {
  211. this._effect = this._baseEffect;
  212. }
  213. // Check
  214. if (!this.emitter || !this._effect.isReady() || !this.particleTexture || !this.particleTexture.isReady() || !this.particles.length)
  215. return 0;
  216. var engine = this._scene.getEngine();
  217. // Render
  218. engine.enableEffect(this._effect);
  219. var viewMatrix = this._scene.getViewMatrix();
  220. this._effect.setTexture("diffuseSampler", this.particleTexture);
  221. this._effect.setMatrix("view", viewMatrix);
  222. this._effect.setMatrix("projection", this._scene.getProjectionMatrix());
  223. this._effect.setVector4("textureMask", this.textureMask.r, this.textureMask.g, this.textureMask.b, this.textureMask.a);
  224. if (BABYLON.clipPlane) {
  225. var invView = viewMatrix.clone();
  226. invView.invert();
  227. this._effect.setMatrix("invView", invView);
  228. this._effect.setVector4("vClipPlane", BABYLON.clipPlane.normal.x, BABYLON.clipPlane.normal.y, BABYLON.clipPlane.normal.z, BABYLON.clipPlane.d);
  229. }
  230. // VBOs
  231. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, this._effect);
  232. // Draw order
  233. if (this.blendMode === BABYLON.ParticleSystem.BLENDMODE_ONEONE) {
  234. engine.setAlphaMode(BABYLON.Engine.ALPHA_ADD);
  235. } else {
  236. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  237. }
  238. engine.draw(true, 0, this.particles.length * 6);
  239. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  240. return this.particles.length;
  241. };
  242. BABYLON.ParticleSystem.prototype.dispose = function () {
  243. if (this._vertexBuffer) {
  244. //this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  245. this._vertexBuffer = null;
  246. }
  247. if (this._indexBuffer) {
  248. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  249. this._indexBuffer = null;
  250. }
  251. if (this.particleTexture) {
  252. this.particleTexture.dispose();
  253. this.particleTexture = null;
  254. }
  255. // Remove from scene
  256. var index = this._scene.particleSystems.indexOf(this);
  257. this._scene.particleSystems.splice(index, 1);
  258. // Callback
  259. if (this.onDispose) {
  260. this.onDispose();
  261. }
  262. };
  263. // Clone
  264. BABYLON.ParticleSystem.prototype.clone = function(name, newEmitter) {
  265. var result = new BABYLON.ParticleSystem(name, this._capacity, this._scene);
  266. BABYLON.Tools.DeepCopy(this, result, ["particles"], ["_vertexDeclaration", "_vertexStrideSize"]);
  267. if (newEmitter === undefined) {
  268. newEmitter = this.emitter;
  269. }
  270. result.emitter = newEmitter;
  271. if (this.particleTexture) {
  272. result.particleTexture = new BABYLON.Texture(this.particleTexture.name, this._scene);
  273. }
  274. result.start();
  275. return result;
  276. };
  277. // Statics
  278. BABYLON.ParticleSystem.BLENDMODE_ONEONE = 0;
  279. BABYLON.ParticleSystem.BLENDMODE_STANDARD = 1;
  280. })();