babylon.particleSystem.js 13 KB

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