Ejemplo n.º 1
0
        public IEnumerator Load()
        {
            GLTFSceneImporter sceneImporter = null;
            ILoader           loader        = null;

            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
                    );
            }
            else
            {
                string directoryPath = URIHelper.GetDirectoryName(GLTFUri);
                loader        = AbstractWebRequestLoader.CreateDefaultRequestLoader(directoryPath);
                sceneImporter = new GLTFSceneImporter(
                    URIHelper.GetFileFromUri(new Uri(GLTFUri)),
                    loader
                    );
            }

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

            // 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;
                }
            }
        }
Ejemplo n.º 2
0
        public IEnumerator Download(Promise <bool> loadComplete)
        {
            string            directoryPath = URIHelper.GetDirectoryName(Url);
            string            relativePath  = Url.Replace(directoryPath, "");
            var               wrl           = new B3DMLoader(AbstractWebRequestLoader.CreateDefaultRequestLoader(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;
                }
            }
        }
Ejemplo n.º 3
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)));
        }
Ejemplo n.º 4
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);
            }
        }