コード例 #1
0
    public static async Task Load(GameObject gameObjectLoader, string url, Action callback, Action <GameObject> onInitializeGltfObject)
    {
        ImporterFactory Factory       = null;
        bool            UseStream     = true;
        string          GLTFUri       = url;
        bool            Multithreaded = true;
        int             MaximumLod    = 300;
        int             Timeout       = 8;

        var importOptions = new ImportOptions
        {
            AsyncCoroutineHelper = gameObjectLoader.GetComponent <AsyncCoroutineHelper>() ?? gameObjectLoader.AddComponent <AsyncCoroutineHelper>()
        };

        GLTFSceneImporter sceneImporter = null;
        var cancelToken = new CancellationTokenSource();

        try
        {
            Factory = Factory ?? ScriptableObject.CreateInstance <DefaultImporterFactory>();
            if (UseStream)
            {
                var    fullPath      = GLTFUri;
                string directoryPath = URIHelper.GetDirectoryName(fullPath);
                importOptions.DataLoader = new FileLoader(directoryPath);
                sceneImporter            = Factory.CreateSceneImporter(Path.GetFileName(GLTFUri), importOptions);
            }
            else
            {
                string directoryPath = URIHelper.GetDirectoryName(GLTFUri);
                importOptions.DataLoader = new WebRequestLoader(directoryPath);
                sceneImporter            = Factory.CreateSceneImporter(URIHelper.GetFileFromUri(new Uri(GLTFUri)), importOptions);
            }
            sceneImporter.SceneParent            = gameObjectLoader.transform;
            sceneImporter.MaximumLod             = MaximumLod;
            sceneImporter.Timeout                = Timeout;
            sceneImporter.IsMultithreaded        = Multithreaded;
            sceneImporter.OnInitializeGltfObject = onInitializeGltfObject;
            mTasks.Add(cancelToken);
            await sceneImporter.LoadSceneAsync(-1, true, null, cancelToken.Token);
        }
        finally
        {
            if (importOptions.DataLoader != null)
            {
                if (sceneImporter != null)
                {
                    mCreatedScenes.Add(sceneImporter.CreatedObject);
                    if (cancelToken.IsCancellationRequested)
                    {
                        sceneImporter.CreatedObject.DestroySelf();
                    }
                    sceneImporter.Dispose();
                    sceneImporter = null;
                }
                importOptions.DataLoader = null;
            }
            callback.InvokeGracefully();
        }
    }
コード例 #2
0
    private IEnumerator LoadGLB(byte[] bytes)
    {
        // Define the publiclyVisible=true, otherwise will get an error.
        // https://stackoverflow.com/questions/1646193/why-does-memorystream-getbuffer-always-throw

        Stream stream = new MemoryStream(bytes, 0, bytes.Length, false, true);

        GLTFRoot gLTFRoot;

        GLTFParser.ParseJson(stream, out gLTFRoot);

        Debug.Log("Successfully parsed INIT packet!");

        GLTFSceneImporter loader = new GLTFSceneImporter(gLTFRoot, null, null, stream);

        loader.Collider        = GLTFSceneImporter.ColliderType.Box;
        loader.IsMultithreaded = false;

        loader.LoadSceneAsync().Wait();

        GameObject obj = loader.LastLoadedScene;

        obj.transform.parent = hyperverseRoot.transform;

        Debug.Log("Loaded scene from INIT packet!");

        // TEST:
        ResetSceneGraph();

        yield return(null);
    }
コード例 #3
0
    private void Load(Stream stream)
    {
        GLTFRoot gLTFRoot;

        GLTFParser.ParseJson(stream, out gLTFRoot);
        var loader = new GLTFSceneImporter(gLTFRoot, null, null, stream);

        loader.LoadSceneAsync();
    }
コード例 #4
0
        async void LoadScene(int SceneIndex)
        {
            foreach (Transform child in transform)
            {
                GameObject.Destroy(child.gameObject);
            }

            _importer = new GLTFSceneImporter(
                _fileName,
                _importOptions
                );

            _importer.SceneParent = gameObject.transform;
            await _importer.LoadSceneAsync(SceneIndex);
        }
