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

fix: update upscale editor #188

Merged
merged 8 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion package/Editor/Images/ImagesUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private void InitializeButtons()
},
{ "Upscale Image", () => {
CommonUtils.FetchTextureFromURL(Images.GetImageDataById(selectedTextureId).Url, response => {
UpscaleEditor.ShowWindow(response, Images.GetImageDataById(selectedTextureId));
UpscaleEditor.UpscaleEditor.ShowWindow(response, Images.GetImageDataById(selectedTextureId));
});
}
},
Expand Down
172 changes: 169 additions & 3 deletions package/Editor/UpscaleEditor/UpscaleEditor.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.EditorCoroutines.Editor;
using UnityEditor;
using UnityEngine;

namespace Scenario.Editor
namespace Scenario.Editor.UpscaleEditor
{
public class UpscaleEditor : EditorWindow
{
private static readonly float MinimumWidth = 1650f;
#region Public Fields
#endregion

#region Private Fields

private static readonly UpscaleEditorUI UpscaleEditorUI = new();

#endregion

#region MonoBehaviourCallback

[MenuItem("Scenario/Editors/Upscale Editor", false, 5)]
public static void ShowWindow()
{
var window = EditorWindow.GetWindow(typeof(UpscaleEditor), false, "Upscale Editor") as UpscaleEditor;
window.minSize = new Vector2(MinimumWidth, window.minSize.y);
window.autoRepaintOnSceneChange = true;
window.minSize = new Vector2(720, 540);
}

public static void ShowWindow(Texture2D selectedTexture, ImageDataStorage.ImageData imageData)
Expand All @@ -25,11 +39,163 @@ public static void ShowWindow(Texture2D selectedTexture, ImageDataStorage.ImageD
private void OnGUI()
{
UpscaleEditorUI.OnGUI(this.position);
UpscaleEditorUI.UpscaleEditor = this;
}

private void OnDestroy()
{
UpscaleEditorUI.currentImage = null;
}

#endregion

#region Public Methods

/// <summary>
/// Trigger the coroutine to get the upscale result.
/// </summary>
/// <param name="_jobId"></param>
/// <param name="_answer"></param>
public void LaunchProgressUpscale(string _jobId, Action<string> _answer)
{
if (!string.IsNullOrEmpty(_jobId))
{
EditorCoroutineUtility.StartCoroutineOwnerless(GetProgressUpscale(_jobId, _answer));
}
}

#endregion

#region Private Methods

/// <summary>
/// Editor Coroutine to processing upscale.
/// Get the progress of the job id and wait the success status.
/// </summary>
/// <param name="_jobId"> JobId to listen </param>
/// <param name="_response"> Return the result of the content at the end of the process </param>
/// <returns></returns>
IEnumerator GetProgressUpscale(string _jobId, Action<string> _response)
{
bool inProgress = true;

while (inProgress)
{
if (inProgress)
{
ApiClient.RestGet($"jobs/{_jobId}", response =>
{
var progressResponse = JsonConvert.DeserializeObject<Root>(response.Content);

if (progressResponse != null)
{
if (!string.IsNullOrEmpty(progressResponse.job.status))
{
if (!progressResponse.job.status.Equals("success"))
{
switch (progressResponse.job.status)
{
case "warming-up":
Debug.Log("Upscale in preparation... wait...");
break;

case "queued":
Debug.Log("Upscale in queue... wait ...");
break;

case "in-progress":
Debug.Log("Upscale in progress... wait...");
break;

default:
Debug.Log("Upscale... wait...");
break;
}
inProgress = true;
}
else
{
Debug.Log("Upscale progress done: " + response.Content);
inProgress = false;
_response?.Invoke(response.Content);
return;
}
}
}
});
yield return new WaitForSecondsRealtime(4);
}
else
{
yield break;
}
}

yield return null;
}

#endregion

}

#region API_DTO

/// <summary>
/// Asset result object
/// </summary>
public class Asset
{
public string id { get; set; }
public string url { get; set; }
public string mimeType { get; set; }
public Metadata metadata { get; set; }
public string ownerId { get; set; }
public string authorId { get; set; }
public DateTime createdAt { get; set; }
public DateTime updatedAt { get; set; }
public string privacy { get; set; }
public List<object> tags { get; set; }
public List<object> collectionIds { get; set; }
}

/// <summary>
/// Job result object
/// </summary>
public class Job
{
public string jobId { get; set; }
public string jobType { get; set; }
public string status { get; set; }
public float progress { get; set; }
public Metadata metadata { get; set; }
}

/// <summary>
/// Metadata result object
/// </summary>
public class Metadata
{
public string type { get; set; }
public string preset { get; set; }
public string parentId { get; set; }
public string rootParentId { get; set; }
public string kind { get; set; }
public string[] assetIds { get; set; }
public int scalingFactor { get; set; }
public bool magic { get; set; }
public bool forceFaceRestoration { get; set; }
public bool photorealist { get; set; }
}

/// <summary>
/// Root object to be deserialized from json.
/// </summary>
public class Root
{
public Asset asset { get; set; }
public Job job { get; set; }
public string image { get; set; }
}

#endregion
}
Loading
Loading