From c7e0191685385c35bfbe05c8dc7479b1d1aefcae Mon Sep 17 00:00:00 2001 From: ousttrue Date: Fri, 12 Feb 2021 21:22:46 +0900 Subject: [PATCH] CreateMaterialAsyncFunc --- .../Runtime/UniGLTF/IO/ImporterContext.cs | 2 +- .../Runtime/UniGLTF/IO/MeshImporter.cs | 4 +- .../MaterialFacotry/MaterialFactory.cs | 66 ++++++++--------- .../MaterialFacotry/MaterialItemBase.cs | 62 +++++++++------- .../MaterialFacotry/PBRMaterialItem.cs | 70 +++++++++---------- .../MaterialFacotry/UnlitMaterialItem.cs | 35 ++++------ Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs | 25 +++---- Assets/VRM/Runtime/IO/VRMImporterContext.cs | 7 +- Assets/VRM/Runtime/IO/VRMMaterialImporter.cs | 23 ++---- 9 files changed, 138 insertions(+), 156 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index b6bbcbbc8..12098ecdd 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -499,7 +499,7 @@ namespace UniGLTF { // root task. do nothing }) - .ContinueWithCoroutine(Scheduler.MainThread, () => m_materialFactory.LoadMaterials()) + .ContinueWithCoroutine(Scheduler.MainThread, m_materialFactory.LoadMaterials) .OnExecute(Scheduler.ThreadPool, parent => { // UniGLTF does not support draco diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs index c4346a974..0f2701cc8 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshImporter.cs @@ -638,7 +638,7 @@ namespace UniGLTF var result = new MeshWithMaterials { Mesh = mesh, - Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x).GetOrCreateAsync(ctx.GetTextureAsync).Result).ToArray() + Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x)).ToArray() }; if (meshContext.BlendShapes.Count > 0) @@ -667,7 +667,7 @@ namespace UniGLTF var result = new MeshWithMaterials { Mesh = mesh, - Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x).GetOrCreateAsync(ctx.GetTextureAsync).Result).ToArray() + Materials = meshContext.MaterialIndices.Select(x => ctx.GetMaterial(x)).ToArray() }; yield return null; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs index 676faf0dd..060143ef7 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialFactory.cs @@ -10,8 +10,6 @@ using UnityEditor; namespace UniGLTF { - public delegate MaterialItemBase MaterialImporter(int i, glTFMaterial x, bool hasVertexColor); - public class MaterialFactory : IDisposable { glTF m_gltf; @@ -24,20 +22,21 @@ namespace UniGLTF public UnityPath ImageBaseDir { get; set; } - MaterialImporter m_materialImporter; - public MaterialImporter MaterialImporter + public delegate Task CreateMaterialAsyncFunc(glTF glTF, int i, GetTextureAsyncFunc getTexture); + CreateMaterialAsyncFunc m_createMaterialAsync; + public CreateMaterialAsyncFunc CreateMaterialAsync { set { - m_materialImporter = value; + m_createMaterialAsync = value; } get { - if (m_materialImporter == null) + if (m_createMaterialAsync == null) { - m_materialImporter = CreateMaterial; + m_createMaterialAsync = MaterialItemBase.DefaultCreateMaterialAsync; } - return m_materialImporter; + return m_createMaterialAsync; } } @@ -111,22 +110,19 @@ namespace UniGLTF throw new NotImplementedException(); } - List m_materials = new List(); - public void AddMaterial(MaterialItemBase material) + List m_materials = new List(); + public IReadOnlyList Materials => m_materials; + public void AddMaterial(Material material) { - var originalName = material.Name; + var originalName = material.name; int j = 2; - while (m_materials.Any(x => x.Name == material.Name)) + while (m_materials.Any(x => x.name == material.name)) { - material.Name = string.Format("{0}({1})", originalName, j++); + material.name = string.Format("{0}({1})", originalName, j++); } m_materials.Add(material); } - public IList GetMaterials() - { - return m_materials; - } - public MaterialItemBase GetMaterial(int index) + public Material GetMaterial(int index) { if (index < 0) return null; if (index >= m_materials.Count) return null; @@ -146,37 +142,31 @@ namespace UniGLTF { // using (MeasureTime("LoadMaterials")) { - if (m_gltf.materials == null || !m_gltf.materials.Any()) + if (m_gltf.materials == null || m_gltf.materials.Count == 0) { - AddMaterial(MaterialImporter(0, null, false)); + var task = CreateMaterialAsync(m_gltf, 0, GetTextureAsync); + while (!task.IsCompleted) + { + yield return null; + } + AddMaterial(task.Result); } else { for (int i = 0; i < m_gltf.materials.Count; ++i) { - AddMaterial(MaterialImporter(i, m_gltf.materials[i], m_gltf.MaterialHasVertexColor(i))); + var task = CreateMaterialAsync(m_gltf, i, GetTextureAsync); + while (!task.IsCompleted) + { + yield return null; + } + AddMaterial(task.Result); } } } yield return null; } - public static MaterialItemBase CreateMaterial(int i, glTFMaterial x, bool hasVertexColor) - { - if (x == null) - { - UnityEngine.Debug.LogWarning("glTFMaterial is empty"); - return new PBRMaterialItem(i, x); - } - - if (glTF_KHR_materials_unlit.IsEnable(x)) - { - return new UnlitMaterialItem(i, x, hasVertexColor); - } - - return new PBRMaterialItem(i, x); - } - public void Dispose() { foreach (var x in ObjectsForSubAsset()) @@ -193,7 +183,7 @@ namespace UniGLTF } foreach (var x in m_materials) { - yield return x.GetOrCreateAsync(GetTextureAsync).Result; + yield return x; } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItemBase.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItemBase.cs index 35191bab6..86e51d7e0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItemBase.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/MaterialItemBase.cs @@ -4,43 +4,24 @@ using UnityEngine; namespace UniGLTF { - public abstract class MaterialItemBase + public static class MaterialItemBase { - protected int m_index; - protected glTFMaterial m_src; - - public string Name { get; set; } - - public MaterialItemBase(int i, glTFMaterial src) - { - m_index = i; - m_src = src; - Name = src != null ? m_src.name : ""; - } - - public abstract Task GetOrCreateAsync(GetTextureAsyncFunc getTexture); - - public Material GetOrCreateForTest() - { - return GetOrCreateAsync(null).Result; - } - - protected Material CreateMaterial(string shaderName) + public static Material CreateMaterial(int index, glTFMaterial src, string shaderName) { var material = new Material(Shader.Find(shaderName)); #if UNITY_EDITOR // textureImporter.SaveAndReimport(); may destroy this material material.hideFlags = HideFlags.DontUnloadUnusedAsset; #endif - material.name = (m_src == null || string.IsNullOrEmpty(m_src.name)) - ? string.Format("material_{0:00}", m_index) - : m_src.name + material.name = (src == null || string.IsNullOrEmpty(src.name)) + ? string.Format("material_{0:00}", index) + : src.name ; return material; } - protected static void SetTextureOffsetAndScale(Material material, glTFTextureInfo textureInfo, string propertyName) + public static void SetTextureOffsetAndScale(Material material, glTFTextureInfo textureInfo, string propertyName) { if (glTF_KHR_texture_transform.TryGet(textureInfo, out glTF_KHR_texture_transform textureTransform)) { @@ -61,5 +42,36 @@ namespace UniGLTF material.SetTextureScale(propertyName, scale); } } + + public static Task DefaultCreateMaterialAsync(glTF gltf, int i, GetTextureAsyncFunc getTexture) + { + + if (i < 0 || i >= gltf.materials.Count) + { + UnityEngine.Debug.LogWarning("glTFMaterial is empty"); + return PBRMaterialItem.CreateAsync(i, null, getTexture); + } + var x = gltf.materials[i]; + + if (glTF_KHR_materials_unlit.IsEnable(x)) + { + var hasVertexColor = gltf.MaterialHasVertexColor(i); + return UnlitMaterialItem.CreateAsync(i, x, getTexture, hasVertexColor); + } + + return PBRMaterialItem.CreateAsync(i, x, getTexture); + } + + /// + /// for unittest + /// + /// + /// + /// + /// + public static Material CreateMaterialForTest(int i, glTFMaterial material) + { + throw new System.NotImplementedException(); + } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/PBRMaterialItem.cs index 9a2602473..77ca1faa6 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/PBRMaterialItem.cs @@ -30,7 +30,7 @@ namespace UniGLTF /// _SrcBlend /// _DstBlend /// _ZWrite - public class PBRMaterialItem : MaterialItemBase + public static class PBRMaterialItem { public const string ShaderName = "Standard"; @@ -42,45 +42,41 @@ namespace UniGLTF Transparent } - public PBRMaterialItem(int i, glTFMaterial src) : base(i, src) - { - } - - public override async Task GetOrCreateAsync(GetTextureAsyncFunc getTexture) + public static async Task CreateAsync(int i, glTFMaterial src, GetTextureAsyncFunc getTexture) { if (getTexture == null) { getTexture = _ => Task.FromResult(null); } - var material = CreateMaterial(ShaderName); + var material = MaterialItemBase.CreateMaterial(i, src, ShaderName); // PBR material - if (m_src != null) + if (src != null) { - if (m_src.pbrMetallicRoughness != null) + if (src.pbrMetallicRoughness != null) { - if (m_src.pbrMetallicRoughness.baseColorFactor != null && m_src.pbrMetallicRoughness.baseColorFactor.Length == 4) + if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) { - var color = m_src.pbrMetallicRoughness.baseColorFactor; + var color = src.pbrMetallicRoughness.baseColorFactor; material.color = (new Color(color[0], color[1], color[2], color[3])).gamma; } - if (m_src.pbrMetallicRoughness.baseColorTexture != null && m_src.pbrMetallicRoughness.baseColorTexture.index != -1) + if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1) { - material.mainTexture = await getTexture(GetTextureParam.Create(m_src.pbrMetallicRoughness.baseColorTexture.index)); + material.mainTexture = await getTexture(GetTextureParam.Create(src.pbrMetallicRoughness.baseColorTexture.index)); // Texture Offset and Scale - SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); + MaterialItemBase.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); } - if (m_src.pbrMetallicRoughness.metallicRoughnessTexture != null && m_src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) + if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) { material.EnableKeyword("_METALLICGLOSSMAP"); var texture = await getTexture(GetTextureParam.CreateMetallic( - m_src.pbrMetallicRoughness.metallicRoughnessTexture.index, - m_src.pbrMetallicRoughness.metallicFactor)); + src.pbrMetallicRoughness.metallicRoughnessTexture.index, + src.pbrMetallicRoughness.metallicFactor)); if (texture != null) { material.SetTexture(GetTextureParam.METALLIC_GLOSS_PROP, texture); @@ -91,69 +87,69 @@ namespace UniGLTF material.SetFloat("_GlossMapScale", 1.0f); // Texture Offset and Scale - SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.metallicRoughnessTexture, "_MetallicGlossMap"); + MaterialItemBase.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.metallicRoughnessTexture, "_MetallicGlossMap"); } else { - material.SetFloat("_Metallic", m_src.pbrMetallicRoughness.metallicFactor); - material.SetFloat("_Glossiness", 1.0f - m_src.pbrMetallicRoughness.roughnessFactor); + material.SetFloat("_Metallic", src.pbrMetallicRoughness.metallicFactor); + material.SetFloat("_Glossiness", 1.0f - src.pbrMetallicRoughness.roughnessFactor); } } - if (m_src.normalTexture != null && m_src.normalTexture.index != -1) + if (src.normalTexture != null && src.normalTexture.index != -1) { material.EnableKeyword("_NORMALMAP"); - var texture = await getTexture(GetTextureParam.CreateNormal(m_src.normalTexture.index)); + var texture = await getTexture(GetTextureParam.CreateNormal(src.normalTexture.index)); if (texture != null) { material.SetTexture(GetTextureParam.NORMAL_PROP, texture); - material.SetFloat("_BumpScale", m_src.normalTexture.scale); + material.SetFloat("_BumpScale", src.normalTexture.scale); } // Texture Offset and Scale - SetTextureOffsetAndScale(material, m_src.normalTexture, "_BumpMap"); + MaterialItemBase.SetTextureOffsetAndScale(material, src.normalTexture, "_BumpMap"); } - if (m_src.occlusionTexture != null && m_src.occlusionTexture.index != -1) + if (src.occlusionTexture != null && src.occlusionTexture.index != -1) { - var texture = await getTexture(GetTextureParam.CreateOcclusion(m_src.occlusionTexture.index)); + var texture = await getTexture(GetTextureParam.CreateOcclusion(src.occlusionTexture.index)); if (texture != null) { material.SetTexture(GetTextureParam.OCCLUSION_PROP, texture); - material.SetFloat("_OcclusionStrength", m_src.occlusionTexture.strength); + material.SetFloat("_OcclusionStrength", src.occlusionTexture.strength); } // Texture Offset and Scale - SetTextureOffsetAndScale(material, m_src.occlusionTexture, "_OcclusionMap"); + MaterialItemBase.SetTextureOffsetAndScale(material, src.occlusionTexture, "_OcclusionMap"); } - if (m_src.emissiveFactor != null - || (m_src.emissiveTexture != null && m_src.emissiveTexture.index != -1)) + if (src.emissiveFactor != null + || (src.emissiveTexture != null && src.emissiveTexture.index != -1)) { material.EnableKeyword("_EMISSION"); material.globalIlluminationFlags &= ~MaterialGlobalIlluminationFlags.EmissiveIsBlack; - if (m_src.emissiveFactor != null && m_src.emissiveFactor.Length == 3) + if (src.emissiveFactor != null && src.emissiveFactor.Length == 3) { - material.SetColor("_EmissionColor", new Color(m_src.emissiveFactor[0], m_src.emissiveFactor[1], m_src.emissiveFactor[2])); + material.SetColor("_EmissionColor", new Color(src.emissiveFactor[0], src.emissiveFactor[1], src.emissiveFactor[2])); } - if (m_src.emissiveTexture != null && m_src.emissiveTexture.index != -1) + if (src.emissiveTexture != null && src.emissiveTexture.index != -1) { - var texture = await getTexture(GetTextureParam.Create(m_src.emissiveTexture.index)); + var texture = await getTexture(GetTextureParam.Create(src.emissiveTexture.index)); if (texture != null) { material.SetTexture("_EmissionMap", texture); } // Texture Offset and Scale - SetTextureOffsetAndScale(material, m_src.emissiveTexture, "_EmissionMap"); + MaterialItemBase.SetTextureOffsetAndScale(material, src.emissiveTexture, "_EmissionMap"); } } BlendMode blendMode = BlendMode.Opaque; // https://forum.unity.com/threads/standard-material-shader-ignoring-setfloat-property-_mode.344557/#post-2229980 - switch (m_src.alphaMode) + switch (src.alphaMode) { case "BLEND": blendMode = BlendMode.Fade; @@ -173,7 +169,7 @@ namespace UniGLTF material.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); material.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.Zero); material.SetInt("_ZWrite", 1); - material.SetFloat("_Cutoff", m_src.alphaCutoff); + material.SetFloat("_Cutoff", src.alphaCutoff); material.EnableKeyword("_ALPHATEST_ON"); material.DisableKeyword("_ALPHABLEND_ON"); material.DisableKeyword("_ALPHAPREMULTIPLY_ON"); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/UnlitMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/UnlitMaterialItem.cs index cadf7272c..9f5a645fe 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/UnlitMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/MaterialFacotry/UnlitMaterialItem.cs @@ -3,55 +3,48 @@ using UnityEngine; namespace UniGLTF { - public class UnlitMaterialItem : MaterialItemBase + public static class UnlitMaterialItem { public const string ShaderName = "UniGLTF/UniUnlit"; - bool m_hasVertexColor; - - public UnlitMaterialItem(int i, glTFMaterial src, bool hasVertexColor) : base(i, src) - { - m_hasVertexColor = hasVertexColor; - } - - public override async Task GetOrCreateAsync(GetTextureAsyncFunc getTexture) + public static async Task CreateAsync(int i, glTFMaterial src, GetTextureAsyncFunc getTexture, bool hasVertexColor) { if (getTexture == null) { getTexture = _ => Task.FromResult(null); } - var material = CreateMaterial(ShaderName); + var material = MaterialItemBase.CreateMaterial(i, src, ShaderName); // texture - if (m_src.pbrMetallicRoughness.baseColorTexture != null) + if (src.pbrMetallicRoughness.baseColorTexture != null) { - material.mainTexture = await getTexture(GetTextureParam.Create(m_src.pbrMetallicRoughness.baseColorTexture.index)); + material.mainTexture = await getTexture(GetTextureParam.Create(src.pbrMetallicRoughness.baseColorTexture.index)); // Texture Offset and Scale - SetTextureOffsetAndScale(material, m_src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); + MaterialItemBase.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); } // color - if (m_src.pbrMetallicRoughness.baseColorFactor != null && m_src.pbrMetallicRoughness.baseColorFactor.Length == 4) + if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) { - var color = m_src.pbrMetallicRoughness.baseColorFactor; + var color = src.pbrMetallicRoughness.baseColorFactor; material.color = (new Color(color[0], color[1], color[2], color[3])).gamma; } //renderMode - if (m_src.alphaMode == "OPAQUE") + if (src.alphaMode == "OPAQUE") { UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Opaque); } - else if (m_src.alphaMode == "BLEND") + else if (src.alphaMode == "BLEND") { UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Transparent); } - else if (m_src.alphaMode == "MASK") + else if (src.alphaMode == "MASK") { UniUnlit.Utils.SetRenderMode(material, UniUnlit.UniUnlitRenderMode.Cutout); - material.SetFloat("_Cutoff", m_src.alphaCutoff); + material.SetFloat("_Cutoff", src.alphaCutoff); } else { @@ -60,7 +53,7 @@ namespace UniGLTF } // culling - if (m_src.doubleSided) + if (src.doubleSided) { UniUnlit.Utils.SetCullMode(material, UniUnlit.UniUnlitCullMode.Off); } @@ -70,7 +63,7 @@ namespace UniGLTF } // VColor - if (m_hasVertexColor) + if (hasVertexColor) { UniUnlit.Utils.SetVColBlendMode(material, UniUnlit.UniUnlitVertexColorBlendOp.Multiply); } diff --git a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs index 0860a5199..b4e47e580 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs @@ -3,6 +3,7 @@ using NUnit.Framework; using UnityEngine; using UniJSON; using System.Linq; +using System.Threading.Tasks; namespace UniGLTF { @@ -32,7 +33,7 @@ namespace UniGLTF var gltfMaterial = materialExporter.ExportMaterial(srcMaterial, textureManager); gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions = gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions.Deserialize(); - var dstMaterial = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var dstMaterial = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual(dstMaterial.mainTextureOffset.x, offset.x, 0.3f); Assert.AreEqual(dstMaterial.mainTextureOffset.y, offset.y, 0.2f); @@ -80,7 +81,7 @@ namespace UniGLTF Assert.IsTrue(glTF_KHR_materials_unlit.IsEnable(gltfMaterial)); - var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name); } } @@ -99,7 +100,7 @@ namespace UniGLTF }, extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(), }; - var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name); } @@ -114,7 +115,7 @@ namespace UniGLTF }, extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(), }; - var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name); } @@ -130,7 +131,7 @@ namespace UniGLTF }, extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(), }; - var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name); } @@ -145,7 +146,7 @@ namespace UniGLTF }, extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(), }; - var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name); } @@ -160,7 +161,7 @@ namespace UniGLTF }, extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(), }; - var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name); } @@ -176,7 +177,7 @@ namespace UniGLTF }, extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(), }; - var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name); } @@ -191,7 +192,7 @@ namespace UniGLTF }, extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(), }; - var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name); } @@ -207,7 +208,7 @@ namespace UniGLTF }, extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(), }; - var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name); } @@ -217,7 +218,7 @@ namespace UniGLTF { extensions = glTF_KHR_materials_unlit.Serialize().Deserialize(), }; - var material = MaterialFactory.CreateMaterial(0, gltfMaterial, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, gltfMaterial); Assert.AreEqual("UniGLTF/UniUnlit", material.shader.name); } } @@ -225,7 +226,7 @@ namespace UniGLTF [Test] public void MaterialImportTest() { - var material = MaterialFactory.CreateMaterial(0, new glTFMaterial { }, false).GetOrCreateForTest(); + var material = MaterialItemBase.CreateMaterialForTest(0, new glTFMaterial { }); Assert.AreEqual("Standard", material.shader.name); } diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index 76d8a2a5b..9c7c7efa1 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -43,7 +43,7 @@ namespace VRM { VRM = vrm; // override material importer - MaterialFactory.MaterialImporter = new VRMMaterialImporter(VRM.materialProperties).CreateMaterial; + MaterialFactory.CreateMaterialAsync = new VRMMaterialImporter(VRM.materialProperties).CreateMaterial; } else { @@ -198,9 +198,8 @@ namespace VRM } } - var material = MaterialFactory.GetMaterials() - .FirstOrDefault(y => y.Name == x.materialName) - .GetOrCreateAsync(MaterialFactory.GetTextureAsync).Result; + var material = MaterialFactory.Materials + .FirstOrDefault(y => y.name == x.materialName); var propertyName = x.propertyName; if (x.propertyName.FastEndsWith("_ST_S") || x.propertyName.FastEndsWith("_ST_T")) diff --git a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs index 26caed56c..85b6acf22 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs @@ -7,7 +7,7 @@ using System.Threading.Tasks; namespace VRM { - public class MToonMaterialItem : MaterialItemBase + public static class MToonMaterialItem { static string[] VRM_SHADER_NAMES = { @@ -21,18 +21,9 @@ namespace VRM "VRM/UnlitTransparentZWrite", }; - glTF_VRM_Material m_vrmMaterial; - bool m_hasVertexColor; - - public MToonMaterialItem(int i, glTFMaterial src, bool hasVertexColor, glTF_VRM_Material vrmMaterial) : base(i, src) + public static async Task CreateAsync(glTF gltf, int m_index, glTF_VRM_Material vrmMaterial, GetTextureAsyncFunc getTexture) { - m_hasVertexColor = hasVertexColor; - m_vrmMaterial = vrmMaterial; - } - - public override async Task GetOrCreateAsync(GetTextureAsyncFunc getTexture) - { - var item = m_vrmMaterial; + var item = vrmMaterial; var shaderName = item.shader; var shader = Shader.Find(shaderName); if (shader == null) @@ -48,7 +39,7 @@ namespace VRM { Debug.LogWarningFormat("unknown shader {0}.", shaderName); } - return await MaterialFactory.CreateMaterial(m_index, m_src, m_hasVertexColor).GetOrCreateAsync(getTexture); + return await MaterialItemBase.DefaultCreateMaterialAsync(gltf, m_index, getTexture); } // @@ -120,15 +111,15 @@ namespace VRM m_materials = materials; } - public MaterialItemBase CreateMaterial(int i, glTFMaterial src, bool hasVertexColor) + public Task CreateMaterial(glTF gltf, int i, GetTextureAsyncFunc getTexture) { if (i == 0 && m_materials.Count == 0) { // dummy - return new PBRMaterialItem(i, src); + return MaterialItemBase.DefaultCreateMaterialAsync(gltf, i, getTexture); } - return new MToonMaterialItem(i, src, hasVertexColor, m_materials[i]); + return MToonMaterialItem.CreateAsync(gltf, i, m_materials[i], getTexture); } } }