Ejemplo n.º 1
0
        private GameObject CreateGLTFScene(string projectFilePath)
        {
            ILoader fileLoader = new FileLoader(Path.GetDirectoryName(projectFilePath));

            ;                       using (var stream = File.OpenRead(projectFilePath))
            {
                GLTFRoot gLTFRoot = GLTFParser.ParseJson(stream);
                var      loader   = new GLTFSceneImporter(gLTFRoot, fileLoader, stream);

                loader.MaximumLod = _maximumLod;

                // HACK: Force the coroutine to run synchronously in the editor
                var stack = new Stack <IEnumerator>();
                stack.Push(loader.LoadScene(isMultithreaded: true));

                while (stack.Count > 0)
                {
                    var enumerator = stack.Pop();
                    if (enumerator.MoveNext())
                    {
                        stack.Push(enumerator);
                        var subEnumerator = enumerator.Current as IEnumerator;
                        if (subEnumerator != null)
                        {
                            stack.Push(subEnumerator);
                        }
                    }
                }
                return(loader.LastLoadedScene);
            }
        }
Ejemplo n.º 2
0
        // todo undo
#if !WINDOWS_UWP
        IEnumerator Start()
        {
            var     fullPath0 = Application.streamingAssetsPath + Path.DirectorySeparatorChar + asset0Path;
            ILoader loader0   = new FileLoader(URIHelper.GetDirectoryName(fullPath0));

            var     fullPath1 = Application.streamingAssetsPath + Path.DirectorySeparatorChar + asset1Path;
            ILoader loader1   = new FileLoader(URIHelper.GetDirectoryName(fullPath1));

            yield return(loader0.LoadStream(Path.GetFileName(asset0Path)));

            var asset0Stream = loader0.LoadedStream;
            var asset0Root   = GLTFParser.ParseJson(asset0Stream);

            yield return(loader1.LoadStream(Path.GetFileName(asset1Path)));

            var asset1Stream = loader1.LoadedStream;
            var asset1Root   = GLTFParser.ParseJson(asset1Stream);

            string newPath = "../../" + URIHelper.GetDirectoryName(asset0Path);

            int previousBufferCount  = asset1Root.Buffers.Count;
            int previousImageCount   = asset1Root.Images.Count;
            int previousSceneCounter = asset1Root.Scenes.Count;

            GLTFHelpers.MergeGLTF(asset1Root, asset0Root);

            for (int i = previousBufferCount; i < asset1Root.Buffers.Count; ++i)
            {
                GLTF.Schema.Buffer buffer = asset1Root.Buffers[i];
                if (!URIHelper.IsBase64Uri(buffer.Uri))
                {
                    buffer.Uri = newPath + buffer.Uri;
                }
            }

            for (int i = previousImageCount; i < asset1Root.Images.Count; ++i)
            {
                Image image = asset1Root.Images[i];
                if (!URIHelper.IsBase64Uri(image.Uri))
                {
                    image.Uri = newPath + image.Uri;
                }
            }

            foreach (NodeId node in asset1Root.Scenes[asset0Root.Scene.Id + previousSceneCounter].Nodes)
            {
                node.Value.Translation.X += 5f;
                asset1Root.Scene.Value.Nodes.Add(node);
            }
            GLTFSceneImporter importer = new GLTFSceneImporter(
                asset1Root,
                loader1
                );

            importer.MaximumLod = MaximumLod;

            yield return(importer.LoadScene(-1, Multithreaded));
        }
