diff --git a/Assets/VRM/Runtime/IO/VRMMToonMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMToonMaterialImporter.cs index 4ab6d48a32..7a5b2b0418 100644 --- a/Assets/VRM/Runtime/IO/VRMMToonMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMToonMaterialImporter.cs @@ -8,6 +8,17 @@ public static class VRMMToonMaterialImporter { public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int materialIdx, out MaterialDescriptor matDesc) { + if (vrm?.materialProperties == null || vrm.materialProperties.Count == 0) + { + matDesc = default; + return false; + } + if (materialIdx < 0 || materialIdx >= vrm.materialProperties.Count) + { + matDesc = default; + return false; + } + var vrmMaterial = vrm.materialProperties[materialIdx]; if (vrmMaterial.shader == VRM.glTF_VRM_Material.VRM_USE_GLTFSHADER) { @@ -42,7 +53,7 @@ public static bool TryCreateParam(GltfData data, glTF_VRM_extensions vrm, int ma foreach (var kv in vrmMaterial.textureProperties) { - if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrm, materialIdx, kv.Key, out var texture)) + if (VRMMToonTextureImporter.TryGetTextureFromMaterialProperty(data, vrmMaterial, kv.Key, out var texture)) { matDesc.TextureSlots.Add(kv.Key, texture.Item2); } diff --git a/Assets/VRM/Runtime/IO/VRMMToonTextureImporter.cs b/Assets/VRM/Runtime/IO/VRMMToonTextureImporter.cs index 596999e89a..de8dd36228 100644 --- a/Assets/VRM/Runtime/IO/VRMMToonTextureImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMToonTextureImporter.cs @@ -12,21 +12,19 @@ public static class VRMMToonTextureImporter var vrmMaterial = vrm.materialProperties[materialIdx]; foreach (var kv in vrmMaterial.textureProperties) { - if (TryGetTextureFromMaterialProperty(data, vrm, materialIdx, kv.Key, out var texture)) + if (TryGetTextureFromMaterialProperty(data, vrmMaterial, kv.Key, out var texture)) { yield return texture; } } } - - public static bool TryGetTextureFromMaterialProperty(GltfData data, glTF_VRM_extensions vrm, int materialIdx, string textureKey, out (SubAssetKey, TextureDescriptor) texture) + public static bool TryGetTextureFromMaterialProperty(GltfData data, glTF_VRM_Material vrmMaterial, string textureKey, out (SubAssetKey, TextureDescriptor) texture) { - var vrmMaterial = vrm.materialProperties[materialIdx]; // 任意の shader の import を許容する if (/*vrmMaterial.shader == MToon.Utils.ShaderName &&*/ vrmMaterial.textureProperties.TryGetValue(textureKey, out var textureIdx)) { var (offset, scale) = (new Vector2(0, 0), new Vector2(1, 1)); - if (TryGetTextureOffsetAndScale(vrm, materialIdx, textureKey, out var os)) + if (TryGetTextureOffsetAndScale(vrmMaterial, textureKey, out var os)) { offset = os.offset; scale = os.scale; @@ -48,10 +46,8 @@ public static bool TryGetTextureFromMaterialProperty(GltfData data, glTF_VRM_ext return false; } - public static bool TryGetTextureOffsetAndScale(glTF_VRM_extensions vrm, int materialIdx, string unityTextureKey, out (Vector2 offset, Vector2 scale) os) + public static bool TryGetTextureOffsetAndScale(glTF_VRM_Material vrmMaterial, string unityTextureKey, out (Vector2 offset, Vector2 scale) os) { - var vrmMaterial = vrm.materialProperties[materialIdx]; - if (vrmMaterial.vectorProperties.TryGetValue(unityTextureKey, out var vector)) { os = (new Vector2(vector[0], vector[1]), new Vector2(vector[2], vector[3]));