1
0
forked from cgvr/DeltaVR

manage spawned object node hierarchy better

This commit is contained in:
2025-12-16 13:30:08 +02:00
parent 5796887f79
commit 90781191b7
4 changed files with 45 additions and 25 deletions

View File

@@ -48,7 +48,8 @@ public class ModelGenerationBox : MonoBehaviour
string modelPath = await PipelineManager.Instance.GenerateModelAsync(inputPrompt);
lastModelPath = modelPath;
GameObject spawnedObject = await PipelineManager.Instance.SpawnModel(modelPath, true);
GameObject spawnedObject = await PipelineManager.Instance.SpawnModel(modelPath);
spawnedObject.AddComponent<Rigidbody>();
spawnedObject.transform.parent = modelSpawnPoint;
spawnedObject.transform.position = modelSpawnPoint.position;

View File

@@ -1,5 +1,6 @@
using GLTFast;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using UnityEngine;
@@ -75,30 +76,27 @@ public class PipelineManager : MonoBehaviour
});
}
public async Task<GameObject> SpawnModel(string modelPath, bool addRigidBody)
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");
string objectName = Path.GetFileName(modelPath);
GameObject spawningParent = new GameObject("Parent-" + objectName);
bool spawnSuccess = await gltf.InstantiateMainSceneAsync(spawnedObject.transform);
bool spawnSuccess = await gltf.InstantiateMainSceneAsync(spawningParent.transform);
if (spawnSuccess)
{
Transform spawnedObjectMainTransform = spawnedObject.transform.GetChild(0).transform;
GameObject spawnedObjectBody = spawnedObjectMainTransform.GetChild(0).transform.gameObject;
Transform spawnedObjectWorldTransform = spawningParent.transform.GetChild(0).transform;
GameObject spawnedObjectBody = spawnedObjectWorldTransform.GetChild(0).transform.gameObject;
MeshCollider collider = spawnedObjectBody.AddComponent<MeshCollider>();
collider.convex = true;
MeshRenderer renderer = spawnedObjectBody.GetComponent<MeshRenderer>();
renderer.material.SetFloat("metallicFactor", 0);
if (addRigidBody)
{
spawnedObjectMainTransform.gameObject.AddComponent<Rigidbody>();
}
return spawnedObject;
spawnedObjectBody.name = objectName;
return spawnedObjectBody;
}
}