Ejemplo n.º 3
0
        private GameObject CreateGLTFScene(string projectFilePath)
        {
            ILoader fileLoader = new FileLoader(Path.GetDirectoryName(projectFilePath));

            using (var stream = File.OpenRead(projectFilePath))
            {
                GLTFRoot gLTFRoot;
                GLTFParser.ParseJson(stream, out gLTFRoot);

                OnGLTFRootIsConstructed?.Invoke(gLTFRoot);

                var loader = new GLTFSceneImporter(Path.GetFullPath(projectFilePath), gLTFRoot, fileLoader, null, stream);
                GLTFSceneImporter.budgetPerFrameInMilliseconds = float.MaxValue;
                loader.addImagesToPersistentCaching            = false; // Since we control the PersistentAssetCache during AB Conversion, we don't want the importer to mess with that
                loader.addMaterialsToPersistentCaching         = false;
                loader.initialVisibility     = true;
                loader.useMaterialTransition = false;
                loader.maximumLod            = _maximumLod;
                loader.isMultithreaded       = true;

                OnGLTFWillLoad?.Invoke(loader);

                // HACK: Force the coroutine to run synchronously in the editor
                var stack = new Stack <IEnumerator>();
                stack.Push(loader.LoadScene());

                while (stack.Count > 0)
                {
                    var enumerator = stack.Pop();

                    try
                    {
                        if (enumerator.MoveNext())
                        {
                            stack.Push(enumerator);
                            var subEnumerator = enumerator.Current as IEnumerator;
                            if (subEnumerator != null)
                            {
                                stack.Push(subEnumerator);
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        Debug.Log("GLTFImporter - CreateGLTFScene Failed: " + e.Message + "\n" + e.StackTrace);
                    }
                }

                return(loader.lastLoadedScene);
            }
        }
Ejemplo n.º 4
0
        public IEnumerator Load()
        {
            GLTFSceneImporter sceneImporter = null;
            ILoader           loader        = null;

            try
            {
                string directoryPath = URIHelper.GetDirectoryName(GLTFUri);
                loader        = new WebRequestLoader(directoryPath);
                sceneImporter = new GLTFSceneImporter(
                    URIHelper.GetFileFromUri(new Uri(GLTFUri)),
                    loader
                    );

                sceneImporter.SceneParent      = gameObject.transform;
                sceneImporter.Collider         = Collider;
                sceneImporter.MaximumLod       = MaximumLod;
                sceneImporter.Timeout          = Timeout;
                sceneImporter.isMultithreaded  = Multithreaded;
                sceneImporter.CustomShaderName = shaderOverride ? shaderOverride.name : null;
                yield return(sceneImporter.LoadScene(-1));

                // 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 (gameObject.GetComponentInChildren <Animation>() != null)
                {
                    anim = gameObject.GetComponentInChildren <Animation>();
                }
                if (anim != null)
                {
                    anim.Play();
                }
            }
            finally
            {
                if (loader != null)
                {
                    sceneImporter.Dispose();
                    sceneImporter = null;
                    loader        = null;
                }
            }
        }
        private GameObject CreateGLTFScene(string projectFilePath)
        {
            ILoader fileLoader = new FileLoader(Path.GetDirectoryName(projectFilePath));

            using (var stream = File.OpenRead(projectFilePath))
            {
                GLTFRoot gLTFRoot;
                GLTFParser.ParseJson(stream, out gLTFRoot);

                OnGLTFRootIsConstructed?.Invoke(gLTFRoot);

                var loader = new GLTFSceneImporter(Path.GetFullPath(projectFilePath), gLTFRoot, fileLoader, null, stream);
                GLTFSceneImporter.budgetPerFrameInMilliseconds = float.MaxValue;
                loader.addImagesToPersistentCaching            = false;
                loader.addMaterialsToPersistentCaching         = false;
                loader.initialVisibility     = true;
                loader.useMaterialTransition = false;
                loader.maximumLod            = _maximumLod;
                loader.isMultithreaded       = true;

                // HACK: Force the coroutine to run synchronously in the editor
                var stack = new Stack <IEnumerator>();
                stack.Push(loader.LoadScene());

                while (stack.Count > 0)
                {
                    var enumerator = stack.Pop();
                    if (enumerator.MoveNext())
                    {
                        stack.Push(enumerator);
                        var subEnumerator = enumerator.Current as IEnumerator;
                        if (subEnumerator != null)
                        {
                            stack.Push(subEnumerator);
                        }
                    }
                }

                return(loader.lastLoadedScene);
            }
        }
Ejemplo n.º 6
0
        public IEnumerator LoadAssetCoroutine()
        {
            if (!string.IsNullOrEmpty(GLTFUri))
            {
                if (VERBOSE)
                {
                    Debug.Log("LoadAssetCoroutine() GLTFUri ->" + GLTFUri);
                }

                asyncCoroutineHelper = gameObject.GetComponent <AsyncCoroutineHelper>() ?? gameObject.AddComponent <AsyncCoroutineHelper>();

                sceneImporter = null;
                ILoader loader = null;

                Destroy(loadedAssetRootGameObject);

                try
                {
                    if (UseStream)
                    {
                        // 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.
                        GLTFUri = GLTFUri.TrimStart(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar });
                        string fullPath      = Path.Combine(Application.streamingAssetsPath, GLTFUri);
                        string directoryPath = URIHelper.GetDirectoryName(fullPath);
                        loader        = new FileLoader(directoryPath);
                        sceneImporter = new GLTFSceneImporter(
                            Path.GetFileName(GLTFUri),
                            loader,
                            asyncCoroutineHelper
                            );
                    }
                    else
                    {
                        loader = new WebRequestLoader("");

                        if (OnWebRequestStartEvent != null)
                        {
                            (loader as WebRequestLoader).OnLoadStreamStart += OnWebRequestStartEvent;
                        }

                        sceneImporter = new GLTFSceneImporter(
                            GLTFUri,
                            loader,
                            asyncCoroutineHelper
                            );
                    }

                    if (sceneImporter.CreatedObject != null)
                    {
                        Destroy(sceneImporter.CreatedObject);
                    }

                    sceneImporter.SceneParent                     = gameObject.transform;
                    sceneImporter.Collider                        = Collider;
                    sceneImporter.maximumLod                      = MaximumLod;
                    sceneImporter.Timeout                         = Timeout;
                    sceneImporter.isMultithreaded                 = Multithreaded;
                    sceneImporter.useMaterialTransition           = UseVisualFeedback;
                    sceneImporter.CustomShaderName                = shaderOverride ? shaderOverride.name : null;
                    sceneImporter.LoadingTextureMaterial          = LoadingTextureMaterial;
                    sceneImporter.initialVisibility               = initialVisibility;
                    sceneImporter.addMaterialsToPersistentCaching = addMaterialsToPersistentCaching;

                    float time = Time.realtimeSinceStartup;

                    queueCount++;

                    state = State.QUEUED;

                    Func <bool> funcTestDistance = () => TestDistance();
                    yield return(new WaitUntil(funcTestDistance));

                    queueCount--;
                    totalDownloadedCount++;

                    IncrementDownloadCount();

                    state = State.DOWNLOADING;

                    yield return(sceneImporter.LoadScene(-1));

                    state = State.COMPLETED;

                    DecrementDownloadCount();

                    // 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;
                        }
                    }
                }
                finally
                {
                    if (loader != null)
                    {
                        if (sceneImporter == null)
                        {
                            Debug.Log("sceneImporter is null, could be due to an invalid URI.", this);
                        }
                        else
                        {
                            loadedAssetRootGameObject = sceneImporter.CreatedObject;

                            sceneImporter?.Dispose();
                            sceneImporter = null;
                        }

                        if (OnWebRequestStartEvent != null)
                        {
                            (loader as WebRequestLoader).OnLoadStreamStart -= OnWebRequestStartEvent;
                            OnWebRequestStartEvent = null;
                        }

                        loader = null;
                    }

                    alreadyLoadedAsset = true;
                    OnFinishedLoadingAsset?.Invoke();
                }
            }
            else
            {
                Debug.Log("couldn't load GLTF because url is empty");
            }

            CoroutineStarter.Stop(loadingRoutine);
            loadingRoutine = null;
            Destroy(loadingPlaceholder);
            Destroy(this);
        }