Quellcode durchsuchen

implementing load assets in babylon file loader

Trevor Baron vor 7 Jahren
Ursprung
Commit
bbde59c573
3 geänderte Dateien mit 430 neuen und 1 gelöschten Zeilen
  1. 1 0
      Tools/Gulp/config.json
  2. 385 1
      src/Loading/Plugins/babylon.babylonFileLoader.ts
  3. 44 0
      src/babylon.scene.ts

+ 1 - 0
Tools/Gulp/config.json

@@ -194,6 +194,7 @@
                 "../../src/Rendering/babylon.renderingManager.js",
                 "../../src/Rendering/babylon.renderingGroup.js",
                 "../../src/babylon.scene.js",
+                "../../src/babylon.assetContainer.js",
                 "../../src/Mesh/babylon.buffer.js",
                 "../../src/Mesh/babylon.vertexBuffer.js",
                 "../../src/Materials/Textures/babylon.internalTexture.js",

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

@@ -626,7 +626,391 @@
         },
         loadAssets: (scene: Scene, data: string, rootUrl: string, onError?: (message: string, exception?: any) => void): SceneAssetContainer =>{
             var container = new SceneAssetContainer(scene);
-            // TODO
+
+            // Entire method running in try block, so ALWAYS logs as far as it got, only actually writes details
+            // when SceneLoader.debugLogging = true (default), or exception encountered.
+            // Everything stored in var log instead of writing separate lines to support only writing in exception,
+            // and avoid problems with multiple concurrent .babylon loads.
+            var log = "importScene has failed JSON parse";
+            try {
+                var parsedData = JSON.parse(data);
+                log = "";
+                var fullDetails = SceneLoader.loggingLevel === SceneLoader.DETAILED_LOGGING;               
+
+                var index: number;
+                var cache: number;
+                // Lights
+                if (parsedData.lights !== undefined && parsedData.lights !== null) {
+                    for (index = 0, cache = parsedData.lights.length; index < cache; index++) {
+                        var parsedLight = parsedData.lights[index];
+                        var light = Light.Parse(parsedLight, scene);
+                        if (light) {
+                            container.lights.push(light);
+                            log += (index === 0 ? "\n\tLights:" : "");
+                            log += "\n\t\t" + light.toString(fullDetails);
+                        }
+                    }
+                }
+
+                // Animations
+                if (parsedData.animations !== undefined && parsedData.animations !== null) {
+                    for (index = 0, cache = parsedData.animations.length; index < cache; index++) {
+                        var parsedAnimation = parsedData.animations[index];
+                        var animation = Animation.Parse(parsedAnimation);
+                        scene.animations.push(animation);
+                        container.animations.push(animation);
+                        log += (index === 0 ? "\n\tAnimations:" : "");
+                        log += "\n\t\t" + animation.toString(fullDetails);
+                    }
+                }
+
+                // Materials
+                if (parsedData.materials !== undefined && parsedData.materials !== null) {
+                    for (index = 0, cache = parsedData.materials.length; index < cache; index++) {
+                        var parsedMaterial = parsedData.materials[index];
+                        var mat = Material.Parse(parsedMaterial, scene, rootUrl);
+                        container.materials.push(mat);
+                        log += (index === 0 ? "\n\tMaterials:" : "");
+                        log += "\n\t\t" + mat.toString(fullDetails);
+                    }
+                }
+
+                if (parsedData.multiMaterials !== undefined && parsedData.multiMaterials !== null) {
+                    for (index = 0, cache = parsedData.multiMaterials.length; index < cache; index++) {
+                        var parsedMultiMaterial = parsedData.multiMaterials[index];
+                        var mmat = Material.ParseMultiMaterial(parsedMultiMaterial, scene);
+                        container.multiMaterials.push(mmat);
+                        log += (index === 0 ? "\n\tMultiMaterials:" : "");
+                        log += "\n\t\t" + mmat.toString(fullDetails);
+                    }
+                }
+
+                // Morph targets
+                if (parsedData.morphTargetManagers !== undefined && parsedData.morphTargetManagers !== null) {
+                    for (var managerData of parsedData.morphTargetManagers) {
+                        container.morphTargetManagers.push(MorphTargetManager.Parse(managerData, scene));
+                    }
+                }
+
+                // Skeletons
+                if (parsedData.skeletons !== undefined && parsedData.skeletons !== null) {
+                    for (index = 0, cache = parsedData.skeletons.length; index < cache; index++) {
+                        var parsedSkeleton = parsedData.skeletons[index];
+                        var skeleton = Skeleton.Parse(parsedSkeleton, scene);
+                        container.skeletons.push(skeleton);
+                        log += (index === 0 ? "\n\tSkeletons:" : "");
+                        log += "\n\t\t" + skeleton.toString(fullDetails);
+                    }
+                }
+
+                // Geometries
+                var geometries = parsedData.geometries;
+                if (geometries !== undefined && geometries !== null) {
+                    var addedGeometry = new Array<Nullable<Geometry>>();
+                    // Boxes
+                    var boxes = geometries.boxes;
+                    if (boxes !== undefined && boxes !== null) {
+                        for (index = 0, cache = boxes.length; index < cache; index++) {
+                            var parsedBox = boxes[index];
+                            addedGeometry.push(Geometry.Primitives.Box.Parse(parsedBox, scene));
+                        }
+                    }
+
+                    // Spheres
+                    var spheres = geometries.spheres;
+                    if (spheres !== undefined && spheres !== null) {
+                        for (index = 0, cache = spheres.length; index < cache; index++) {
+                            var parsedSphere = spheres[index];
+                            addedGeometry.push(Geometry.Primitives.Sphere.Parse(parsedSphere, scene));
+                        }
+                    }
+
+                    // Cylinders
+                    var cylinders = geometries.cylinders;
+                    if (cylinders !== undefined && cylinders !== null) {
+                        for (index = 0, cache = cylinders.length; index < cache; index++) {
+                            var parsedCylinder = cylinders[index];
+                            addedGeometry.push(Geometry.Primitives.Cylinder.Parse(parsedCylinder, scene));
+                        }
+                    }
+
+                    // Toruses
+                    var toruses = geometries.toruses;
+                    if (toruses !== undefined && toruses !== null) {
+                        for (index = 0, cache = toruses.length; index < cache; index++) {
+                            var parsedTorus = toruses[index];
+                            addedGeometry.push(Geometry.Primitives.Torus.Parse(parsedTorus, scene));
+                        }
+                    }
+
+                    // Grounds
+                    var grounds = geometries.grounds;
+                    if (grounds !== undefined && grounds !== null) {
+                        for (index = 0, cache = grounds.length; index < cache; index++) {
+                            var parsedGround = grounds[index];
+                            addedGeometry.push(Geometry.Primitives.Ground.Parse(parsedGround, scene));
+                        }
+                    }
+
+                    // Planes
+                    var planes = geometries.planes;
+                    if (planes !== undefined && planes !== null) {
+                        for (index = 0, cache = planes.length; index < cache; index++) {
+                            var parsedPlane = planes[index];
+                            addedGeometry.push(Geometry.Primitives.Plane.Parse(parsedPlane, scene));
+                        }
+                    }
+
+                    // TorusKnots
+                    var torusKnots = geometries.torusKnots;
+                    if (torusKnots !== undefined && torusKnots !== null) {
+                        for (index = 0, cache = torusKnots.length; index < cache; index++) {
+                            var parsedTorusKnot = torusKnots[index];
+                            addedGeometry.push(Geometry.Primitives.TorusKnot.Parse(parsedTorusKnot, scene));
+                        }
+                    }
+
+                    // VertexData
+                    var vertexData = geometries.vertexData;
+                    if (vertexData !== undefined && vertexData !== null) {
+                        for (index = 0, cache = vertexData.length; index < cache; index++) {
+                            var parsedVertexData = vertexData[index];
+                            addedGeometry.push(Geometry.Parse(parsedVertexData, scene, rootUrl));
+                        }
+                    }
+
+                    addedGeometry.forEach((g)=>{
+                        if(g){
+                            container.geometries.push(g);
+                        }
+                    })
+                }
+                
+                // Transform nodes
+                if (parsedData.transformNodes !== undefined && parsedData.transformNodes !== null) {
+                    for (index = 0, cache = parsedData.transformNodes.length; index < cache; index++) {
+                        var parsedTransformNode = parsedData.transformNodes[index];
+                        var node = TransformNode.Parse(parsedTransformNode, scene, rootUrl);
+                        container.transformNodes.push(node);
+                    }
+                }                
+
+                // Meshes
+                if (parsedData.meshes !== undefined && parsedData.meshes !== null) {
+                    for (index = 0, cache = parsedData.meshes.length; index < cache; index++) {
+                        var parsedMesh = parsedData.meshes[index];
+                        var mesh = <AbstractMesh>Mesh.Parse(parsedMesh, scene, rootUrl);
+                        container.meshes.push(mesh);
+                        log += (index === 0 ? "\n\tMeshes:" : "");
+                        log += "\n\t\t" + mesh.toString(fullDetails);
+                    }
+                }
+
+                // Cameras
+                if (parsedData.cameras !== undefined && parsedData.cameras !== null) {
+                    for (index = 0, cache = parsedData.cameras.length; index < cache; index++) {
+                        var parsedCamera = parsedData.cameras[index];
+                        var camera = Camera.Parse(parsedCamera, scene);
+                        container.cameras.push(camera);
+                        log += (index === 0 ? "\n\tCameras:" : "");
+                        log += "\n\t\t" + camera.toString(fullDetails);
+                    }
+                }
+
+                // Browsing all the graph to connect the dots
+                for (index = 0, cache = scene.cameras.length; index < cache; index++) {
+                    var camera = scene.cameras[index];
+                    if (camera._waitingParentId) {
+                        camera.parent = scene.getLastEntryByID(camera._waitingParentId);
+                        camera._waitingParentId = null;
+                    }
+                }
+
+                for (index = 0, cache = scene.lights.length; index < cache; index++) {
+                    let light = scene.lights[index];
+                    if (light && light._waitingParentId) {
+                        light.parent = scene.getLastEntryByID(light._waitingParentId);
+                        light._waitingParentId = null;
+                    }
+                }
+
+                // Sounds
+                // TODO: add sound
+                var loadedSounds: Sound[] = [];
+                var loadedSound: Sound;
+                if (AudioEngine && parsedData.sounds !== undefined && parsedData.sounds !== null) {
+                    for (index = 0, cache = parsedData.sounds.length; index < cache; index++) {
+                        var parsedSound = parsedData.sounds[index];
+                        if (Engine.audioEngine.canUseWebAudio) {
+                            if (!parsedSound.url) parsedSound.url = parsedSound.name;
+                            if (!loadedSounds[parsedSound.url]) {
+                                loadedSound = Sound.Parse(parsedSound, scene, rootUrl);
+                                loadedSounds[parsedSound.url] = loadedSound;
+                            }
+                            else {
+                                Sound.Parse(parsedSound, scene, rootUrl, loadedSounds[parsedSound.url]);
+                            }
+                        } else {
+                            new Sound(parsedSound.name, null, scene);
+                        }
+                    }
+                }
+
+                loadedSounds = [];
+
+                // Connect parents & children and parse actions
+                for (index = 0, cache = scene.transformNodes.length; index < cache; index++) {
+                    var transformNode = scene.transformNodes[index];
+                    if (transformNode._waitingParentId) {
+                        transformNode.parent = scene.getLastEntryByID(transformNode._waitingParentId);
+                        transformNode._waitingParentId = null;
+                    }
+                }                
+                for (index = 0, cache = scene.meshes.length; index < cache; index++) {
+                    var mesh = scene.meshes[index];
+                    if (mesh._waitingParentId) {
+                        mesh.parent = scene.getLastEntryByID(mesh._waitingParentId);
+                        mesh._waitingParentId = null;
+                    }
+                    if (mesh._waitingActions) {
+                        ActionManager.Parse(mesh._waitingActions, mesh, scene);
+                        mesh._waitingActions = null;
+                    }
+                }
+
+                // freeze world matrix application
+                for (index = 0, cache = scene.meshes.length; index < cache; index++) {
+                    var currentMesh = scene.meshes[index];
+                    if (currentMesh._waitingFreezeWorldMatrix) {
+                        currentMesh.freezeWorldMatrix();
+                        currentMesh._waitingFreezeWorldMatrix = null;
+                    } else {
+                        currentMesh.computeWorldMatrix(true);
+                    }
+                }
+
+                // Particles Systems
+                if (parsedData.particleSystems !== undefined && parsedData.particleSystems !== null) {
+                    for (index = 0, cache = parsedData.particleSystems.length; index < cache; index++) {
+                        var parsedParticleSystem = parsedData.particleSystems[index];
+                        var ps = ParticleSystem.Parse(parsedParticleSystem, scene, rootUrl);
+                        container.particleSystems.push(ps);
+                    }
+                }
+
+                // Lens flares
+                if (parsedData.lensFlareSystems !== undefined && parsedData.lensFlareSystems !== null) {
+                    for (index = 0, cache = parsedData.lensFlareSystems.length; index < cache; index++) {
+                        var parsedLensFlareSystem = parsedData.lensFlareSystems[index];
+                        var lf = LensFlareSystem.Parse(parsedLensFlareSystem, scene, rootUrl);
+                        container.lensFlareSystems.push(lf);
+                    }
+                }
+
+                // Shadows
+                if (parsedData.shadowGenerators !== undefined && parsedData.shadowGenerators !== null) {
+                    for (index = 0, cache = parsedData.shadowGenerators.length; index < cache; index++) {
+                        var parsedShadowGenerator = parsedData.shadowGenerators[index];
+                        var sg = ShadowGenerator.Parse(parsedShadowGenerator, scene);
+                        container.shadowGenerators.push(sg);
+                    }
+                }
+
+                // Lights exclusions / inclusions
+                for (index = 0, cache = scene.lights.length; index < cache; index++) {
+                    let light = scene.lights[index];
+                    // Excluded check
+                    if (light._excludedMeshesIds.length > 0) {
+                        for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
+                            var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
+
+                            if (excludedMesh) {
+                                light.excludedMeshes.push(excludedMesh);
+                            }
+                        }
+
+                        light._excludedMeshesIds = [];
+                    }
+
+                    // Included check
+                    if (light._includedOnlyMeshesIds.length > 0) {
+                        for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
+                            var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
+
+                            if (includedOnlyMesh) {
+                                light.includedOnlyMeshes.push(includedOnlyMesh);
+                            }
+                        }
+
+                        light._includedOnlyMeshesIds = [];
+                    }
+                }
+
+                // Actions (scene)
+                if (parsedData.actions !== undefined && parsedData.actions !== null) {
+                    ActionManager.Parse(parsedData.actions, null, scene);
+                }
+                
+                container.cameras.forEach((o)=>{
+                    scene.removeCamera(o);
+                });
+                container.lights.forEach((o)=>{
+                    scene.removeLight(o);
+                });
+                container.meshes.forEach((o)=>{
+                    scene.removeMesh(o);
+                });
+                container.skeletons.forEach((o)=>{
+                    scene.removeSkeleton(o);
+                });
+                container.particleSystems.forEach((o)=>{
+                    scene.removeParticleSystem(o);
+                });
+                container.particleSystems.forEach((o)=>{
+                    scene.removeParticleSystem(o);
+                });
+                container.animations.forEach((o)=>{
+                    scene.removeAnimation(o);
+                });
+                container.multiMaterials.forEach((o)=>{
+                    scene.removeMultiMaterial(o);
+                });
+                container.materials.forEach((o)=>{
+                    scene.removeMaterial(o);
+                });
+                container.morphTargetManagers.forEach((o)=>{
+                    scene.removeMorphTargetManager(o);
+                });
+                container.geometries.forEach((o)=>{
+                    scene.removeGeometry(o);
+                });
+                container.transformNodes.forEach((o)=>{
+                    scene.removeTransformNode(o);
+                });
+                container.lensFlareSystems.forEach((o)=>{
+                    scene.removeLensFlareSystem(o);
+                });
+                container.actionManagers.forEach((o)=>{
+                    scene.removeActionManager(o);
+                });
+                // TODO, do shadow generators need to be removed somehow?
+
+                return container;
+
+            } catch (err) {
+                let msg = logOperation("importScene", parsedData ? parsedData.producer : "Unknown") + log;
+                if (onError) {
+                    onError(msg, err);
+                } else {
+                    Tools.Log(msg);
+                    throw err;
+                }
+            } finally {
+                if (log !== null && SceneLoader.loggingLevel !== SceneLoader.NO_LOGGING) {
+                    Tools.Log(logOperation("importScene", parsedData ? parsedData.producer : "Unknown") + (SceneLoader.loggingLevel !== SceneLoader.MINIMAL_LOGGING ? log : ""));
+                }
+            }
+
             return container;
         }
     });

