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(); } // Update is called once per frame void Update() { } async void OnTriggerEnter(Collider other) { if (isLoading) return; KbmController controller = other.GetComponent(); XROrigin playerOrigin = other.GetComponent(); 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; } }