forked from cgvr/DeltaVR
84 lines
2.4 KiB
C#
84 lines
2.4 KiB
C#
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 imageDisplay;
|
|
public Texture2D LastTexture { get; private set; }
|
|
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.GetTextOutput();
|
|
string refinedPrompt = inputPrompt + promptSuffix;
|
|
|
|
isLoading = true;
|
|
meshRenderer.material = loadingMaterial;
|
|
|
|
byte[] imageBytes = await InvokeAiClient.Instance.GenerateImage(refinedPrompt);
|
|
LastTexture = CreateTexture(imageBytes);
|
|
Sprite sprite = CreateSprite(LastTexture);
|
|
imageDisplay.sprite = sprite;
|
|
|
|
isLoading = false;
|
|
meshRenderer.material = inactiveMaterial;
|
|
}
|
|
}
|
|
|
|
private Texture2D CreateTexture(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;
|
|
|
|
return tex;
|
|
}
|
|
|
|
private Sprite CreateSprite(Texture2D tex)
|
|
{
|
|
var sprite = Sprite.Create(
|
|
tex,
|
|
new Rect(0, 0, tex.width, tex.height),
|
|
new Vector2(0.5f, 0.5f),
|
|
pixelsPerUnit: 100f
|
|
);
|
|
|
|
return sprite;
|
|
}
|
|
}
|