public IEnumerator Download(Promise <bool> loadComplete)
        {
            string            directoryPath = URIHelper.GetDirectoryName(Url);
            string            relativePath  = Url.Replace(directoryPath, "");
            var               wrl           = new B3DMLoader(new WebRequestLoader(directoryPath));
            GLTFSceneImporter sceneImporter = new GLTFSceneImporter(
                relativePath,
                wrl
                );

            sceneImporter.SceneParent      = gameObject.transform;
            sceneImporter.CustomShaderName = ShaderOverride ? ShaderOverride.name : null;
            sceneImporter.MaximumLod       = MaximumLod;
            if (addColliders)
            {
                sceneImporter.Collider = GLTFSceneImporter.ColliderType.Mesh;
            }
            yield return(sceneImporter.LoadScene(-1, Multithreaded, sceneObject =>
            {
                if (sceneObject != null)
                {
                    loadComplete.Resolve(true);
                }
                else
                {
                    loadComplete.Resolve(false);
                }
                Destroy(this);
            }));

            // 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;
                }
            }
        }
Example #2
0
        public IEnumerator Download(Promise <bool> loadComplete)
        {
            string url  = UrlUtils.ReplaceDataProtocol(Url);
            string dir  = UrlUtils.GetBaseUri(url);
            string file = UrlUtils.GetLastPathSegment(url);

            ILoader loader = AbstractWebRequestLoader.CreateDefaultRequestLoader(dir); //.glb, .gltf

            if (file.EndsWith(".b3dm", StringComparison.OrdinalIgnoreCase))
            {
                loader = new B3DMLoader(loader);
            }
            var sceneImporter = new GLTFSceneImporter(file, loader);

            sceneImporter.SceneParent      = gameObject.transform;
            sceneImporter.CustomShaderName = ShaderOverride ? ShaderOverride.name : null;
            sceneImporter.MaximumLod       = MaximumLod;
            sceneImporter.Collider         =
                AddColliders ? GLTFSceneImporter.ColliderType.Mesh : GLTFSceneImporter.ColliderType.None;

            loadComplete = loadComplete ?? new Promise <bool>();
            yield return(sceneImporter.LoadScene(-1, Multithreaded,
                                                 sceneObject => loadComplete.Resolve(sceneObject != null)));
        }
Example #3
0
        public static IEnumerator Load(IndexMode mode, string tileUrl, Action <Unity3DTileIndex> success,
                                       Action <IndexMode, string, string> fail)
        {
            if (mode == IndexMode.Default || mode == IndexMode.None)
            {
                success(null);
                yield break;
            }

            string url  = UrlUtils.ReplaceDataProtocol(tileUrl);
            string dir  = UrlUtils.GetBaseUri(tileUrl);
            string file = UrlUtils.GetLastPathSegment(tileUrl);

            var filesToTry = new Queue <string>();

            if (mode == IndexMode.ExternalPNG)
            {
                filesToTry.Enqueue(UrlUtils.StripUrlExtension(file) + "_index.png");
                filesToTry.Enqueue(UrlUtils.ChangeUrlExtension(file, ".png"));
            }
            else if (mode == IndexMode.ExternalPPM)
            {
                filesToTry.Enqueue(UrlUtils.StripUrlExtension(file) + "_index.ppm");
                filesToTry.Enqueue(UrlUtils.ChangeUrlExtension(file, ".ppm"));
            }
            else if (mode == IndexMode.ExternalPPMZ)
            {
                filesToTry.Enqueue(UrlUtils.StripUrlExtension(file) + "_index.ppmz");
                filesToTry.Enqueue(UrlUtils.ChangeUrlExtension(file, ".ppmz"));
            }
            else
            {
                filesToTry.Enqueue(file);
            }

            Stream    stream    = null;
            Exception exception = null;
            string    ext       = null;

            while (stream == null && filesToTry.Count > 0)
            {
                exception = null;
                IEnumerator enumerator = null;
                ILoader     loader     = null;
                try
                {
                    file   = filesToTry.Dequeue();
                    ext    = UrlUtils.GetUrlExtension(file).ToLower();
                    loader = AbstractWebRequestLoader.CreateDefaultRequestLoader(dir);
                    if (ext == ".b3dm")
                    {
                        loader = new B3DMLoader(loader);
                    }
                    //yield return loader.LoadStream(file); //works but can't catch exceptions
                    enumerator = loader.LoadStream(file);
                }
                catch (Exception ex)
                {
                    exception = ex;
                }
                while (exception == null)
                {
                    try
                    {
                        if (!enumerator.MoveNext())
                        {
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        exception = ex;
                        break;
                    }
                    yield return(enumerator.Current);
                }

                if (exception == null && loader.LoadedStream != null && loader.LoadedStream.Length > 0)
                {
                    stream = loader.LoadedStream;
                    stream.Seek(0, SeekOrigin.Begin);
                }
            }

            if (stream == null || stream.Length == 0)
            {
                fail(mode, tileUrl, "download failed" + (exception != null ? (" " + exception.Message) : ""));
                yield break;
            }

            try
            {
                if (ext == ".b3dm" || ext == ".glb")
                {
                    success(LoadFromGLB(stream));
                }
                else if (ext == ".gltf")
                {
                    success(LoadFromGLTF(stream));
                }
                else if (ext == ".png")
                {
                    success(LoadFromPNG(stream));
                }
                else if (ext == ".ppm" || ext == ".ppmz")
                {
                    success(LoadFromPPM(stream, compressed: ext == ".ppmz"));
                }
                else
                {
                    fail(mode, tileUrl, "unhandled file type: " + ext);
                }
            }
            catch (Exception ex)
            {
                fail(mode, tileUrl, "failed to parse " + file + ": " + ex.Message + "\n" + ex.StackTrace);
            }
        }