babylon.particleSystem.js 16 KB

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