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.1 #1

Merged
merged 18 commits into from
Jun 10, 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 demo scene
  • Loading branch information
StephenHodgson committed May 29, 2023
commit 68604d4a5b3040073b78c5b4a2d269f8c09a0fca
8 changes: 8 additions & 0 deletions BlockadeLabs/Assets/Demo.meta

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

20 changes: 20 additions & 0 deletions BlockadeLabs/Assets/Demo/BlockadeLabs.Demo.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "BlockadeLabs.Demo",
"rootNamespace": "BlockadeLabs.Demo",
"references": [
"GUID:0e64303a4898a4d4ba94306e88e6c025",
"GUID:a6609af893242c7438d701ddd4cce46a",
"GUID:6ab1da3c58a70364e92dc36aaec78f43",
"GUID:7958db66189566541a6363568aee1575",
"GUID:6055be8ebefd69e48b49212b09b47b2f"
],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}
7 changes: 7 additions & 0 deletions BlockadeLabs/Assets/Demo/BlockadeLabs.Demo.asmdef.meta

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

110 changes: 110 additions & 0 deletions BlockadeLabs/Assets/Demo/SkyboxDemo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using BlockadeLabs.Skyboxes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using Utilities.Extensions;

namespace BlockadeLabs.Demo
{
public class SkyboxDemo : MonoBehaviour
{
[SerializeField]
private TMP_InputField promptInputField;

[SerializeField]
private TMP_Dropdown skyboxStyleDropdown;

[SerializeField]
private Button generateButton;

[SerializeField]
private Material skyboxMaterial;

private BlockadeLabsClient api;

private CancellationTokenSource lifetimeCancellationTokenSource;

private IReadOnlyList<SkyboxStyle> skyboxOptions;

private void OnValidate()
{
promptInputField.Validate();
skyboxStyleDropdown.Validate();
generateButton.Validate();
}

private void Awake()
{
OnValidate();
lifetimeCancellationTokenSource = new CancellationTokenSource();

try
{
api = new BlockadeLabsClient();
}
catch (Exception e)
{
Debug.LogError($"Failed to create {nameof(BlockadeLabsClient)}!\n{e}");
enabled = false;
return;
}

GetSkyboxStyles();
generateButton.onClick.AddListener(GenerateSkybox);
promptInputField.onSubmit.AddListener(GenerateSkybox);
promptInputField.onValueChanged.AddListener(ValidateInput);
}

private void ValidateInput(string input)
{
generateButton.interactable = !string.IsNullOrWhiteSpace(input);
}

private void GenerateSkybox() => GenerateSkybox(promptInputField.text);

private async void GenerateSkybox(string prompt)
{
if (string.IsNullOrWhiteSpace(prompt))
{
Debug.LogWarning("No prompt given!");
return;
}

try
{
var request = new SkyboxRequest(prompt, skyboxStyleId: skyboxOptions[skyboxStyleDropdown.value].Id);
var (texture, skyboxInfo) = await api.SkyboxEndpoint.GenerateSkyboxAsync(request, lifetimeCancellationTokenSource.Token);
skyboxMaterial.mainTexture = texture;
Debug.Log($"Successfully created skybox: {skyboxInfo.Id}");
}
catch (Exception e)
{
Debug.LogError(e);
}
}

private void OnDestroy()
{
lifetimeCancellationTokenSource.Cancel();
}

private async void GetSkyboxStyles()
{
try
{
skyboxOptions = await api.SkyboxEndpoint.GetSkyboxStylesAsync(lifetimeCancellationTokenSource.Token).ConfigureAwait(true);
var dropdownOptions = new List<TMP_Dropdown.OptionData>(skyboxOptions.Count);
dropdownOptions.AddRange(skyboxOptions.Select(skyboxStyle => new TMP_Dropdown.OptionData(skyboxStyle.Name)));
skyboxStyleDropdown.options = dropdownOptions;
}
catch (Exception e)
{
Debug.LogError(e);
}
}
}
}
11 changes: 11 additions & 0 deletions BlockadeLabs/Assets/Demo/SkyboxDemo.cs.meta

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

Loading