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

feat: save and add perams #88

Merged
merged 6 commits into from
Oct 31, 2023
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
10 changes: 10 additions & 0 deletions Scenario/Editor/ImageEditor/ImageEditorUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public ImageEditorUI(ImageEditor imageEditor)
new ActionButton { Text = "Save mask to file", Tooltip = "Save mask to file", OnClick = SaveMaskToFile },
new ActionButton { Text = "Fill all", Tooltip = "Fill all", OnClick = FillAll },*/
new ActionButton { Text = "Create Image", Tooltip = "Create a new image", OnClick = CreateImage },
new ActionButton { Text = "Save Image", Tooltip = "Save image for later use", OnClick = SaveImage },
new ActionButton { Text = "Clear", Tooltip = "Clear", OnClick = Clear },
new ActionButton { Text = "Undo", Tooltip = "Undo", OnClick = UndoCanvas },
new ActionButton { Text = "Redo", Tooltip = "Redo", OnClick = RedoCanvas },
Expand Down Expand Up @@ -781,6 +782,15 @@ private void CreateImage()
uploadedImage.Apply();
}

private void SaveImage()
{
string saveImagePath = EditorUtility.SaveFilePanel("Save image", "", "", "png,jpeg,jpg");
if (!string.IsNullOrEmpty(saveImagePath))
{
CommonUtils.SaveTextureAsPNGAtPath(canvasImage, saveImagePath);
}
}

