shadowGeneratorSceneComponent.ts 4.8 KB

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