babylon.particleSystem.js 15 KB

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