shadowGeneratorSceneComponent.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { SmartArrayNoDuplicate } from "../../Misc/smartArray";
  2. import { Scene } from "../../scene";
  3. import { RenderTargetTexture } from "../../Materials/Textures/renderTargetTexture";
  4. import { ShadowGenerator } from "./shadowGenerator";
  5. import { CascadedShadowGenerator } from "./cascadedShadowGenerator";
  6. import { SceneComponentConstants, ISceneSerializableComponent } from "../../sceneComponent";
  7. import { _TimeToken } from "../../Instrumentation/timeToken";
  8. import { AbstractScene } from "../../abstractScene";
  9. // Adds the parser to the scene parsers.
  10. AbstractScene.AddParser(SceneComponentConstants.NAME_SHADOWGENERATOR, (parsedData: any, scene: Scene) => {
  11. // Shadows
  12. if (parsedData.shadowGenerators !== undefined && parsedData.shadowGenerators !== null) {
  13. for (var index = 0, cache = parsedData.shadowGenerators.length; index < cache; index++) {
  14. var parsedShadowGenerator = parsedData.shadowGenerators[index];
  15. if (parsedShadowGenerator.className === CascadedShadowGenerator.CLASSNAME) {
  16. CascadedShadowGenerator.Parse(parsedShadowGenerator, scene);
  17. } else {
  18. ShadowGenerator.Parse(parsedShadowGenerator, scene);
  19. }
  20. // SG would be available on their associated lights
  21. }
  22. }
  23. });
  24. /**
  25. * Defines the shadow generator component responsible to manage any shadow generators
  26. * in a given scene.
  27. */
  28. export class ShadowGeneratorSceneComponent implements ISceneSerializableComponent {
  29. /**
  30. * The component name helpfull to identify the component in the list of scene components.
  31. */
  32. public readonly name = SceneComponentConstants.NAME_SHADOWGENERATOR;
  33. /**
  34. * The scene the component belongs to.
  35. */
  36. public scene: Scene;
  37. /**
  38. * Creates a new instance of the component for the given scene
  39. * @param scene Defines the scene to register the component in
  40. */
  41. constructor(scene: Scene) {
  42. this.scene = scene;
  43. }
  44. /**
  45. * Registers the component in a given scene
  46. */
  47. public register(): void {
  48. this.scene._gatherRenderTargetsStage.registerStep(SceneComponentConstants.STEP_GATHERRENDERTARGETS_SHADOWGENERATOR, this, this._gatherRenderTargets);
  49. }
  50. /**
  51. * Rebuilds the elements related to this component in case of
  52. * context lost for instance.
  53. */
  54. public rebuild(): void {
  55. // Nothing To Do Here.
  56. }
  57. /**
  58. * Serializes the component data to the specified json object
  59. * @param serializationObject The object to serialize to
  60. */
  61. public serialize(serializationObject: any): void {
  62. // Shadows
  63. serializationObject.shadowGenerators = [];
  64. var lights = this.scene.lights;
  65. for (let light of lights) {
  66. let shadowGenerator = light.getShadowGenerator();
  67. if (shadowGenerator) {
  68. serializationObject.shadowGenerators.push(shadowGenerator.serialize());
  69. }
  70. }
  71. }
  72. /**
  73. * Adds all the elements from the container to the scene
  74. * @param container the container holding the elements
  75. */
  76. public addFromContainer(container: AbstractScene): void {
  77. // Nothing To Do Here. (directly attached to a light)
  78. }
  79. /**
  80. * Removes all the elements in the container from the scene
  81. * @param container contains the elements to remove
  82. * @param dispose if the removed element should be disposed (default: false)
  83. */
  84. public removeFromContainer(container: AbstractScene, dispose?: boolean): void {
  85. // Nothing To Do Here. (directly attached to a light)
  86. }
  87. /**
  88. * Rebuilds the elements related to this component in case of
  89. * context lost for instance.
  90. */
  91. public dispose(): void {
  92. // Nothing To Do Here.
  93. }
  94. private _gatherRenderTargets(renderTargets: SmartArrayNoDuplicate<RenderTargetTexture>): void {
  95. // Shadows
  96. var scene = this.scene;
  97. if (this.scene.shadowsEnabled) {
  98. for (var lightIndex = 0; lightIndex < scene.lights.length; lightIndex++) {
  99. var light = scene.lights[lightIndex];
  100. var shadowGenerator = light.getShadowGenerator();
  101. if (light.isEnabled() && light.shadowEnabled && shadowGenerator) {
  102. var shadowMap = <RenderTargetTexture>(shadowGenerator.getShadowMap());
  103. if (scene.textures.indexOf(shadowMap) !== -1) {
  104. renderTargets.push(shadowMap);
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. ShadowGenerator._SceneComponentInitialization = (scene: Scene) => {
  112. let component = scene._getComponent(SceneComponentConstants.NAME_SHADOWGENERATOR);
  113. if (!component) {
  114. component = new ShadowGeneratorSceneComponent(scene);
  115. scene._addComponent(component);
  116. }
  117. };
  118. CascadedShadowGenerator._SceneComponentInitialization = (scene: Scene) => {
  119. let component = scene._getComponent(SceneComponentConstants.NAME_SHADOWGENERATOR);
  120. if (!component) {
  121. component = new ShadowGeneratorSceneComponent(scene);
  122. scene._addComponent(component);
  123. }
  124. };