static void LoadLocalFbx(string path, System.Action <GameObject> process) { var loader = new TriLib.AssetLoaderAsync(); TriLib.AssetLoaderOptions opts = GetTriLibOpts(); loader.LoadFromFileWithTextures(path, opts, null, gameObject => { PrepareImportedModel(gameObject, ""); process(gameObject); }); }
/// <summary> /// Internal asset download coroutine. /// </summary> /// <returns>The coroutine IEnumerator.</returns> /// <param name="assetUri">Asset URI.</param> /// <param name="assetExtension">Asset extension.</param> /// <param name="onAssetLoaded">On asset loaded event.</param> /// <param name="options">Asset loading options.</param> /// <param name="wrapperGameObject">Wrapper <see cref="UnityEngine.GameObject"/> to load the asset into.</param> private IEnumerator DoDownloadAsset(string assetUri, string assetExtension, ObjectLoadedHandle onAssetLoaded, AssetLoaderOptions options = null, GameObject wrapperGameObject = null) { _unityWebRequest = UnityWebRequest.Get(assetUri); _unityWebRequest.timeout = Timeout; #if UNITY_2017_3_OR_NEWER yield return(_unityWebRequest.SendWebRequest()); #else yield return(_unityWebRequest.Send()); #endif if (string.IsNullOrEmpty(_unityWebRequest.error)) { var data = _unityWebRequest.downloadHandler.data; try { if (Async) { using (var assetLoaderAsync = new AssetLoaderAsync()) { assetLoaderAsync.LoadFromMemoryWithTextures(data, assetExtension, options, wrapperGameObject, onAssetLoaded); } } else { using (var assetLoader = new AssetLoader()) { assetLoader.OnObjectLoaded += onAssetLoaded; assetLoader.LoadFromMemoryWithTextures(data, assetExtension, options, wrapperGameObject); } } } catch (Exception exception) { _error = exception.ToString(); if (onAssetLoaded != null) { onAssetLoaded(null); } } } else { onAssetLoaded(null); } _unityWebRequest.Dispose(); _unityWebRequest = null; }
/// <summary> /// Internal asset download coroutine. /// </summary> /// <returns>The coroutine IEnumerator.</returns> /// <param name="assetUri">Asset URI.</param> /// <param name="assetExtension">Asset extension.</param> /// <param name="onAssetLoaded">On asset loaded event.</param> /// <param name="options">Asset loading options.</param> /// <param name="wrapperGameObject">Wrapper <see cref="UnityEngine.GameObject"/> to load the asset into.</param> /// <param name="progressCallback">Callback used to retrieve file loading percentage.</param> private IEnumerator DoDownloadAsset(string assetUri, string assetExtension, ObjectLoadedHandle onAssetLoaded, AssetLoaderOptions options = null, GameObject wrapperGameObject = null, AssimpInterop.ProgressCallback progressCallback = null) { _unityWebRequest = UnityWebRequest.Get(assetUri); _unityWebRequest.timeout = Timeout; yield return(_unityWebRequest.SendWebRequest()); if (string.IsNullOrEmpty(_unityWebRequest.error)) { var data = _unityWebRequest.downloadHandler.data; try { if (Async) { using (var assetLoaderAsync = new AssetLoaderAsync()) { try { assetLoaderAsync.LoadFromMemoryWithTextures(data, assetExtension, options, wrapperGameObject, onAssetLoaded, null, null, null, progressCallback); } catch (Exception e) { _error = e.ToString(); if (onAssetLoaded != null) { onAssetLoaded(null); } } } } else { using (var assetLoader = new AssetLoader()) { assetLoader.OnObjectLoaded += onAssetLoaded; try { assetLoader.LoadFromMemoryWithTextures(data, assetExtension, options, wrapperGameObject, null, null, null, progressCallback); } catch (Exception e) { _error = e.ToString(); if (onAssetLoaded != null) { onAssetLoaded(null); } } } } } catch (Exception exception) { _error = exception.ToString(); if (onAssetLoaded != null) { onAssetLoaded(null); } } } else { _error = _unityWebRequest.error; if (onAssetLoaded != null) { onAssetLoaded(null); } } _unityWebRequest.Dispose(); _unityWebRequest = null; }
public void Visit(PolyVoosAsset asset) { string assetName = asset.assetId; string uri = asset.GetUri(); PolyApi.GetAsset(assetName, maybeAsset => { if (MaybeLogError(assetName, maybeAsset)) { cache.CreateCacheEntry(uri, GameObject.CreatePrimitive(PrimitiveType.Cube), null); return; } PolyAsset polyAsset = maybeAsset.Value; // We need both the asset and the thumbnail to consider it "downloaded". // We'll download them in parallel, and whichever one finishes second // will call SetCacheEntry. GameObject assetObject = null; Texture2D thumbnail = null; PolyApi.FetchThumbnail(polyAsset, (_, maybeImported) => { if (MaybeLogError(assetName, maybeImported)) { return; } thumbnail = polyAsset.thumbnailTexture; // If we finished first, don't SetCacheEntry yet. if (assetObject != null) { // Ok, both resources are done. Report completion! // Set the cache (and flush the wait list) cache.CreateCacheEntry(uri, assetObject, thumbnail); } }); System.Action <GameObject> onAssetImported = importedGameObject => { cache.downloadedPolyAssets.Add(polyAsset); assetObject = importedGameObject; assetObject.name = assetName; PrepareImportedModel(assetObject, polyAsset.description); // If we finished first, don't SetCacheEntry yet. if (thumbnail != null) { // Ok, both resources are done. Report completion! // Set the cache (and flush the wait list) cache.CreateCacheEntry(uri, assetObject, thumbnail); } }; var objFormat = polyAsset.GetFormatIfExists(PolyFormatType.OBJ); var fbx = polyAsset.GetFormatIfExists(PolyFormatType.FBX); #if USE_TRILIB // Blocks models, like the pug, have both GLTFs and FBX's. But, TRILIB // doesn't seem to load Blocks FBX well, so don't do it. However, it's // not trivial to detect Blocks models. So our current heuristic is // going to simply be, only load the FBX if it has FBX but NOT OBJ. At // least as of 20190325, actual FBX uploads don't have OBJs. In the // future, we can even peek at the GLTF and see if it was generated by // Blocks. if (objFormat == null && fbx != null) { string tempDir = Util.CreateTempDirectory(); Dictionary <string, string> url2path = new Dictionary <string, string>(); foreach (var file in fbx.resources) { string localPath = System.IO.Path.Combine(tempDir, file.relativePath); url2path[file.url] = localPath; } // The root string rootPath = System.IO.Path.Combine(tempDir, fbx.root.relativePath); url2path[fbx.root.url] = rootPath; Util.DownloadFilesToDisk(url2path, () => { // All done! using (var loader = new TriLib.AssetLoaderAsync()) { TriLib.AssetLoaderOptions opts = GetTriLibOpts(); loader.LoadFromFileWithTextures(rootPath, opts, null, obj => { System.IO.Directory.Delete(tempDir, true); onAssetImported(obj); }); } }, errors => { System.IO.Directory.Delete(tempDir, true); Debug.LogError($"Could not download Poly FBX for asset ID {assetName}. Errors: {string.Join("\n", errors)}"); return; }); } else #endif { PolyApi.Import(polyAsset, cache.polyImportOptions, (_, maybeImported) => { if (MaybeLogError(assetName, maybeImported)) { return; } onAssetImported(maybeImported.Value.gameObject); }); } }); }