babylon.particleSystem.js 15 KB

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