diff --git a/Unity/Thing.cs b/Unity/Thing.cs index ab2fea1..43476eb 100644 --- a/Unity/Thing.cs +++ b/Unity/Thing.cs @@ -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 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(); + +#if pHUMANOID4 + if (parentThing.objectType == 7) { + HumanoidControl hc = parentThing.gameObject.GetComponent(); + if (hc == null) + hc = parentThing.gameObject.AddComponent(); + + 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(); + // if (foundThing == null) { + // allThings.Remove(thing); + + // foundThing = foundObj.AddComponent(); + // foundThing.networkId = thing.networkId; + // foundThing.objectId = thing.objectId; + // allThings.Add(foundThing); + // Destroy(thing.gameObject); + // } + // } + // } + } + + /// /// Handle a Pose event ///