Skip to content

Commit

Permalink
com.rest.blockadelabs 1.3.1 (#16)
Browse files Browse the repository at this point in the history
- Added ability to specify multiple export options when requesting new skybox
- Added SkyboxRequest.HqDepth option
- Added DefaultExportOptions
- Added SkyboxExportOption keys for easier convenience when getting exported asset
- Updated docs
  • Loading branch information
StephenHodgson committed Dec 15, 2023
1 parent f53da9f commit 39ebe70
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ foreach (var exportOption in exportOptions)
{
Debug.Log($"{exportOption.Id}: {exportOption.Name} | {exportOption.Key}");
}

var request = new SkyboxRequest("mars", enhancePrompt: true);
// Generates ALL export options for the skybox
var skyboxInfo = await api.SkyboxEndpoint.GenerateSkyboxAsync(request, exportOptions);
Debug.Log($"Successfully created skybox: {skyboxInfo.Id}");
```

#### [Generate Skybox](https://api-documentation.blockadelabs.com/api/skybox.html#generate-skybox)
Expand All @@ -178,7 +183,7 @@ var request = new SkyboxRequest("mars", enhancePrompt: true);
var skyboxInfo = await api.SkyboxEndpoint.GenerateSkyboxAsync(request);
Debug.Log($"Successfully created skybox: {skyboxInfo.Id}");

if (skyboxInfo.TryGetAsset<Texture2D>("equirectangular-png", out var texture))
if (skyboxInfo.TryGetAsset<Texture2D>(SkyboxExportOption.Equirectangular_PNG, out var texture))
{
skyboxMaterial.mainTexture = texture;
}
Expand All @@ -193,10 +198,10 @@ var skyboxId = 42;
var api = new BlockadeLabsClient();
var skyboxInfo = await api.SkyboxEndpoint.GetSkyboxInfoAsync(skyboxId);
Debug.Log($"Skybox: {result.Id}");
// Note: If you wish to use the returned skybox textures you'll need to additionally call await SkyboxInfo.LoadAssetsAsync(); before you can assign them to a material property.
// Note: If you wish to use the returned skybox info textures you'll need to additionally call await SkyboxInfo.LoadAssetsAsync(); before you can assign them to a material property.
await skyboxInfo.LoadAssetsAsync();

if (skyboxInfo.TryGetAsset<Texture2D>("equirectangular-png", out var texture))
if (skyboxInfo.TryGetAsset<Texture2D>(SkyboxExportOption.Equirectangular_PNG, out var texture))
{
skyboxMaterial.mainTexture = texture;
}
Expand All @@ -206,18 +211,18 @@ if (skyboxInfo.TryGetAsset<Texture2D>("equirectangular-png", out var texture))

Exports the skybox with the requested export type.

> Note: You can also specify the export types when initially generating a skybox.
```csharp
var skyboxId = 42;
var api = new BlockadeLabsClient();
var skyboxInfo = await api.SkyboxEndpoint.GetSkyboxInfoAsync(skyboxId);
var exportOptions = await api.SkyboxEndpoint.GetAllSkyboxExportOptionsAsync();
var exportOption = exportOptions.FirstOrDefault(option => option.Key == "depth-map-png");
skyboxInfo = await api.SkyboxEndpoint.ExportSkyboxAsync(skyboxInfo, exportOption);
skyboxInfo = await api.SkyboxEndpoint.ExportSkyboxAsync(skyboxInfo, DefaultExportOptions.DepthMap_PNG);
await skyboxInfo.LoadAssetsAsync();

if (skyboxInfo.TryGetAsset<Texture2D>("depth-map-png", out var texture))
if (skyboxInfo.TryGetAsset<Texture2D>(SkyboxExportOption.DepthMap_PNG, out var texture))
{
skyboxMaterial.depthTexture = skyboxInfo.DepthTexture;
skyboxMaterial.depthTexture = texture;
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,20 +739,20 @@ private void RenderHistory()
{
switch (export.Key)
{
case "cube-map-default-png":
case "cube-map-roblox-png":
case SkyboxExportOption.CubeMap_PNG:
case SkyboxExportOption.CubeMap_Roblox_PNG:
EditorGUILayout.ObjectField(asset, typeof(Cubemap), false);
break;
case "equirectangular-png":
case "equirectangular-jpg":
case "depth-map-png":
case "hdri-hdr":
case "hdri-exr":
case SkyboxExportOption.Equirectangular_JPG:
case SkyboxExportOption.Equirectangular_PNG:
case SkyboxExportOption.DepthMap_PNG:
case SkyboxExportOption.HDRI_HDR:
case SkyboxExportOption.HDRI_EXR:
EditorGUILayout.ObjectField(asset, typeof(Texture2D), false);
break;
case "video-landscape-mp4":
case "video-portrait-mp4":
case "video-square-mp4":
case SkyboxExportOption.Video_LandScape_MP4:
case SkyboxExportOption.Video_Portrait_MP4:
case SkyboxExportOption.Video_Square_MP4:
EditorGUILayout.ObjectField(asset, typeof(VideoClip), false);
break;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
using System.Threading.Tasks;
using Unity.SharpZipLib.Zip;
using UnityEngine;
using UnityEngine.Scripting;

namespace BlockadeLabs
{
[Preserve]
internal static class ExportUtilities
{
/// <summary>
Expand All @@ -19,6 +21,7 @@ internal static class ExportUtilities
/// <param name="path"></param>
/// <param name="cancellationToken"></param>
/// <returns>List of files in the archive.</returns>
[Preserve]
internal static async Task<IReadOnlyList<string>> UnZipAsync(string path, CancellationToken cancellationToken = default)
{
path = path.Replace("file:https://", string.Empty);
Expand Down Expand Up @@ -103,6 +106,7 @@ internal static async Task<IReadOnlyList<string>> UnZipAsync(string path, Cancel
/// </summary>
/// <param name="textures">List of local files to be used to create <see cref="Cubemap"/>.</param>
/// <returns><see cref="Cubemap"/></returns>
[Preserve]
internal static Cubemap BuildCubemap(IReadOnlyList<Texture2D> textures)
{
if (textures is not { Count: 6 })
Expand Down Expand Up @@ -141,13 +145,15 @@ internal static Cubemap BuildCubemap(IReadOnlyList<Texture2D> textures)
return cubemap;
}

[Preserve]
internal enum TextureRotation
{
Clockwise90,
Counterclockwise90,
Rotate180
}

[Preserve]
internal static Texture2D Rotate(this Texture2D texture, TextureRotation rotation)
{
var width = texture.width;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using UnityEngine.Scripting;

namespace BlockadeLabs.Skyboxes
{
[Preserve]
public static class DefaultExportOptions
{
[Preserve]
public static SkyboxExportOption Equirectangular_JPG { get; } = new SkyboxExportOption(1, "JPG", SkyboxExportOption.Equirectangular_JPG);

[Preserve] public static SkyboxExportOption Equirectangular_PNG { get; } = new SkyboxExportOption(2, "PNG", SkyboxExportOption.Equirectangular_PNG);
public static SkyboxExportOption CubeMap_Roblox_PNG { get; } = new SkyboxExportOption(3, "Cube Map - Roblox", SkyboxExportOption.CubeMap_Roblox_PNG);

[Preserve] public static SkyboxExportOption HDRI_HDR { get; } = new SkyboxExportOption(4, "HDRI HDR", SkyboxExportOption.HDRI_HDR);
public static SkyboxExportOption HDRI_EXR { get; } = new SkyboxExportOption(5, "HDRI EXR", SkyboxExportOption.HDRI_EXR);

[Preserve] public static SkyboxExportOption DepthMap_PNG { get; } = new SkyboxExportOption(6, "Depth Map", SkyboxExportOption.DepthMap_PNG);
public static SkyboxExportOption Video_LandScape_MP4 { get; } = new SkyboxExportOption(7, "Video Landscape", SkyboxExportOption.Video_LandScape_MP4);

[Preserve] public static SkyboxExportOption Video_Portrait_MP4 { get; } = new SkyboxExportOption(8, "Video Portrait", SkyboxExportOption.Video_Portrait_MP4);
public static SkyboxExportOption Video_Square_MP4 { get; } = new SkyboxExportOption(9, "Video Square", SkyboxExportOption.Video_Square_MP4);

[Preserve] public static SkyboxExportOption CubeMap_PNG { get; } = new SkyboxExportOption(10, "Cube Map - Default", SkyboxExportOption.CubeMap_PNG);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
Expand Down Expand Up @@ -77,7 +76,18 @@ public async Task<IReadOnlyList<SkyboxStyle>> GetSkyboxStylesAsync(CancellationT
/// <param name="pollingInterval">Optional, polling interval in seconds.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="SkyboxInfo"/>.</returns>
public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, SkyboxExportOption exportOption = null, int? pollingInterval = null, CancellationToken cancellationToken = default)
public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, SkyboxExportOption exportOption, int? pollingInterval = null, CancellationToken cancellationToken = default)
=> await GenerateSkyboxAsync(skyboxRequest, new[] { exportOption }, pollingInterval, cancellationToken);

/// <summary>
/// Generate a skybox image.
/// </summary>
/// <param name="skyboxRequest"><see cref="SkyboxRequest"/>.</param>
/// <param name="exportOptions">Optional, <see cref="SkyboxExportOption"/>s.</param>
/// <param name="pollingInterval">Optional, polling interval in seconds.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns><see cref="SkyboxInfo"/>.</returns>
public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, SkyboxExportOption[] exportOptions = null, int? pollingInterval = null, CancellationToken cancellationToken = default)
{
var formData = new WWWForm();
formData.AddField("prompt", skyboxRequest.Prompt);
Expand Down Expand Up @@ -107,6 +117,11 @@ public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, S
formData.AddField("remix_imagine_id", skyboxRequest.RemixImagineId.Value);
}

if (skyboxRequest.HqDepth.HasValue)
{
formData.AddField("return_depth_hq", skyboxRequest.HqDepth.Value.ToString());
}

if (skyboxRequest.ControlImage != null)
{
if (!string.IsNullOrWhiteSpace(skyboxRequest.ControlModel))
Expand Down Expand Up @@ -149,24 +164,23 @@ public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, S
throw new Exception($"Failed to generate skybox! {skyboxInfo.Id} -> {skyboxInfo.Status}\nError: {skyboxInfo.ErrorMessage}\n{skyboxInfo}");
}

if (exportOption != null)
var exportTasks = new List<Task>();

if (exportOptions != null)
{
skyboxInfo = await ExportSkyboxAsync(skyboxInfo, exportOption, pollingInterval, cancellationToken);
foreach (var exportOption in exportOptions)
{
exportTasks.Add(ExportSkyboxAsync(skyboxInfo, exportOption, pollingInterval, cancellationToken));
}
}
else
{
var exportOptions = await GetAllSkyboxExportOptionsAsync(cancellationToken);
var info = skyboxInfo;
var pngExportTasks = new List<Task>
{
ExportSkyboxAsync(info, exportOptions.FirstOrDefault(option => option.Key.Equals("equirectangular-png")), pollingInterval, cancellationToken),
ExportSkyboxAsync(info, exportOptions.FirstOrDefault(option => option.Key.Equals("depth-map-png")), pollingInterval, cancellationToken)
};

await Task.WhenAll(pngExportTasks).ConfigureAwait(true);
skyboxInfo = await GetSkyboxInfoAsync(skyboxInfo.Id, cancellationToken);
exportTasks.Add(ExportSkyboxAsync(skyboxInfo, DefaultExportOptions.Equirectangular_PNG, pollingInterval, cancellationToken));
exportTasks.Add(ExportSkyboxAsync(skyboxInfo, DefaultExportOptions.DepthMap_PNG, pollingInterval, cancellationToken));
}

await Task.WhenAll(exportTasks).ConfigureAwait(true);
skyboxInfo = await GetSkyboxInfoAsync(skyboxInfo.Id, cancellationToken);
await skyboxInfo.LoadAssetsAsync(EnableDebug, cancellationToken);
return skyboxInfo;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,26 @@
using Newtonsoft.Json;
using System;
using UnityEngine;
using UnityEngine.Scripting;

namespace BlockadeLabs.Skyboxes
{
[Preserve]
[Serializable]
public sealed class SkyboxExportOption
{
public const string Equirectangular_JPG = "equirectangular-jpg";
public const string Equirectangular_PNG = "equirectangular-png";
public const string CubeMap_Roblox_PNG = "cube-map-roblox-png";
public const string HDRI_HDR = "hdri-hdr";
public const string HDRI_EXR = "hdri-exr";
public const string DepthMap_PNG = "depth-map-png";
public const string Video_LandScape_MP4 = "video-landscape-mp4";
public const string Video_Portrait_MP4 = "video-portrait-mp4";
public const string Video_Square_MP4 = "video-square-mp4";
public const string CubeMap_PNG = "cube-map-default-png";

[Preserve]
[JsonConstructor]
public SkyboxExportOption(
[JsonProperty("id")] int id,
Expand All @@ -20,21 +34,27 @@ public sealed class SkyboxExportOption
this.key = key;
}

[Preserve]
[SerializeField]
private string name;

[Preserve]
[JsonProperty("name")]
public string Name => name;

[Preserve]
[SerializeField]
private int id;

[Preserve]
[JsonProperty("id")]
public int Id => id;

[Preserve]
[SerializeField]
private string key;

[Preserve]
[JsonProperty("key")]
public string Key => key;
}
Expand Down
Loading

0 comments on commit 39ebe70

Please sign in to comment.