1
0
forked from cgvr/DeltaVR

refactored model generation code out of test box into separate pipeline manager

This commit is contained in:
2025-12-01 17:06:42 +02:00
parent a66cb8f62c
commit 40e273f51e
6 changed files with 98 additions and 78 deletions

View File

@@ -119,16 +119,17 @@ public class ArcheryRange : NetworkBehaviour
var randomPos = targetStartPosition.position + new Vector3(
Random.Range(minRandomOffset.x, maxRandomOffset.x),
(float) Math.Round(Random.Range(minRandomOffset.y, maxRandomOffset.y)),
Random.Range(minRandomOffset.z, maxRandomOffset.z));
Random.Range(minRandomOffset.z, maxRandomOffset.z)
);
var target = SpawnTarget(randomPos);
_targets.Add(target);
}
private ArcheryTarget SpawnTarget(Vector3 randomPos)
{
var prefab = Instantiate(targetPrefab, randomPos, Quaternion.identity, null);
// TODO: replace target prefab's child with the generated model
ArcheryTarget target = prefab.GetComponent<ArcheryTarget>();
target.endPosition = targetEndPosition.position;
target.addScore = AddScore;

View File

@@ -1,10 +1,7 @@
using System;
using _PROJECT.Scripts.Bow;
using _PROJECT.Scripts.Bow.Extra;
using FishNet.Object;
using FishNet.Object.Synchronizing;
using UnityEngine;
using Random = UnityEngine.Random;
public class ArcheryTarget : NetworkBehaviour, IArrowHittable
{
@@ -14,11 +11,9 @@ public class ArcheryTarget : NetworkBehaviour, IArrowHittable
public float forwardSpeed = 2f;
public Action<float> addScore;
private bool _flipDirection;
private void Awake()
{
_flipDirection = Random.value > 0.5f;
}
// Update is called once per frame
@@ -28,11 +23,12 @@ public class ArcheryTarget : NetworkBehaviour, IArrowHittable
float step = forwardSpeed * Time.deltaTime;
var position = transform.position;
if (Math.Abs(position.x - endPosition.x) < 0.1) Destroy(gameObject);
if (Math.Abs(position.x - endPosition.x) < 0.1)
{
Destroy(gameObject);
}
transform.position = Vector3.MoveTowards(position,
new Vector3(endPosition.x, position.y, position.z), step);
transform.position = Vector3.MoveTowards(position, new Vector3(endPosition.x, position.y, position.z), step);
}
public void Hit(Arrow arrow)

View File

@@ -0,0 +1,51 @@
using UnityEngine;
public class ModelGenerationTestBox : MonoBehaviour
{
public Material activeMaterial;
public Material inactiveMaterial;
public Transform modelSpawnPoint;
public string inputPrompt;
private MeshRenderer meshRenderer;
// Start is called before the first frame update
void Start()
{
meshRenderer = GetComponent<MeshRenderer>();
}
// Update is called once per frame
void Update()
{
}
async void OnTriggerEnter(Collider other)
{
KbmController controller = other.GetComponent<KbmController>();
if (controller != null)
{
meshRenderer.material = activeMaterial;
string modelPath = await PipelineManager.Instance.GenerateModelAsync(inputPrompt);
//LoadModel("D:\\henrisel\\DeltaVR3DModelGeneration\\3d-generation-pipeline\\models\\2025-11-17-16-13-33\\mesh.glb");
GameObject spawnedObject = await PipelineManager.Instance.SpawnModel(modelPath);
spawnedObject.transform.parent = modelSpawnPoint;
spawnedObject.transform.position = modelSpawnPoint.position;
}
}
private void OnTriggerExit(Collider other)
{
KbmController controller = other.GetComponent<KbmController>();
if (controller != null)
{
meshRenderer.material = inactiveMaterial;
}
}
}

View File

@@ -1,22 +1,21 @@
using UnityEngine;
using GLTFast;
using System.Diagnostics;
using System.Threading.Tasks;
using GLTFast;
using UnityEngine;
public class ModelGenerationPipelineStarter : MonoBehaviour
public class PipelineManager : MonoBehaviour
{
public Material activeMaterial;
public Material inactiveMaterial;
public Transform modelSpawnPoint;
public static PipelineManager Instance { get; private set; }
private MeshRenderer meshRenderer;
public string inputPrompt;
private void Awake()
{
Instance = this;
}
// Start is called before the first frame update
void Start()
{
meshRenderer = GetComponent<MeshRenderer>();
}
// Update is called once per frame
@@ -25,33 +24,10 @@ public class ModelGenerationPipelineStarter : MonoBehaviour
}
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()
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";
@@ -94,42 +70,23 @@ public class ModelGenerationPipelineStarter : MonoBehaviour
}
}
return null;
throw new System.Exception("Failed to generate 3D model!");
}
});
}
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)
public async Task<GameObject> SpawnModel(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);
if (spawnSuccess)
{
Transform spawnedObjectMainTransform = spawnedObject.transform.GetChild(0).transform;
GameObject spawnedObjectBody = spawnedObjectMainTransform.GetChild(0).transform.gameObject;
MeshCollider collider = spawnedObjectBody.AddComponent<MeshCollider>();
@@ -138,7 +95,11 @@ public class ModelGenerationPipelineStarter : MonoBehaviour
renderer.material.SetFloat("metallicFactor", 0);
spawnedObjectMainTransform.gameObject.AddComponent<Rigidbody>();
return spawnedObject;
}
}
throw new System.Exception("Failed to spawn GameObject from model" + modelPath);
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 19e82e42c38cf2d4b912baa8d60c5407
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: