shadowGeneratorSceneComponent.ts 5.0 KB

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