From de9713a7683455215b520cb387d7fc39fe8fe2a5 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Sat, 3 Aug 2024 02:45:54 +0900 Subject: [PATCH 1/2] refactor --- .../UniGLTF/IO/MaterialIO/Import/MaterialFactory.cs | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialFactory.cs index 5f96c22e4..322d691fc 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialFactory.cs @@ -136,7 +136,8 @@ namespace UniGLTF if (texture != null) { material.SetTexture(kv.Key, texture); - SetTextureOffsetAndScale(material, kv.Key, kv.Value.Offset, kv.Value.Scale); + material.SetTextureOffset(kv.Key, kv.Value.Offset); + material.SetTextureScale(kv.Key, kv.Value.Scale); } } @@ -174,11 +175,5 @@ namespace UniGLTF return material; } - - public static void SetTextureOffsetAndScale(Material material, string propertyName, Vector2 offset, Vector2 scale) - { - material.SetTextureOffset(propertyName, offset); - material.SetTextureScale(propertyName, scale); - } } } From 6842cd933cd92afe625626a7d7242d7241a99b46 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Sat, 3 Aug 2024 03:01:45 +0900 Subject: [PATCH 2/2] VrmAnimation depends on IMaterialDescriptorGenerator instead of "Standard" --- .../IO/MaterialIO/Import/MaterialFactory.cs | 38 +++++++++++-------- .../Vrm10AnimationInstance.cs | 3 +- .../VRM10/Runtime/IO/VrmAnimationImporter.cs | 5 ++- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialFactory.cs index 322d691fc..ab4d0955b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/Import/MaterialFactory.cs @@ -10,11 +10,9 @@ namespace UniGLTF public class MaterialFactory : IResponsibilityForDestroyObjects { private readonly IReadOnlyDictionary m_externalMap; - - /// - /// デフォルトマテリアルの MaterialDescriptor は IMaterialDescriptorGenerator の実装によって異なるので外から渡す - /// + private readonly SubAssetKey m_defaultMaterialKey = new SubAssetKey(typeof(Material), "__UNIGLTF__DEFAULT__MATERIAL__"); private readonly MaterialDescriptor m_defaultMaterialParams; + private readonly List m_materials = new List(); /// /// gltfPritmitive.material が無い場合のデフォルトマテリアル @@ -23,6 +21,9 @@ namespace UniGLTF /// private Material m_defaultMaterial; + public IReadOnlyList Materials => m_materials; + + public MaterialFactory(IReadOnlyDictionary externalMaterialMap, MaterialDescriptor defaultMaterialParams) { m_externalMap = externalMaterialMap; @@ -45,18 +46,6 @@ namespace UniGLTF } } - List m_materials = new List(); - public IReadOnlyList Materials => m_materials; - void Remove(Material material) - { - var index = m_materials.FindIndex(x => x.Asset == material); - if (index >= 0) - { - m_materials.RemoveAt(index); - - } - } - public void Dispose() { foreach (var x in m_materials) @@ -67,6 +56,11 @@ namespace UniGLTF UnityObjectDestroyer.DestroyRuntimeOrEditor(x.Asset); } } + + if (m_defaultMaterial != null) + { + UnityObjectDestroyer.DestroyRuntimeOrEditor(m_defaultMaterial); + } } /// @@ -90,6 +84,12 @@ namespace UniGLTF m_materials.Remove(x); } } + + if (m_defaultMaterial != null) + { + take(m_defaultMaterialKey, m_defaultMaterial); + m_defaultMaterial = null; + } } public Material GetMaterial(int index) @@ -101,6 +101,12 @@ namespace UniGLTF public async Task GetDefaultMaterialAsync(IAwaitCaller awaitCaller) { + if (m_externalMap.ContainsKey(m_defaultMaterialKey)) + { + m_defaultMaterial = m_externalMap[m_defaultMaterialKey]; + return m_externalMap[m_defaultMaterialKey]; + } + if (m_defaultMaterial == null) { m_defaultMaterial = await LoadAsync(m_defaultMaterialParams, (_, _) => null, awaitCaller); diff --git a/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10AnimationInstance.cs b/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10AnimationInstance.cs index 39bce21b7..2835f6ab6 100644 --- a/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10AnimationInstance.cs +++ b/Assets/VRM10/Runtime/Components/VrmAnimationInstance/Vrm10AnimationInstance.cs @@ -171,7 +171,7 @@ namespace UniVRM10 public float custom_98; public float custom_99; - public void Initialize(IEnumerable keys) + public void Initialize(IEnumerable keys, Material material) { var humanoid = gameObject.AddComponent(); if (humanoid.AssignBonesFromAnimator()) @@ -183,7 +183,6 @@ namespace UniVRM10 // create SkinnedMesh for bone visualize var animator = this.GetComponentOrThrow(); BoxMan = SkeletonMeshUtility.CreateRenderer(animator); - var material = new Material(Shader.Find("Standard")); BoxMan.sharedMaterial = material; var mesh = BoxMan.sharedMesh; mesh.name = "box-man"; diff --git a/Assets/VRM10/Runtime/IO/VrmAnimationImporter.cs b/Assets/VRM10/Runtime/IO/VrmAnimationImporter.cs index 933a3727e..4928ffe0a 100644 --- a/Assets/VRM10/Runtime/IO/VrmAnimationImporter.cs +++ b/Assets/VRM10/Runtime/IO/VrmAnimationImporter.cs @@ -244,6 +244,9 @@ namespace UniVRM10 } Data.GLTF.scenes[0].nodes = Data.GLTF.scenes[0].nodes.Take(1).ToArray(); + // 可視化メッシュ用マテリアル。base.LoadAsync を呼ぶ前に生成する。 + var defaultMaterial = await MaterialFactory.GetDefaultMaterialAsync(awaitCaller); + // Humanoid Animation が Gltf アニメーションとしてロードされる var instance = await base.LoadAsync(awaitCaller, measureTime); @@ -285,7 +288,7 @@ namespace UniVRM10 // VRMA-animation solver var animationInstance = instance.gameObject.AddComponent(); - animationInstance.Initialize(expressions.Select(x => x.Key)); + animationInstance.Initialize(expressions.Select(x => x.Key), defaultMaterial); return instance; }