diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index ec8019808..dbc0e108d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -129,7 +129,7 @@ namespace UniGLTF { if (m_materialImporter == null) { - m_materialImporter = new MaterialImporter(ShaderStore, (int index) => this.GetTexture(index)); + m_materialImporter = new MaterialImporter(ShaderStore); } return m_materialImporter; } @@ -678,13 +678,13 @@ namespace UniGLTF { if (GLTF.materials == null || !GLTF.materials.Any()) { - AddMaterial(MaterialImporter.CreateMaterial(0, null, false)); + AddMaterial(MaterialImporter.CreateMaterial(0, null, false, GetTexture)); } else { for (int i = 0; i < GLTF.materials.Count; ++i) { - AddMaterial(MaterialImporter.CreateMaterial(i, GLTF.materials[i], GLTF.MaterialHasVertexColor(i))); + AddMaterial(MaterialImporter.CreateMaterial(i, GLTF.materials[i], GLTF.MaterialHasVertexColor(i), GetTexture)); } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialImporter.cs index f46b279b7..b124c8d39 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialImporter.cs @@ -12,14 +12,13 @@ namespace UniGLTF { IShaderStore m_shaderStore; - protected Func GetTextureFunc; - - public MaterialImporter(IShaderStore shaderStore, Func getTextureFunc) + public MaterialImporter(IShaderStore shaderStore) { m_shaderStore = shaderStore; - GetTextureFunc = getTextureFunc; } + public delegate TextureItem GetTextureFunc(int index); + private enum BlendMode { Opaque, @@ -55,7 +54,7 @@ namespace UniGLTF /// _SrcBlend /// _DstBlend /// _ZWrite - public virtual Material CreateMaterial(int i, glTFMaterial x, bool hasVertexColor) + public virtual Material CreateMaterial(int i, glTFMaterial x, bool hasVertexColor, GetTextureFunc getTexture) { var shader = m_shaderStore.GetShader(x); //Debug.LogFormat("[{0}]{1}", i, shader.name); @@ -82,7 +81,7 @@ namespace UniGLTF // texture if (x.pbrMetallicRoughness.baseColorTexture != null) { - var texture = GetTextureFunc(x.pbrMetallicRoughness.baseColorTexture.index); + var texture = getTexture(x.pbrMetallicRoughness.baseColorTexture.index); if (texture != null) { material.mainTexture = texture.Texture; @@ -151,7 +150,7 @@ namespace UniGLTF if (x.pbrMetallicRoughness.baseColorTexture != null && x.pbrMetallicRoughness.baseColorTexture.index != -1) { - var texture = GetTextureFunc(x.pbrMetallicRoughness.baseColorTexture.index); + var texture = getTexture(x.pbrMetallicRoughness.baseColorTexture.index); if (texture != null) { material.mainTexture = texture.Texture; @@ -164,7 +163,7 @@ namespace UniGLTF if (x.pbrMetallicRoughness.metallicRoughnessTexture != null && x.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) { material.EnableKeyword("_METALLICGLOSSMAP"); - var texture = GetTextureFunc(x.pbrMetallicRoughness.metallicRoughnessTexture.index); + var texture = getTexture(x.pbrMetallicRoughness.metallicRoughnessTexture.index); if (texture != null) { var prop = "_MetallicGlossMap"; @@ -189,7 +188,7 @@ namespace UniGLTF if (x.normalTexture != null && x.normalTexture.index != -1) { material.EnableKeyword("_NORMALMAP"); - var texture = GetTextureFunc(x.normalTexture.index); + var texture = getTexture(x.normalTexture.index); if (texture != null) { var prop = "_BumpMap"; @@ -203,7 +202,7 @@ namespace UniGLTF if (x.occlusionTexture != null && x.occlusionTexture.index != -1) { - var texture = GetTextureFunc(x.occlusionTexture.index); + var texture = getTexture(x.occlusionTexture.index); if (texture != null) { var prop = "_OcclusionMap"; @@ -228,7 +227,7 @@ namespace UniGLTF if (x.emissiveTexture != null && x.emissiveTexture.index != -1) { - var texture = GetTextureFunc(x.emissiveTexture.index); + var texture = getTexture(x.emissiveTexture.index); if (texture != null) { material.SetTexture("_EmissionMap", texture.Texture); diff --git a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs index d34f3dd4c..2ba1e589e 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MaterialTests.cs @@ -33,8 +33,8 @@ namespace UniGLTF gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions = gltfMaterial.pbrMetallicRoughness.baseColorTexture.extensions.Deserialize(); var shaderStore = new ShaderStore(null); - var materialImporter = new MaterialImporter(shaderStore, (int index) => { return null; }); - var dstMaterial = materialImporter.CreateMaterial(0, gltfMaterial, false); + var materialImporter = new MaterialImporter(shaderStore); + var dstMaterial = materialImporter.CreateMaterial(0, gltfMaterial, false, x => null); Assert.AreEqual(dstMaterial.mainTextureOffset.x, offset.x, 0.3f); Assert.AreEqual(dstMaterial.mainTextureOffset.y, offset.y, 0.2f); @@ -222,10 +222,10 @@ namespace UniGLTF public void MaterialImportTest() { var shaderStore = new ShaderStore(null); - var materialImporter = new MaterialImporter(shaderStore, null); + var materialImporter = new MaterialImporter(shaderStore); { - var material = materialImporter.CreateMaterial(0, new glTFMaterial { }, false); + var material = materialImporter.CreateMaterial(0, new glTFMaterial { }, false, null); Assert.AreEqual("Standard", material.shader.name); } } diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index 4bb9f6c6c..ca8197fda 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -42,7 +42,7 @@ namespace VRM { VRM = vrm; // override material importer - SetMaterialImporter(new VRMMaterialImporter(this, VRM.materialProperties)); + SetMaterialImporter(new VRMMaterialImporter(new ShaderStore(this), VRM.materialProperties)); } else { diff --git a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs index af08ab048..47610ef61 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs @@ -9,7 +9,7 @@ namespace VRM public class VRMMaterialImporter : MaterialImporter { List m_materials; - public VRMMaterialImporter(ImporterContext context, List materials) : base(new ShaderStore(context), (int index) => context.GetTexture(index)) + public VRMMaterialImporter(IShaderStore shaderStore, List materials) : base(shaderStore) { m_materials = materials; } @@ -26,9 +26,9 @@ namespace VRM "VRM/UnlitTransparentZWrite", }; - public override Material CreateMaterial(int i, glTFMaterial src, bool hasVertexColor) + public override Material CreateMaterial(int i, glTFMaterial src, bool hasVertexColor, GetTextureFunc getTexture) { - if(i==0 && m_materials.Count == 0) + if (i == 0 && m_materials.Count == 0) { // dummy return new Material(Shader.Find("Standard")); @@ -50,7 +50,7 @@ namespace VRM { Debug.LogWarningFormat("unknown shader {0}.", shaderName); } - return base.CreateMaterial(i, src, hasVertexColor); + return base.CreateMaterial(i, src, hasVertexColor, getTexture); } // @@ -81,7 +81,7 @@ namespace VRM } foreach (var kv in item.textureProperties) { - var texture = base.GetTextureFunc(kv.Value); + var texture = getTexture(kv.Value); if (texture != null) { var converted = texture.ConvertTexture(kv.Key);