Przeglądaj źródła

Merge branch 'master' of https://github.com/BabylonJS/Babylon.js

David Catuhe 7 lat temu
rodzic
commit
80a38ea401

+ 2 - 1
Tools/Gulp/config.json

@@ -609,7 +609,8 @@
         },
         "shadows": {
             "files": [
-                "../../src/Lights/Shadows/babylon.shadowGenerator.js"
+                "../../src/Lights/Shadows/babylon.shadowGenerator.js",
+                "../../src/Lights/Shadows/babylon.shadowGeneratorSceneComponent.js"
             ],
             "dependUpon": [
                 "postProcesses"

+ 4 - 0
src/Layer/babylon.effectLayerSceneComponent.ts

@@ -2,6 +2,10 @@
     // Adds the parser to the scene parsers.
     AbstractScene.AddParser(SceneComponentConstants.NAME_EFFECTLAYER, (parsedData: any, scene: Scene, container: AssetContainer, rootUrl: string) => {
         if (parsedData.effectLayers) {
+            if (!container.effectLayers) {
+                container.effectLayers = new Array<EffectLayer>();
+            }
+
             for (let index = 0; index < parsedData.effectLayers.length; index++) {
                 var effectLayer = EffectLayer.Parse(parsedData.effectLayers[index], scene, rootUrl);
                 container.effectLayers.push(effectLayer);

+ 4 - 0
src/LensFlare/babylon.lensFlareSystemSceneComponent.ts

@@ -3,6 +3,10 @@
     AbstractScene.AddParser(SceneComponentConstants.NAME_LENSFLARESYSTEM, (parsedData: any, scene: Scene, container: AssetContainer, rootUrl: string) => {
         // Lens flares
         if (parsedData.lensFlareSystems !== undefined && parsedData.lensFlareSystems !== null) {
+            if (!container.lensFlareSystems) {
+                container.lensFlareSystems = new Array<LensFlareSystem>();
+            }
+
             for (let index = 0, cache = parsedData.lensFlareSystems.length; index < cache; index++) {
                 var parsedLensFlareSystem = parsedData.lensFlareSystems[index];
                 var lf = LensFlareSystem.Parse(parsedLensFlareSystem, scene, rootUrl);

+ 6 - 0
src/Lights/Shadows/babylon.shadowGenerator.ts

@@ -686,6 +686,12 @@
             this._scene = light.getScene();
             light._shadowGenerator = this;
 
+            let component = this._scene._getComponent(SceneComponentConstants.NAME_SHADOWGENERATOR);
+            if (!component) {
+                component = new ShadowGeneratorSceneComponent(this._scene);
+                this._scene._addComponent(component);
+            }
+
             // Texture type fallback from float to int if not supported.
             var caps = this._scene.getEngine().getCaps();
 

+ 110 - 0
src/Lights/Shadows/babylon.shadowGeneratorSceneComponent.ts

@@ -0,0 +1,110 @@
+module BABYLON {
+    // Adds the parser to the scene parsers.
+    AbstractScene.AddParser(SceneComponentConstants.NAME_SHADOWGENERATOR, (parsedData: any, scene: Scene, container: AssetContainer, rootUrl: string) => {
+        // Shadows
+        if (parsedData.shadowGenerators !== undefined && parsedData.shadowGenerators !== null) {
+            for (var index = 0, cache = parsedData.shadowGenerators.length; index < cache; index++) {
+                var parsedShadowGenerator = parsedData.shadowGenerators[index];
+                ShadowGenerator.Parse(parsedShadowGenerator, scene);
+                // SG would be available on their associated lights
+            }
+        }
+    });
+
+    /**
+     * Defines the shadow generator component responsible to manage any shadow generators
+     * in a given scene.
+     */
+    export class ShadowGeneratorSceneComponent implements ISceneSerializableComponent {
+        /**
+         * The component name helpfull to identify the component in the list of scene components.
+         */
+        public readonly name = SceneComponentConstants.NAME_SHADOWGENERATOR;
+
+        /**
+         * The scene the component belongs to.
+         */
+        public scene: Scene;
+
+        /**
+         * Creates a new instance of the component for the given scene
+         * @param scene Defines the scene to register the component in
+         */
+        constructor(scene: Scene) {
+            this.scene = scene;
+        }
+
+        /**
+         * Registers the component in a given scene
+         */
+        public register(): void {
+            this.scene._gatherRenderTargetsStage.registerStep(SceneComponentConstants.STEP_GATHERRENDERTARGETS_SHADOWGENERATOR, this, this._gatherRenderTargets);
+        }
+
+        /**
+         * Rebuilds the elements related to this component in case of
+         * context lost for instance.
+         */
+        public rebuild(): void {
+            // Nothing To Do Here.
+        }
+
+        /**
+         * Serializes the component data to the specified json object
+         * @param serializationObject The object to serialize to
+         */
+        public serialize(serializationObject: any): void {
+            // Shadows
+            serializationObject.shadowGenerators = [];
+            var lights = this.scene.lights;
+            for (let light of lights) {
+                let shadowGenerator = light.getShadowGenerator();
+                if (shadowGenerator) {
+                    serializationObject.shadowGenerators.push(shadowGenerator.serialize());
+                }
+            }
+        }
+
+        /**
+         * Adds all the element from the container to the scene
+         * @param container the container holding the elements
+         */
+        public addFromContainer(container: AbstractScene): void {
+            // Nothing To Do Here. (directly attached to a light)
+        }
+
+        /**
+         * Removes all the elements in the container from the scene
+         * @param container contains the elements to remove 
+         */
+        public removeFromContainer(container: AbstractScene): void{
+            // Nothing To Do Here. (directly attached to a light)
+        }
+
+        /**
+         * Rebuilds the elements related to this component in case of
+         * context lost for instance.
+         */
+        public dispose(): void {
+            // Nothing To Do Here.
+        }
+
+        private _gatherRenderTargets(renderTargets: SmartArrayNoDuplicate<RenderTargetTexture>): void {
+            // Shadows
+            var scene = this.scene;
+            if (this.scene.shadowsEnabled) {
+                for (var lightIndex = 0; lightIndex < scene.lights.length; lightIndex++) {
+                    var light = scene.lights[lightIndex];
+                    var shadowGenerator = light.getShadowGenerator();
+
+                    if (light.isEnabled() && light.shadowEnabled && shadowGenerator) {
+                        var shadowMap = <RenderTargetTexture>(shadowGenerator.getShadowMap());
+                        if (scene.textures.indexOf(shadowMap) !== -1) {
+                            renderTargets.push(shadowMap);
+                        }
+                    }
+                }
+            }
+        }
+    }
+} 

+ 1 - 1
src/Lights/babylon.hemisphericLight.ts

@@ -70,7 +70,7 @@
          * Returns the shadow generator associated to the light.
          * @returns Always null for hemispheric lights because it does not support shadows.
          */
-        public getShadowGenerator(): Nullable<ShadowGenerator> {
+        public getShadowGenerator(): Nullable<IShadowGenerator> {
             return null;
         }
 

+ 0 - 9
src/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -293,15 +293,6 @@
                 }
             }
 
-            // Shadows
-            if (parsedData.shadowGenerators !== undefined && parsedData.shadowGenerators !== null) {
-                for (index = 0, cache = parsedData.shadowGenerators.length; index < cache; index++) {
-                    var parsedShadowGenerator = parsedData.shadowGenerators[index];
-                    ShadowGenerator.Parse(parsedShadowGenerator, scene);
-                    // SG would be available on their associated lights
-                }
-            }
-
             // Lights exclusions / inclusions
             for (index = 0, cache = scene.lights.length; index < cache; index++) {
                 let light = scene.lights[index];

+ 3 - 3
src/PostProcess/RenderPipeline/Pipelines/babylon.standardRenderingPipeline.ts

@@ -457,7 +457,7 @@
 
             this.volumetricLightPostProcess.onApply = (effect: Effect) => {
                 if (this.sourceLight && this.sourceLight.getShadowGenerator() && this._scene.activeCamera) {
-                    var generator = <ShadowGenerator>this.sourceLight.getShadowGenerator();
+                    var generator = this.sourceLight.getShadowGenerator()!;
 
                     effect.setTexture("shadowMapSampler", generator.getShadowMap());
                     effect.setTexture("positionSampler", geometry.textures[2]);
@@ -471,8 +471,8 @@
                     effect.setFloat("scatteringCoefficient", this.volumetricLightCoefficient);
                     effect.setFloat("scatteringPower", this.volumetricLightPower);
 
-                    depthValues.x = generator.getLight().getDepthMinZ(this._scene.activeCamera);
-                    depthValues.y = generator.getLight().getDepthMaxZ(this._scene.activeCamera);
+                    depthValues.x = this.sourceLight.getDepthMinZ(this._scene.activeCamera);
+                    depthValues.y = this.sourceLight.getDepthMaxZ(this._scene.activeCamera);
                     effect.setVector2("depthValues", depthValues);
                 }
             };

+ 0 - 11
src/Tools/babylon.sceneSerializer.ts

@@ -279,17 +279,6 @@
                 serializationObject.particleSystems.push(scene.particleSystems[index].serialize());
             }
 
-            // Shadows
-            serializationObject.shadowGenerators = [];
-            for (index = 0; index < scene.lights.length; index++) {
-                light = scene.lights[index];
-
-                let shadowGenerator = light.getShadowGenerator();
-                if (shadowGenerator) {
-                    serializationObject.shadowGenerators.push(shadowGenerator.serialize());
-                }
-            }
-
             // Action Manager
             if (scene.actionManager) {
                 serializationObject.actions = scene.actionManager.serialize("scene");

+ 0 - 15
src/babylon.scene.ts

@@ -4556,21 +4556,6 @@
                 this._engine.clear(this.clearColor, this.autoClear || this.forceWireframe || this.forcePointsCloud, this.autoClearDepthAndStencil, this.autoClearDepthAndStencil);
             }
 
-            // Shadows
-            if (this.shadowsEnabled) {
-                for (var lightIndex = 0; lightIndex < this.lights.length; lightIndex++) {
-                    var light = this.lights[lightIndex];
-                    var shadowGenerator = light.getShadowGenerator();
-
-                    if (light.isEnabled() && light.shadowEnabled && shadowGenerator) {
-                        var shadowMap = <RenderTargetTexture>(shadowGenerator.getShadowMap());
-                        if (this.textures.indexOf(shadowMap) !== -1) {
-                            this._renderTargets.push(shadowMap);
-                        }
-                    }
-                }
-            }
-
             // Collects render targets from external components.
             for (let step of this._gatherRenderTargetsStage) {
                 step.action(this._renderTargets);

+ 5 - 3
src/babylon.sceneComponent.ts

@@ -17,6 +17,7 @@
         public static readonly NAME_SPRITE = "Sprite";
         public static readonly NAME_OUTLINERENDERER = "Outline";
         public static readonly NAME_PROCEDURALTEXTURE = "ProceduralTexture";
+        public static readonly NAME_SHADOWGENERATOR = "ShadowGenerator";
 
         public static readonly STEP_ISREADYFORMESH_EFFECTLAYER = 0;
 
@@ -48,9 +49,10 @@
         public static readonly STEP_AFTERCAMERADRAW_EFFECTLAYER_DRAW = 3;
         public static readonly STEP_AFTERCAMERADRAW_LAYER = 4;
 
-        public static readonly STEP_GATHERRENDERTARGETS_GEOMETRYBUFFERRENDERER = 0;
-        public static readonly STEP_GATHERRENDERTARGETS_DEPTHRENDERER = 1;
-        public static readonly STEP_GATHERRENDERTARGETS_POSTPROCESSRENDERPIPELINEMANAGER = 2;
+        public static readonly STEP_GATHERRENDERTARGETS_SHADOWGENERATOR = 0;
+        public static readonly STEP_GATHERRENDERTARGETS_GEOMETRYBUFFERRENDERER = 1;
+        public static readonly STEP_GATHERRENDERTARGETS_DEPTHRENDERER = 2;
+        public static readonly STEP_GATHERRENDERTARGETS_POSTPROCESSRENDERPIPELINEMANAGER = 3;
 
         public static readonly STEP_REBUILDGEOMETRY_POSTPROCESSRENDERPIPELINEMANAGER = 0;