forked from cgvr/DeltaVR
set up shape detection minigame pieces
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
using FishNet.Component.Transforming;
|
||||
using FishNet.Object;
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ShapeDetectionMinigameController : MonoBehaviour
|
||||
{
|
||||
public MicrophoneStand microphoneStand;
|
||||
public PushableButton imageGenerationButton;
|
||||
public PushableButton modelGenerationButton;
|
||||
|
||||
public string imageGenerationPromptSuffix = ", single object, front and side fully visible, realistic style, plain neutral background, clear details, soft studio lighting, true-to-scale";
|
||||
public Texture2D GeneratedTexture { get; private set; }
|
||||
public Image imageDisplay;
|
||||
|
||||
public GameObject GeneratedModel { get; private set; }
|
||||
public Transform modelSpawnPoint;
|
||||
public string shapeScannerTag = "ShapeScannable";
|
||||
|
||||
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()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private async void InvokeImageGeneration()
|
||||
{
|
||||
string inputPrompt = microphoneStand.GetTextOutput();
|
||||
string refinedPrompt = inputPrompt + imageGenerationPromptSuffix;
|
||||
|
||||
byte[] imageBytes = await InvokeAiClient.Instance.GenerateImage(refinedPrompt);
|
||||
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);
|
||||
InitializeSpawnedObject(spawnedObject);
|
||||
|
||||
// Destroy previous generated object
|
||||
Destroy(GeneratedModel);
|
||||
GeneratedModel = spawnedObject;
|
||||
|
||||
modelGenerationButton.Deactivate();
|
||||
modelGenerationInProgress = false;
|
||||
}
|
||||
|
||||
private void InitializeSpawnedObject(GameObject spawnedObject)
|
||||
{
|
||||
Rigidbody rigidbody = spawnedObject.AddComponent<Rigidbody>();
|
||||
rigidbody.useGravity = false;
|
||||
rigidbody.isKinematic = true;
|
||||
spawnedObject.AddComponent<NetworkObject>();
|
||||
spawnedObject.AddComponent<NetworkTransform>();
|
||||
spawnedObject.transform.parent = modelSpawnPoint;
|
||||
spawnedObject.transform.position = modelSpawnPoint.position;
|
||||
spawnedObject.tag = shapeScannerTag;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user