babylon.particleSystem.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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 interface IParticleSystem {
  10. id: string;
  11. name: string;
  12. emitter: AbstractMesh | Vector3;
  13. renderingGroupId: number;
  14. layerMask: number;
  15. isStarted(): boolean;
  16. animate(): void;
  17. render();
  18. dispose(): void;
  19. clone(name: string, newEmitter: any): IParticleSystem;
  20. serialize(): any;
  21. rebuild(): void
  22. }
  23. export class ParticleSystem implements IDisposable, IAnimatable, IParticleSystem {
  24. // Statics
  25. public static BLENDMODE_ONEONE = 0;
  26. public static BLENDMODE_STANDARD = 1;
  27. // Members
  28. public animations: Animation[] = [];
  29. public id: string;
  30. public renderingGroupId = 0;
  31. public emitter: AbstractMesh | Vector3 = null;
  32. public emitRate = 10;
  33. public manualEmitCount = -1;
  34. public updateSpeed = 0.01;
  35. public targetStopDuration = 0;
  36. public disposeOnStop = false;
  37. public minEmitPower = 1;
  38. public maxEmitPower = 1;
  39. public minLifeTime = 1;
  40. public maxLifeTime = 1;
  41. public minSize = 1;
  42. public maxSize = 1;
  43. public minAngularSpeed = 0;
  44. public maxAngularSpeed = 0;
  45. public particleTexture: Texture;
  46. public layerMask: number = 0x0FFFFFFF;
  47. public customShader: any = null;
  48. public preventAutoStart: boolean = false;
  49. private _epsilon: number;
  50. /**
  51. * An event triggered when the system is disposed.
  52. * @type {BABYLON.Observable}
  53. */
  54. public onDisposeObservable = new Observable<ParticleSystem>();
  55. private _onDisposeObserver: Observer<ParticleSystem>;
  56. public set onDispose(callback: () => void) {
  57. if (this._onDisposeObserver) {
  58. this.onDisposeObservable.remove(this._onDisposeObserver);
  59. }
  60. this._onDisposeObserver = this.onDisposeObservable.add(callback);
  61. }
  62. public updateFunction: (particles: Particle[]) => void;
  63. public onAnimationEnd: () => void = null;
  64. public blendMode = ParticleSystem.BLENDMODE_ONEONE;
  65. public forceDepthWrite = false;
  66. public gravity = Vector3.Zero();
  67. public direction1 = new Vector3(0, 1.0, 0);
  68. public direction2 = new Vector3(0, 1.0, 0);
  69. public minEmitBox = new Vector3(-0.5, -0.5, -0.5);
  70. public maxEmitBox = new Vector3(0.5, 0.5, 0.5);
  71. public color1 = new Color4(1.0, 1.0, 1.0, 1.0);
  72. public color2 = new Color4(1.0, 1.0, 1.0, 1.0);
  73. public colorDead = new Color4(0, 0, 0, 1.0);
  74. public textureMask = new Color4(1.0, 1.0, 1.0, 1.0);
  75. public startDirectionFunction: (emitPower: number, worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle) => void;
  76. public startPositionFunction: (worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle) => void;
  77. private particles = new Array<Particle>();
  78. private _capacity: number;
  79. private _scene: Scene;
  80. private _stockParticles = new Array<Particle>();
  81. private _newPartsExcess = 0;
  82. private _vertexData: Float32Array;
  83. private _vertexBuffer: Buffer;
  84. private _vertexBuffers: { [key: string]: VertexBuffer } = {};
  85. private _indexBuffer: WebGLBuffer;
  86. private _effect: Effect;
  87. private _customEffect: Effect;
  88. private _cachedDefines: string;
  89. private _scaledColorStep = new Color4(0, 0, 0, 0);
  90. private _colorDiff = new Color4(0, 0, 0, 0);
  91. private _scaledDirection = Vector3.Zero();
  92. private _scaledGravity = Vector3.Zero();
  93. private _currentRenderId = -1;
  94. private _alive: boolean;
  95. private _started = false;
  96. private _stopped = false;
  97. private _actualFrame = 0;
  98. private _scaledUpdateSpeed: number;
  99. // sheet animation
  100. public startSpriteCellID = 0;
  101. public endSpriteCellID = 0;
  102. public spriteCellLoop = true;
  103. public spriteCellChangeSpeed = 0;
  104. public spriteCellWidth = 0;
  105. public spriteCellHeight = 0;
  106. private _vertexBufferSize = 11;
  107. public get isAnimationSheetEnabled(): Boolean {
  108. return this._isAnimationSheetEnabled;
  109. }
  110. // end of sheet animation
  111. constructor(public name: string, capacity: number, scene: Scene, customEffect?: Effect, private _isAnimationSheetEnabled: boolean = false, epsilon: number = 0.01) {
  112. this.id = name;
  113. this._capacity = capacity;
  114. this._epsilon = epsilon;
  115. if (_isAnimationSheetEnabled) {
  116. this._vertexBufferSize = 12;
  117. }
  118. this._scene = scene || Engine.LastCreatedScene;
  119. this._customEffect = customEffect;
  120. scene.particleSystems.push(this);
  121. this._createIndexBuffer();
  122. // 11 floats per particle (x, y, z, r, g, b, a, angle, size, offsetX, offsetY) + 1 filler
  123. this._vertexData = new Float32Array(capacity * this._vertexBufferSize * 4);
  124. this._vertexBuffer = new Buffer(scene.getEngine(), this._vertexData, true, this._vertexBufferSize);
  125. var positions = this._vertexBuffer.createVertexBuffer(VertexBuffer.PositionKind, 0, 3);
  126. var colors = this._vertexBuffer.createVertexBuffer(VertexBuffer.ColorKind, 3, 4);
  127. var options = this._vertexBuffer.createVertexBuffer("options", 7, 4);
  128. if (this._isAnimationSheetEnabled) {
  129. var cellIndexBuffer = this._vertexBuffer.createVertexBuffer("cellIndex", 11, 1);
  130. this._vertexBuffers["cellIndex"] = cellIndexBuffer;
  131. }
  132. this._vertexBuffers[VertexBuffer.PositionKind] = positions;
  133. this._vertexBuffers[VertexBuffer.ColorKind] = colors;
  134. this._vertexBuffers["options"] = options;
  135. // Default behaviors
  136. this.startDirectionFunction = (emitPower: number, worldMatrix: Matrix, directionToUpdate: Vector3, particle: Particle): void => {
  137. var randX = randomNumber(this.direction1.x, this.direction2.x);
  138. var randY = randomNumber(this.direction1.y, this.direction2.y);
  139. var randZ = randomNumber(this.direction1.z, this.direction2.z);
  140. Vector3.TransformNormalFromFloatsToRef(randX * emitPower, randY * emitPower, randZ * emitPower, worldMatrix, directionToUpdate);
  141. }
  142. this.startPositionFunction = (worldMatrix: Matrix, positionToUpdate: Vector3, particle: Particle): void => {
  143. var randX = randomNumber(this.minEmitBox.x, this.maxEmitBox.x);
  144. var randY = randomNumber(this.minEmitBox.y, this.maxEmitBox.y);
  145. var randZ = randomNumber(this.minEmitBox.z, this.maxEmitBox.z);
  146. Vector3.TransformCoordinatesFromFloatsToRef(randX, randY, randZ, worldMatrix, positionToUpdate);
  147. }
  148. this.updateFunction = (particles: Particle[]): void => {
  149. for (var index = 0; index < particles.length; index++) {
  150. var particle = particles[index];
  151. particle.age += this._scaledUpdateSpeed;
  152. if (particle.age >= particle.lifeTime) { // Recycle by swapping with last particle
  153. this.recycleParticle(particle);
  154. index--;
  155. continue;
  156. }
  157. else {
  158. particle.colorStep.scaleToRef(this._scaledUpdateSpeed, this._scaledColorStep);
  159. particle.color.addInPlace(this._scaledColorStep);
  160. if (particle.color.a < 0)
  161. particle.color.a = 0;
  162. particle.angle += particle.angularSpeed * this._scaledUpdateSpeed;
  163. particle.direction.scaleToRef(this._scaledUpdateSpeed, this._scaledDirection);
  164. particle.position.addInPlace(this._scaledDirection);
  165. this.gravity.scaleToRef(this._scaledUpdateSpeed, this._scaledGravity);
  166. particle.direction.addInPlace(this._scaledGravity);
  167. if (this._isAnimationSheetEnabled) {
  168. particle.updateCellIndex(this._scaledUpdateSpeed);
  169. }
  170. }
  171. }
  172. }
  173. }
  174. private _createIndexBuffer() {
  175. var indices = [];
  176. var index = 0;
  177. for (var count = 0; count < this._capacity; count++) {
  178. indices.push(index);
  179. indices.push(index + 1);
  180. indices.push(index + 2);
  181. indices.push(index);
  182. indices.push(index + 2);
  183. indices.push(index + 3);
  184. index += 4;
  185. }
  186. this._indexBuffer = this._scene.getEngine().createIndexBuffer(indices);
  187. }
  188. public recycleParticle(particle: Particle): void {
  189. var lastParticle = this.particles.pop();
  190. if (lastParticle !== particle) {
  191. lastParticle.copyTo(particle);
  192. this._stockParticles.push(lastParticle);
  193. }
  194. }
  195. public getCapacity(): number {
  196. return this._capacity;
  197. }
  198. public isAlive(): boolean {
  199. return this._alive;
  200. }
  201. public isStarted(): boolean {
  202. return this._started;
  203. }
  204. public start(): void {
  205. this._started = true;
  206. this._stopped = false;
  207. this._actualFrame = 0;
  208. }
  209. public stop(): void {
  210. this._stopped = true;
  211. }
  212. // animation sheet
  213. public _appendParticleVertex(index: number, particle: Particle, offsetX: number, offsetY: number): void {
  214. var offset = index * this._vertexBufferSize;
  215. this._vertexData[offset] = particle.position.x;
  216. this._vertexData[offset + 1] = particle.position.y;
  217. this._vertexData[offset + 2] = particle.position.z;
  218. this._vertexData[offset + 3] = particle.color.r;
  219. this._vertexData[offset + 4] = particle.color.g;
  220. this._vertexData[offset + 5] = particle.color.b;
  221. this._vertexData[offset + 6] = particle.color.a;
  222. this._vertexData[offset + 7] = particle.angle;
  223. this._vertexData[offset + 8] = particle.size;
  224. this._vertexData[offset + 9] = offsetX;
  225. this._vertexData[offset + 10] = offsetY;
  226. }
  227. public _appendParticleVertexWithAnimation(index: number, particle: Particle, offsetX: number, offsetY: number): void {
  228. if (offsetX === 0)
  229. offsetX = this._epsilon;
  230. else if (offsetX === 1)
  231. offsetX = 1 - this._epsilon;
  232. if (offsetY === 0)
  233. offsetY = this._epsilon;
  234. else if (offsetY === 1)
  235. offsetY = 1 - this._epsilon;
  236. var offset = index * this._vertexBufferSize;
  237. this._vertexData[offset] = particle.position.x;
  238. this._vertexData[offset + 1] = particle.position.y;
  239. this._vertexData[offset + 2] = particle.position.z;
  240. this._vertexData[offset + 3] = particle.color.r;
  241. this._vertexData[offset + 4] = particle.color.g;
  242. this._vertexData[offset + 5] = particle.color.b;
  243. this._vertexData[offset + 6] = particle.color.a;
  244. this._vertexData[offset + 7] = particle.angle;
  245. this._vertexData[offset + 8] = particle.size;
  246. this._vertexData[offset + 9] = offsetX;
  247. this._vertexData[offset + 10] = offsetY;
  248. this._vertexData[offset + 11] = particle.cellIndex;
  249. }
  250. private _update(newParticles: number): void {
  251. // Update current
  252. this._alive = this.particles.length > 0;
  253. this.updateFunction(this.particles);
  254. // Add new ones
  255. var worldMatrix;
  256. if ((<AbstractMesh>this.emitter).position) {
  257. var emitterMesh = (<AbstractMesh>this.emitter);
  258. worldMatrix = emitterMesh.getWorldMatrix();
  259. } else {
  260. var emitterPosition = (<Vector3>this.emitter);
  261. worldMatrix = Matrix.Translation(emitterPosition.x, emitterPosition.y, emitterPosition.z);
  262. }
  263. var particle: Particle;
  264. for (var index = 0; index < newParticles; index++) {
  265. if (this.particles.length === this._capacity) {
  266. break;
  267. }
  268. if (this._stockParticles.length !== 0) {
  269. particle = this._stockParticles.pop();
  270. particle.age = 0;
  271. particle.cellIndex = this.startSpriteCellID;
  272. } else {
  273. particle = new Particle(this);
  274. }
  275. this.particles.push(particle);
  276. var emitPower = randomNumber(this.minEmitPower, this.maxEmitPower);
  277. this.startDirectionFunction(emitPower, worldMatrix, particle.direction, particle);
  278. particle.lifeTime = randomNumber(this.minLifeTime, this.maxLifeTime);
  279. particle.size = randomNumber(this.minSize, this.maxSize);
  280. particle.angularSpeed = randomNumber(this.minAngularSpeed, this.maxAngularSpeed);
  281. this.startPositionFunction(worldMatrix, particle.position, particle);
  282. var step = randomNumber(0, 1.0);
  283. Color4.LerpToRef(this.color1, this.color2, step, particle.color);
  284. this.colorDead.subtractToRef(particle.color, this._colorDiff);
  285. this._colorDiff.scaleToRef(1.0 / particle.lifeTime, particle.colorStep);
  286. }
  287. }
  288. private _getEffect(): Effect {
  289. if (this._customEffect) {
  290. return this._customEffect;
  291. };
  292. var defines = [];
  293. if (this._scene.clipPlane) {
  294. defines.push("#define CLIPPLANE");
  295. }
  296. if (this._isAnimationSheetEnabled) {
  297. defines.push("#define ANIMATESHEET");
  298. }
  299. // Effect
  300. var join = defines.join("\n");
  301. if (this._cachedDefines !== join) {
  302. this._cachedDefines = join;
  303. var attributesNamesOrOptions: any;
  304. var effectCreationOption: any;
  305. if (this._isAnimationSheetEnabled) {
  306. attributesNamesOrOptions = [VertexBuffer.PositionKind, VertexBuffer.ColorKind, "options", "cellIndex"];
  307. effectCreationOption = ["invView", "view", "projection", "particlesInfos", "vClipPlane", "textureMask"];
  308. }
  309. else {
  310. attributesNamesOrOptions = [VertexBuffer.PositionKind, VertexBuffer.ColorKind, "options"];
  311. effectCreationOption = ["invView", "view", "projection", "vClipPlane", "textureMask"]
  312. }
  313. this._effect = this._scene.getEngine().createEffect(
  314. "particles",
  315. attributesNamesOrOptions,
  316. effectCreationOption,
  317. ["diffuseSampler"], join);
  318. }
  319. return this._effect;
  320. }
  321. public animate(): void {
  322. if (!this._started)
  323. return;
  324. var effect = this._getEffect();
  325. // Check
  326. if (!this.emitter || !effect.isReady() || !this.particleTexture || !this.particleTexture.isReady())
  327. return;
  328. if (this._currentRenderId === this._scene.getRenderId()) {
  329. return;
  330. }
  331. this._currentRenderId = this._scene.getRenderId();
  332. this._scaledUpdateSpeed = this.updateSpeed * this._scene.getAnimationRatio();
  333. // determine the number of particles we need to create
  334. var newParticles;
  335. if (this.manualEmitCount > -1) {
  336. newParticles = this.manualEmitCount;
  337. this._newPartsExcess = 0;
  338. this.manualEmitCount = 0;
  339. } else {
  340. newParticles = ((this.emitRate * this._scaledUpdateSpeed) >> 0);
  341. this._newPartsExcess += this.emitRate * this._scaledUpdateSpeed - newParticles;
  342. }
  343. if (this._newPartsExcess > 1.0) {
  344. newParticles += this._newPartsExcess >> 0;
  345. this._newPartsExcess -= this._newPartsExcess >> 0;
  346. }
  347. this._alive = false;
  348. if (!this._stopped) {
  349. this._actualFrame += this._scaledUpdateSpeed;
  350. if (this.targetStopDuration && this._actualFrame >= this.targetStopDuration)
  351. this.stop();
  352. } else {
  353. newParticles = 0;
  354. }
  355. this._update(newParticles);
  356. // Stopped?
  357. if (this._stopped) {
  358. if (!this._alive) {
  359. this._started = false;
  360. if (this.onAnimationEnd) {
  361. this.onAnimationEnd();
  362. }
  363. if (this.disposeOnStop) {
  364. this._scene._toBeDisposed.push(this);
  365. }
  366. }
  367. }
  368. // Animation sheet
  369. if (this._isAnimationSheetEnabled) {
  370. this.appendParticleVertexes = this.appenedParticleVertexesWithSheet;
  371. }
  372. else {
  373. this.appendParticleVertexes = this.appenedParticleVertexesNoSheet;
  374. }
  375. // Update VBO
  376. var offset = 0;
  377. for (var index = 0; index < this.particles.length; index++) {
  378. var particle = this.particles[index];
  379. this.appendParticleVertexes(offset, particle);
  380. offset += 4;
  381. }
  382. this._vertexBuffer.update(this._vertexData);
  383. }
  384. public appendParticleVertexes: (offset: number, particle: Particle) => void = null;
  385. private appenedParticleVertexesWithSheet(offset: number, particle: Particle) {
  386. this._appendParticleVertexWithAnimation(offset++, particle, 0, 0);
  387. this._appendParticleVertexWithAnimation(offset++, particle, 1, 0);
  388. this._appendParticleVertexWithAnimation(offset++, particle, 1, 1);
  389. this._appendParticleVertexWithAnimation(offset++, particle, 0, 1);
  390. }
  391. private appenedParticleVertexesNoSheet(offset: number, particle: Particle) {
  392. this._appendParticleVertex(offset++, particle, 0, 0);
  393. this._appendParticleVertex(offset++, particle, 1, 0);
  394. this._appendParticleVertex(offset++, particle, 1, 1);
  395. this._appendParticleVertex(offset++, particle, 0, 1);
  396. }
  397. public rebuild(): void {
  398. this._createIndexBuffer();
  399. this._vertexBuffer._rebuild();
  400. }
  401. public render(): number {
  402. var effect = this._getEffect();
  403. // Check
  404. if (!this.emitter || !effect.isReady() || !this.particleTexture || !this.particleTexture.isReady() || !this.particles.length)
  405. return 0;
  406. var engine = this._scene.getEngine();
  407. // Render
  408. engine.enableEffect(effect);
  409. engine.setState(false);
  410. var viewMatrix = this._scene.getViewMatrix();
  411. effect.setTexture("diffuseSampler", this.particleTexture);
  412. effect.setMatrix("view", viewMatrix);
  413. effect.setMatrix("projection", this._scene.getProjectionMatrix());
  414. if (this._isAnimationSheetEnabled) {
  415. var baseSize = this.particleTexture.getBaseSize();
  416. effect.setFloat3("particlesInfos", this.spriteCellWidth / baseSize.width, this.spriteCellHeight / baseSize.height, baseSize.width / this.spriteCellWidth);
  417. }
  418. effect.setFloat4("textureMask", this.textureMask.r, this.textureMask.g, this.textureMask.b, this.textureMask.a);
  419. if (this._scene.clipPlane) {
  420. var clipPlane = this._scene.clipPlane;
  421. var invView = viewMatrix.clone();
  422. invView.invert();
  423. effect.setMatrix("invView", invView);
  424. effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  425. }
  426. // VBOs
  427. engine.bindBuffers(this._vertexBuffers, this._indexBuffer, effect);
  428. // Draw order
  429. if (this.blendMode === ParticleSystem.BLENDMODE_ONEONE) {
  430. engine.setAlphaMode(Engine.ALPHA_ONEONE);
  431. } else {
  432. engine.setAlphaMode(Engine.ALPHA_COMBINE);
  433. }
  434. if (this.forceDepthWrite) {
  435. engine.setDepthWrite(true);
  436. }
  437. engine.draw(true, 0, this.particles.length * 6);
  438. engine.setAlphaMode(Engine.ALPHA_DISABLE);
  439. return this.particles.length;
  440. }
  441. public dispose(): void {
  442. if (this._vertexBuffer) {
  443. this._vertexBuffer.dispose();
  444. this._vertexBuffer = null;
  445. }
  446. if (this._indexBuffer) {
  447. this._scene.getEngine()._releaseBuffer(this._indexBuffer);
  448. this._indexBuffer = null;
  449. }
  450. if (this.particleTexture) {
  451. this.particleTexture.dispose();
  452. this.particleTexture = null;
  453. }
  454. // Remove from scene
  455. var index = this._scene.particleSystems.indexOf(this);
  456. if (index > -1) {
  457. this._scene.particleSystems.splice(index, 1);
  458. }
  459. // Callback
  460. this.onDisposeObservable.notifyObservers(this);
  461. this.onDisposeObservable.clear();
  462. }
  463. // Clone
  464. public clone(name: string, newEmitter: any): ParticleSystem {
  465. var custom: Effect = null;
  466. var program: any = null;
  467. if (this.customShader != null) {
  468. program = this.customShader;
  469. var defines: string = (program.shaderOptions.defines.length > 0) ? program.shaderOptions.defines.join("\n") : "";
  470. custom = this._scene.getEngine().createEffectForParticles(program.shaderPath.fragmentElement, program.shaderOptions.uniforms, program.shaderOptions.samplers, defines);
  471. }
  472. var result = new ParticleSystem(name, this._capacity, this._scene, custom);
  473. result.customShader = program;
  474. Tools.DeepCopy(this, result, ["particles", "customShader"]);
  475. if (newEmitter === undefined) {
  476. newEmitter = this.emitter;
  477. }
  478. result.emitter = newEmitter;
  479. if (this.particleTexture) {
  480. result.particleTexture = new Texture(this.particleTexture.url, this._scene);
  481. }
  482. if (!this.preventAutoStart) {
  483. result.start();
  484. }
  485. return result;
  486. }
  487. public serialize(): any {
  488. var serializationObject: any = {};
  489. serializationObject.name = this.name;
  490. serializationObject.id = this.id;
  491. // Emitter
  492. if ((<AbstractMesh>this.emitter).position) {
  493. var emitterMesh = (<AbstractMesh>this.emitter);
  494. serializationObject.emitterId = emitterMesh.id;
  495. } else {
  496. var emitterPosition = (<Vector3>this.emitter);
  497. serializationObject.emitter = emitterPosition.asArray();
  498. }
  499. serializationObject.capacity = this.getCapacity();
  500. if (this.particleTexture) {
  501. serializationObject.textureName = this.particleTexture.name;
  502. }
  503. // Animations
  504. Animation.AppendSerializedAnimations(this, serializationObject);
  505. // Particle system
  506. serializationObject.minAngularSpeed = this.minAngularSpeed;
  507. serializationObject.maxAngularSpeed = this.maxAngularSpeed;
  508. serializationObject.minSize = this.minSize;
  509. serializationObject.maxSize = this.maxSize;
  510. serializationObject.minEmitPower = this.minEmitPower;
  511. serializationObject.maxEmitPower = this.maxEmitPower;
  512. serializationObject.minLifeTime = this.minLifeTime;
  513. serializationObject.maxLifeTime = this.maxLifeTime;
  514. serializationObject.emitRate = this.emitRate;
  515. serializationObject.minEmitBox = this.minEmitBox.asArray();
  516. serializationObject.maxEmitBox = this.maxEmitBox.asArray();
  517. serializationObject.gravity = this.gravity.asArray();
  518. serializationObject.direction1 = this.direction1.asArray();
  519. serializationObject.direction2 = this.direction2.asArray();
  520. serializationObject.color1 = this.color1.asArray();
  521. serializationObject.color2 = this.color2.asArray();
  522. serializationObject.colorDead = this.colorDead.asArray();
  523. serializationObject.updateSpeed = this.updateSpeed;
  524. serializationObject.targetStopDuration = this.targetStopDuration;
  525. serializationObject.textureMask = this.textureMask.asArray();
  526. serializationObject.blendMode = this.blendMode;
  527. serializationObject.customShader = this.customShader;
  528. serializationObject.preventAutoStart = this.preventAutoStart;
  529. return serializationObject;
  530. }
  531. public static Parse(parsedParticleSystem: any, scene: Scene, rootUrl: string): ParticleSystem {
  532. var name = parsedParticleSystem.name;
  533. var custom: Effect = null;
  534. var program: any = null;
  535. if (parsedParticleSystem.customShader) {
  536. program = parsedParticleSystem.customShader;
  537. var defines: string = (program.shaderOptions.defines.length > 0) ? program.shaderOptions.defines.join("\n") : "";
  538. custom = scene.getEngine().createEffectForParticles(program.shaderPath.fragmentElement, program.shaderOptions.uniforms, program.shaderOptions.samplers, defines);
  539. }
  540. var particleSystem = new ParticleSystem(name, parsedParticleSystem.capacity, scene, custom);
  541. particleSystem.customShader = program;
  542. if (parsedParticleSystem.id) {
  543. particleSystem.id = parsedParticleSystem.id;
  544. }
  545. // Auto start
  546. if (parsedParticleSystem.preventAutoStart) {
  547. particleSystem.preventAutoStart = parsedParticleSystem.preventAutoStart;
  548. }
  549. // Texture
  550. if (parsedParticleSystem.textureName) {
  551. particleSystem.particleTexture = new Texture(rootUrl + parsedParticleSystem.textureName, scene);
  552. particleSystem.particleTexture.name = parsedParticleSystem.textureName;
  553. }
  554. // Emitter
  555. if (parsedParticleSystem.emitterId) {
  556. particleSystem.emitter = scene.getLastMeshByID(parsedParticleSystem.emitterId);
  557. } else {
  558. particleSystem.emitter = Vector3.FromArray(parsedParticleSystem.emitter);
  559. }
  560. // Animations
  561. if (parsedParticleSystem.animations) {
  562. for (var animationIndex = 0; animationIndex < parsedParticleSystem.animations.length; animationIndex++) {
  563. var parsedAnimation = parsedParticleSystem.animations[animationIndex];
  564. particleSystem.animations.push(Animation.Parse(parsedAnimation));
  565. }
  566. }
  567. if (parsedParticleSystem.autoAnimate) {
  568. scene.beginAnimation(particleSystem, parsedParticleSystem.autoAnimateFrom, parsedParticleSystem.autoAnimateTo, parsedParticleSystem.autoAnimateLoop, parsedParticleSystem.autoAnimateSpeed || 1.0);
  569. }
  570. // Particle system
  571. particleSystem.minAngularSpeed = parsedParticleSystem.minAngularSpeed;
  572. particleSystem.maxAngularSpeed = parsedParticleSystem.maxAngularSpeed;
  573. particleSystem.minSize = parsedParticleSystem.minSize;
  574. particleSystem.maxSize = parsedParticleSystem.maxSize;
  575. particleSystem.minLifeTime = parsedParticleSystem.minLifeTime;
  576. particleSystem.maxLifeTime = parsedParticleSystem.maxLifeTime;
  577. particleSystem.minEmitPower = parsedParticleSystem.minEmitPower;
  578. particleSystem.maxEmitPower = parsedParticleSystem.maxEmitPower;
  579. particleSystem.emitRate = parsedParticleSystem.emitRate;
  580. particleSystem.minEmitBox = Vector3.FromArray(parsedParticleSystem.minEmitBox);
  581. particleSystem.maxEmitBox = Vector3.FromArray(parsedParticleSystem.maxEmitBox);
  582. particleSystem.gravity = Vector3.FromArray(parsedParticleSystem.gravity);
  583. particleSystem.direction1 = Vector3.FromArray(parsedParticleSystem.direction1);
  584. particleSystem.direction2 = Vector3.FromArray(parsedParticleSystem.direction2);
  585. particleSystem.color1 = Color4.FromArray(parsedParticleSystem.color1);
  586. particleSystem.color2 = Color4.FromArray(parsedParticleSystem.color2);
  587. particleSystem.colorDead = Color4.FromArray(parsedParticleSystem.colorDead);
  588. particleSystem.updateSpeed = parsedParticleSystem.updateSpeed;
  589. particleSystem.targetStopDuration = parsedParticleSystem.targetStopDuration;
  590. particleSystem.textureMask = Color4.FromArray(parsedParticleSystem.textureMask);
  591. particleSystem.blendMode = parsedParticleSystem.blendMode;
  592. if (!particleSystem.preventAutoStart) {
  593. particleSystem.start();
  594. }
  595. return particleSystem;
  596. }
  597. }
  598. }