Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com.rest.blockadelabs 1.0.0-preview.5 #6

Merged
merged 3 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added control image and control model to requests
  • Loading branch information
StephenHodgson committed Jun 26, 2023
commit 8024fc12b8cf93b1c1b64b6834d3754ff5878d79
2 changes: 2 additions & 0 deletions BlockadeLabs/Assets/Demo/SkyboxDemo.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using BlockadeLabs.Skyboxes;
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
Expand Down Expand Up @@ -51,7 +52,7 @@ public async Task<IReadOnlyList<SkyboxStyle>> GetSkyboxStylesAsync(CancellationT
/// <param name="skyboxRequest"><see cref="SkyboxRequest"/>.</param>
/// <param name="pollingInterval">Optional, polling interval in seconds.</param>
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param>
/// <returns></returns>
/// <returns><see cref="SkyboxInfo"/>.</returns>
public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, int? pollingInterval = null, CancellationToken cancellationToken = default)
{
var formData = new WWWForm();
Expand Down Expand Up @@ -82,8 +83,21 @@ public async Task<SkyboxInfo> GenerateSkyboxAsync(SkyboxRequest skyboxRequest, i
formData.AddField("return_depth", skyboxRequest.Depth.ToString());
}

if (skyboxRequest.ControlImage != null)
{
if (!string.IsNullOrWhiteSpace(skyboxRequest.ControlModel))
{
formData.AddField("control_model", skyboxRequest.ControlModel);
}

using var imageData = new MemoryStream();
await skyboxRequest.ControlImage.CopyToAsync(imageData, cancellationToken);
formData.AddBinaryData("control_image", imageData.ToArray(), skyboxRequest.ControlImageFileName);
skyboxRequest.Dispose();
}

var response = await Rest.PostAsync(GetUrl("skybox"), formData, parameters: new RestParameters(client.DefaultRequestHeaders), cancellationToken);
response.Validate(true);
response.Validate();
var skyboxInfo = JsonConvert.DeserializeObject<SkyboxInfo>(response.Body, client.JsonSerializationOptions);

while (!cancellationToken.IsCancellationRequested)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System;
using System.IO;
using UnityEngine;

namespace BlockadeLabs.Skyboxes
{
public sealed class SkyboxRequest
public sealed class SkyboxRequest : IDisposable
{
/// <summary>
/// Creates a new Skybox Request.
/// </summary>
/// <param name="prompt">
/// Text prompt describing the skybox world you wish to create.
/// Maximum number of characters: 550.
/// If you are using <see cref="SkyboxStyleId"/> then the maximum number of characters is defined
/// in the max-char response parameter defined for each style.
/// </param>
/// <param name="negativeText">
/// Describe things to avoid in the skybox world you wish to create.
/// Maximum number of characters: 200.
/// If you are using <see cref="SkyboxStyleId"/> then the maximum number of characters is defined
/// in the negative-text-max-char response parameter defined for each style.
/// </param>
/// <param name="seed">
/// Send 0 for a random seed generation.
/// Any other number (1-2147483647) set will be used to "freeze" the image generator generator and
/// create similar images when run again with the same seed and settings.
/// </param>
/// <param name="skyboxStyleId">
/// Id of predefined style that influences the overall aesthetic of your skybox generation.
/// </param>
/// <param name="remixImagineId">
/// ID of a previously generated skybox.
/// </param>
/// <param name="depth">
/// Return depth map image.
/// </param>
public SkyboxRequest(
string prompt,
string negativeText = null,
Expand All @@ -18,16 +53,263 @@ public sealed class SkyboxRequest
Depth = depth;
}

/// <summary>
/// Creates a new Skybox Request.
/// </summary>
/// <param name="prompt">
/// Text prompt describing the skybox world you wish to create.
/// Maximum number of characters: 550.
/// If you are using <see cref="SkyboxStyleId"/> then the maximum number of characters is defined
/// in the max-char response parameter defined for each style.
/// </param>
/// <param name="controlImagePath">
/// File path to the control image for the request.
/// </param>
/// <param name="controlModel">
/// Model used for the <see cref="ControlImage"/>.
/// Currently the only option is: "scribble".
/// </param>
/// <param name="negativeText">
/// Describe things to avoid in the skybox world you wish to create.
/// Maximum number of characters: 200.
/// If you are using <see cref="SkyboxStyleId"/> then the maximum number of characters is defined
/// in the negative-text-max-char response parameter defined for each style.
/// </param>
/// <param name="seed">
/// Send 0 for a random seed generation.
/// Any other number (1-2147483647) set will be used to "freeze" the image generator generator and
/// create similar images when run again with the same seed and settings.
/// </param>
/// <param name="skyboxStyleId">
/// Id of predefined style that influences the overall aesthetic of your skybox generation.
/// </param>
/// <param name="remixImagineId">
/// ID of a previously generated skybox.
/// </param>
/// <param name="depth">
/// Return depth map image.
/// </param>
public SkyboxRequest(
string prompt,
string controlImagePath,
string controlModel = null,
string negativeText = null,
int? seed = null,
int? skyboxStyleId = null,
int? remixImagineId = null,
bool depth = false)
: this(
prompt,
File.OpenRead(controlImagePath),
Path.GetFileName(controlImagePath),
controlModel,
negativeText,
seed,
skyboxStyleId,
remixImagineId,
depth)
{
}

/// <summary>
/// Creates a new Skybox Request.
/// </summary>
/// <param name="prompt">
/// Text prompt describing the skybox world you wish to create.
/// Maximum number of characters: 550.
/// If you are using <see cref="SkyboxStyleId"/> then the maximum number of characters is defined
/// in the max-char response parameter defined for each style.
/// </param>
/// <param name="controlImage">
/// <see cref="Texture2D"/> Control image used to influence the generation.
/// The image needs to be exactly 1024 pixels wide and 512 pixels tall PNG equirectangular projection image
/// of a scribble with black background and white brush strokes.
/// </param>
/// <param name="controlModel">
/// Model used for the <see cref="ControlImage"/>.
/// Currently the only option is: "scribble".
/// </param>
/// <param name="negativeText">
/// Describe things to avoid in the skybox world you wish to create.
/// Maximum number of characters: 200.
/// If you are using <see cref="SkyboxStyleId"/> then the maximum number of characters is defined
/// in the negative-text-max-char response parameter defined for each style.
/// </param>
/// <param name="seed">
/// Send 0 for a random seed generation.
/// Any other number (1-2147483647) set will be used to "freeze" the image generator generator and
/// create similar images when run again with the same seed and settings.
/// </param>
/// <param name="skyboxStyleId">
/// Id of predefined style that influences the overall aesthetic of your skybox generation.
/// </param>
/// <param name="remixImagineId">
/// ID of a previously generated skybox.
/// </param>
/// <param name="depth">
/// Return depth map image.
/// </param>
public SkyboxRequest(
string prompt,
Texture2D controlImage,
string controlModel = null,
string negativeText = null,
int? seed = null,
int? skyboxStyleId = null,
int? remixImagineId = null,
bool depth = false)
: this(
prompt,
new MemoryStream(controlImage.EncodeToPNG()),
!string.IsNullOrWhiteSpace(controlImage.name) ? $"{controlImage.name}.png" : null,
controlModel,
negativeText,
seed,
skyboxStyleId,
remixImagineId,
depth)
{
if (controlImage.height != 512 || controlImage.width != 1024)
{
throw new ArgumentException($"{nameof(ControlImage)} dimensions should be 512x1024");
}
}

/// <summary>
/// Creates a new Skybox Request.
/// </summary>
/// <param name="prompt">
/// Text prompt describing the skybox world you wish to create.
/// Maximum number of characters: 550.
/// If you are using <see cref="SkyboxStyleId"/> then the maximum number of characters is defined
/// in the max-char response parameter defined for each style.
/// </param>
/// <param name="controlImage">
/// <see cref="Stream"/> data of control image for request.
/// </param>
/// <param name="controlImageFileName">
/// File name of <see cref="controlImage"/>.
/// </param>
/// <param name="controlModel">
/// Model used for the <see cref="ControlImage"/>.
/// Currently the only option is: "scribble".
/// </param>
/// <param name="negativeText">
/// Describe things to avoid in the skybox world you wish to create.
/// Maximum number of characters: 200.
/// If you are using <see cref="SkyboxStyleId"/> then the maximum number of characters is defined
/// in the negative-text-max-char response parameter defined for each style.
/// </param>
/// <param name="seed">
/// Send 0 for a random seed generation.
/// Any other number (1-2147483647) set will be used to "freeze" the image generator generator and
/// create similar images when run again with the same seed and settings.
/// </param>
/// <param name="skyboxStyleId">
/// Id of predefined style that influences the overall aesthetic of your skybox generation.
/// </param>
/// <param name="remixImagineId">
/// ID of a previously generated skybox.
/// </param>
/// <param name="depth">
/// Return depth map image.
/// </param>
public SkyboxRequest(
string prompt,
Stream controlImage,
string controlImageFileName,
string controlModel = null,
string negativeText = null,
int? seed = null,
int? skyboxStyleId = null,
int? remixImagineId = null,
bool depth = false)
: this(prompt, negativeText, seed, skyboxStyleId, remixImagineId, depth)
{
ControlImage = controlImage;

if (string.IsNullOrWhiteSpace(controlImageFileName))
{
const string defaultImageName = "control_image.png";
controlImageFileName = defaultImageName;
}

ControlImageFileName = controlImageFileName;

ControlModel = controlModel;
}

~SkyboxRequest() => Dispose(false);

/// <summary>
/// Text prompt describing the skybox world you wish to create.
/// Maximum number of characters: 550.
/// If you are using <see cref="SkyboxStyleId"/> then the maximum number of characters is defined
/// in the max-char response parameter defined for each style.
/// </summary>
public string Prompt { get; }

/// <summary>
/// Describe things to avoid in the skybox world you wish to create.
/// Maximum number of characters: 200.
/// If you are using <see cref="SkyboxStyleId"/> then the maximum number of characters is defined
/// in the negative-text-max-char response parameter defined for each style.
/// </summary>
public string NegativeText { get; }

/// <summary>
/// Send 0 for a random seed generation.
/// Any other number (1-2147483647) set will be used to "freeze" the image generator generator and
/// create similar images when run again with the same seed and settings.
/// </summary>
public int? Seed { get; }

/// <summary>
/// Id of predefined style that influences the overall aesthetic of your skybox generation.
/// </summary>
public int? SkyboxStyleId { get; }

/// <summary>
/// ID of a previously generated skybox.
/// </summary>
public int? RemixImagineId { get; }

/// <summary>
/// Return depth map image.
/// </summary>
public bool Depth { get; }

/// <summary>
/// Control image used to influence the generation.
/// The image needs to be exactly 1024 pixels wide and 512 pixels tall PNG equirectangular projection image
/// of a scribble with black background and white brush strokes.
/// </summary>
public Stream ControlImage { get; }

/// <summary>
/// File name of <see cref="ControlImage"/>.
/// </summary>
public string ControlImageFileName { get; }

/// <summary>
/// Model used for the <see cref="ControlImage"/>.
/// Currently the only option is: "scribble".
/// </summary>
public string ControlModel { get; }

private void Dispose(bool disposing)
{
if (disposing)
{
ControlImage?.Close();
ControlImage?.Dispose();
}
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
}