First step to supporting GLTF

This commit is contained in:
Pascal Serrarens 2025-05-12 17:43:08 +02:00
parent f611a0755c
commit c4c01bedae

View File

@ -2,6 +2,9 @@
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
#if GLTF
using GLTFast;
#endif
namespace RoboidControl.Unity {
@ -118,7 +121,8 @@ namespace RoboidControl.Unity {
string extension = core.modelUrl[core.modelUrl.LastIndexOf(".")..];
if (extension == ".jpg" || extension == ".png")
StartCoroutine(LoadJPG());
else if (extension == ".gltf" || extension == ".glb")
ProcessGltfModel(core);
break;
case PoseMsg.Id:
Debug.Log($"{this.core.id} Handle Pose");
@ -162,6 +166,88 @@ namespace RoboidControl.Unity {
}
}
bool loadingModel = false;
private async void ProcessGltfModel(RoboidControl.Thing coreThing) {
#if GLTF
if (!loadingModel) {
loadingModel = true;
Debug.Log("Loading GLTF model from :" + coreThing.modelUrl);
GltfImport gltfImport = new GltfImport();
bool success = await gltfImport.Load(coreThing.modelUrl);
if (success) {
Transform parentTransform = this.transform;
Thing[] things = FindObjectsOfType<Thing>();
Thing parentThing = null;
foreach (Thing thing in things) {
if (thing.core.id == coreThing.id) {
parentTransform = thing.transform;
parentThing = thing;
}
}
await gltfImport.InstantiateMainSceneAsync(parentTransform);
SkinnedMeshRenderer[] meshRenderers = parentTransform.GetComponentsInChildren<SkinnedMeshRenderer>();
#if pHUMANOID4
if (parentThing.objectType == 7) {
HumanoidControl hc = parentThing.gameObject.GetComponent<HumanoidControl>();
if (hc == null)
hc = parentThing.gameObject.AddComponent<HumanoidControl>();
foreach (SkinnedMeshRenderer meshRenderer in meshRenderers) {
if (meshRenderer.rootBone != null) {
Debug.Log("Found a skinned mesh with bones");
hc.RetrieveBonesFrom(meshRenderer.rootBone);
break;
}
}
}
#endif
parentTransform.localScale = Vector3.one;
if (meshRenderers.Length > 0) {
foreach (SkinnedMeshRenderer meshRenderer in meshRenderers) {
if (meshRenderer.rootBone != null) {
Debug.Log("Found a skinned mesh with bones");
ScanForThings(meshRenderer.rootBone);
break;
}
}
}
else {
ScanForThings(parentTransform);
}
}
else {
this.transform.localScale = Vector3.one * 1;
}
}
loadingModel = true;
#endif
}
private void ScanForThings(Transform rootTransform) {
// Thing[] thingArray = allThings.ToArray();
// for (int thingIx = 0; thingIx < thingArray.Length; thingIx++) {
// Thing thing = thingArray[thingIx];
// GameObject foundObj = FindThingByName(thing, rootTransform);
// if (foundObj != null && foundObj != thing.gameObject) {
// Thing foundThing = foundObj.GetComponent<Thing>();
// if (foundThing == null) {
// allThings.Remove(thing);
// foundThing = foundObj.AddComponent<Thing>();
// foundThing.networkId = thing.networkId;
// foundThing.objectId = thing.objectId;
// allThings.Add(foundThing);
// Destroy(thing.gameObject);
// }
// }
// }
}
/// <summary>
/// Handle a Pose event
/// </summary>