Browse Source

Merge pull request #583 from RaananW/importer

SerializeMesh for single meshes and import warnings
Raanan Weber 10 years ago
parent
commit
d96999bf07

+ 14 - 13
src/Loading/Plugins/babylon.babylonFileLoader.js

@@ -677,7 +677,6 @@ var BABYLON;
                 }
                 var effectiveTarget = propertyPath.split(".");
                 var values = value.split(",");
-                // Get effective Target
                 for (var i = 0; i < effectiveTarget.length; i++) {
                     target = target[effectiveTarget[i]];
                 }
@@ -780,7 +779,6 @@ var BABYLON;
                 for (var i = 0; i < parsedAction.children.length; i++)
                     traverse(parsedAction.children[i], trigger, condition, newAction, null);
             };
-            // triggers
             for (var i = 0; i < parsedActions.children.length; i++) {
                 var triggerParams;
                 var trigger = parsedActions.children[i];
@@ -801,14 +799,19 @@ var BABYLON;
             var soundName = parsedSound.name;
             var soundUrl = rootUrl + soundName;
             var options = {
-                autoplay: parsedSound.autoplay, loop: parsedSound.loop, volume: parsedSound.volume,
-                spatialSound: parsedSound.spatialSound, maxDistance: parsedSound.maxDistance,
+                autoplay: parsedSound.autoplay,
+                loop: parsedSound.loop,
+                volume: parsedSound.volume,
+                spatialSound: parsedSound.spatialSound,
+                maxDistance: parsedSound.maxDistance,
                 rolloffFactor: parsedSound.rolloffFactor,
                 refDistance: parsedSound.refDistance,
                 distanceModel: parsedSound.distanceModel,
                 playbackRate: parsedSound.playbackRate
             };
-            var newSound = new BABYLON.Sound(soundName, soundUrl, scene, function () { scene._removePendingData(newSound); }, options);
+            var newSound = new BABYLON.Sound(soundName, soundUrl, scene, function () {
+                scene._removePendingData(newSound);
+            }, options);
             scene._addPendingData(newSound);
             if (parsedSound.position) {
                 var soundPosition = BABYLON.Vector3.FromArray(parsedSound.position);
@@ -1055,6 +1058,9 @@ var BABYLON;
                                         });
                                     }
                                 });
+                                if (!found) {
+                                    BABYLON.Tools.Warn("Geometry not found for mesh " + parsedMesh.id);
+                                }
                             }
                         }
                         // Material ?
@@ -1078,7 +1084,9 @@ var BABYLON;
                             }
                             if (!materialFound) {
                                 loadedMaterialsIds.push(parsedMesh.materialId);
-                                parseMaterialById(parsedMesh.materialId, parsedData, scene, rootUrl);
+                                if (!parseMaterialById(parsedMesh.materialId, parsedData, scene, rootUrl)) {
+                                    BABYLON.Tools.Warn("Material not found for mesh " + parsedMesh.id);
+                                }
                             }
                         }
                         // Skeleton ?
@@ -1098,7 +1106,6 @@ var BABYLON;
                         meshes.push(mesh);
                     }
                 }
-                // Connecting parents
                 for (index = 0; index < scene.meshes.length; index++) {
                     var currentMesh = scene.meshes[index];
                     if (currentMesh._waitingParentId) {
@@ -1133,7 +1140,6 @@ var BABYLON;
                     scene.fogEnd = parsedData.fogEnd;
                     scene.fogDensity = parsedData.fogDensity;
                 }
-                // Lights
                 for (var index = 0; index < parsedData.lights.length; index++) {
                     var parsedLight = parsedData.lights[index];
                     parseLight(parsedLight, scene);
@@ -1226,12 +1232,10 @@ var BABYLON;
                         }
                     }
                 }
-                // Meshes
                 for (index = 0; index < parsedData.meshes.length; index++) {
                     var parsedMesh = parsedData.meshes[index];
                     parseMesh(parsedMesh, scene, rootUrl);
                 }
-                // Cameras
                 for (index = 0; index < parsedData.cameras.length; index++) {
                     var parsedCamera = parsedData.cameras[index];
                     parseCamera(parsedCamera, scene);
@@ -1239,7 +1243,6 @@ var BABYLON;
                 if (parsedData.activeCameraID) {
                     scene.setActiveCameraByID(parsedData.activeCameraID);
                 }
-                // Browsing all the graph to connect the dots
                 for (index = 0; index < scene.cameras.length; index++) {
                     var camera = scene.cameras[index];
                     if (camera._waitingParentId) {
@@ -1266,7 +1269,6 @@ var BABYLON;
                         }
                     }
                 }
-                // Connect parents & children and parse actions
                 for (index = 0; index < scene.meshes.length; index++) {
                     var mesh = scene.meshes[index];
                     if (mesh._waitingParentId) {
@@ -1309,4 +1311,3 @@ var BABYLON;
         });
     })(Internals = BABYLON.Internals || (BABYLON.Internals = {}));
 })(BABYLON || (BABYLON = {}));
-//# sourceMappingURL=babylon.babylonFileLoader.js.map

+ 6 - 1
src/Loading/Plugins/babylon.babylonFileLoader.ts

@@ -1293,6 +1293,9 @@
                                     
                                 }
                             });
+							if(!found) {
+								Tools.Warn("Geometry not found for mesh " + parsedMesh.id);
+							}
                         }
                     }
 
