forked from cgvr/DeltaVR
port InvokeAI API client to Unity, use it in ImageGenerationBox
This commit is contained in:
75
Assets/_PROJECT/Scripts/ModeGeneration/ImageGenerationBox.cs
Normal file
75
Assets/_PROJECT/Scripts/ModeGeneration/ImageGenerationBox.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System;
|
||||
using Unity.XR.CoreUtils;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ImageGenerationBox : MonoBehaviour
|
||||
{
|
||||
public Material inactiveMaterial;
|
||||
public Material loadingMaterial;
|
||||
|
||||
public VoiceTranscriptionBox voiceTranscriptionTestBox;
|
||||
public Image UIImage;
|
||||
public string promptSuffix = ", single object, front and side fully visible, realistic style, plain neutral background, clear details, soft studio lighting, true-to-scale";
|
||||
|
||||
private MeshRenderer meshRenderer;
|
||||
private bool isLoading;
|
||||
|
||||
// Start is called before the first frame update
|
||||
void Start()
|
||||
{
|
||||
meshRenderer = GetComponent<MeshRenderer>();
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
void Update()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
async void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (isLoading) return;
|
||||
|
||||
KbmController controller = other.GetComponent<KbmController>();
|
||||
XROrigin playerOrigin = other.GetComponent<XROrigin>();
|
||||
if (controller != null || playerOrigin != null)
|
||||
{
|
||||
string inputPrompt = voiceTranscriptionTestBox.LastTextOutput;
|
||||
string refinedPrompt = inputPrompt + promptSuffix;
|
||||
|
||||
isLoading = true;
|
||||
meshRenderer.material = loadingMaterial;
|
||||
|
||||
byte[] imageBytes = await InvokeAiClient.Instance.GenerateImage(refinedPrompt);
|
||||
Sprite sprite = CreateSprite(imageBytes);
|
||||
UIImage.sprite = sprite;
|
||||
|
||||
isLoading = false;
|
||||
meshRenderer.material = inactiveMaterial;
|
||||
}
|
||||
}
|
||||
|
||||
private Sprite CreateSprite(byte[] imageBytes)
|
||||
{
|
||||
var tex = new Texture2D(2, 2, TextureFormat.RGBA32, false);
|
||||
// ImageConversion.LoadImage returns bool (true = success)
|
||||
if (!ImageConversion.LoadImage(tex, imageBytes, markNonReadable: false))
|
||||
{
|
||||
Destroy(tex);
|
||||
throw new InvalidOperationException("Failed to decode image bytes into Texture2D.");
|
||||
}
|
||||
|
||||
tex.filterMode = FilterMode.Bilinear;
|
||||
tex.wrapMode = TextureWrapMode.Clamp;
|
||||
|
||||
var sprite = Sprite.Create(
|
||||
tex,
|
||||
new Rect(0, 0, tex.width, tex.height),
|
||||
new Vector2(0.5f, 0.5f),
|
||||
pixelsPerUnit: 100f
|
||||
);
|
||||
|
||||
return sprite;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user