babylon.particleSystem.js 14 KB

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