Ejemplo n.º 1
0
    private void ImportResults(PolyStatusOr <PolyListAssetsResult> result)
    {
        // Set options for import so the assets aren't crazy sizes
        PolyImportOptions options = PolyImportOptions.Default();

        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        options.desiredSize   = 5.0f;
        options.recenter      = true;

        // List our assets
        List <PolyAsset> assetsInUse = new List <PolyAsset>();

        // Loop through the list and display the first 3
        for (int i = 0; i < Mathf.Min(5, result.Value.assets.Count); i++)
        {
            // Import our assets into the scene with the ImportAsset function
            PolyApi.Import(result.Value.assets[i], options, ImportAsset);
            PolyAsset asset = result.Value.assets[i];
            assetsInUse.Add(asset);

            //attributionsText.text = PolyApi.GenerateAttributions(includeStatic: false, runtimeAssets: assetsInUse);
        }

        string attribution = PolyApi.GenerateAttributions(includeStatic: false, runtimeAssets: assetsInUse);

        InterfaceManager.GetInstance().AddMessageToBox(attribution);
    }
Ejemplo n.º 2
0
 static PolyGridAsset()
 {
     s_Options = PolyImportOptions.Default();
     s_Options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
     s_Options.desiredSize   = 1.0f;
     s_Options.recenter      = true;
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Checks if the asset has the contents of the format to import, fetching them if need be; then imports
 /// the asset.
 /// </summary>
 /// <param name="asset">The asset who's format is being imported.</param>
 /// <param name="format">The format to import.</param>
 /// <param name="options">The import options for this asset.</param>
 /// <param name="callback">The callback to call when this is finished.</param>
 private void FetchAndImportFormat(PolyAsset asset, PolyFormat format, PolyImportOptions options,
                                   PolyApi.ImportCallback callback = null)
 {
     if (format.root.contents != null)
     {
         // If asset already has the gltf package, proceed directly to importing it.
         ImportFormat(asset, format, options, callback);
     }
     else
     {
         // Otherwise, first fetch the package and then import the model.
         FetchFormatFiles(asset, format.formatType, (PolyAsset resultAsset, PolyStatus status) => {
             PolyFormat fetchedFormat = resultAsset.GetFormatIfExists(format.formatType);
             if (fetchedFormat != null)
             {
                 ImportFormat(asset, fetchedFormat, options, callback);
             }
             else
             {
                 if (callback != null)
                 {
                     callback(asset, new PolyStatusOr <PolyImportResult>(
                                  PolyStatus.Error("Could not fetch format files for asset")));
                 }
             }
         });
     }
 }
Ejemplo n.º 4
0
        private static GameObject DoImport(string gltfPath, PolyImportOptions options, bool savePrefab = false)
        {
            var text = File.ReadAllText(gltfPath);

            GltfSchemaVersion version;

            if (text.Contains("\"version\": \"1"))
            {
                version = GltfSchemaVersion.GLTF1;
            }
            else if (text.Contains("\"version\": \"2"))
            {
                version = GltfSchemaVersion.GLTF2;
            }
            else
            {
                // well, just guess I suppose... it's just test code
                version = GltfSchemaVersion.GLTF1;
            }

            Debug.LogFormat("Import {0} as {1}", gltfPath, version);
            IUriLoader binLoader = new BufferedStreamLoader(Path.GetDirectoryName(gltfPath));

            using (TextReader reader = new StreamReader(gltfPath)) {
                var result = ImportGltf.Import(version, reader, binLoader, options);
                if (savePrefab)
                {
                    SaveAsSinglePrefab(result, Path.ChangeExtension(gltfPath, ".prefab"));
                }
                return(result.root);
            }
        }
Ejemplo n.º 5
0
        // Callback invoked when the featured assets results are returned.
        private static void GetAssetCallback(PolyStatusOr <PolyToolkit.PolyAsset> result, Action <GameObject> onImport)
        {
            if (!result.Ok)
            {
                Debug.LogError("Failed to get assets. Reason: " + result.Status);
                //statusText.text = "ERROR: " + result.Status;
                return;
            }
            Debug.Log("Successfully got asset!");

            // Set the import options.
            PolyImportOptions options = PolyImportOptions.Default();

            // We want to rescale the imported mesh to a specific size.
            options.rescalingMode = PolyImportOptions.RescalingMode.CONVERT;
            // The specific size we want assets rescaled to (fit in a 5x5x5 box):
            options.desiredSize = 5.0f;
            // We want the imported assets to be recentered such that their centroid coincides with the origin:
            options.recenter = true;

            //statusText.text = "Importing...";
            PolyApi.Import(result.Value, options, (asset, importResult) =>
            {
                if (result.Ok)
                {
                    onImport(importResult.Value.gameObject);
                }
            });
        }
Ejemplo n.º 6
0
 void Start()
 {
     PolyToolkit.PolyApi.GetAsset("assets/6LSB0OZK8I7", // ← id
                                  result => {
         PolyApi.Import(result.Value, PolyImportOptions.Default());
     });
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Imports the given format of the given asset, asynchronously in a background thread.
        /// Calls the supplied callback when done.
        /// </summary>
        public void ImportAsync(PolyAsset asset, PolyFormat format, PolyImportOptions options,
                                AsyncImportCallback callback = null)
        {
            ImportOperation operation = new ImportOperation();

            operation.instance = this;
            operation.asset    = asset;
            operation.format   = format;
            operation.options  = options;
            operation.callback = callback;
            operation.status   = PolyStatus.Success();
            operation.loader   = new FormatLoader(format);
            if (Application.isPlaying)
            {
                Task.Run(() => BackgroundThreadProc(operation));
                //ThreadPool.QueueUserWorkItem(new WaitCallback(BackgroundThreadProc), operation);
            }
            else
            {
                // If we are in the editor, don't do this in a background thread. Do it directly
                // here on the main thread.
                BackgroundThreadProc(operation);
                Update();
            }
        }
Ejemplo n.º 8
0
    void HandleRequest(PolyStatusOr <PolyListAssetsResult> result)
    {
        if (!result.Ok)
        {
            // Handle error.
            Debug.LogError("Failed to get assets. Reason: " + result.Status);
            currCount++;
            return;
        }
        // Success. result.Value is a PolyListAssetsResult and
        // result.Value.assets is a list of PolyAssets.
        if (result.Value.assets.Count == 0)
        {
            Debug.Log("No asset found");
            currCount++;
            return;
        }

        foreach (PolyAsset asset in result.Value.assets)
        {
            // Set the import options.
            PolyImportOptions options = PolyImportOptions.Default();
            // We want to rescale the imported mesh to a specific size.
            options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
            // The specific size we want assets rescaled to:
            options.desiredSize = 0.05f;
            // We want the imported assets to be recentered such that their centroid coincides with the origin:
            options.recenter = true;

            PolyApi.Import(asset, options, ImportAssetCallback);
        }
    }
    // Callback invoked when the featured assets results are returned.
    private void ListAssetsCallback(PolyStatusOr <PolyListAssetsResult> result)
    {
        if (!result.Ok)
        {
            Debug.LogError("Failed to get featured assets. :( Reason: " + result.Status);
            statusText.text = "ERROR: " + result.Status;
            return;
        }
        Debug.Log("Successfully got featured assets!");
        statusText.text = "Importing...";

        // Set the import options.
        PolyImportOptions options = PolyImportOptions.Default();

        // We want to rescale the imported meshes to a specific size.
        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        // The specific size we want assets rescaled to (fit in a 1x1x1 box):
        options.desiredSize = 1.0f;
        // We want the imported assets to be recentered such that their centroid coincides with the origin:
        options.recenter = true;

        // Now let's get the first 5 featured assets and put them on the scene.
        List <PolyAsset> assetsInUse = new List <PolyAsset>();

        for (int i = 0; i < Mathf.Min(5, result.Value.assets.Count); i++)
        {
            // Import this asset.
            PolyApi.Import(result.Value.assets[i], options, ImportAssetCallback);
            assetsInUse.Add(result.Value.assets[i]);
        }

        // Show attributions for the assets we display.
        attributionsText.text = PolyApi.GenerateAttributions(includeStatic: true, runtimeAssets: assetsInUse);
    }
    // Callback invoked when the featured assets results are returned.
    private void GetAssetCallback(PolyStatusOr <PolyListAssetsResult> result)
    {
        if (!result.Ok)
        {
            Debug.LogError("Failed to get assets. Reason: " + result.Status);

            statusText.text = "ERROR: " + result.Status;

            return;
        }
        Debug.Log("Successfully got asset!");

        foreach (PolyAsset asset in result.Value.assets)
        {
            // Set the import options.
            PolyImportOptions options = PolyImportOptions.Default();
            // We want to rescale the imported mesh to a specific size.
            options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
            // The specific size we want assets rescaled to (fit in a 5x5x5 box):
            options.desiredSize = 0.4f;
            // We want the imported assets to be recentered such that their centroid coincides with the origin:
            options.recenter = true;

            statusText.text = "Importing...";
            PolyApi.Import(asset, options, ImportAssetCallback);
        }
    }
Ejemplo n.º 11
0
        public static void TestImportGltf2Scale()
        {
            PolyImportOptions options = new PolyImportOptions();

            options.rescalingMode = PolyImportOptions.RescalingMode.CONVERT;
            options.scaleFactor   = 2.0f;
            DoImport(Path.Combine(RepoRoot, kComputer), options);
        }
Ejemplo n.º 12
0
        public static void TestImportGltf2TargetSize()
        {
            PolyImportOptions options = new PolyImportOptions();

            options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
            options.desiredSize   = 50;
            DoImport(Path.Combine(RepoRoot, kComputer), options);
        }
 public void TestImport(string id)
 {
     PolyApi.GetAsset(id, result => {
         if (result.Ok)
         {
             PolyApi.Import(result.Value, PolyImportOptions.Default());
         }
     });
 }
    //When user choose an assest, this method is called.
    public void ChooseAssest()
    {
        PolyImportOptions options = PolyImportOptions.Default();

        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        options.desiredSize   = .5f;
        options.recenter      = true;
        PolyApi.Import(objectAsset, options, generatingAsset);
    }
Ejemplo n.º 15
0
    /// <summary>
    /// Import asset from clicked thumbnail button
    /// </summary>
    /// <param name="asset">The asset to be imported</param>
    public static void ImportAsset(PolyAsset asset)
    {
        PolyImportOptions options = PolyImportOptions.Default();

        options.rescalingMode = PolyImportOptions.RescalingMode.CONVERT;
        options.desiredSize   = 0.2f;
        options.recenter      = true;

        PolyApi.Import(asset, options, ImportAssetCallback);
        Instance.scrollPanel.SetActive(false);
        Instance.loadingPanel.SetActive(true);
    }
Ejemplo n.º 16
0
    private void GetAssetCallback(PolyStatusOr <PolyAsset> result)
    {
        if (!result.Ok)
        {
            Debug.LogError("Failed to get assets. Reason: " + result.Status);
            //statusText.text = "ERROR: " + result.Status;
            return;
        }
        Debug.Log("Successfully got asset!");
        PolyImportOptions options = PolyImportOptions.Default();

        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        options.desiredSize   = 0.5f;
        PolyApi.Import(result.Value, options, ImportAssetCallback);
    }
Ejemplo n.º 17
0
    /// <summary>
    /// Callback for Poly asset GET call.
    /// </summary>
    /// <param name="result">Result of the GET call</param>
    private void GetAssetCallback(PolyStatusOr <PolyAsset> result)
    {
        if (!result.Ok)
        {
            return;
        }

        PolyImportOptions options = PolyImportOptions.Default();

        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        options.desiredSize   = 1.0f;
        options.recenter      = true;

        PolyApi.Import(result.Value, options, ImportAssetCallback);
    }
Ejemplo n.º 18
0
    private static void GetAssetCallback(PolyStatusOr <PolyAsset> getAssetResult, GetPolyResultCallback callback)
    {
        if (!getAssetResult.Ok)
        {
            Debug.LogError("<color=red> Failed to get assets. Reason: " + getAssetResult.Status + "</color>");
            return;
        }

        PolyImportOptions options = PolyImportOptions.Default();

        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        options.desiredSize   = 5.0f;
        options.recenter      = true;

        PolyApi.Import(getAssetResult.Value, options, (asset, importResult) => ImportAssetCallback(asset, importResult, callback));
    }
Ejemplo n.º 19
0
 /// <summary>
 /// Imports the relevant format and corrects that the designated glTF format if need be.
 /// </summary>
 private void ImportFormat(PolyAsset asset, PolyFormat format, PolyImportOptions options,
                           PolyApi.ImportCallback callback)
 {
     asyncImporter.ImportAsync(asset, format, options,
                               (PolyStatus status, GameObject root, IEnumerable meshCreator) => {
         if (!status.ok)
         {
             // Failed.
             callback(asset, new PolyStatusOr <PolyImportResult>(status));
             return;
         }
         PolyImportResult result    = new PolyImportResult(root);
         result.mainThreadThrottler = meshCreator;
         callback(asset, new PolyStatusOr <PolyImportResult>(result));
     });
 }
        ////////////////////////////////////////
        //
        // Import Asset

        public void ImportAsset(PolyAsset asset, Action <GameObject> callback)
        {
            // Set the import options.
            PolyImportOptions options = PolyImportOptions.Default();

            // We want to rescale the imported mesh to a specific size.
            options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
            // The specific size we want assets rescaled to.
            options.desiredSize = .1f;
            // We want the imported assets to be recentered such that their centroid coincides with the origin.
            options.recenter = true;

            PolyApi.Import(asset, options, (importedAsset, result) =>
            {
                OnAssetImported(importedAsset, result, callback);
            });
        }
Ejemplo n.º 21
0
    public void AddFromPoly(PolyAsset asset)
    {
        PolyApi.Import(
            asset,
            PolyImportOptions.Default(),
            (_, res) =>
        {
            Debug.Log("Best asset: " + _.displayName + " by: " + _.authorName);

            if (!res.Ok)
            {
                Debug.LogError("Failed to import asset. :( Reason: " + res.Status);
                return;
            }

            res.Value.gameObject.AddComponent <Rotate>();
        });
    }
Ejemplo n.º 22
0
        public static void TestImportSelection()
        {
            var gltfAssets = Selection.objects
                             .Select(o => AssetDatabase.GetAssetPath(o))
                             .Where(p => !string.IsNullOrEmpty(p))
                             .Where(p => p.Contains(".gltf"))
                             .ToArray();

            if (gltfAssets.Length == 0)
            {
                Debug.LogFormat("No selected .gltf assets");
                return;
            }
            foreach (var asset in gltfAssets)
            {
                DoImport(asset, PolyImportOptions.Default(), true);
            }
        }
Ejemplo n.º 23
0
    /// <summary>
    /// Specified callback on GET Asset.
    /// </summary>
    /// <param name="result">Result of the GET method</param>
    public void GetAssetCallback(PolyStatusOr <PolyAsset> result)
    {
        if (!result.Ok)
        {
            StatusField.text = "ERROR: " + result.Status;
            isRunning        = false;
            return;
        }

        PolyImportOptions options = PolyImportOptions.Default();

        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        options.desiredSize   = 5.0f;
        options.recenter      = true;

        StatusField.text = "Importing...";
        PolyApi.Import(result.Value, options, ImportAssetCallback);
    }
Ejemplo n.º 24
0
    private void MenuItemClicked(PolyAsset asset)
    {
        if (selectedAsset == asset)
        {
            return;
        }

        ui.AddMessageToBox(asset.displayName + " selected");
        selectedAsset = asset;

        // Set options for import so the assets aren't crazy sizes
        PolyImportOptions options = PolyImportOptions.Default();

        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        options.desiredSize   = 1.0f;
        options.recenter      = true;

        PolyApi.Import(asset, options, ParseToGameObject);
    }
Ejemplo n.º 25
0
        public void Import(PolyAsset asset, PolyImportOptions options, PolyApi.ImportCallback callback = null)
        {
            PolyFormat gltfFormat  = asset.GetFormatIfExists(PolyFormatType.GLTF);
            PolyFormat gltf2Format = asset.GetFormatIfExists(PolyFormatType.GLTF_2);

            if (gltf2Format != null && gltfFormat == null)
            {
                FetchAndImportFormat(asset, gltf2Format, options, callback);
            }
            else if (gltfFormat != null)
            {
                FetchAndImportFormat(asset, gltfFormat, options, callback);
            }
            else
            {
                callback(asset, new PolyStatusOr <PolyImportResult>(
                             PolyStatus.Error("Neither glTF or glTF_2 format was present in asset")));
            }
        }
    // Callback invoked when the featured assets results are returned.
    private void GetAssetCallback2(PolyStatusOr <PolyAsset> result)
    {
        if (!result.Ok)
        {
            Debug.LogError("Failed to get assets. Reason: " + result.Status);
            return;
        }
        Debug.Log("Successfully got asset!");

        // Set the import options.
        PolyImportOptions options = PolyImportOptions.Default();

        // We want to rescale the imported mesh to a specific size.
        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        // The specific size we want assets rescaled to (fit in a 5x5x5 box):
        //options.desiredSize = 1.0f;
        // We want the imported assets to be recentered such that their centroid coincides with the origin:
        options.recenter = true;

        PolyApi.Import(result.Value, options, ImportAssetCallback);
    }
Ejemplo n.º 27
0
    void ImportAsset(int idx)
    {
        Debug.Log("button clicked)");
        List <PolyAsset> assetsInUse = new List <PolyAsset>();

        // Set the import options.
        PolyImportOptions options = PolyImportOptions.Default();

        // We want to rescale the imported meshes to a specific size.
        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        // The specific size we want assets rescaled to (fit in a 1x1x1 box):
        options.desiredSize = 1.0f;
        // We want the imported assets to be recentered such that their centroid coincides with the origin:
        options.recenter = true;

        Debug.Log("import asset " + idx);
        PolyApi.Import(assetsInPalette[idx], options, ImportAssetCallback);
        assetsInUse.Add(assetsInPalette[idx]);

        // Show attributions for the assets we display.
        //attributionsText.text = PolyApi.GenerateAttributions(includeStatic: true, runtimeAssets: assetsInUse);
    }
Ejemplo n.º 28
0
    private void GetDonuts(PolyStatusOr <PolyListAssetsResult> result)
    {
        // Set options for import so the assets aren't crazy sizes
        PolyImportOptions options = PolyImportOptions.Default();

        options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
        options.desiredSize   = 2.0f;
        options.recenter      = true;
        // List our assets
        List <PolyAsset> assetsInUse = new List <PolyAsset>();

        // Loop through the list and display the first 3
        for (int i = 0; i < Mathf.Min(3, result.Value.assets.Count); i++)
        {
            // Import our assets into the scene with the ImportDonuts function
            PolyApi.Import(result.Value.assets[i], options, ImportDonuts);
            assetsInUse.Add(result.Value.assets[i]);

            // Attributions Text
            // attributionsText.text = PolyApi.GenerateAttributions(includeStatic: false, runtimeAssets: assetsInUse);
        }
    }
Ejemplo n.º 29
0
        private void CmdImp(string[] args)
        {
            int index;

            if (args.Length == 0 || !int.TryParse(args[0], out index))
            {
                PrintLn("*** Specify the index of the asset to import (as given by the 'show' command).");
                return;
            }
            if (index < 0 || index >= currentResults.Count)
            {
                PrintLn("*** Invalid index. Use the 'show' command to see the valid assets.");
                return;
            }
            PolyAsset         assetToImport = currentResults[index];
            PolyImportOptions options       = new PolyImportOptions();

            options.rescalingMode = GetEnumOpt(args, "-m", PolyImportOptions.RescalingMode.FIT);
            options.recenter      = !HasOpt(args, "--no-center");
            options.desiredSize   = GetFloatOpt(args, "--ds", 5.0f);
            options.scaleFactor   = GetFloatOpt(args, "--sf", 1.0f);
            PrintLn("Importing asset... May take a while. Please wait!");
            PolyApi.Import(assetToImport, options, (PolyAsset asset, PolyStatusOr <PolyImportResult> result) => {
                if (!result.Ok)
                {
                    PrintLn("ERROR: failed to import {0}: {1}", asset.name, result.Status);
                    return;
                }
                PrintLn("Successfully imported asset '{0}' ({1})", asset.name, asset.displayName);

                if (currentAsset != null)
                {
                    Destroy(currentAsset);
                    currentAsset = null;
                }

                currentAsset = result.Value.gameObject;
            });
        }
Ejemplo n.º 30
0
        private void AlreadyGotAssetList()
        {
            if (_statusText == null)
            {
                return;
            }

            _statusText.text = "Importing...";

            // Set the import options.
            PolyImportOptions options = PolyImportOptions.Default();

            // We want to rescale the imported meshes to a specific size.
            options.rescalingMode = PolyImportOptions.RescalingMode.FIT;
            // The specific size we want assets rescaled to (fit in a 1x1x1 box):
            options.desiredSize = 5.0f;
            // We want the imported assets to be recentered such that their centroid coincides with the origin:
            options.recenter = true;

            int i = Random.Range(0, results.Value.assets.Count - 1);

            PolyApi.Import(results.Value.assets[i], options, ImportAssetCallback);
        }