コード例 #5
0
        private async Task <IList <UnityEngine.Object> > LoadGltfFromStream(WebRequestLoader loader, Stream stream, ColliderType colliderType)
        {
            var assets = new List <UnityEngine.Object>(30);

            // pre-parse glTF document so we can get a scene count
            // run this on a threadpool thread so that the Unity main thread is not blocked
            GLTF.Schema.GLTFRoot gltfRoot = null;
            try
            {
                await Task.Run(() =>
                {
                    GLTF.GLTFParser.ParseJson(stream, out gltfRoot);
                });
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
            if (gltfRoot == null)
            {
                throw new GLTFLoadException("Failed to parse glTF");
            }
            stream.Position = 0;

            using (GLTFSceneImporter importer =
                       MREAPI.AppsAPI.GLTFImporterFactory.CreateImporter(gltfRoot, loader, _asyncHelper, stream))
            {
                importer.SceneParent = MREAPI.AppsAPI.AssetCache.CacheRootGO.transform;
                importer.Collider    = colliderType.ToGLTFColliderType();

                // load textures
                if (gltfRoot.Textures != null)
                {
                    for (var i = 0; i < gltfRoot.Textures.Count; i++)
                    {
                        await importer.LoadTextureAsync(gltfRoot.Textures[i], i, true);

                        var texture = importer.GetTexture(i);
                        texture.name = gltfRoot.Textures[i].Name ?? $"texture:{i}";
                        assets.Add(texture);
                    }
                }

                // load meshes
                if (gltfRoot.Meshes != null)
                {
                    var cancellationSource = new System.Threading.CancellationTokenSource();
                    for (var i = 0; i < gltfRoot.Meshes.Count; i++)
                    {
                        var mesh = await importer.LoadMeshAsync(i, cancellationSource.Token);

                        mesh.name = gltfRoot.Meshes[i].Name ?? $"mesh:{i}";
                        assets.Add(mesh);
                    }
                }

                // load materials
                if (gltfRoot.Materials != null)
                {
                    for (var i = 0; i < gltfRoot.Materials.Count; i++)
                    {
                        var matdef   = gltfRoot.Materials[i];
                        var material = await importer.LoadMaterialAsync(i);

                        material.name = matdef.Name ?? $"material:{i}";
                        assets.Add(material);
                    }
                }

                // load prefabs
                if (gltfRoot.Scenes != null)
                {
                    for (var i = 0; i < gltfRoot.Scenes.Count; i++)
                    {
                        await importer.LoadSceneAsync(i).ConfigureAwait(true);

                        GameObject rootObject = importer.LastLoadedScene;
                        rootObject.name = gltfRoot.Scenes[i].Name ?? $"scene:{i}";

                        var animation = rootObject.GetComponent <UnityEngine.Animation>();
                        if (animation != null)
                        {
                            animation.playAutomatically = false;

                            // initialize mapping so we know which gameobjects are targeted by which animation clips
                            var mapping = rootObject.AddComponent <PrefabAnimationTargets>();
                            mapping.Initialize(gltfRoot, i);
                        }

                        MWGOTreeWalker.VisitTree(rootObject, (go) =>
                        {
                            go.layer = MREAPI.AppsAPI.LayerApplicator.DefaultLayer;
                        });

                        assets.Add(rootObject);
                    }
                }
            }

            return(assets);
        }
コード例 #6
0
        private async Task <IList <Asset> > LoadAssetsFromGLTF(AssetSource source, Guid containerId, ColliderType colliderType)
        {
            IList <Asset>      assets        = new List <Asset>();
            DeterministicGuids guidGenerator = new DeterministicGuids(UtilMethods.StringToGuid(
                                                                          $"{containerId}:{source.ParsedUri.AbsoluteUri}"));

            // download file
            UtilMethods.GetUrlParts(source.ParsedUri.AbsoluteUri, out string rootUrl, out string filename);
            var loader = new WebRequestLoader(rootUrl);
            await loader.LoadStream(filename);

            // pre-parse glTF document so we can get a scene count
            // TODO: run this in thread
            GLTF.GLTFParser.ParseJson(loader.LoadedStream, out GLTF.Schema.GLTFRoot gltfRoot);

            GLTFSceneImporter importer =
                MREAPI.AppsAPI.GLTFImporterFactory.CreateImporter(gltfRoot, loader, _asyncHelper, loader.LoadedStream);

            importer.SceneParent = MREAPI.AppsAPI.AssetCache.CacheRootGO().transform;
            importer.Collider    = colliderType.ToGLTFColliderType();

            // load prefabs
            if (gltfRoot.Scenes != null)
            {
                for (var i = 0; i < gltfRoot.Scenes.Count; i++)
                {
                    await importer.LoadSceneAsync(i);

                    GameObject rootObject = importer.LastLoadedScene;
                    rootObject.name = gltfRoot.Scenes[i].Name ?? $"scene:{i}";
                    MWGOTreeWalker.VisitTree(rootObject, (go) =>
                    {
                        go.layer = UnityConstants.ActorLayerIndex;
                    });

                    var def = GenerateAssetPatch(rootObject, guidGenerator.Next());
                    def.Name   = rootObject.name;
                    def.Source = new AssetSource(source.ContainerType, source.Uri, $"scene:{i}");
                    MREAPI.AppsAPI.AssetCache.CacheAsset(rootObject, def.Id, containerId, source);
                    assets.Add(def);
                }
            }

            // load textures
            if (gltfRoot.Textures != null)
            {
                for (var i = 0; i < gltfRoot.Textures.Count; i++)
                {
                    await importer.LoadTextureAsync(gltfRoot.Textures[i], i, true);

                    var texture = importer.GetTexture(i);
                    texture.name = gltfRoot.Textures[i].Name ?? $"texture:{i}";

                    var asset = GenerateAssetPatch(texture, guidGenerator.Next());
                    asset.Name   = texture.name;
                    asset.Source = new AssetSource(source.ContainerType, source.Uri, $"texture:{i}");
                    MREAPI.AppsAPI.AssetCache.CacheAsset(texture, asset.Id, containerId, source);
                    assets.Add(asset);
                }
            }

            // load materials
            if (gltfRoot.Materials != null)
            {
                for (var i = 0; i < gltfRoot.Materials.Count; i++)
                {
                    var matdef   = gltfRoot.Materials[i];
                    var material = await importer.LoadMaterialAsync(i);

                    material.name = matdef.Name ?? $"material:{i}";

                    var asset = GenerateAssetPatch(material, guidGenerator.Next());
                    asset.Name   = material.name;
                    asset.Source = new AssetSource(source.ContainerType, source.Uri, $"material:{i}");
                    MREAPI.AppsAPI.AssetCache.CacheAsset(material, asset.Id, containerId, source);
                    assets.Add(asset);
                }
            }

            importer.Dispose();

            return(assets);
        }
コード例 #7
0
        private async Task <IList <Asset> > LoadAssetsFromGLTF(AssetSource source, Guid containerId, ColliderType colliderType)
        {
            IList <Asset>      assets        = new List <Asset>();
            DeterministicGuids guidGenerator = new DeterministicGuids(UtilMethods.StringToGuid(
                                                                          $"{containerId}:{source.ParsedUri.AbsoluteUri}"));

            // download file
            var rootUrl = URIHelper.GetDirectoryName(source.ParsedUri.AbsoluteUri);
            var loader  = new WebRequestLoader(rootUrl);
            var stream  = await loader.LoadStreamAsync(URIHelper.GetFileFromUri(source.ParsedUri));

            // pre-parse glTF document so we can get a scene count
            // TODO: run this in thread
            GLTF.GLTFParser.ParseJson(stream, out GLTF.Schema.GLTFRoot gltfRoot);
            stream.Position = 0;

            GLTFSceneImporter importer =
                MREAPI.AppsAPI.GLTFImporterFactory.CreateImporter(gltfRoot, loader, _asyncHelper, stream);

            importer.SceneParent = MREAPI.AppsAPI.AssetCache.CacheRootGO().transform;
            importer.Collider    = colliderType.ToGLTFColliderType();

            // load textures
            if (gltfRoot.Textures != null)
            {
                for (var i = 0; i < gltfRoot.Textures.Count; i++)
                {
                    await importer.LoadTextureAsync(gltfRoot.Textures[i], i, true);

                    var texture = importer.GetTexture(i);
                    texture.name = gltfRoot.Textures[i].Name ?? $"texture:{i}";

                    var asset = GenerateAssetPatch(texture, guidGenerator.Next());
                    asset.Name   = texture.name;
                    asset.Source = new AssetSource(source.ContainerType, source.Uri, $"texture:{i}");
                    MREAPI.AppsAPI.AssetCache.CacheAsset(texture, asset.Id, containerId, source);
                    assets.Add(asset);
                }
            }

            // load meshes
            if (gltfRoot.Meshes != null)
            {
                var cancellationSource = new System.Threading.CancellationTokenSource();
                for (var i = 0; i < gltfRoot.Meshes.Count; i++)
                {
                    var mesh = await importer.LoadMeshAsync(i, cancellationSource.Token);

                    mesh.name = gltfRoot.Meshes[i].Name ?? $"mesh:{i}";

                    var asset = GenerateAssetPatch(mesh, guidGenerator.Next());
                    asset.Name   = mesh.name;
                    asset.Source = new AssetSource(source.ContainerType, source.Uri, $"mesh:{i}");
                    var colliderGeo = colliderType == ColliderType.Mesh ?
                                      (ColliderGeometry) new MeshColliderGeometry()
                    {
                        MeshId = asset.Id
                    } :
                    (ColliderGeometry) new BoxColliderGeometry()
                    {
                        Size = (mesh.bounds.size * 0.8f).CreateMWVector3()
                    };
                    MREAPI.AppsAPI.AssetCache.CacheAsset(mesh, asset.Id, containerId, source, colliderGeo);
                    assets.Add(asset);
                }
            }

            // load materials
            if (gltfRoot.Materials != null)
            {
                for (var i = 0; i < gltfRoot.Materials.Count; i++)
                {
                    var matdef   = gltfRoot.Materials[i];
                    var material = await importer.LoadMaterialAsync(i);

                    material.name = matdef.Name ?? $"material:{i}";

                    var asset = GenerateAssetPatch(material, guidGenerator.Next());
                    asset.Name   = material.name;
                    asset.Source = new AssetSource(source.ContainerType, source.Uri, $"material:{i}");
                    MREAPI.AppsAPI.AssetCache.CacheAsset(material, asset.Id, containerId, source);
                    assets.Add(asset);
                }
            }

            // load prefabs
            if (gltfRoot.Scenes != null)
            {
                for (var i = 0; i < gltfRoot.Scenes.Count; i++)
                {
                    await importer.LoadSceneAsync(i).ConfigureAwait(true);

                    GameObject rootObject = importer.LastLoadedScene;
                    rootObject.name = gltfRoot.Scenes[i].Name ?? $"scene:{i}";

                    var animation = rootObject.GetComponent <UnityEngine.Animation>();
                    if (animation != null)
                    {
                        animation.playAutomatically = false;

                        // initialize mapping so we know which gameobjects are targeted by which animation clips
                        var mapping = rootObject.AddComponent <PrefabAnimationTargets>();
                        mapping.Initialize(gltfRoot, i);
                    }

                    MWGOTreeWalker.VisitTree(rootObject, (go) =>
                    {
                        go.layer = MREAPI.AppsAPI.LayerApplicator.DefaultLayer;
                    });

                    var def = GenerateAssetPatch(rootObject, guidGenerator.Next());
                    def.Name   = rootObject.name;
                    def.Source = new AssetSource(source.ContainerType, source.Uri, $"scene:{i}");
                    MREAPI.AppsAPI.AssetCache.CacheAsset(rootObject, def.Id, containerId, source);
                    assets.Add(def);
                }
            }

            importer.Dispose();

            return(assets);
        }
コード例 #8
0
        public async Task LoadGltfAsync(GameObject newObject, string fullPath, ModelImportOptions importOptions)
        {
            asyncCoroutineHelper = newObject.GetComponent <AsyncCoroutineHelper>() ?? gameObject.AddComponent <AsyncCoroutineHelper>();
            //GLTFSceneImporter sceneImporter = null;
            ILoader loader = null;

            try
            {
                // Path.Combine treats paths that start with the separator character
                // as absolute paths, ignoring the first path passed in. This removes
                // that character to properly handle a filename written with it.
                fullPath = fullPath.TrimStart(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
                string directoryPath = URIHelper.GetDirectoryName(fullPath);
                loader        = new FileLoader(directoryPath);
                sceneImporter = new GLTFSceneImporter(
                    Path.GetFileName(fullPath),
                    loader,
                    asyncCoroutineHelper
                    );
                sceneImporter.SceneParent = newObject.transform;
                sceneImporter.Collider    = Collider;
                if (importOptions != null)
                {
                    if (importOptions.buildColliders)
                    {
                        sceneImporter.Collider = GLTFSceneImporter.ColliderType.MeshConvex;
                        //if (importOptions.colliderConvex)
                        //{
                        //    sceneImporter.Collider = GLTFSceneImporter.ColliderType.MeshConvex;
                        //}
                        //else
                        //{
                        //    sceneImporter.Collider = GLTFSceneImporter.ColliderType.Mesh;
                        //}
                    }
                    else
                    {
                        sceneImporter.Collider = GLTFSceneImporter.ColliderType.None;
                    }
                }
                sceneImporter.MaximumLod       = MaximumLod;
                sceneImporter.Timeout          = Timeout;
                sceneImporter.isMultithreaded  = Multithreaded;
                sceneImporter.CustomShaderName = shaderOverride ? shaderOverride.name : null;
                await sceneImporter.LoadSceneAsync();

                // Override the shaders on all materials if a shader is provided
                if (shaderOverride != null)
                {
                    Renderer[] renderers = gameObject.GetComponentsInChildren <Renderer>();
                    foreach (Renderer renderer in renderers)
                    {
                        renderer.sharedMaterial.shader = shaderOverride;
                    }
                }

                if (PlayAnimationOnLoad)
                {
                    Animation[] animations = sceneImporter.LastLoadedScene.GetComponents <Animation>();
                    foreach (Animation anim in animations)
                    {
                        anim.Play();
                    }
                }
            }
            finally
            {
                if (loader != null)
                {
                    sceneImporter?.Dispose();
                    sceneImporter = null;
                    loader        = null;
                }
            }
        }
コード例 #9
0
        private async Task <IList <Godot.Object> > LoadGltfFromStream(WebRequestLoader loader, Stream stream, ColliderType colliderType)
        {
            var assets = new List <Godot.Object>(30);

            // pre-parse glTF document so we can get a scene count
            // run this on a threadpool thread so that the Unity main thread is not blocked
            GLTF.Schema.GLTFRoot gltfRoot = null;
            try
            {
                await Task.Run(() =>
                {
                    GLTF.GLTFParser.ParseJson(stream, out gltfRoot);
                });
            }
            catch (Exception e)
            {
                GD.PrintErr(e);
            }
            if (gltfRoot == null)
            {
                throw new GLTFLoadException("Failed to parse glTF");
            }
            stream.Position = 0;

            using (GLTFSceneImporter importer =
                       MREAPI.AppsAPI.GLTFImporterFactory.CreateImporter(gltfRoot, loader, null /* FIXME _asyncHelper*/, stream))
            {
                importer.SceneParent = MREAPI.AppsAPI.AssetCache.CacheRootGO;
                importer.Collider    = colliderType.ToGLTFColliderType();

                if (gltfRoot.Textures != null)
                {
                    for (var i = 0; i < gltfRoot.Textures.Count; i++)
                    {
                        await importer.LoadTextureAsync(gltfRoot.Textures[i], i, true);

                        var texture = importer.GetTexture(i);
                        texture.ResourceName = gltfRoot.Textures[i].Name ?? $"texture:{i}";
                        assets.Add(texture);
                    }
                }

                // load meshes
                if (gltfRoot.Meshes != null)
                {
                    var cancellationSource = new System.Threading.CancellationTokenSource();
                    for (var i = 0; i < gltfRoot.Meshes.Count; i++)
                    {
                        var mesh = await importer.LoadMeshAsync(i, cancellationSource.Token);

                        mesh.ResourceName = gltfRoot.Meshes[i].Name ?? $"mesh:{i}";
                        assets.Add(mesh);
                    }
                }

                // load materials
                if (gltfRoot.Materials != null)
                {
                    for (var i = 0; i < gltfRoot.Materials.Count; i++)
                    {
                        var matdef   = gltfRoot.Materials[i];
                        var material = await importer.LoadMaterialAsync(i);

                        material.ResourceName = matdef.Name ?? $"material:{i}";
                        assets.Add(material);
                    }
                }

                // load prefabs
                if (gltfRoot.Scenes != null)
                {
                    for (var i = 0; i < gltfRoot.Scenes.Count; i++)
                    {
                        await importer.LoadSceneAsync(i).ConfigureAwait(true);

                        Node rootObject = importer.LastLoadedScene;
                        rootObject.Name = gltfRoot.Scenes[i].Name ?? $"scene:{i}";

                        var animation = rootObject.GetChild <Godot.AnimationPlayer>();
                        if (animation != null)
                        {
                            animation.AssignedAnimation = null;

                            // initialize mapping so we know which gameobjects are targeted by which animation clips
                            var mapping = new PrefabAnimationTargets();
                            rootObject.AddChild(mapping);
                            mapping.Initialize(gltfRoot, i);
                        }

                        assets.Add(rootObject);
                    }
                }
            }

            return(assets);
        }