@@ -1320,7 +1323,9 @@
 
                         if (!materialFound) {
                             loadedMaterialsIds.push(parsedMesh.materialId);
-                            parseMaterialById(parsedMesh.materialId, parsedData, scene, rootUrl);
+							if(!parseMaterialById(parsedMesh.materialId, parsedData, scene, rootUrl)) {
+								Tools.Warn("Material not found for mesh " + parsedMesh.id);
+							}
                         }
                     }
 

+ 41 - 2
src/Tools/babylon.sceneSerializer.js

@@ -524,7 +524,7 @@ var BABYLON;
             var geometryId = geometry.id;
             serializationObject.geometryId = geometryId;
             if (!mesh.getScene().getGeometryByID(geometryId)) {
-                // geometry was in the memory but not added to the scene, nevertheless it's better to serialize too be able to reload the mesh with its geometry
+                // geometry was in the memory but not added to the scene, nevertheless it's better to serialize to be able to reload the mesh with its geometry
                 serializeGeometry(geometry, serializationScene.geometries);
             }
             // SubMeshes
@@ -686,8 +686,47 @@ var BABYLON;
             }
             return serializationObject;
         };
+        SceneSerializer.SerializeMesh = function (mesh) {
+            var serializationObject = {};
+            //only works if the mesh is already loaded
+            if (mesh.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_LOADED || mesh.delayLoadState === BABYLON.Engine.DELAYLOADSTATE_NONE) {
+                //serialize material
+                if (mesh.material) {
+                    if (mesh.material instanceof BABYLON.StandardMaterial) {
+                        serializationObject.materials = [];
+                        serializationObject.materials.push(serializeMaterial(mesh.material));
+                    }
+                    else if (mesh.material instanceof BABYLON.MultiMaterial) {
+                        serializationObject.multiMaterials = [];
+                        serializationObject.multiMaterials.push(serializeMultiMaterial(mesh.material));
+                    }
+                }
+                //serialize geometry
+                var geometry = mesh._geometry;
+                if (geometry) {
+                    serializationObject.geometries = {};
+                    serializationObject.geometries.boxes = [];
+                    serializationObject.geometries.spheres = [];
+                    serializationObject.geometries.cylinders = [];
+                    serializationObject.geometries.toruses = [];
+                    serializationObject.geometries.grounds = [];
+                    serializationObject.geometries.planes = [];
+                    serializationObject.geometries.torusKnots = [];
+                    serializationObject.geometries.vertexData = [];
+                    serializeGeometry(geometry, serializationObject.geometries);
+                }
+                // Skeletons
+                if (mesh.skeleton) {
+                    serializationObject.skeletons = [];
+                    serializationObject.skeletons.push(serializeSkeleton(mesh.skeleton));
+                }
+                //serialize the actual mesh
+                serializationObject.meshes = [];
+                serializationObject.meshes.push(serializeMesh(mesh, serializationObject));
+            }
+            return serializationObject;
+        };
         return SceneSerializer;
     })();
     BABYLON.SceneSerializer = SceneSerializer;
 })(BABYLON || (BABYLON = {}));
-//# sourceMappingURL=babylon.sceneSerializer.js.map

+ 46 - 1
src/Tools/babylon.sceneSerializer.ts

@@ -651,7 +651,7 @@
             serializationObject.geometryId = geometryId;
 
             if (!mesh.getScene().getGeometryByID(geometryId)) {
-                // geometry was in the memory but not added to the scene, nevertheless it's better to serialize too be able to reload the mesh with its geometry
+                // geometry was in the memory but not added to the scene, nevertheless it's better to serialize to be able to reload the mesh with its geometry
                 serializeGeometry(geometry, serializationScene.geometries);
             }
 
@@ -842,5 +842,50 @@
 
             return serializationObject;
         }
+		
+		public static SerializeMesh(mesh: Mesh) : any {
+			var serializationObject: any = {};
+			
+			//only works if the mesh is already loaded
+			if (mesh.delayLoadState === Engine.DELAYLOADSTATE_LOADED || mesh.delayLoadState === Engine.DELAYLOADSTATE_NONE) {
+				//serialize material
+				if(mesh.material) {
+					if (mesh.material instanceof StandardMaterial) {
+						serializationObject.materials = [];
+						serializationObject.materials.push(serializeMaterial(<StandardMaterial>mesh.material));
+					} else if (mesh.material instanceof MultiMaterial) {
+						serializationObject.multiMaterials = [];
+						serializationObject.multiMaterials.push(serializeMultiMaterial(<MultiMaterial>mesh.material));
+					}
+				}
+				//serialize geometry
+				var geometry = mesh._geometry;
+				if (geometry) {
+					serializationObject.geometries = {};
+				
+					serializationObject.geometries.boxes = [];
+					serializationObject.geometries.spheres = [];
+					serializationObject.geometries.cylinders = [];
+					serializationObject.geometries.toruses = [];
+					serializationObject.geometries.grounds = [];
+					serializationObject.geometries.planes = [];
+					serializationObject.geometries.torusKnots = [];
+					serializationObject.geometries.vertexData = [];
+				
+					serializeGeometry(geometry, serializationObject.geometries);
+				}
+				// Skeletons
+				if (mesh.skeleton) {
+					serializationObject.skeletons = [];
+					serializationObject.skeletons.push(serializeSkeleton(mesh.skeleton));
+				}
+				
+				//serialize the actual mesh
+				serializationObject.meshes = [];
+				serializationObject.meshes.push(serializeMesh(mesh, serializationObject));
+			}
+			
+			return serializationObject;
+		}
     }
 }