forked from cgvr/DeltaVR
refactored model generation code out of test box into separate pipeline manager
This commit is contained in:
@@ -118,17 +118,18 @@ 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));
|
||||
(float) Math.Round(Random.Range(minRandomOffset.y, maxRandomOffset.y)),
|
||||
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;
|
||||
@@ -148,7 +149,7 @@ public class ArcheryRange : NetworkBehaviour
|
||||
_targets = new List<ArcheryTarget>();
|
||||
if (_maxScore < _score) _maxScore = _score;
|
||||
|
||||
if(_presentPlayers.Count != 0) // If there are players in the area.
|
||||
if (_presentPlayers.Count != 0) // If there are players in the area.
|
||||
{
|
||||
// Gives the score to the player longest-lasting in the area. It would be better to give it to the player that fired the starting arrow, but I'm not spending 10 hours on this.
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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.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);
|
||||
}
|
||||
}
|
||||
@@ -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