babylon.lensFlareSystemSceneComponent.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. module BABYLON {
  2. // Adds the parser to the scene parsers.
  3. AbstractScene.AddParser(SceneComponentConstants.NAME_LENSFLARESYSTEM, (parsedData: any, scene: Scene, container: AssetContainer, rootUrl: string) => {
  4. // Lens flares
  5. if (parsedData.lensFlareSystems !== undefined && parsedData.lensFlareSystems !== null) {
  6. for (let index = 0, cache = parsedData.lensFlareSystems.length; index < cache; index++) {
  7. var parsedLensFlareSystem = parsedData.lensFlareSystems[index];
  8. var lf = LensFlareSystem.Parse(parsedLensFlareSystem, scene, rootUrl);
  9. container.lensFlareSystems.push(lf);
  10. }
  11. }
  12. });
  13. export interface AbstractScene {
  14. /**
  15. * The list of lens flare system added to the scene
  16. * @see http://doc.babylonjs.com/how_to/how_to_use_lens_flares
  17. */
  18. lensFlareSystems: Array<LensFlareSystem>;
  19. /**
  20. * Removes the given lens flare system from this scene.
  21. * @param toRemove The lens flare system to remove
  22. * @returns The index of the removed lens flare system
  23. */
  24. removeLensFlareSystem(toRemove: LensFlareSystem): number;
  25. /**
  26. * Adds the given lens flare system to this scene
  27. * @param newLensFlareSystem The lens flare system to add
  28. */
  29. addLensFlareSystem(newLensFlareSystem: LensFlareSystem): void;
  30. /**
  31. * Gets a lens flare system using its name
  32. * @param name defines the name to look for
  33. * @returns the lens flare system or null if not found
  34. */
  35. getLensFlareSystemByName(name: string): Nullable<LensFlareSystem>;
  36. /**
  37. * Gets a lens flare system using its id
  38. * @param id defines the id to look for
  39. * @returns the lens flare system or null if not found
  40. */
  41. getLensFlareSystemByID(id: string): Nullable<LensFlareSystem>;
  42. }
  43. AbstractScene.prototype.getLensFlareSystemByName = function(name: string): Nullable<LensFlareSystem> {
  44. for (var index = 0; index < this.lensFlareSystems.length; index++) {
  45. if (this.lensFlareSystems[index].name === name) {
  46. return this.lensFlareSystems[index];
  47. }
  48. }
  49. return null;
  50. }
  51. AbstractScene.prototype.getLensFlareSystemByID = function(id: string): Nullable<LensFlareSystem> {
  52. for (var index = 0; index < this.lensFlareSystems.length; index++) {
  53. if (this.lensFlareSystems[index].id === id) {
  54. return this.lensFlareSystems[index];
  55. }
  56. }
  57. return null;
  58. }
  59. AbstractScene.prototype.removeLensFlareSystem = function(toRemove: LensFlareSystem): number {
  60. var index = this.lensFlareSystems.indexOf(toRemove);
  61. if (index !== -1) {
  62. this.lensFlareSystems.splice(index, 1);
  63. }
  64. return index;
  65. }
  66. AbstractScene.prototype.addLensFlareSystem = function(newLensFlareSystem: LensFlareSystem): void {
  67. this.lensFlareSystems.push(newLensFlareSystem);
  68. }
  69. /**
  70. * Defines the lens flare scene component responsible to manage any lens flares
  71. * in a given scene.
  72. */
  73. export class LensFlareSystemSceneComponent implements ISceneSerializableComponent {
  74. /**
  75. * The component name helpfull to identify the component in the list of scene components.
  76. */
  77. public readonly name = SceneComponentConstants.NAME_LENSFLARESYSTEM;
  78. /**
  79. * The scene the component belongs to.
  80. */
  81. public scene: Scene;
  82. /**
  83. * Creates a new instance of the component for the given scene
  84. * @param scene Defines the scene to register the component in
  85. */
  86. constructor(scene: Scene) {
  87. this.scene = scene;
  88. scene.lensFlareSystems = new Array<LensFlareSystem>();
  89. }
  90. /**
  91. * Registers the component in a given scene
  92. */
  93. public register(): void {
  94. this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LENSFLARESYSTEM, this, this._draw);
  95. }
  96. /**
  97. * Rebuilds the elements related to this component in case of
  98. * context lost for instance.
  99. */
  100. public rebuild(): void {
  101. // Nothing to do for lens flare
  102. }
  103. /**
  104. * Adds all the element from the container to the scene
  105. * @param container the container holding the elements
  106. */
  107. public addFromContainer(container: AbstractScene): void {
  108. if (!container.lensFlareSystems) {
  109. return;
  110. }
  111. container.lensFlareSystems.forEach((o) => {
  112. this.scene.addLensFlareSystem(o);
  113. });
  114. }
  115. /**
  116. * Removes all the elements in the container from the scene
  117. * @param container contains the elements to remove
  118. */
  119. public removeFromContainer(container: AbstractScene): void {
  120. if (!container.lensFlareSystems) {
  121. return;
  122. }
  123. container.lensFlareSystems.forEach((o) => {
  124. this.scene.removeLensFlareSystem(o);
  125. });
  126. }
  127. /**
  128. * Serializes the component data to the specified json object
  129. * @param serializationObject The object to serialize to
  130. */
  131. public serialize(serializationObject: any): void {
  132. // Lens flares
  133. serializationObject.lensFlareSystems = [];
  134. let lensFlareSystems = this.scene.lensFlareSystems;
  135. for (let lensFlareSystem of lensFlareSystems) {
  136. serializationObject.lensFlareSystems.push(lensFlareSystem.serialize());
  137. }
  138. }
  139. /**
  140. * Disposes the component and the associated ressources.
  141. */
  142. public dispose(): void {
  143. let lensFlareSystems = this.scene.lensFlareSystems;
  144. while (lensFlareSystems.length) {
  145. lensFlareSystems[0].dispose();
  146. }
  147. }
  148. private _draw(camera: Camera): void {
  149. // Lens flares
  150. if (this.scene.lensFlaresEnabled) {
  151. let lensFlareSystems = this.scene.lensFlareSystems;
  152. Tools.StartPerformanceCounter("Lens flares", lensFlareSystems.length > 0);
  153. for (let lensFlareSystem of lensFlareSystems) {
  154. if ((camera.layerMask & lensFlareSystem.layerMask) !== 0) {
  155. lensFlareSystem.render();
  156. }
  157. }
  158. Tools.EndPerformanceCounter("Lens flares", lensFlareSystems.length > 0);
  159. }
  160. }
  161. }
  162. }