forked from cgvr/DeltaVR
refactored model generation code out of test box into separate pipeline manager
This commit is contained in:
@@ -119,16 +119,17 @@ public class ArcheryRange : NetworkBehaviour
|
|||||||
var randomPos = targetStartPosition.position + new Vector3(
|
var randomPos = targetStartPosition.position + new Vector3(
|
||||||
Random.Range(minRandomOffset.x, maxRandomOffset.x),
|
Random.Range(minRandomOffset.x, maxRandomOffset.x),
|
||||||
(float) Math.Round(Random.Range(minRandomOffset.y, maxRandomOffset.y)),
|
(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);
|
var target = SpawnTarget(randomPos);
|
||||||
|
|
||||||
_targets.Add(target);
|
_targets.Add(target);
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArcheryTarget SpawnTarget(Vector3 randomPos)
|
private ArcheryTarget SpawnTarget(Vector3 randomPos)
|
||||||
{
|
{
|
||||||
var prefab = Instantiate(targetPrefab, randomPos, Quaternion.identity, null);
|
var prefab = Instantiate(targetPrefab, randomPos, Quaternion.identity, null);
|
||||||
|
// TODO: replace target prefab's child with the generated model
|
||||||
ArcheryTarget target = prefab.GetComponent<ArcheryTarget>();
|
ArcheryTarget target = prefab.GetComponent<ArcheryTarget>();
|
||||||
target.endPosition = targetEndPosition.position;
|
target.endPosition = targetEndPosition.position;
|
||||||
target.addScore = AddScore;
|
target.addScore = AddScore;
|
||||||
|
|||||||
@@ -1,10 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using _PROJECT.Scripts.Bow;
|
|
||||||
using _PROJECT.Scripts.Bow.Extra;
|
|
||||||
using FishNet.Object;
|
using FishNet.Object;
|
||||||
using FishNet.Object.Synchronizing;
|
using FishNet.Object.Synchronizing;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
using Random = UnityEngine.Random;
|
|
||||||
|
|
||||||
public class ArcheryTarget : NetworkBehaviour, IArrowHittable
|
public class ArcheryTarget : NetworkBehaviour, IArrowHittable
|
||||||
{
|
{
|
||||||
@@ -14,11 +11,9 @@ public class ArcheryTarget : NetworkBehaviour, IArrowHittable
|
|||||||
public float forwardSpeed = 2f;
|
public float forwardSpeed = 2f;
|
||||||
public Action<float> addScore;
|
public Action<float> addScore;
|
||||||
|
|
||||||
private bool _flipDirection;
|
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
_flipDirection = Random.value > 0.5f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
@@ -28,11 +23,12 @@ public class ArcheryTarget : NetworkBehaviour, IArrowHittable
|
|||||||
float step = forwardSpeed * Time.deltaTime;
|
float step = forwardSpeed * Time.deltaTime;
|
||||||
var position = transform.position;
|
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)
|
public void Hit(Arrow arrow)
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,22 +1,21 @@
|
|||||||
using UnityEngine;
|
using GLTFast;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using GLTFast;
|
using UnityEngine;
|
||||||
|
|
||||||
public class ModelGenerationPipelineStarter : MonoBehaviour
|
public class PipelineManager : MonoBehaviour
|
||||||
{
|
{
|
||||||
public Material activeMaterial;
|
public static PipelineManager Instance { get; private set; }
|
||||||
public Material inactiveMaterial;
|
|
||||||
public Transform modelSpawnPoint;
|
|
||||||
|
|
||||||
private MeshRenderer meshRenderer;
|
private void Awake()
|
||||||
|
{
|
||||||
public string inputPrompt;
|
Instance = this;
|
||||||
|
}
|
||||||
|
|
||||||
// Start is called before the first frame update
|
// Start is called before the first frame update
|
||||||
void Start()
|
void Start()
|
||||||
{
|
{
|
||||||
meshRenderer = GetComponent<MeshRenderer>();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update is called once per frame
|
// Update is called once per frame
|
||||||
@@ -25,33 +24,10 @@ public class ModelGenerationPipelineStarter : MonoBehaviour
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OnTriggerEnter(Collider other)
|
public async Task<string> GenerateModelAsync(string inputPrompt)
|
||||||
{
|
|
||||||
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(() =>
|
return await Task.Run(() =>
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
// Path to your virtual environment's python.exe
|
// Path to your virtual environment's python.exe
|
||||||
string pythonExe = @"D:\users\henrisel\DeltaVR3DModelGeneration\3d-generation-pipeline\.venv\Scripts\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()
|
public async Task<GameObject> SpawnModel(string modelPath)
|
||||||
{
|
|
||||||
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();
|
var gltf = new GltfImport();
|
||||||
bool loadSuccess = await gltf.Load(modelPath);
|
bool loadSuccess = await gltf.Load(modelPath);
|
||||||
UnityEngine.Debug.Log("Load model success: " + loadSuccess);
|
|
||||||
if (loadSuccess)
|
if (loadSuccess)
|
||||||
{
|
{
|
||||||
GameObject spawnedObject = new GameObject("spawned model");
|
GameObject spawnedObject = new GameObject("spawned model");
|
||||||
spawnedObject.transform.parent = modelSpawnPoint;
|
|
||||||
spawnedObject.transform.position = modelSpawnPoint.position;
|
|
||||||
|
|
||||||
bool spawnSuccess = await gltf.InstantiateMainSceneAsync(spawnedObject.transform);
|
bool spawnSuccess = await gltf.InstantiateMainSceneAsync(spawnedObject.transform);
|
||||||
UnityEngine.Debug.Log("Spawn model success: " + spawnSuccess);
|
if (spawnSuccess)
|
||||||
|
{
|
||||||
|
|
||||||
Transform spawnedObjectMainTransform = spawnedObject.transform.GetChild(0).transform;
|
Transform spawnedObjectMainTransform = spawnedObject.transform.GetChild(0).transform;
|
||||||
GameObject spawnedObjectBody = spawnedObjectMainTransform.GetChild(0).transform.gameObject;
|
GameObject spawnedObjectBody = spawnedObjectMainTransform.GetChild(0).transform.gameObject;
|
||||||
MeshCollider collider = spawnedObjectBody.AddComponent<MeshCollider>();
|
MeshCollider collider = spawnedObjectBody.AddComponent<MeshCollider>();
|
||||||
@@ -138,7 +95,11 @@ public class ModelGenerationPipelineStarter : MonoBehaviour
|
|||||||
renderer.material.SetFloat("metallicFactor", 0);
|
renderer.material.SetFloat("metallicFactor", 0);
|
||||||
|
|
||||||
spawnedObjectMainTransform.gameObject.AddComponent<Rigidbody>();
|
spawnedObjectMainTransform.gameObject.AddComponent<Rigidbody>();
|
||||||
|
|
||||||
|
return spawnedObject;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
throw new System.Exception("Failed to spawn GameObject from model" + modelPath);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 19e82e42c38cf2d4b912baa8d60c5407
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
||||||
Reference in New Issue
Block a user