1
0
forked from cgvr/DeltaVR
Files
DeltaVR3DModelGeneration/Assets/_PROJECT/Scripts/3DModeGeneration/PipelineManager.cs

105 lines
3.4 KiB
C#

using GLTFast;
using System.Diagnostics;
using System.Threading.Tasks;
using UnityEngine;
public class PipelineManager : MonoBehaviour
{
public static PipelineManager Instance { get; private set; }
private void Awake()
{
Instance = this;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public async Task<string> GenerateModelAsync(string inputPrompt)
{
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();
}
}
throw new System.Exception("Failed to generate 3D model!");
}
});
}
public async Task<GameObject> SpawnModel(string modelPath)
{
var gltf = new GltfImport();
bool loadSuccess = await gltf.Load(modelPath);
if (loadSuccess)
{
GameObject spawnedObject = new GameObject("spawned model");
bool spawnSuccess = await gltf.InstantiateMainSceneAsync(spawnedObject.transform);
if (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>();
return spawnedObject;
}
}
throw new System.Exception("Failed to spawn GameObject from model" + modelPath);
}
}