/*private void FillAll()
{
// Add logic to fill all
Expand Down
48 changes: 42 additions & 6 deletions Scenario/Editor/Images/ImageDataClasses.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Numerics;

namespace Scenario
{
Expand All @@ -11,6 +13,12 @@ public class ImageData
public string Id { get; set; }
public string Url { get; set; }
public string InferenceId { get; set; }
public string Prompt { get; set; }
public float Steps { get; set; }
public UnityEngine.Vector2 Size { get; set; }
public float Guidance { get; set; }
public string Scheduler { get; set; }
public string Seed { get; set; }
}
}

Expand All @@ -20,20 +28,48 @@ public class InferencesResponse
public string nextPaginationToken { get; set; }
}

public class Inference
{
public string id { get; set; }
public List<Image> images { get; set; }
}

public class Image
{
public string id { get; set; }
public string url { get; set; }
public string seed { get; set; }
}

public class TokenResponse
{
public string nextPaginationToken { get; set; }
}

public class Parameters
{
public bool intermediateImages { get; set; }
public int guidance { get; set; }
public int numInferenceSteps { get; set; }
public int numSamples { get; set; }
public int tokenMerging { get; set; }
public int width { get; set; }
public bool hideResults { get; set; }
public string type { get; set; }
public string prompt { get; set; }
public string negativePrompt { get; set; }
public int height { get; set; }
}

public class Inference
{
public string id { get; set; }
public string userId { get; set; }
public string ownerId { get; set; }
public string authorId { get; set; }
public string modelId { get; set; }
public DateTime createdAt { get; set; }
public Parameters parameters { get; set; }
public string status { get; set; }
public List<Image> images { get; set; }
public int imagesNumber { get; set; }
public int progress { get; set; }
public string displayPrompt { get; set; }
}


}
8 changes: 7 additions & 1 deletion Scenario/Editor/Images/Images.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,13 @@ private static void GetInferencesData()
{
Id = image.id,
Url = image.url,
InferenceId = inference.id
InferenceId = inference.id,
Prompt = inference.parameters.prompt,
Steps = inference.parameters.numInferenceSteps,
Size = new Vector2(inference.parameters.width,inference.parameters.height),
Guidance = inference.parameters.guidance,
Scheduler = "Default",
Seed = image.seed,
});
}
}
Expand Down
56 changes: 55 additions & 1 deletion Scenario/Editor/Images/ImagesUI.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using static Scenario.ImageDataStorage;
Expand Down Expand Up @@ -119,6 +120,37 @@ public void OnGUI(Rect position)
DrawSelectedTextureSection(position, previewWidth, scrollViewWidth);
}

private void DrawImageData()
{
var currentImageData = PromptImages.imageDataList[selectedTextureIndex];
GUILayout.BeginVertical();
{
CustomStyle.Label("Prompt:");
CustomStyle.Label($"{currentImageData.Prompt}");
CustomStyle.Space(padding);
GUILayout.BeginHorizontal();
{
CustomStyle.Label($"Steps: {currentImageData.Steps}");
CustomStyle.Label($"Size: {currentImageData.Size}");
}
GUILayout.EndHorizontal();
CustomStyle.Space(padding);
GUILayout.BeginHorizontal();
{
CustomStyle.Label($"Guidance: {currentImageData.Guidance}");
CustomStyle.Label($"Scheduler: {currentImageData.Scheduler}");
}
GUILayout.EndHorizontal();
CustomStyle.Space(padding);
GUILayout.BeginHorizontal();
{
CustomStyle.Label($"Seed: {currentImageData.Seed}");
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}

private static void DrawBackground(Rect position)
{
Color backgroundColor = EditorStyle.GetBackgroundColor();
Expand Down Expand Up @@ -149,6 +181,7 @@ private void DrawScrollableArea(float previewWidth)
CustomStyle.Space(10);
DrawSecondButtons();
CustomStyle.Space(10);
DrawImageData();
}
GUILayout.EndVertical();
CustomStyle.Space(10);
Expand Down Expand Up @@ -177,12 +210,33 @@ private void DrawSecondButtons()
}
}

public void RefineImage()
{
if (PromptWindow.promptWindowUI == null)
{
Debug.LogError("Prompt Window not found: Open Prompt Window, and try again.");
return;
}
var imageData = ImageDataStorage.imageDataList[selectedTextureIndex + firstImageIndex];
PromptWindowUI.imageUpload = selectedTexture;
PromptWindow.promptWindowUI.isImageToImage = true;
PromptWindow.promptWindowUI.isTextToImage = false;
PromptWindow.promptWindowUI.promptinputText = imageData.Prompt;
PromptWindow.promptWindowUI.tags = imageData.Prompt.Split(',').ToList();
PromptWindow.promptWindowUI.samplesliderValue = imageData.Steps;
PromptWindow.promptWindowUI.widthSliderValue = (int)imageData.Size.x;
PromptWindow.promptWindowUI.heightSliderValue = (int)imageData.Size.y;
PromptWindow.promptWindowUI.guidancesliderValue = imageData.Guidance;
PromptWindow.promptWindowUI.seedinputText = imageData.Seed;
PromptWindow.ShowWindow();
}

private void DrawFirstButtons()
{
string[] buttonNames = { "Refine Image", "Download", "Delete" };
System.Action[] buttonCallbacks =
{
() => PromptWindowUI.imageUpload = selectedTexture,
() => RefineImage(),
() => CommonUtils.SaveTextureAsPNG(selectedTexture),
() =>
{
Expand Down
12 changes: 11 additions & 1 deletion Scenario/Editor/PromptImages/PromptImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,17 @@ private static async void FetchGeneratedImages()
{
if (!imageDataList.Exists(x => x.Id == image.Id))
{
ImageDataStorage.ImageData newImageData = new ImageDataStorage.ImageData { Id = image.Id, Url = image.Url, InferenceId = image.InferenceId };
ImageDataStorage.ImageData newImageData = new() {
Id = image.Id,
Url = image.Url,
InferenceId = image.InferenceId,
Prompt = image.Prompt,
Steps = image.Steps,
Size = image.Size,
Guidance = image.Guidance,
Scheduler = image.Scheduler,
Seed = image.Seed,
};
imageDataList.Insert(0, newImageData);

Texture2D texture = await LoadTexture(image.Url);
Expand Down
37 changes: 35 additions & 2 deletions Scenario/Editor/PromptImages/PromptImagesUI.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
Expand Down Expand Up @@ -84,11 +85,12 @@ private void DrawScrollableArea(float previewWidth)
CustomStyle.Space(10);
DrawSecondButtons();
CustomStyle.Space(10);
DrawImageData();
}
GUILayout.EndVertical();
CustomStyle.Space(10);
}

private void DrawSelectedImage(float previewWidth)
{
GUILayout.Label("Selected Image", EditorStyles.boldLabel);
Expand All @@ -108,7 +110,38 @@ private void DrawSelectedImage(float previewWidth)
}
GUILayout.EndHorizontal();
}


private void DrawImageData()
{
var currentImageData = PromptImages.imageDataList[selectedTextureIndex];
GUILayout.BeginVertical();
{
CustomStyle.Label("Prompt:");
CustomStyle.Label($"{currentImageData.Prompt}");
CustomStyle.Space(padding);
GUILayout.BeginHorizontal();
{
CustomStyle.Label($"Steps: {currentImageData.Steps}");
CustomStyle.Label($"Size: {currentImageData.Size}");
}
GUILayout.EndHorizontal();
CustomStyle.Space(padding);
GUILayout.BeginHorizontal();
{
CustomStyle.Label($"Guidance: {currentImageData.Guidance}");
CustomStyle.Label($"Scheduler: {currentImageData.Scheduler}");
}
GUILayout.EndHorizontal();
CustomStyle.Space(padding);
GUILayout.BeginHorizontal();
{
CustomStyle.Label($"Seed: {currentImageData.Seed}");
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}

private void DrawFirstButtons()
{
string[] buttonNames = { "Refine Image", "Download", "Delete" };
Expand Down
10 changes: 8 additions & 2 deletions Scenario/Editor/PromptWindow/PromptWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class PromptWindow : EditorWindow
{
internal static List<ImageDataStorage.ImageData> generatedImagesData = new();

private PromptWindowUI promptWindowUI;
public static PromptWindowUI promptWindowUI;

private string inferenceId = "";
private EditorCoroutine inferenceStatusCoroutine;
Expand Down Expand Up @@ -333,7 +333,13 @@ private IEnumerator GetInferenceStatus()
{
Id = img.Id,
Url = img.Url,
InferenceId = this.inferenceId
InferenceId = this.inferenceId,
Prompt = promptWindowUI.promptinputText,
Steps = promptWindowUI.samplesliderValue,
Size = new Vector2(promptWindowUI.widthSliderValue, promptWindowUI.heightSliderValue),
Guidance = promptWindowUI.guidancesliderValue,
Scheduler = "Default",
Seed = promptWindowUI.seedinputText,
});
}
EditorCoroutineUtility.StopCoroutine(inferenceStatusCoroutine);
Expand Down
6 changes: 3 additions & 3 deletions Scenario/Editor/PromptWindow/PromptWindowUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,12 @@ public partial class PromptWindowUI
internal float guidancesliderValue = 7;
internal string postedModelName = "Choose Model";
internal string seedinputText = "";

internal bool isTextToImage = true;

private int dragFromIndex = -1;
private int negativeDragFromIndex = -1;
private string inputText = "";
private string negativeInputText = "";
private bool isTextToImage = true;
private bool showSettings = true;
private bool controlNetFoldout = false;
private Vector2 scrollPosition;
Expand All @@ -66,7 +66,7 @@ public partial class PromptWindowUI
private readonly int[] allowedWidthValues = { 512, 576, 640, 688, 704, 768, 912, 1024 };
private readonly int[] allowedHeightValues = { 512, 576, 640, 688, 704, 768, 912, 1024 };

private List<string> tags = new();
internal List<string> tags = new();
private List<Rect> tagRects = new();
private List<string> negativeTags = new();
private List<Rect> negativeTagRects = new();
Expand Down
1 change: 0 additions & 1 deletion Scenario/Editor/PromptWindow/Views/ImageSettingsView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ private void RenderImageSettingsSection(bool shouldAutoGenerateSeed)
int sliderWidth = Mathf.RoundToInt(EditorGUIUtility.currentViewWidth * sliderWidthPercentage);

imagesliderIntValue = Mathf.RoundToInt(imagesliderValue);

EditorGUILayout.BeginHorizontal();
{
GUILayout.Label("Images: " + imagesliderIntValue, GUILayout.Width(labelWidth));
Expand Down
16 changes: 15 additions & 1 deletion Scenario/Plugins/CommonUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,21 @@ public static Texture2D CreateColorTexture(Color color)
texture.Apply();
return texture;
}


public static void SaveTextureAsPNGAtPath(Texture2D texture2D, string filePath)
{
if (filePath == null || filePath == "") { Debug.LogError("Must have valid file path"); return; }
byte[] pngBytes = texture2D.EncodeToPNG();
SaveImageBytesToPath(filePath, pngBytes);
}

public static void SaveImageBytesToPath(string filePath, byte[] pngBytes)
{
File.WriteAllBytes(filePath, pngBytes);
RefreshAssetDatabase();
Debug.Log("Saved image to: " + filePath);
}

public static void SaveTextureAsPNG(Texture2D texture2D, string fileName = "")
{
if (fileName == null || fileName == "") { fileName = GetRandomImageFileName(); }
Expand Down
Loading