diff --git a/Scripts/Format/VRMImporter.cs b/Scripts/Format/VRMImporter.cs index ef9033313..f31d68784 100644 --- a/Scripts/Format/VRMImporter.cs +++ b/Scripts/Format/VRMImporter.cs @@ -181,7 +181,7 @@ namespace VRM MaterialName = x.materialName, ValueName = x.propertyName, TargetValue = value, - BaseValue = context.Materials.First(y => y.name == x.materialName).GetColor(x.propertyName), + BaseValue = context.GetMaterials().First(y => y.name == x.materialName).GetColor(x.propertyName), }; }) .ToArray(); @@ -297,15 +297,24 @@ namespace VRM static IEnumerator LoadMaterials(VRMImporterContext context) { + Func getTexture = x => + { + if (x < 0 || x >= context.Textures.Count) + { + return null; + } + return context.Textures[x]; + }; + if (context.GLTF.materials == null || !context.GLTF.materials.Any()) { - context.Materials.Add(context.MaterialImporter.CreateMaterial(context, 0)); + context.AddMaterial(context.MaterialImporter.CreateMaterial(0, null, getTexture)); } else { for (int i = 0; i < context.GLTF.materials.Count; ++i) { - context.Materials.Add(context.MaterialImporter.CreateMaterial(context, i)); + context.AddMaterial(context.MaterialImporter.CreateMaterial(i, context.GLTF.materials[i], getTexture)); yield return null; } } diff --git a/Scripts/Format/VRMMaterialImporter.cs b/Scripts/Format/VRMMaterialImporter.cs index d7ff8a3e4..03aada0a6 100644 --- a/Scripts/Format/VRMMaterialImporter.cs +++ b/Scripts/Format/VRMMaterialImporter.cs @@ -2,7 +2,7 @@ using UniGLTF; using UnityEngine; using System.Linq; - +using System; namespace VRM { @@ -47,7 +47,7 @@ namespace VRM "VRM/MToon", }; - public override Material CreateMaterial(ImporterContext ctx, int i) + public override Material CreateMaterial(int i, glTFMaterial src, Func getTexture) { var item = m_materials[i]; var shaderName = item.shader; @@ -60,12 +60,12 @@ namespace VRM if (VRM_SHADER_NAMES.Contains(shaderName)) { Debug.LogErrorFormat("shader {0} not found. set Assets/VRM/Shaders/VRMShaders to Edit - project setting - Graphics - preloaded shaders", shaderName); - return base.CreateMaterial(ctx, i); + return base.CreateMaterial(i, src, getTexture); } else { Debug.LogWarningFormat("unknown shader {0}.", shaderName); - return base.CreateMaterial(ctx, i); + return base.CreateMaterial(i, src, getTexture); } } @@ -97,7 +97,10 @@ namespace VRM } foreach (var kv in item.textureProperties) { - material.SetTexture(kv.Key, ctx.Textures[kv.Value].Texture); + var texture = getTexture(kv.Value); + if (texture != null) { + material.SetTexture(kv.Key, texture.Texture); + } } foreach (var kv in item.keywordMap) { diff --git a/UniGLTF b/UniGLTF index c1e5f0828..3bc3985f4 160000 --- a/UniGLTF +++ b/UniGLTF @@ -1 +1 @@ -Subproject commit c1e5f0828a7404476406cb99fccd8bf2cc1b2990 +Subproject commit 3bc3985f4dc04d3d0808a0957675365385893cac