From e3929b2d04c5e93948d98d8337d8e422851e3a5a Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 19 Jul 2021 16:40:15 +0900 Subject: [PATCH 1/4] add interfaces. --- .../Runtime/UniGLTF/RuntimeGltfInstance.cs | 88 +++++++++++++++++-- 1 file changed, 80 insertions(+), 8 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs b/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs index 89c6560aa..f92788385 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs @@ -6,7 +6,7 @@ namespace UniGLTF { /// /// ImporterContext の Load 結果の GltfModel - /// + /// /// Runtime でモデルを Destory したときに関連リソース(Texture, Material...などの UnityEngine.Object)を自動的に Destroy する。 /// public class RuntimeGltfInstance : MonoBehaviour, IResponsibilityForDestroyObjects @@ -16,17 +16,89 @@ namespace UniGLTF /// public GameObject Root => this.gameObject; - List<(SubAssetKey, UnityEngine.Object)> m_resources = new List<(SubAssetKey, UnityEngine.Object)>(); + /// + /// Runtime resources. + /// ex. Material, Texture, Mesh. + /// + public IReadOnlyList<(SubAssetKey, UnityEngine.Object)> RuntimeResources => _resources; - public IReadOnlyList<(SubAssetKey, UnityEngine.Object)> RuntimeResources => m_resources; + /// + /// Materials. + /// + public IReadOnlyList Materials => _materials; + + /// + /// Textures. + /// + public IReadOnlyList Textures => _textures; + + /// + /// Meshes. + /// + public IReadOnlyList Meshes => _meshes; + + /// + /// Renderers. + /// ex. MeshRenderer, SkinnedMeshRenderer. + /// + public IReadOnlyList Renderers => _renderers; + + /// + /// Mesh Renderers. + /// + public IReadOnlyList MeshRenderers => _meshRenderers; + + /// + /// Skinned Mesh Renderers. + /// + public IReadOnlyList SkinnedMeshRenderers => _skinnedMeshRenderers; + + private readonly List<(SubAssetKey, UnityEngine.Object)> _resources = new List<(SubAssetKey, UnityEngine.Object)>(); + private readonly List _materials = new List(); + private readonly List _textures = new List(); + private readonly List _meshes = new List(); + private readonly List _renderers = new List(); + private readonly List _meshRenderers = new List(); + private readonly List _skinnedMeshRenderers = new List(); public static RuntimeGltfInstance AttachTo(GameObject go, ImporterContext context) { var loaded = go.AddComponent(); context.TransferOwnership((k, o) => { - loaded.m_resources.Add((k, o)); + if (o == null) return; + + loaded._resources.Add((k, o)); + + switch (o) + { + case Material material: + loaded._materials.Add(material); + break; + case Texture texture: + loaded._textures.Add(texture); + break; + case Mesh mesh: + loaded._meshes.Add(mesh); + break; + } }); + + foreach (var renderer in go.GetComponentsInChildren()) + { + loaded._renderers.Add(renderer); + + switch (renderer) + { + case MeshRenderer meshRenderer: + loaded._meshRenderers.Add(meshRenderer); + break; + case SkinnedMeshRenderer skinnedMeshRenderer: + loaded._skinnedMeshRenderers.Add(skinnedMeshRenderer); + break; + } + } + return loaded; } @@ -49,18 +121,18 @@ namespace UniGLTF void OnDestroy() { Debug.Log("UnityResourceDestroyer.OnDestroy"); - foreach (var (key, x) in m_resources) + foreach (var (_, obj) in _resources) { - UnityObjectDestoyer.DestroyRuntimeOrEditor(x); + UnityObjectDestoyer.DestroyRuntimeOrEditor(obj); } } public void TransferOwnership(TakeResponsibilityForDestroyObjectFunc take) { - foreach (var (key, x) in m_resources.ToArray()) + foreach (var (key, x) in _resources.ToArray()) { take(key, x); - m_resources.Remove((key, x)); + _resources.Remove((key, x)); } } From 5baee4aabf029c0c4b9dae3f4f205fdde5de8ed4 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 19 Jul 2021 16:41:20 +0900 Subject: [PATCH 2/4] Fix the bug about unrelated renderers was manipulated. --- Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs b/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs index f92788385..62eef99ba 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs @@ -104,7 +104,7 @@ namespace UniGLTF public void ShowMeshes() { - foreach (var r in GetComponentsInChildren()) + foreach (var r in Renderers) { r.enabled = true; } @@ -112,9 +112,9 @@ namespace UniGLTF public void EnableUpdateWhenOffscreen() { - foreach (var smr in GetComponentsInChildren()) + foreach (var skinnedMeshRenderer in SkinnedMeshRenderers) { - smr.updateWhenOffscreen = true; + skinnedMeshRenderer.updateWhenOffscreen = true; } } From b6fc095ce17a39afd313cace24b730232ef570ae Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 19 Jul 2021 16:51:20 +0900 Subject: [PATCH 3/4] Add animationClip property. --- Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs b/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs index 62eef99ba..e1de0da7e 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs @@ -18,7 +18,7 @@ namespace UniGLTF /// /// Runtime resources. - /// ex. Material, Texture, Mesh. + /// ex. Material, Texture, AnimationClip, Mesh. /// public IReadOnlyList<(SubAssetKey, UnityEngine.Object)> RuntimeResources => _resources; @@ -32,6 +32,11 @@ namespace UniGLTF /// public IReadOnlyList Textures => _textures; + /// + /// Animation Clips. + /// + public IReadOnlyList AnimationClips => _animationClips; + /// /// Meshes. /// @@ -56,6 +61,7 @@ namespace UniGLTF private readonly List<(SubAssetKey, UnityEngine.Object)> _resources = new List<(SubAssetKey, UnityEngine.Object)>(); private readonly List _materials = new List(); private readonly List _textures = new List(); + private readonly List _animationClips = new List(); private readonly List _meshes = new List(); private readonly List _renderers = new List(); private readonly List _meshRenderers = new List(); @@ -78,6 +84,9 @@ namespace UniGLTF case Texture texture: loaded._textures.Add(texture); break; + case AnimationClip animationClip: + loaded._animationClips.Add(animationClip); + break; case Mesh mesh: loaded._meshes.Add(mesh); break; From 2f970a200cd66dbeff3c923bd706f4c0853ab4dc Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 19 Jul 2021 16:57:39 +0900 Subject: [PATCH 4/4] Add Nodes property. --- .../Runtime/UniGLTF/RuntimeGltfInstance.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs b/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs index e1de0da7e..5ec350bdb 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/RuntimeGltfInstance.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using UnityEngine; using VRMShaders; @@ -14,7 +15,12 @@ namespace UniGLTF /// /// this is UniGLTF root gameObject /// - public GameObject Root => this.gameObject; + public GameObject Root => (this != null) ? this.gameObject : null; + + /// + /// Transforms with gltf node index. + /// + public IReadOnlyList Nodes => _nodes; /// /// Runtime resources. @@ -58,6 +64,7 @@ namespace UniGLTF /// public IReadOnlyList SkinnedMeshRenderers => _skinnedMeshRenderers; + private readonly List _nodes = new List(); private readonly List<(SubAssetKey, UnityEngine.Object)> _resources = new List<(SubAssetKey, UnityEngine.Object)>(); private readonly List _materials = new List(); private readonly List _textures = new List(); @@ -70,12 +77,20 @@ namespace UniGLTF public static RuntimeGltfInstance AttachTo(GameObject go, ImporterContext context) { var loaded = go.AddComponent(); + + foreach (var node in context.Nodes) + { + // Maintain index order. + loaded._nodes.Add(node); + } + context.TransferOwnership((k, o) => { if (o == null) return; loaded._resources.Add((k, o)); + switch (o) { case Material material: