Browse Source

Making sure toSerialize acts as Set

When adding N meshes with the same parent, the parent would be added N
times. Now fixed.
I have also added withChildren to allow to serialize a parent together
with all of its children.
Raanan Weber 10 years ago
parent
commit
a2276097ac
2 changed files with 23 additions and 6 deletions
  1. 12 3
      src/Tools/babylon.sceneSerializer.js
  2. 11 3
      src/Tools/babylon.sceneSerializer.ts

+ 12 - 3
src/Tools/babylon.sceneSerializer.js

@@ -739,14 +739,23 @@ var BABYLON;
             }
             return serializationObject;
         };
-        SceneSerializer.SerializeMesh = function (toSerialize /* Mesh || Mesh[] */, withParents) {
+        SceneSerializer.SerializeMesh = function (toSerialize /* Mesh || Mesh[] */, withParents, withChildren) {
             if (withParents === void 0) { withParents = false; }
+            if (withChildren === void 0) { withChildren = false; }
             var serializationObject = {};
             toSerialize = (toSerialize instanceof Array) ? toSerialize : [toSerialize];
-            if (withParents) {
+            if (withParents || withChildren) {
                 //deliberate for loop! not for each, appended should be processed as well.
                 for (var i = 0; i < toSerialize.length; ++i) {
-                    if (toSerialize[i].parent) {
+                    if (withChildren) {
+                        toSerialize[i].getDescendants().forEach(function (node) {
+                            if (node instanceof BABYLON.Mesh && (toSerialize.indexOf(node) < 0)) {
+                                toSerialize.push(node);
+                            }
+                        });
+                    }
+                    //make sure the array doesn't contain the object already
+                    if (withParents && toSerialize[i].parent && (toSerialize.indexOf(toSerialize[i].parent) < 0)) {
                         toSerialize.push(toSerialize[i].parent);
                     }
                 }

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

@@ -903,15 +903,23 @@
             return serializationObject;
         }
 
-        public static SerializeMesh(toSerialize: any /* Mesh || Mesh[] */, withParents: boolean = false): any {
+        public static SerializeMesh(toSerialize: any /* Mesh || Mesh[] */, withParents: boolean = false, withChildren: boolean = false): any {
             var serializationObject: any = {};
 
             toSerialize = (toSerialize instanceof Array) ? toSerialize : [toSerialize];
 
-            if (withParents) {
+			if (withParents || withChildren) {
                 //deliberate for loop! not for each, appended should be processed as well.
                 for (var i = 0; i < toSerialize.length; ++i) {
-                    if (toSerialize[i].parent) {
+					if(withChildren) {
+						toSerialize[i].getDescendants().forEach((node) => {
+							if(node instanceof Mesh && (toSerialize.indexOf(node) < 0)) {
+								toSerialize.push(node);
+							}
+						});
+					}
+					//make sure the array doesn't contain the object already
+                    if (withParents && toSerialize[i].parent && (toSerialize.indexOf(toSerialize[i].parent) < 0)) {
                         toSerialize.push(toSerialize[i].parent);
                     }
                 }