babylon.particleSystem.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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;
  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. particles.splice(index, 1);
  92. _this._stockParticles.push(particle);
  93. index--;
  94. continue;
  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.getCapacity = function () {
  110. return this._capacity;
  111. };
  112. ParticleSystem.prototype.isAlive = function () {
  113. return this._alive;
  114. };
  115. ParticleSystem.prototype.isStarted = function () {
  116. return this._started;
  117. };
  118. ParticleSystem.prototype.start = function () {
  119. this._started = true;
  120. this._stopped = false;
  121. this._actualFrame = 0;
  122. };
  123. ParticleSystem.prototype.stop = function () {
  124. this._stopped = true;
  125. };
  126. ParticleSystem.prototype._appendParticleVertex = function (index, particle, offsetX, offsetY) {
  127. var offset = index * 11;
  128. this._vertices[offset] = particle.position.x;
  129. this._vertices[offset + 1] = particle.position.y;
  130. this._vertices[offset + 2] = particle.position.z;
  131. this._vertices[offset + 3] = particle.color.r;
  132. this._vertices[offset + 4] = particle.color.g;
  133. this._vertices[offset + 5] = particle.color.b;
  134. this._vertices[offset + 6] = particle.color.a;
  135. this._vertices[offset + 7] = particle.angle;
  136. this._vertices[offset + 8] = particle.size;
  137. this._vertices[offset + 9] = offsetX;
  138. this._vertices[offset + 10] = offsetY;
  139. };
  140. ParticleSystem.prototype._update = function (newParticles) {
  141. // Update current
  142. this._alive = this.particles.length > 0;
  143. this.updateFunction(this.particles);
  144. // Add new ones
  145. var worldMatrix;
  146. if (this.emitter.position) {
  147. worldMatrix = this.emitter.getWorldMatrix();
  148. } else {
  149. worldMatrix = BABYLON.Matrix.Translation(this.emitter.x, this.emitter.y, this.emitter.z);
  150. }
  151. for (var index = 0; index < newParticles; index++) {
  152. if (this.particles.length === this._capacity) {
  153. break;
  154. }
  155. if (this._stockParticles.length !== 0) {
  156. var particle = this._stockParticles.pop();
  157. particle.age = 0;
  158. } else {
  159. particle = new BABYLON.Particle();
  160. }
  161. this.particles.push(particle);
  162. var emitPower = randomNumber(this.minEmitPower, this.maxEmitPower);
  163. this.startDirectionFunction(emitPower, worldMatrix, particle.direction);
  164. particle.lifeTime = randomNumber(this.minLifeTime, this.maxLifeTime);
  165. particle.size = randomNumber(this.minSize, this.maxSize);
  166. particle.angularSpeed = randomNumber(this.minAngularSpeed, this.maxAngularSpeed);
  167. this.startPositionFunction(worldMatrix, particle.position);
  168. var step = randomNumber(0, 1.0);
  169. BABYLON.Color4.LerpToRef(this.color1, this.color2, step, particle.color);
  170. this.colorDead.subtractToRef(particle.color, this._colorDiff);
  171. this._colorDiff.scaleToRef(1.0 / particle.lifeTime, particle.colorStep);
  172. }
  173. };
  174. ParticleSystem.prototype._getEffect = function () {
  175. if (this._customEffect) {
  176. return this._customEffect;
  177. }
  178. ;
  179. var defines = [];
  180. if (this._scene.clipPlane) {
  181. defines.push("#define CLIPPLANE");
  182. }
  183. // Effect
  184. var join = defines.join("\n");
  185. if (this._cachedDefines !== join) {
  186. this._cachedDefines = join;
  187. this._effect = this._scene.getEngine().createEffect("particles", ["position", "color", "options"], ["invView", "view", "projection", "vClipPlane", "textureMask"], ["diffuseSampler"], join);
  188. }
  189. return this._effect;
  190. };
  191. ParticleSystem.prototype.animate = function () {
  192. if (!this._started)
  193. return;
  194. var effect = this._getEffect();
  195. // Check
  196. if (!this.emitter || !effect.isReady() || !this.particleTexture || !this.particleTexture.isReady())
  197. return;
  198. if (this._currentRenderId === this._scene.getRenderId()) {
  199. return;
  200. }
  201. this._currentRenderId = this._scene.getRenderId();
  202. this._scaledUpdateSpeed = this.updateSpeed * this._scene.getAnimationRatio();
  203. // determine the number of particles we need to create
  204. var emitCout;
  205. if (this.manualEmitCount > -1) {
  206. emitCout = this.manualEmitCount;
  207. this.manualEmitCount = 0;
  208. } else {
  209. emitCout = this.emitRate;
  210. }
  211. var newParticles = ((emitCout * this._scaledUpdateSpeed) >> 0);
  212. this._newPartsExcess += emitCout * this._scaledUpdateSpeed - newParticles;
  213. if (this._newPartsExcess > 1.0) {
  214. newParticles += this._newPartsExcess >> 0;
  215. this._newPartsExcess -= this._newPartsExcess >> 0;
  216. }
  217. this._alive = false;
  218. if (!this._stopped) {
  219. this._actualFrame += this._scaledUpdateSpeed;
  220. if (this.targetStopDuration && this._actualFrame >= this.targetStopDuration)
  221. this.stop();
  222. } else {
  223. newParticles = 0;
  224. }
  225. this._update(newParticles);
  226. // Stopped?
  227. if (this._stopped) {
  228. if (!this._alive) {
  229. this._started = false;
  230. if (this.disposeOnStop) {
  231. this._scene._toBeDisposed.push(this);
  232. }
  233. }
  234. }
  235. // Update VBO
  236. var offset = 0;
  237. for (var index = 0; index < this.particles.length; index++) {
  238. var particle = this.particles[index];
  239. this._appendParticleVertex(offset++, particle, 0, 0);
  240. this._appendParticleVertex(offset++, particle, 1, 0);
  241. this._appendParticleVertex(offset++, particle, 1, 1);
  242. this._appendParticleVertex(offset++, particle, 0, 1);
  243. }
  244. var engine = this._scene.getEngine();
  245. engine.updateDynamicVertexBuffer(this._vertexBuffer, this._vertices);
  246. };
  247. ParticleSystem.prototype.render = function () {
  248. var effect = this._getEffect();
  249. // Check
  250. if (!this.emitter || !effect.isReady() || !this.particleTexture || !this.particleTexture.isReady() || !this.particles.length)
  251. return 0;
  252. var engine = this._scene.getEngine();
  253. // Render
  254. engine.enableEffect(effect);
  255. engine.setState(false);
  256. var viewMatrix = this._scene.getViewMatrix();
  257. effect.setTexture("diffuseSampler", this.particleTexture);
  258. effect.setMatrix("view", viewMatrix);
  259. effect.setMatrix("projection", this._scene.getProjectionMatrix());
  260. effect.setFloat4("textureMask", this.textureMask.r, this.textureMask.g, this.textureMask.b, this.textureMask.a);
  261. if (this._scene.clipPlane) {
  262. var clipPlane = this._scene.clipPlane;
  263. var invView = viewMatrix.clone();
  264. invView.invert();
  265. effect.setMatrix("invView", invView);
  266. effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  267. }
  268. // VBOs
  269. engine.bindBuffers(this._vertexBuffer, this._indexBuffer, this._vertexDeclaration, this._vertexStrideSize, effect);
  270. // Draw order
  271. if (this.blendMode === ParticleSystem.BLENDMODE_ONEONE) {
  272. engine.setAlphaMode(BABYLON.Engine.ALPHA_ADD);
  273. } else {
  274. engine.setAlphaMode(BABYLON.Engine.ALPHA_COMBINE);
  275. }
  276. if (this.forceDepthWrite) {
  277. engine.setDepthWrite(true);
  278. }
  279. engine.draw(true, 0, this.particles.length * 6);
  280. engine.setAlphaMode(BABYLON.Engine.ALPHA_DISABLE);
  281. return this.particles.length;
  282. };
  283. ParticleSystem.prototype.dispose = function () {
  284. if (this._vertexBuffer) {
  285. this._scene.getEngine()._releaseBuffer(this._vertexBuffer);
  286. this._vertexBuffer = null;
  287. }
  288. if (this._indexBuffer) {
  289. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  290. this._indexBuffer = null;
  291. }
  292. if (this.particleTexture) {
  293. this.particleTexture.dispose();
  294. this.particleTexture = null;
  295. }
  296. // Remove from scene
  297. var index = this._scene.particleSystems.indexOf(this);
  298. this._scene.particleSystems.splice(index, 1);
  299. // Callback
  300. if (this.onDispose) {
  301. this.onDispose();
  302. }
  303. };
  304. // Clone
  305. ParticleSystem.prototype.clone = function (name, newEmitter) {
  306. var result = new ParticleSystem(name, this._capacity, this._scene);
  307. BABYLON.Tools.DeepCopy(this, result, ["particles"], ["_vertexDeclaration", "_vertexStrideSize"]);
  308. if (newEmitter === undefined) {
  309. newEmitter = this.emitter;
  310. }
  311. result.emitter = newEmitter;
  312. if (this.particleTexture) {
  313. result.particleTexture = new BABYLON.Texture(this.particleTexture.url, this._scene);
  314. }
  315. result.start();
  316. return result;
  317. };
  318. ParticleSystem.BLENDMODE_ONEONE = 0;
  319. ParticleSystem.BLENDMODE_STANDARD = 1;
  320. return ParticleSystem;
  321. })();
  322. BABYLON.ParticleSystem = ParticleSystem;
  323. })(BABYLON || (BABYLON = {}));
  324. //# sourceMappingURL=babylon.particleSystem.js.map