babylon.particleSystem.js 14 KB

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