forked from cgvr/DeltaVR
145 lines
4.5 KiB
C#
145 lines
4.5 KiB
C#
using UnityEngine;
|
|
using System.Diagnostics;
|
|
using System.Threading.Tasks;
|
|
using GLTFast;
|
|
|
|
public class ModelGenerationPipelineStarter : MonoBehaviour
|
|
{
|
|
public Material activeMaterial;
|
|
public Material inactiveMaterial;
|
|
public Transform modelSpawnPoint;
|
|
|
|
private MeshRenderer meshRenderer;
|
|
|
|
public string inputPrompt;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
meshRenderer = GetComponent<MeshRenderer>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
void OnTriggerEnter(Collider other)
|
|
{
|
|
KbmController controller = other.GetComponent<KbmController>();
|
|
if (controller != null)
|
|
{
|
|
meshRenderer.material = activeMaterial;
|
|
|
|
StartModeGenerationPipeline();
|
|
//LoadModel("D:\\henrisel\\DeltaVR3DModelGeneration\\3d-generation-pipeline\\models\\2025-11-17-16-13-33\\mesh.glb");
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit(Collider other)
|
|
{
|
|
KbmController controller = other.GetComponent<KbmController>();
|
|
if (controller != null)
|
|
{
|
|
meshRenderer.material = inactiveMaterial;
|
|
}
|
|
}
|
|
|
|
private async Task<string> GenerateModelAsync()
|
|
{
|
|
return await Task.Run(() =>
|
|
{
|
|
|
|
|
|
// Path to your virtual environment's python.exe
|
|
string pythonExe = @"D:\users\henrisel\DeltaVR3DModelGeneration\3d-generation-pipeline\.venv\Scripts\python.exe";
|
|
|
|
// Path to your Python script
|
|
string scriptPath = @"D:\users\henrisel\DeltaVR3DModelGeneration\3d-generation-pipeline\start_pipeline.py";
|
|
|
|
// Arguments to pass to the script
|
|
string arguments = $"{scriptPath} --prompt \"{inputPrompt}\"";
|
|
|
|
ProcessStartInfo psi = new ProcessStartInfo
|
|
{
|
|
FileName = pythonExe,
|
|
Arguments = arguments,
|
|
UseShellExecute = false,
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
CreateNoWindow = true
|
|
};
|
|
|
|
using (Process process = new Process())
|
|
{
|
|
process.StartInfo = psi;
|
|
process.OutputDataReceived += (sender, e) => UnityEngine.Debug.Log(e.Data);
|
|
process.ErrorDataReceived += (sender, e) => UnityEngine.Debug.LogError(e.Data);
|
|
|
|
process.Start();
|
|
|
|
string output = process.StandardOutput.ReadToEnd();
|
|
string error = process.StandardError.ReadToEnd();
|
|
|
|
process.WaitForExit();
|
|
|
|
|
|
// Extract model path from output
|
|
foreach (string line in output.Split('\n'))
|
|
{
|
|
if (line.StartsWith("Generated 3D model file: "))
|
|
{
|
|
return line.Replace("Generated 3D model file: ", "").Trim();
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
});
|
|
}
|
|
|
|
private async void StartModeGenerationPipeline()
|
|
{
|
|
string modelPath = await GenerateModelAsync();
|
|
|
|
if (!string.IsNullOrEmpty(modelPath))
|
|
{
|
|
UnityEngine.Debug.Log("Got generated model path: " + modelPath);
|
|
await LoadModel(modelPath);
|
|
}
|
|
else
|
|
{
|
|
UnityEngine.Debug.LogError("Model path not found in Python output.");
|
|
}
|
|
|
|
}
|
|
|
|
private async Task LoadModel(string modelPath)
|
|
{
|
|
var gltf = new GltfImport();
|
|
bool loadSuccess = await gltf.Load(modelPath);
|
|
UnityEngine.Debug.Log("Load model success: " + loadSuccess);
|
|
if (loadSuccess)
|
|
{
|
|
GameObject spawnedObject = new GameObject("spawned model");
|
|
spawnedObject.transform.parent = modelSpawnPoint;
|
|
spawnedObject.transform.position = modelSpawnPoint.position;
|
|
|
|
bool spawnSuccess = await gltf.InstantiateMainSceneAsync(spawnedObject.transform);
|
|
UnityEngine.Debug.Log("Spawn model success: " + spawnSuccess);
|
|
|
|
|
|
Transform spawnedObjectMainTransform = spawnedObject.transform.GetChild(0).transform;
|
|
GameObject spawnedObjectBody = spawnedObjectMainTransform.GetChild(0).transform.gameObject;
|
|
MeshCollider collider = spawnedObjectBody.AddComponent<MeshCollider>();
|
|
collider.convex = true;
|
|
MeshRenderer renderer = spawnedObjectBody.GetComponent<MeshRenderer>();
|
|
renderer.material.SetFloat("metallicFactor", 0);
|
|
|
|
spawnedObjectMainTransform.gameObject.AddComponent<Rigidbody>();
|
|
}
|
|
|
|
}
|
|
}
|