+ 44 - 0
src/babylon.scene.ts

@@ -2315,6 +2315,50 @@
             return index;
         }
 
+        
+        public removeParticleSystem(toRemove: ParticleSystem): number {
+            var index = this.particleSystems.indexOf(toRemove);
+            if (index !== -1) {
+                this.particleSystems.splice(index, 1);
+            }
+            return index;
+        };
+        public removeAnimation(toRemove: Animation): number {
+            var index = this.animations.indexOf(toRemove);
+            if (index !== -1) {
+                this.animations.splice(index, 1);
+            }
+            return index;
+        };
+        public removeMultiMaterial(toRemove: MultiMaterial): number {
+            var index = this.multiMaterials.indexOf(toRemove);
+            if (index !== -1) {
+                this.multiMaterials.splice(index, 1);
+            }
+            return index;
+        };
+        public removeMaterial(toRemove: Material): number {
+            var index = this.materials.indexOf(toRemove);
+            if (index !== -1) {
+                this.materials.splice(index, 1);
+            }
+            return index;
+        };
+        public removeLensFlareSystem(toRemove:LensFlareSystem){
+            var index = this.lensFlareSystems.indexOf(toRemove);
+            if (index !== -1) {
+                this.lensFlareSystems.splice(index, 1);
+            }
+            return index;
+        };
+        public removeActionManager(toRemove:ActionManager){
+            var index = this._actionManagers.indexOf(toRemove);
+            if (index !== -1) {
+                this._actionManagers.splice(index, 1);
+            }
+            return index;
+        };
+
         public addLight(newLight: Light) {
             this.lights.push(newLight);
             this.sortLightsByPriority();