浏览代码

Added support for shadowgenerators for Delta file

David `Deltakosh` Catuhe 5 年之前
父节点
当前提交
e350f8f768
共有 2 个文件被更改,包括 29 次插入2 次删除
  1. 11 0
      src/Lights/Shadows/shadowGenerator.ts
  2. 18 2
      src/Misc/sceneRecorder.ts

+ 11 - 0
src/Lights/Shadows/shadowGenerator.ts

@@ -66,6 +66,8 @@ export interface ICustomShaderOptions {
  * Interface to implement to create a shadow generator compatible with BJS.
  */
 export interface IShadowGenerator {
+    /** Gets or set the id of the shadow generator. It will be the one from the light if not defined */
+    id: string;
     /**
      * Gets the main RTT containing the shadow map (usually storing depth from the light point of view).
      * @returns The render target texture if present otherwise, null
@@ -218,6 +220,9 @@ export class ShadowGenerator implements IShadowGenerator {
      */
     public static readonly QUALITY_LOW = 2;
 
+    /** Gets or set the id of the shadow generator. It will be the one from the light if not defined */
+    public id: string;
+
     /** Gets or sets the custom shader name to use */
     public customShaderOptions: ICustomShaderOptions;
 
@@ -816,6 +821,7 @@ export class ShadowGenerator implements IShadowGenerator {
         this._light = light;
         this._scene = light.getScene();
         light._shadowGenerator = this;
+        this.id = light.id;
 
         ShadowGenerator._SceneComponentInitialization(this._scene);
 
@@ -1728,6 +1734,7 @@ export class ShadowGenerator implements IShadowGenerator {
 
         serializationObject.className = this.getClassName();
         serializationObject.lightId = this._light.id;
+        serializationObject.id = this._light.id;
         serializationObject.mapSize = shadowMap.getRenderSize();
         serializationObject.forceBackFacesOnly = this.forceBackFacesOnly;
         serializationObject.darkness = this.getDarkness();
@@ -1787,6 +1794,10 @@ export class ShadowGenerator implements IShadowGenerator {
             });
         }
 
+        if (parsedShadowGenerator.id !== undefined) {
+            shadowGenerator.id = parsedShadowGenerator.id;
+        }
+
         shadowGenerator.forceBackFacesOnly = !!parsedShadowGenerator.forceBackFacesOnly;
 
         if (parsedShadowGenerator.darkness !== undefined) {

+ 18 - 2
src/Misc/sceneRecorder.ts

@@ -10,6 +10,7 @@ import { MultiMaterial } from '../Materials/multiMaterial';
 import { TransformNode } from '../Meshes/transformNode';
 import { ParticleSystem } from '../Particles/particleSystem';
 import { MorphTargetManager } from '../Morph/morphTargetManager';
+import { ShadowGenerator } from '../Lights/Shadows/shadowGenerator';
 
 /**
  * Class used to record delta files between 2 scene states
@@ -173,6 +174,18 @@ export class SceneRecorder {
         }
     }
 
+    private static GetShadowGeneratorById(scene: Scene, id: string) {
+        var generators = scene.lights.map(l => l.getShadowGenerator());
+
+        for (var generator of generators) {
+            if (generator && generator.id === id) {
+                return generator;
+            } 
+        }
+
+        return null;
+    }
+
     /**
      * Apply a given delta to a given scene
      * @param deltaJSON defines the JSON containing the delta
@@ -180,7 +193,7 @@ export class SceneRecorder {
      */
     public static ApplyDelta(deltaJSON: any | string, scene: Scene) {
 
-        if (deltaJSON.toString) {
+        if (typeof deltaJSON === 'string') {
             deltaJSON = JSON.parse(deltaJSON);
         }
 
@@ -190,13 +203,16 @@ export class SceneRecorder {
             var source = deltaJSON[prop];
             var property = anyScene[prop];
 
-            if (Array.isArray(property)) { // Restore array
+            if (Array.isArray(property) || prop === "shadowGenerators") { // Restore array
                 switch (prop) {
                     case "cameras":
                         this._ApplyDeltaForEntity(source, scene, scene.getCameraByID.bind(scene), (data) => Camera.Parse(data, scene));
                         break;
                     case "lights":
                         this._ApplyDeltaForEntity(source, scene, scene.getLightByID.bind(scene), (data) => Light.Parse(data, scene));
+                        break;                        
+                    case "shadowGenerators":
+                        this._ApplyDeltaForEntity(source, scene, id => this.GetShadowGeneratorById(scene, id), (data) => ShadowGenerator.Parse(data, scene));
                         break;
                     case "meshes":
                         this._ApplyDeltaForEntity(source, scene, scene.getMeshByID.bind(scene), (data) => Mesh.Parse(data, scene, ""));