lensFlareSystemSceneComponent.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { Tools } from "../Misc/tools.js";
  2. import { SceneComponentConstants } from "../sceneComponent.js";
  3. import { AbstractScene } from "../abstractScene.js";
  4. import { LensFlareSystem } from "./lensFlareSystem.js";
  5. // Adds the parser to the scene parsers.
  6. AbstractScene.AddParser(SceneComponentConstants.NAME_LENSFLARESYSTEM, (parsedData, scene, container, rootUrl) => {
  7. // Lens flares
  8. if (parsedData.lensFlareSystems !== undefined && parsedData.lensFlareSystems !== null) {
  9. if (!container.lensFlareSystems) {
  10. container.lensFlareSystems = [];
  11. }
  12. for (let index = 0, cache = parsedData.lensFlareSystems.length; index < cache; index++) {
  13. const parsedLensFlareSystem = parsedData.lensFlareSystems[index];
  14. const lf = LensFlareSystem.Parse(parsedLensFlareSystem, scene, rootUrl);
  15. container.lensFlareSystems.push(lf);
  16. }
  17. }
  18. });
  19. AbstractScene.prototype.getLensFlareSystemByName = function (name) {
  20. for (let index = 0; index < this.lensFlareSystems.length; index++) {
  21. if (this.lensFlareSystems[index].name === name) {
  22. return this.lensFlareSystems[index];
  23. }
  24. }
  25. return null;
  26. };
  27. AbstractScene.prototype.getLensFlareSystemById = function (id) {
  28. for (let index = 0; index < this.lensFlareSystems.length; index++) {
  29. if (this.lensFlareSystems[index].id === id) {
  30. return this.lensFlareSystems[index];
  31. }
  32. }
  33. return null;
  34. };
  35. AbstractScene.prototype.getLensFlareSystemByID = function (id) {
  36. return this.getLensFlareSystemById(id);
  37. };
  38. AbstractScene.prototype.removeLensFlareSystem = function (toRemove) {
  39. const index = this.lensFlareSystems.indexOf(toRemove);
  40. if (index !== -1) {
  41. this.lensFlareSystems.splice(index, 1);
  42. }
  43. return index;
  44. };
  45. AbstractScene.prototype.addLensFlareSystem = function (newLensFlareSystem) {
  46. this.lensFlareSystems.push(newLensFlareSystem);
  47. };
  48. /**
  49. * Defines the lens flare scene component responsible to manage any lens flares
  50. * in a given scene.
  51. */
  52. export class LensFlareSystemSceneComponent {
  53. /**
  54. * Creates a new instance of the component for the given scene
  55. * @param scene Defines the scene to register the component in
  56. */
  57. constructor(scene) {
  58. /**
  59. * The component name helpful to identify the component in the list of scene components.
  60. */
  61. this.name = SceneComponentConstants.NAME_LENSFLARESYSTEM;
  62. this.scene = scene;
  63. scene.lensFlareSystems = [];
  64. }
  65. /**
  66. * Registers the component in a given scene
  67. */
  68. register() {
  69. this.scene._afterCameraDrawStage.registerStep(SceneComponentConstants.STEP_AFTERCAMERADRAW_LENSFLARESYSTEM, this, this._draw);
  70. }
  71. /**
  72. * Rebuilds the elements related to this component in case of
  73. * context lost for instance.
  74. */
  75. rebuild() {
  76. for (let index = 0; index < this.scene.lensFlareSystems.length; index++) {
  77. this.scene.lensFlareSystems[index].rebuild();
  78. }
  79. }
  80. /**
  81. * Adds all the elements from the container to the scene
  82. * @param container the container holding the elements
  83. */
  84. addFromContainer(container) {
  85. if (!container.lensFlareSystems) {
  86. return;
  87. }
  88. container.lensFlareSystems.forEach((o) => {
  89. this.scene.addLensFlareSystem(o);
  90. });
  91. }
  92. /**
  93. * Removes all the elements in the container from the scene
  94. * @param container contains the elements to remove
  95. * @param dispose if the removed element should be disposed (default: false)
  96. */
  97. removeFromContainer(container, dispose) {
  98. if (!container.lensFlareSystems) {
  99. return;
  100. }
  101. container.lensFlareSystems.forEach((o) => {
  102. this.scene.removeLensFlareSystem(o);
  103. if (dispose) {
  104. o.dispose();
  105. }
  106. });
  107. }
  108. /**
  109. * Serializes the component data to the specified json object
  110. * @param serializationObject The object to serialize to
  111. */
  112. serialize(serializationObject) {
  113. // Lens flares
  114. serializationObject.lensFlareSystems = [];
  115. const lensFlareSystems = this.scene.lensFlareSystems;
  116. for (const lensFlareSystem of lensFlareSystems) {
  117. serializationObject.lensFlareSystems.push(lensFlareSystem.serialize());
  118. }
  119. }
  120. /**
  121. * Disposes the component and the associated resources.
  122. */
  123. dispose() {
  124. const lensFlareSystems = this.scene.lensFlareSystems;
  125. while (lensFlareSystems.length) {
  126. lensFlareSystems[0].dispose();
  127. }
  128. }
  129. _draw(camera) {
  130. // Lens flares
  131. if (this.scene.lensFlaresEnabled) {
  132. const lensFlareSystems = this.scene.lensFlareSystems;
  133. Tools.StartPerformanceCounter("Lens flares", lensFlareSystems.length > 0);
  134. for (const lensFlareSystem of lensFlareSystems) {
  135. if ((camera.layerMask & lensFlareSystem.layerMask) !== 0) {
  136. lensFlareSystem.render();
  137. }
  138. }
  139. Tools.EndPerformanceCounter("Lens flares", lensFlareSystems.length > 0);
  140. }
  141. }
  142. }
  143. LensFlareSystem._SceneComponentInitialization = (scene) => {
  144. let component = scene._getComponent(SceneComponentConstants.NAME_LENSFLARESYSTEM);
  145. if (!component) {
  146. component = new LensFlareSystemSceneComponent(scene);
  147. scene._addComponent(component);
  148. }
  149. };
  150. //# sourceMappingURL=lensFlareSystemSceneComponent.js.map