コード例 #1
0
ファイル: AssetCache.cs プロジェクト: zachary2234/gamebuilder
        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);
                    });
                }
            });
        }