babylon.particleSystem.js 14 KB

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