babylon.particleSystem.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  1. module BABYLON {
  2. var randomNumber = (min: number, max: number): number => {
  3. if (min === max) {
  4. return (min);
  5. }
  6. var random = Math.random();
  7. return ((random * (max - min)) + min);
  8. }
  9. export class ParticleSystem implements IDisposable, IAnimatable {
  10. // Statics
  11. public static BLENDMODE_ONEONE = 0;
  12. public static BLENDMODE_STANDARD = 1;
  13. // Members
  14. public animations: Animation[] = [];
  15. public id: string;
  16. public renderingGroupId = 0;
  17. public emitter = null;
  18. public emitRate = 10;
  19. public manualEmitCount = -1;
  20. public updateSpeed = 0.01;
  21. public targetStopDuration = 0;
  22. public disposeOnStop = false;
  23. public minEmitPower = 1;
  24. public maxEmitPower = 1;
  25. public minLifeTime = 1;
  26. public maxLifeTime = 1;
  27. public minSize = 1;
  28. public maxSize = 1;
  29. public minAngularSpeed = 0;
  30. public maxAngularSpeed = 0;
  31. public particleTexture: Texture;
  32. public layerMask: number = 0x0FFFFFFF;
  33. /**
  34. * An event triggered when the system is disposed.
  35. * @type {BABYLON.Observable}
  36. */
  37. public onDisposeObservable = new Observable<ParticleSystem>();
  38. private _onDisposeObserver: Observer<ParticleSystem>;
  39. public set onDispose(callback: () => void) {
  40. if (this._onDisposeObserver) {
  41. this.onDisposeObservable.remove(this._onDisposeObserver);
  42. }
  43. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  44. }
  45. public updateFunction: (particles: Particle[]) => void;
  46. public blendMode = ParticleSystem.BLENDMODE_ONEONE;
  47. public forceDepthWrite = false;
  48. public gravity = Vector3.Zero();
  49. public direction1 = new Vector3(0, 1.0, 0);
  50. public direction2 = new Vector3(0, 1.0, 0);
  51. public minEmitBox = new Vector3(-0.5, -0.5, -0.5);
  52. public maxEmitBox = new Vector3(0.5, 0.5, 0.5);
  53. public color1 = new Color4(1.0, 1.0, 1.0, 1.0);
  54. public color2 = new Color4(1.0, 1.0, 1.0, 1.0);
  55. public colorDead = new Color4(0, 0, 0, 1.0);
  56. public textureMask = new Color4(1.0, 1.0, 1.0, 1.0);
  57. public startDirectionFunction: (emitPower: number, worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle) => void;
  58. public startPositionFunction: (worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle) => void;
  59. private particles = new Array<Particle>();
  60. private _capacity: number;
  61. private _scene: Scene;
  62. private _stockParticles = new Array<Particle>();
  63. private _newPartsExcess = 0;
  64. private _vertexData: Float32Array;
  65. private _vertexBuffer: Buffer;
  66. private _vertexBuffers: { [key: string]: VertexBuffer } = {};
  67. private _indexBuffer: WebGLBuffer;
  68. private _effect: Effect;
  69. private _customEffect: Effect;
  70. private _cachedDefines: string;
  71. private _scaledColorStep = new Color4(0, 0, 0, 0);
  72. private _colorDiff = new Color4(0, 0, 0, 0);
  73. private _scaledDirection = Vector3.Zero();
  74. private _scaledGravity = Vector3.Zero();
  75. private _currentRenderId = -1;
  76. private _alive: boolean;
  77. private _started = false;
  78. private _stopped = false;
  79. private _actualFrame = 0;
  80. private _scaledUpdateSpeed: number;
  81. constructor(public name: string, capacity: number, scene: Scene, customEffect?: Effect) {
  82. this.id = name;
  83. this._capacity = capacity;
  84. this._scene = scene;
  85. this._customEffect = customEffect;
  86. scene.particleSystems.push(this);
  87. var indices = [];
  88. var index = 0;
  89. for (var count = 0; count < capacity; count++) {
  90. indices.push(index);
  91. indices.push(index + 1);
  92. indices.push(index + 2);
  93. indices.push(index);
  94. indices.push(index + 2);
  95. indices.push(index + 3);
  96. index += 4;
  97. }
  98. this._indexBuffer = scene.getEngine().createIndexBuffer(indices);
  99. // 11 floats per particle (x, y, z, r, g, b, a, angle, size, offsetX, offsetY) + 1 filler
  100. this._vertexData = new Float32Array(capacity * 11 * 4);
  101. this._vertexBuffer = new Buffer(scene.getEngine(), this._vertexData, true, 11);
  102. var positions = this._vertexBuffer.createVertexBuffer(VertexBuffer.PositionKind, 0, 3);
  103. var colors = this._vertexBuffer.createVertexBuffer(VertexBuffer.ColorKind, 3, 4);
  104. var options = this._vertexBuffer.createVertexBuffer("options", 7, 4);
  105. this._vertexBuffers[VertexBuffer.PositionKind] = positions;
  106. this._vertexBuffers[VertexBuffer.ColorKind] = colors;
  107. this._vertexBuffers["options"] = options;
  108. // Default behaviors
  109. this.startDirectionFunction = (emitPower: number, worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle): void => {
  110. var randX = randomNumber(this.direction1.x, this.direction2.x);
  111. var randY = randomNumber(this.direction1.y, this.direction2.y);
  112. var randZ = randomNumber(this.direction1.z, this.direction2.z);
  113. Vector3.TransformNormalFromFloatsToRef(randX * emitPower, randY * emitPower, randZ * emitPower, worldMatrix, directionToUpdate);
  114. }
  115. this.startPositionFunction = (worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle): void => {
  116. var randX = randomNumber(this.minEmitBox.x, this.maxEmitBox.x);
  117. var randY = randomNumber(this.minEmitBox.y, this.maxEmitBox.y);
  118. var randZ = randomNumber(this.minEmitBox.z, this.maxEmitBox.z);
  119. Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, worldMatrix, positionToUpdate);
  120. }
  121. this.updateFunction = (particles: Particle[]): void => {
  122. for (var index = 0; index < particles.length; index++) {
  123. var particle = particles[index];
  124. particle.age += this._scaledUpdateSpeed;
  125. if (particle.age >= particle.lifeTime) { // Recycle by swapping with last particle
  126. this.recycleParticle(particle);
  127. index--;
  128. continue;
  129. }
  130. else {
  131. particle.colorStep.scaleToRef(this._scaledUpdateSpeed, this._scaledColorStep);
  132. particle.color.addInPlace(this._scaledColorStep);
  133. if (particle.color.a < 0)
  134. particle.color.a = 0;
  135. particle.angle += particle.angularSpeed * this._scaledUpdateSpeed;
  136. particle.direction.scaleToRef(this._scaledUpdateSpeed, this._scaledDirection);
  137. particle.position.addInPlace(this._scaledDirection);
  138. this.gravity.scaleToRef(this._scaledUpdateSpeed, this._scaledGravity);
  139. particle.direction.addInPlace(this._scaledGravity);
  140. }
  141. }
  142. }
  143. }
  144. public recycleParticle(particle: Particle): void {
  145. var lastParticle = this.particles.pop();
  146. if (lastParticle !== particle) {
  147. lastParticle.copyTo(particle);
  148. this._stockParticles.push(lastParticle);
  149. }
  150. }
  151. public getCapacity(): number {
  152. return this._capacity;
  153. }
  154. public isAlive(): boolean {
  155. return this._alive;
  156. }
  157. public isStarted(): boolean {
  158. return this._started;
  159. }
  160. public start(): void {
  161. this._started = true;
  162. this._stopped = false;
  163. this._actualFrame = 0;
  164. }
  165. public stop(): void {
  166. this._stopped = true;
  167. }
  168. public _appendParticleVertex(index: number, particle: Particle, offsetX: number, offsetY: number): void {
  169. var offset = index * 11;
  170. this._vertexData[offset] = particle.position.x;
  171. this._vertexData[offset + 1] = particle.position.y;
  172. this._vertexData[offset + 2] = particle.position.z;
  173. this._vertexData[offset + 3] = particle.color.r;
  174. this._vertexData[offset + 4] = particle.color.g;
  175. this._vertexData[offset + 5] = particle.color.b;
  176. this._vertexData[offset + 6] = particle.color.a;
  177. this._vertexData[offset + 7] = particle.angle;
  178. this._vertexData[offset + 8] = particle.size;
  179. this._vertexData[offset + 9] = offsetX;
  180. this._vertexData[offset + 10] = offsetY;
  181. }
  182. private _update(newParticles: number): void {
  183. // Update current
  184. this._alive = this.particles.length > 0;
  185. this.updateFunction(this.particles);
  186. // Add new ones
  187. var worldMatrix;
  188. if (this.emitter.position) {
  189. worldMatrix = this.emitter.getWorldMatrix();
  190. } else {
  191. worldMatrix = Matrix.Translation(this.emitter.x, this.emitter.y, this.emitter.z);
  192. }
  193. var particle: Particle;
  194. for (var index = 0; index < newParticles; index++) {
  195. if (this.particles.length === this._capacity) {
  196. break;
  197. }
  198. if (this._stockParticles.length !== 0) {
  199. particle = this._stockParticles.pop();
  200. particle.age = 0;
  201. } else {
  202. particle = new Particle();
  203. }
  204. this.particles.push(particle);
  205. var emitPower = randomNumber(this.minEmitPower, this.maxEmitPower);
  206. this.startDirectionFunction(emitPower, worldMatrix, particle.direction, particle);
  207. particle.lifeTime = randomNumber(this.minLifeTime, this.maxLifeTime);
  208. particle.size = randomNumber(this.minSize, this.maxSize);
  209. particle.angularSpeed = randomNumber(this.minAngularSpeed, this.maxAngularSpeed);
  210. this.startPositionFunction(worldMatrix, particle.position, particle);
  211. var step = randomNumber(0, 1.0);
  212. Color4.LerpToRef(this.color1, this.color2, step, particle.color);
  213. this.colorDead.subtractToRef(particle.color, this._colorDiff);
  214. this._colorDiff.scaleToRef(1.0 / particle.lifeTime, particle.colorStep);
  215. }
  216. }
  217. private _getEffect(): Effect {
  218. if (this._customEffect) {
  219. return this._customEffect;
  220. };
  221. var defines = [];
  222. if (this._scene.clipPlane) {
  223. defines.push("#define CLIPPLANE");
  224. }
  225. // Effect
  226. var join = defines.join("\n");
  227. if (this._cachedDefines !== join) {
  228. this._cachedDefines = join;
  229. this._effect = this._scene.getEngine().createEffect(
  230. "particles",
  231. [VertexBuffer.PositionKind, VertexBuffer.ColorKind, "options"],
  232. ["invView", "view", "projection", "vClipPlane", "textureMask"],
  233. ["diffuseSampler"], join);
  234. }
  235. return this._effect;
  236. }
  237. public animate(): void {
  238. if (!this._started)
  239. return;
  240. var effect = this._getEffect();
  241. // Check
  242. if (!this.emitter || !effect.isReady() || !this.particleTexture || !this.particleTexture.isReady())
  243. return;
  244. if (this._currentRenderId === this._scene.getRenderId()) {
  245. return;
  246. }
  247. this._currentRenderId = this._scene.getRenderId();
  248. this._scaledUpdateSpeed = this.updateSpeed * this._scene.getAnimationRatio();
  249. // determine the number of particles we need to create
  250. var newParticles;
  251. if (this.manualEmitCount > -1) {
  252. newParticles = this.manualEmitCount;
  253. this._newPartsExcess = 0;
  254. this.manualEmitCount = 0;
  255. } else {
  256. newParticles = ((this.emitRate * this._scaledUpdateSpeed) >> 0);
  257. this._newPartsExcess += this.emitRate * this._scaledUpdateSpeed - newParticles;
  258. }
  259. if (this._newPartsExcess > 1.0) {
  260. newParticles += this._newPartsExcess >> 0;
  261. this._newPartsExcess -= this._newPartsExcess >> 0;
  262. }
  263. this._alive = false;
  264. if (!this._stopped) {
  265. this._actualFrame += this._scaledUpdateSpeed;
  266. if (this.targetStopDuration && this._actualFrame >= this.targetStopDuration)
  267. this.stop();
  268. } else {
  269. newParticles = 0;
  270. }
  271. this._update(newParticles);
  272. // Stopped?
  273. if (this._stopped) {
  274. if (!this._alive) {
  275. this._started = false;
  276. if (this.disposeOnStop) {
  277. this._scene._toBeDisposed.push(this);
  278. }
  279. }
  280. }
  281. // Update VBO
  282. var offset = 0;
  283. for (var index = 0; index < this.particles.length; index++) {
  284. var particle = this.particles[index];
  285. this._appendParticleVertex(offset++, particle, 0, 0);
  286. this._appendParticleVertex(offset++, particle, 1, 0);
  287. this._appendParticleVertex(offset++, particle, 1, 1);
  288. this._appendParticleVertex(offset++, particle, 0, 1);
  289. }
  290. this._vertexBuffer.update(this._vertexData);
  291. }
  292. public render(): number {
  293. var effect = this._getEffect();
  294. // Check
  295. if (!this.emitter || !effect.isReady() || !this.particleTexture || !this.particleTexture.isReady() || !this.particles.length)
  296. return 0;
  297. var engine = this._scene.getEngine();
  298. // Render
  299. engine.enableEffect(effect);
  300. engine.setState(false);
  301. var viewMatrix = this._scene.getViewMatrix();
  302. effect.setTexture("diffuseSampler", this.particleTexture);
  303. effect.setMatrix("view", viewMatrix);
  304. effect.setMatrix("projection", this._scene.getProjectionMatrix());
  305. effect.setFloat4("textureMask", this.textureMask.r, this.textureMask.g, this.textureMask.b, this.textureMask.a);
  306. if (this._scene.clipPlane) {
  307. var clipPlane = this._scene.clipPlane;
  308. var invView = viewMatrix.clone();
  309. invView.invert();
  310. effect.setMatrix("invView", invView);
  311. effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  312. }
  313. // VBOs
  314. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);
  315. // Draw order
  316. if (this.blendMode === ParticleSystem.BLENDMODE_ONEONE) {
  317. engine.setAlphaMode(Engine.ALPHA_ONEONE);
  318. } else {
  319. engine.setAlphaMode(Engine.ALPHA_COMBINE);
  320. }
  321. if (this.forceDepthWrite) {
  322. engine.setDepthWrite(true);
  323. }
  324. engine.draw(true, 0, this.particles.length * 6);
  325. engine.setAlphaMode(Engine.ALPHA_DISABLE);
  326. return this.particles.length;
  327. }
  328. public dispose(): void {
  329. if (this._vertexBuffer) {
  330. this._vertexBuffer.dispose();
  331. this._vertexBuffer = null;
  332. }
  333. if (this._indexBuffer) {
  334. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  335. this._indexBuffer = null;
  336. }
  337. if (this.particleTexture) {
  338. this.particleTexture.dispose();
  339. this.particleTexture = null;
  340. }
  341. // Remove from scene
  342. var index = this._scene.particleSystems.indexOf(this);
  343. this._scene.particleSystems.splice(index, 1);
  344. // Callback
  345. this.onDisposeObservable.notifyObservers(this);
  346. this.onDisposeObservable.clear();
  347. }
  348. // Clone
  349. public clone(name: string, newEmitter: any): ParticleSystem {
  350. var result = new ParticleSystem(name, this._capacity, this._scene);
  351. Tools.DeepCopy(this, result, ["particles"]);
  352. if (newEmitter === undefined) {
  353. newEmitter = this.emitter;
  354. }
  355. result.emitter = newEmitter;
  356. if (this.particleTexture) {
  357. result.particleTexture = new Texture(this.particleTexture.url, this._scene);
  358. }
  359. result.start();
  360. return result;
  361. }
  362. public serialize(): any {
  363. var serializationObject: any = {};
  364. serializationObject.name = this.name;
  365. serializationObject.id = this.id;
  366. // Emitter
  367. if (this.emitter.position) {
  368. serializationObject.emitterId = this.emitter.id;
  369. } else {
  370. serializationObject.emitter = this.emitter.asArray();
  371. }
  372. serializationObject.capacity = this.getCapacity();
  373. if (this.particleTexture) {
  374. serializationObject.textureName = this.particleTexture.name;
  375. }
  376. // Animations
  377. Animation.AppendSerializedAnimations(this, serializationObject);
  378. // Particle system
  379. serializationObject.minAngularSpeed = this.minAngularSpeed;
  380. serializationObject.maxAngularSpeed = this.maxAngularSpeed;
  381. serializationObject.minSize = this.minSize;
  382. serializationObject.maxSize = this.maxSize;
  383. serializationObject.minEmitPower = this.minEmitPower;
  384. serializationObject.maxEmitPower = this.maxEmitPower;
  385. serializationObject.minLifeTime = this.minLifeTime;
  386. serializationObject.maxLifeTime = this.maxLifeTime;
  387. serializationObject.emitRate = this.emitRate;
  388. serializationObject.minEmitBox = this.minEmitBox.asArray();
  389. serializationObject.maxEmitBox = this.maxEmitBox.asArray();
  390. serializationObject.gravity = this.gravity.asArray();
  391. serializationObject.direction1 = this.direction1.asArray();
  392. serializationObject.direction2 = this.direction2.asArray();
  393. serializationObject.color1 = this.color1.asArray();
  394. serializationObject.color2 = this.color2.asArray();
  395. serializationObject.colorDead = this.colorDead.asArray();
  396. serializationObject.updateSpeed = this.updateSpeed;
  397. serializationObject.targetStopDuration = this.targetStopDuration;
  398. serializationObject.textureMask = this.textureMask.asArray();
  399. serializationObject.blendMode = this.blendMode;
  400. return serializationObject;
  401. }
  402. public static Parse(parsedParticleSystem: any, scene: Scene, rootUrl: string): ParticleSystem {
  403. var name = parsedParticleSystem.name;
  404. var particleSystem = new ParticleSystem(name, parsedParticleSystem.capacity, scene);
  405. if (parsedParticleSystem.id) {
  406. particleSystem.id = parsedParticleSystem.id;
  407. }
  408. // Texture
  409. if (parsedParticleSystem.textureName) {
  410. particleSystem.particleTexture = new Texture(rootUrl + parsedParticleSystem.textureName, scene);
  411. particleSystem.particleTexture.name = parsedParticleSystem.textureName;
  412. }
  413. // Emitter
  414. if (parsedParticleSystem.emitterId) {
  415. particleSystem.emitter = scene.getLastMeshByID(parsedParticleSystem.emitterId);
  416. } else {
  417. particleSystem.emitter = Vector3.FromArray(parsedParticleSystem.emitter);
  418. }
  419. // Animations
  420. if (parsedParticleSystem.animations) {
  421. for (var animationIndex = 0; animationIndex < parsedParticleSystem.animations.length; animationIndex++) {
  422. var parsedAnimation = parsedParticleSystem.animations[animationIndex];
  423. particleSystem.animations.push(Animation.Parse(parsedAnimation));
  424. }
  425. }
  426. if (parsedParticleSystem.autoAnimate) {
  427. scene.beginAnimation(particleSystem, parsedParticleSystem.autoAnimateFrom, parsedParticleSystem.autoAnimateTo, parsedParticleSystem.autoAnimateLoop, parsedParticleSystem.autoAnimateSpeed || 1.0);
  428. }
  429. // Particle system
  430. particleSystem.minAngularSpeed = parsedParticleSystem.minAngularSpeed;
  431. particleSystem.maxAngularSpeed = parsedParticleSystem.maxAngularSpeed;
  432. particleSystem.minSize = parsedParticleSystem.minSize;
  433. particleSystem.maxSize = parsedParticleSystem.maxSize;
  434. particleSystem.minLifeTime = parsedParticleSystem.minLifeTime;
  435. particleSystem.maxLifeTime = parsedParticleSystem.maxLifeTime;
  436. particleSystem.minEmitPower = parsedParticleSystem.minEmitPower;
  437. particleSystem.maxEmitPower = parsedParticleSystem.maxEmitPower;
  438. particleSystem.emitRate = parsedParticleSystem.emitRate;
  439. particleSystem.minEmitBox = Vector3.FromArray(parsedParticleSystem.minEmitBox);
  440. particleSystem.maxEmitBox = Vector3.FromArray(parsedParticleSystem.maxEmitBox);
  441. particleSystem.gravity = Vector3.FromArray(parsedParticleSystem.gravity);
  442. particleSystem.direction1 = Vector3.FromArray(parsedParticleSystem.direction1);
  443. particleSystem.direction2 = Vector3.FromArray(parsedParticleSystem.direction2);
  444. particleSystem.color1 = Color4.FromArray(parsedParticleSystem.color1);
  445. particleSystem.color2 = Color4.FromArray(parsedParticleSystem.color2);
  446. particleSystem.colorDead = Color4.FromArray(parsedParticleSystem.colorDead);
  447. particleSystem.updateSpeed = parsedParticleSystem.updateSpeed;
  448. particleSystem.targetStopDuration = parsedParticleSystem.targetStopDuration;
  449. particleSystem.textureMask = Color4.FromArray(parsedParticleSystem.textureMask);
  450. particleSystem.blendMode = parsedParticleSystem.blendMode;
  451. if (!parsedParticleSystem.preventAutoStart) {
  452. particleSystem.start();
  453. }
  454. return particleSystem;
  455. }
  456. }
  457. }