babylon.shadowGeneratorSceneComponent.ts 4.2 KB

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