public Unity3DTileset(Unity3DTilesetOptions tilesetOptions, AbstractTilesetBehaviour behaviour) { this.TilesetOptions = tilesetOptions; this.Behaviour = behaviour; this.RequestManager = behaviour.RequestManager; this.ProcessingQueue = behaviour.ProcessingQueue; this.LRUContent = behaviour.LRUCache; this.Traversal = new Unity3DTilesetTraversal(this, behaviour.SceneOptions); this.DeepestDepth = 0; string url = UrlUtils.ReplaceDataProtocol(tilesetOptions.Url); if (UrlUtils.GetLastPathSegment(url).EndsWith(".json", StringComparison.OrdinalIgnoreCase)) { this.basePath = UrlUtils.GetBaseUri(url); this.tilesetUrl = url; } else { this.basePath = url; this.tilesetUrl = UrlUtils.JoinUrls(url, "tileset.json"); } LoadTilesetJson(this.tilesetUrl).Then(json => { // Load Tileset (main tileset or a reference tileset) this.tileset = Schema.Tileset.FromJson(json); this.Root = LoadTileset(this.tilesetUrl, this.tileset, null); this.readyPromise.Resolve(this); }).Catch(error => { Debug.LogError(error.Message + "\n" + error.StackTrace); }); }
private Unity3DTile LoadTileset(string tilesetUrl, Schema.Tileset tileset, Unity3DTile parentTile) { if (tileset.Asset == null) { Debug.LogError("Tileset must have an asset property"); return(null); } if (tileset.Asset.Version != "0.0" && tileset.Asset.Version != "1.0") { Debug.LogError("Tileset must be 3D Tiles version 0.0 or 1.0"); return(null); } // Add tileset version to base path bool hasVersionQuery = new Regex(@"/[?&]v=/").IsMatch(tilesetUrl); if (!hasVersionQuery && !new Uri(tilesetUrl).IsFile) { string version = "0.0"; if (tileset.Asset.TilesetVersion != null) { version = tileset.Asset.TilesetVersion; } string versionQuery = "v=" + version; this.basePath = UrlUtils.SetQuery(this.basePath, versionQuery); tilesetUrl = UrlUtils.SetQuery(tilesetUrl, versionQuery); } // A tileset.json referenced from a tile may exist in a different directory than the root tileset. // Get the basePath relative to the external tileset. string basePath = UrlUtils.GetBaseUri(tilesetUrl); Unity3DTile rootTile = new Unity3DTile(this, basePath, tileset.Root, parentTile); Statistics.NumberOfTilesTotal++; // Loop through the Tile json data and create a tree of Unity3DTiles Stack <Unity3DTile> stack = new Stack <Unity3DTile>(); stack.Push(rootTile); while (stack.Count > 0) { Unity3DTile tile3D = stack.Pop(); for (int i = 0; i < tile3D.tile.Children.Count; i++) { Unity3DTile child = new Unity3DTile(this, basePath, tile3D.tile.Children[i], tile3D); this.DeepestDepth = Math.Max(child.Depth, this.DeepestDepth); Statistics.NumberOfTilesTotal++; stack.Push(child); } // TODO consider using CullWithChildrenBounds optimization here } this.loadTimestamp = DateTime.UtcNow; return(rootTile); }
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))); }
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); } }