diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs index 502aec107..326834f80 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/GltfSerializer.g.cs @@ -475,9 +475,9 @@ public static void Serialize_gltf_textures_ITEM(JsonFormatter f, glTFTexture val f.Value(value.sampler); } - if(value.source>=0){ + if(value.source.HasValue){ f.Key("source"); - f.Value(value.source); + f.Value(value.source.Value); } if(value.extensions!=null){ diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFTexture.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFTexture.cs index 7601237e6..c7dc033ae 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFTexture.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFTexture.cs @@ -58,8 +58,7 @@ namespace UniGLTF [JsonSchema(Minimum = 0)] public int sampler; - [JsonSchema(Minimum = 0)] - public int source; + public int? source; // empty schemas public glTFExtension extensions; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs index 526a7faed..815d6e238 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using UnityEngine; using VRMShaders; using ColorSpace = VRMShaders.ColorSpace; @@ -70,13 +71,19 @@ namespace UniGLTF var vectors = new Dictionary(); var actions = new List>(); - var standardTexDesc = default(TextureDescriptor); + TextureDescriptor? standardTexDesc = default; if (src.pbrMetallicRoughness != null || src.occlusionTexture != null) { if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null) { - SubAssetKey key; - (key, standardTexDesc) = GltfPbrTextureImporter.StandardTexture(data, src); + if (GltfPbrTextureImporter.TryStandardTexture(data, src, out var value)) + { + if (string.IsNullOrEmpty(value.Item2.UnityObjectName)) + { + throw new ArgumentNullException(); + } + standardTexDesc = value.Item2; + } } if (src.pbrMetallicRoughness.baseColorFactor != null && @@ -90,15 +97,22 @@ namespace UniGLTF if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1) { - var (key, textureParam) = GltfPbrTextureImporter.BaseColorTexture(data, src); - textureSlots.Add("_MainTex", textureParam); + if (GltfPbrTextureImporter.TryBaseColorTexture(data, src, out var value)) + { + if (string.IsNullOrEmpty(value.Item2.UnityObjectName)) + { + throw new ArgumentNullException(); + } + textureSlots.Add("_MainTex", value.Item2); + } } if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && - src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) + src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1 && + standardTexDesc.HasValue) { actions.Add(material => material.EnableKeyword("_METALLICGLOSSMAP")); - textureSlots.Add("_MetallicGlossMap", standardTexDesc); + textureSlots.Add("_MetallicGlossMap", standardTexDesc.Value); // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212. floatValues.Add("_Metallic", 1.0f); floatValues.Add("_GlossMapScale", 1.0f); @@ -113,14 +127,20 @@ namespace UniGLTF if (src.normalTexture != null && src.normalTexture.index != -1) { actions.Add(material => material.EnableKeyword("_NORMALMAP")); - var (_, textureParam) = GltfPbrTextureImporter.NormalTexture(data, src); - textureSlots.Add("_BumpMap", textureParam); - floatValues.Add("_BumpScale", src.normalTexture.scale); + if (GltfPbrTextureImporter.TryNormalTexture(data, src, out var value)) + { + if (string.IsNullOrEmpty(value.Item2.UnityObjectName)) + { + throw new ArgumentNullException(); + } + textureSlots.Add("_BumpMap", value.Item2); + floatValues.Add("_BumpScale", src.normalTexture.scale); + } } - if (src.occlusionTexture != null && src.occlusionTexture.index != -1) + if (src.occlusionTexture != null && src.occlusionTexture.index != -1 && standardTexDesc.HasValue) { - textureSlots.Add("_OcclusionMap", standardTexDesc); + textureSlots.Add("_OcclusionMap", standardTexDesc.Value); floatValues.Add("_OcclusionStrength", src.occlusionTexture.strength); } @@ -152,8 +172,14 @@ namespace UniGLTF if (src.emissiveTexture != null && src.emissiveTexture.index != -1) { - var (key, textureParam) = GltfPbrTextureImporter.EmissiveTexture(data, src); - textureSlots.Add("_EmissionMap", textureParam); + if (GltfPbrTextureImporter.TryEmissiveTexture(data, src, out var value)) + { + if (string.IsNullOrEmpty(value.Item2.UnityObjectName)) + { + throw new ArgumentNullException(); + } + textureSlots.Add("_EmissionMap", value.Item2); + } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs index 7bbde1681..38c45c2b8 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs @@ -42,11 +42,11 @@ namespace UniGLTF // texture if (src.pbrMetallicRoughness.baseColorTexture != null) { - var (offset, scale) = - GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture); - var (key, textureParam) = GltfTextureImporter.CreateSrgb(data, - src.pbrMetallicRoughness.baseColorTexture.index, offset, scale); - textureSlots.Add("_MainTex", textureParam); + var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture); + if (GltfTextureImporter.TryCreateSrgb(data, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale, out var value)) + { + textureSlots.Add("_MainTex", value.Item2); + } } matDesc = new MaterialDescriptor( diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/GltfPbrURPMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/GltfPbrURPMaterialImporter.cs index e9996cfac..6c65f2984 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/GltfPbrURPMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/URP/GltfPbrURPMaterialImporter.cs @@ -42,13 +42,19 @@ namespace UniGLTF var actions = new List>(); var src = data.GLTF.materials[i]; - var standardTexDesc = default(TextureDescriptor); + TextureDescriptor? standardTexDesc = default; if (src.pbrMetallicRoughness != null || src.occlusionTexture != null) { if (src.pbrMetallicRoughness.metallicRoughnessTexture != null || src.occlusionTexture != null) { - SubAssetKey key; - (key, standardTexDesc) = GltfPbrTextureImporter.StandardTexture(data, src); + if (GltfPbrTextureImporter.TryStandardTexture(data, src, out var value)) + { + if (string.IsNullOrEmpty(value.Item2.UnityObjectName)) + { + throw new ArgumentNullException(); + } + standardTexDesc = value.Item2; + } } if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) @@ -61,15 +67,17 @@ namespace UniGLTF if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1) { - var (key, textureParam) = GltfPbrTextureImporter.BaseColorTexture(data, src); - // from _MainTex ! - textureSlots.Add("_BaseMap", textureParam); + if (GltfPbrTextureImporter.TryBaseColorTexture(data, src, out var value)) + { + // from _MainTex ! + textureSlots.Add("_BaseMap", value.Item2); + } } - if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1) + if (src.pbrMetallicRoughness.metallicRoughnessTexture != null && src.pbrMetallicRoughness.metallicRoughnessTexture.index != -1 && standardTexDesc.HasValue) { actions.Add(material => material.EnableKeyword("_METALLICGLOSSMAP")); - textureSlots.Add("_MetallicGlossMap", standardTexDesc); + textureSlots.Add("_MetallicGlossMap", standardTexDesc.Value); // Set 1.0f as hard-coded. See: https://github.com/dwango/UniVRM/issues/212. floatValues.Add("_Metallic", 1.0f); floatValues.Add("_GlossMapScale", 1.0f); @@ -87,14 +95,17 @@ namespace UniGLTF if (src.normalTexture != null && src.normalTexture.index != -1) { actions.Add(material => material.EnableKeyword("_NORMALMAP")); - var (key, textureParam) = GltfPbrTextureImporter.NormalTexture(data, src); - textureSlots.Add("_BumpMap", textureParam); - floatValues.Add("_BumpScale", src.normalTexture.scale); + if (GltfPbrTextureImporter.TryNormalTexture(data, src, out var value)) + { + var (key, textureParam) = value; + textureSlots.Add("_BumpMap", textureParam); + floatValues.Add("_BumpScale", src.normalTexture.scale); + } } - if (src.occlusionTexture != null && src.occlusionTexture.index != -1) + if (src.occlusionTexture != null && src.occlusionTexture.index != -1 && standardTexDesc.HasValue) { - textureSlots.Add("_OcclusionMap", standardTexDesc); + textureSlots.Add("_OcclusionMap", standardTexDesc.Value); floatValues.Add("_OcclusionStrength", src.occlusionTexture.strength); } @@ -125,8 +136,10 @@ namespace UniGLTF if (src.emissiveTexture != null && src.emissiveTexture.index != -1) { - var (key, textureParam) = GltfPbrTextureImporter.EmissiveTexture(data, src); - textureSlots.Add("_EmissionMap", textureParam); + if (GltfPbrTextureImporter.TryEmissiveTexture(data, src, out var value)) + { + textureSlots.Add("_EmissionMap", value.Item2); + } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs index bdb31f01d..ce3d4e5aa 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs @@ -172,16 +172,19 @@ namespace UniGLTF for (var textureIdx = 0; textureIdx < GLTF.textures.Count; ++textureIdx) { var gltfTexture = GLTF.textures[textureIdx]; - var gltfImage = GLTF.images[gltfTexture.source]; - if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:")) + if (gltfTexture.source.HasValidIndex()) { - // from image uri - gltfTexture.name = Path.GetFileNameWithoutExtension(gltfImage.uri); - } - if (string.IsNullOrEmpty(gltfTexture.name)) - { - // use image name - gltfTexture.name = gltfImage.name; + var gltfImage = GLTF.images[gltfTexture.source.Value]; + if (!string.IsNullOrEmpty(gltfImage.uri) && !gltfImage.uri.StartsWith("data:")) + { + // from image uri + gltfTexture.name = Path.GetFileNameWithoutExtension(gltfImage.uri); + } + if (string.IsNullOrEmpty(gltfTexture.name)) + { + // use image name + gltfTexture.name = gltfImage.name; + } } if (string.IsNullOrEmpty(gltfTexture.name)) { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfPbrTextureImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfPbrTextureImporter.cs index 5d7ab1dfa..d86c5a2b1 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfPbrTextureImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfPbrTextureImporter.cs @@ -15,7 +15,10 @@ namespace UniGLTF // base color if (m.pbrMetallicRoughness?.baseColorTexture != null) { - yield return BaseColorTexture(data, m); + if (TryBaseColorTexture(data, m, out var value)) + { + yield return value; + } } // metallic roughness @@ -28,13 +31,19 @@ namespace UniGLTF // emission if (m.emissiveTexture != null) { - yield return EmissiveTexture(data, m); + if (TryEmissiveTexture(data, m, out var value)) + { + yield return value; + } } // normal if (m.normalTexture != null) { - yield return NormalTexture(data, m); + if (TryNormalTexture(data, m, out var value)) + { + yield return value; + } } // occlusion @@ -47,17 +56,20 @@ namespace UniGLTF // metallicSmooth and occlusion if (metallicRoughnessTexture.HasValue || occlusionTexture.HasValue) { - yield return StandardTexture(data, m); + if (TryStandardTexture(data, m, out var value)) + { + yield return value; + } } } - public static (SubAssetKey, TextureDescriptor) BaseColorTexture(GltfData data, glTFMaterial src) + public static bool TryBaseColorTexture(GltfData data, glTFMaterial src, out (SubAssetKey, TextureDescriptor) value) { var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture); - return GltfTextureImporter.CreateSrgb(data, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale); + return GltfTextureImporter.TryCreateSrgb(data, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale, out value); } - public static (SubAssetKey, TextureDescriptor) StandardTexture(GltfData data, glTFMaterial src) + public static bool TryStandardTexture(GltfData data, glTFMaterial src, out (SubAssetKey, TextureDescriptor) value) { var metallicFactor = 1.0f; var roughnessFactor = 1.0f; @@ -67,25 +79,24 @@ namespace UniGLTF roughnessFactor = src.pbrMetallicRoughness.roughnessFactor; } var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.metallicRoughnessTexture); - return GltfTextureImporter.CreateStandard(data, + return GltfTextureImporter.TryCreateStandard(data, src.pbrMetallicRoughness?.metallicRoughnessTexture?.index, src.occlusionTexture?.index, offset, scale, metallicFactor, - roughnessFactor); + roughnessFactor, out value); } - public static (SubAssetKey, TextureDescriptor) NormalTexture(GltfData data, glTFMaterial src) + public static bool TryNormalTexture(GltfData data, glTFMaterial src, out (SubAssetKey, TextureDescriptor) value) { var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.normalTexture); - return GltfTextureImporter.CreateNormal(data, src.normalTexture.index, offset, scale); + return GltfTextureImporter.TryCreateNormal(data, src.normalTexture.index, offset, scale, out value); } - public static (SubAssetKey, TextureDescriptor) EmissiveTexture(GltfData data, glTFMaterial src) + public static bool TryEmissiveTexture(GltfData data, glTFMaterial src, out (SubAssetKey, TextureDescriptor) value) { var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.emissiveTexture); - return GltfTextureImporter.CreateSrgb(data, src.emissiveTexture.index, offset, scale); + return GltfTextureImporter.TryCreateSrgb(data, src.emissiveTexture.index, offset, scale, out value); } - } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs index bde1d5d2b..1c55e2774 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs @@ -35,10 +35,16 @@ namespace UniGLTF return (texDesc.SubAssetKey, texDesc); } - public static (SubAssetKey, TextureDescriptor) CreateSrgb(GltfData data, int textureIndex, Vector2 offset, Vector2 scale) + public static bool TryCreateSrgb(GltfData data, int textureIndex, Vector2 offset, Vector2 scale, out (SubAssetKey, TextureDescriptor) value) { var gltfTexture = data.GLTF.textures[textureIndex]; - var gltfImage = data.GLTF.images[gltfTexture.source]; + if (!gltfTexture.source.HasValidIndex()) + { + value = default; + return false; + } + + var gltfImage = data.GLTF.images[gltfTexture.source.Value]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.sRGB, gltfTexture.name, gltfImage.uri); var sampler = TextureSamplerUtil.CreateSampler(data.GLTF, textureIndex); var param = new TextureDescriptor( @@ -50,13 +56,20 @@ namespace UniGLTF default, () => Task.FromResult(GetImageBytesFromTextureIndex(data, textureIndex)), default, default, default, default, default); - return (param.SubAssetKey, param); + value = (param.SubAssetKey, param); + return true; } - public static (SubAssetKey, TextureDescriptor) CreateLinear(GltfData data, int textureIndex, Vector2 offset, Vector2 scale) + public static bool TryCreateLinear(GltfData data, int textureIndex, Vector2 offset, Vector2 scale, out (SubAssetKey, TextureDescriptor) value) { var gltfTexture = data.GLTF.textures[textureIndex]; - var gltfImage = data.GLTF.images[gltfTexture.source]; + if (!gltfTexture.source.HasValidIndex()) + { + value = default; + return false; + } + + var gltfImage = data.GLTF.images[gltfTexture.source.Value]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.Linear, gltfTexture.name, gltfImage.uri); var sampler = TextureSamplerUtil.CreateSampler(data.GLTF, textureIndex); var param = new TextureDescriptor( @@ -69,13 +82,20 @@ namespace UniGLTF default, () => Task.FromResult(GetImageBytesFromTextureIndex(data, textureIndex)), default, default, default, default, default); - return (param.SubAssetKey, param); + value = (param.SubAssetKey, param); + return true; } - public static (SubAssetKey, TextureDescriptor) CreateNormal(GltfData data, int textureIndex, Vector2 offset, Vector2 scale) + public static bool TryCreateNormal(GltfData data, int textureIndex, Vector2 offset, Vector2 scale, out (SubAssetKey, TextureDescriptor) value) { var gltfTexture = data.GLTF.textures[textureIndex]; - var gltfImage = data.GLTF.images[gltfTexture.source]; + if (!gltfTexture.source.HasValidIndex()) + { + value = default; + return false; + } + + var gltfImage = data.GLTF.images[gltfTexture.source.Value]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.NormalMap, gltfTexture.name, gltfImage.uri); var sampler = TextureSamplerUtil.CreateSampler(data.GLTF, textureIndex); var param = new TextureDescriptor( @@ -88,10 +108,11 @@ namespace UniGLTF default, () => Task.FromResult(GetImageBytesFromTextureIndex(data, textureIndex)), default, default, default, default, default); - return (param.SubAssetKey, param); + value = (param.SubAssetKey, param); + return true; } - public static (SubAssetKey, TextureDescriptor) CreateStandard(GltfData data, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, Vector2 offset, Vector2 scale, float metallicFactor, float roughnessFactor) + public static bool TryCreateStandard(GltfData data, int? metallicRoughnessTextureIndex, int? occlusionTextureIndex, Vector2 offset, Vector2 scale, float metallicFactor, float roughnessFactor, out (SubAssetKey, TextureDescriptor) value) { string name = default; @@ -100,21 +121,33 @@ namespace UniGLTF if (metallicRoughnessTextureIndex.HasValue) { var gltfTexture = data.GLTF.textures[metallicRoughnessTextureIndex.Value]; - name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, data.GLTF.images[gltfTexture.source].uri); - sampler = TextureSamplerUtil.CreateSampler(data.GLTF, metallicRoughnessTextureIndex.Value); - getMetallicRoughnessAsync = () => Task.FromResult(GetImageBytesFromTextureIndex(data, metallicRoughnessTextureIndex.Value)); + if (gltfTexture.source.HasValidIndex()) + { + name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, data.GLTF.images[gltfTexture.source.Value].uri); + sampler = TextureSamplerUtil.CreateSampler(data.GLTF, metallicRoughnessTextureIndex.Value); + getMetallicRoughnessAsync = () => Task.FromResult(GetImageBytesFromTextureIndex(data, metallicRoughnessTextureIndex.Value)); + } } GetTextureBytesAsync getOcclusionAsync = default; if (occlusionTextureIndex.HasValue) { var gltfTexture = data.GLTF.textures[occlusionTextureIndex.Value]; - if (string.IsNullOrEmpty(name)) + if (gltfTexture.source.HasValidIndex()) { - name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, data.GLTF.images[gltfTexture.source].uri); + if (string.IsNullOrEmpty(name)) + { + name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, data.GLTF.images[gltfTexture.source.Value].uri); + } + sampler = TextureSamplerUtil.CreateSampler(data.GLTF, occlusionTextureIndex.Value); + getOcclusionAsync = () => Task.FromResult(GetImageBytesFromTextureIndex(data, occlusionTextureIndex.Value)); } - sampler = TextureSamplerUtil.CreateSampler(data.GLTF, occlusionTextureIndex.Value); - getOcclusionAsync = () => Task.FromResult(GetImageBytesFromTextureIndex(data, occlusionTextureIndex.Value)); + } + + if (string.IsNullOrEmpty(name)) + { + value = default; + return false; } var texDesc = new TextureDescriptor( @@ -128,7 +161,13 @@ namespace UniGLTF getMetallicRoughnessAsync, getOcclusionAsync, default, default, default, default); - return (texDesc.SubAssetKey, texDesc); + value = (texDesc.SubAssetKey, texDesc); + if (string.IsNullOrEmpty(value.Item2.UnityObjectName)) + { + throw new ArgumentNullException(); + } + Debug.Log("${name}"); + return true; } public static (Vector2, Vector2) GetTextureOffsetAndScale(glTFTextureInfo textureInfo) diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index 8f73cf326..56f7d2fb3 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -301,8 +301,11 @@ namespace VRM meta.Title = gltfMeta.title; if (gltfMeta.texture >= 0) { - var (key, param) = GltfTextureImporter.CreateSrgb(Data, gltfMeta.texture, Vector2.zero, Vector2.one); - meta.Thumbnail = await TextureFactory.GetTextureAsync(param, awaitCaller) as Texture2D; + if (GltfTextureImporter.TryCreateSrgb(Data, gltfMeta.texture, Vector2.zero, Vector2.one, out var value)) + { + var (key, param) = value; + meta.Thumbnail = await TextureFactory.GetTextureAsync(param, awaitCaller) as Texture2D; + } } meta.AllowedUser = gltfMeta.allowedUser; meta.ViolentUssage = gltfMeta.violentUssage; diff --git a/Assets/VRM/Runtime/IO/VRMMToonTextureImporter.cs b/Assets/VRM/Runtime/IO/VRMMToonTextureImporter.cs index 52485a320..a05ca5836 100644 --- a/Assets/VRM/Runtime/IO/VRMMToonTextureImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMToonTextureImporter.cs @@ -33,13 +33,10 @@ namespace VRM switch (textureKey) { case MToon.Utils.PropBumpMap: - texture = GltfTextureImporter.CreateNormal(data, textureIdx, offset, scale); - break; + return GltfTextureImporter.TryCreateNormal(data, textureIdx, offset, scale, out texture); default: - texture = GltfTextureImporter.CreateSrgb(data, textureIdx, offset, scale); - break; + return GltfTextureImporter.TryCreateSrgb(data, textureIdx, offset, scale, out texture); } - return true; } texture = default; diff --git a/Assets/VRM/Runtime/IO/VrmTextureDescriptorGenerator.cs b/Assets/VRM/Runtime/IO/VrmTextureDescriptorGenerator.cs index 047d866cb..c8906f937 100644 --- a/Assets/VRM/Runtime/IO/VrmTextureDescriptorGenerator.cs +++ b/Assets/VRM/Runtime/IO/VrmTextureDescriptorGenerator.cs @@ -69,8 +69,7 @@ namespace VRM { if (vrm.meta.texture > -1) { - texture = GltfTextureImporter.CreateSrgb(data, vrm.meta.texture, Vector2.zero, Vector2.one); - return true; + return GltfTextureImporter.TryCreateSrgb(data, vrm.meta.texture, Vector2.zero, Vector2.one, out texture); } texture = default; diff --git a/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs b/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs index edbb26db1..770ff94bf 100644 --- a/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs +++ b/Assets/VRM10/Runtime/IO/Texture/Vrm10MToonTextureImporter.cs @@ -62,8 +62,7 @@ namespace UniVRM10 { try { - pair = GltfPbrTextureImporter.BaseColorTexture(data, src); - return true; + return GltfPbrTextureImporter.TryBaseColorTexture(data, src, out pair); } catch (NullReferenceException) { @@ -81,8 +80,7 @@ namespace UniVRM10 { try { - pair = GltfPbrTextureImporter.EmissiveTexture(data, src); - return true; + return GltfPbrTextureImporter.TryEmissiveTexture(data, src, out pair); } catch (NullReferenceException) { @@ -101,8 +99,7 @@ namespace UniVRM10 { try { - pair = GltfPbrTextureImporter.NormalTexture(data, src); - return true; + return GltfPbrTextureImporter.TryNormalTexture(data, src, out pair); } catch (NullReferenceException) { @@ -151,8 +148,7 @@ namespace UniVRM10 try { var (offset, scale) = GetTextureOffsetAndScale(info); - pair = GltfTextureImporter.CreateSrgb(data, info.index, offset, scale); - return true; + return GltfTextureImporter.TryCreateSrgb(data, info.index, offset, scale, out pair); } catch (NullReferenceException) { @@ -170,8 +166,7 @@ namespace UniVRM10 try { var (offset, scale) = GetTextureOffsetAndScale(info); - pair = GltfTextureImporter.CreateLinear(data, info.index, offset, scale); - return true; + return GltfTextureImporter.TryCreateLinear(data, info.index, offset, scale, out pair); } catch (NullReferenceException) { diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/SubAssetKey.cs b/Assets/VRMShaders/GLTF/IO/Runtime/SubAssetKey.cs index d9bd042b0..ffd1e18f7 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/SubAssetKey.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/SubAssetKey.cs @@ -55,9 +55,14 @@ namespace VRMShaders public SubAssetKey(Type type, string name) { - if (type == null || string.IsNullOrEmpty(name)) + if (type == null) { - throw new System.ArgumentNullException(); + throw new System.ArgumentNullException("type"); + } + + if (string.IsNullOrEmpty(name)) + { + throw new System.ArgumentNullException("name"); } if (!type.IsSubclassOf(typeof(UnityEngine.Object))) diff --git a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/TextureDescriptor.cs b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/TextureDescriptor.cs index 37b4ea2c7..83c434c58 100644 --- a/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/TextureDescriptor.cs +++ b/Assets/VRMShaders/GLTF/IO/Runtime/Texture/Importer/TextureDescriptor.cs @@ -52,6 +52,10 @@ namespace VRMShaders GetTextureBytesAsync i4, GetTextureBytesAsync i5) { + if (string.IsNullOrEmpty(name)) + { + throw new ArgumentNullException("name"); + } UnityObjectName = name; Offset = offset; Scale = scale;