babylon.particleSystem.js 16 KB

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