babylon.particleSystem.js 14 KB

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