1
0
forked from cgvr/DeltaVR
Files
DeltaVR3DModelGeneration/Assets/_PROJECT/Scripts/ModeGeneration/ArcheryRange/ArcheryRangeModelGenerationController.cs

84 lines
2.7 KiB
C#

using System;
using UnityEngine;
using UnityEngine.UI;
public class ArcheryRangeModelGenerationController : MonoBehaviour
{
public MicrophoneStand microphoneStand;
public PushableButton imageGenerationButton;
public PushableButton modelGenerationButton;
public Texture2D GeneratedTexture { get; private set; }
public Image imageDisplay;
public GameObject GeneratedModel { get; private set; }
public float generatedObjectRotationSpeed = 10f;
public Transform modelDisplay;
public Material modelDisplayActiveMaterial;
public Transform wire;
public Material wireActiveMaterial;
private bool modelGenerationInProgress;
// Start is called before the first frame update
void Start()
{
imageGenerationButton.OnButtonPressed += InvokeImageGeneration;
modelGenerationButton.OnButtonPressed += InvokeModelGeneration;
modelGenerationInProgress = false;
}
// Update is called once per frame
void Update()
{
if (GeneratedModel != null)
{
GeneratedModel.transform.Rotate(Vector3.up, generatedObjectRotationSpeed * Time.deltaTime);
}
}
private async void InvokeImageGeneration()
{
string inputPrompt = microphoneStand.GetTextOutput();
byte[] imageBytes = await InvokeAiClient.Instance.GenerateImage(inputPrompt);
GeneratedTexture = ModelGenerationUtils.CreateTexture(imageBytes);
Sprite sprite = ModelGenerationUtils.CreateSprite(GeneratedTexture);
imageDisplay.sprite = sprite;
imageGenerationButton.Deactivate();
if (!modelGenerationInProgress)
{
modelGenerationButton.Deactivate();
}
}
private async void InvokeModelGeneration()
{
modelGenerationInProgress = true;
string encodedTexture = Convert.ToBase64String(GeneratedTexture.EncodeToJPG());
byte[] encodedModel = await TrellisClient.Instance.GenerateModel(encodedTexture);
GameObject spawnedObject = await ModelGenerationUtils.Instance.SpawnModel(encodedModel);
// Destroy previous generated object
Destroy(GeneratedModel);
spawnedObject.transform.parent = modelDisplay;
spawnedObject.transform.position = modelDisplay.position;
GeneratedModel = spawnedObject;
OnModelReady();
modelGenerationButton.Deactivate();
modelGenerationInProgress = false;
}
private void OnModelReady()
{
foreach (MeshRenderer meshRenderer in wire.GetComponentsInChildren<MeshRenderer>())
{
meshRenderer.material = wireActiveMaterial;
}
modelDisplay.GetComponent<MeshRenderer>().material = modelDisplayActiveMaterial;
}
}