diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs index deb3e1e04..18a15adfa 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs @@ -34,58 +34,6 @@ namespace UniGLTF return $"material_{index:00}"; } - public MaterialImportParam GetMaterialParam(GltfParser parser, int i) - { - foreach (var tryCreate in GltfMaterialParamProcessors) - { - if (tryCreate(parser, i, out MaterialImportParam param)) - { - return param; - } - } - - // fallback -#if VRM_DEVELOP - Debug.LogWarning($"material: {i} out of range. fallback"); -#endif - return new MaterialImportParam(MaterialName(i, null), GltfPBRMaterial.ShaderName); - } - - public static (Vector2, Vector2) GetTextureOffsetAndScale(glTFTextureInfo textureInfo) - { - if (glTF_KHR_texture_transform.TryGet(textureInfo, out var textureTransform)) - { - return GetTextureOffsetAndScale(textureTransform); - } - return (new Vector2(0, 0), new Vector2(1, 1)); - } - - public static (Vector2, Vector2) GetTextureOffsetAndScale(glTF_KHR_texture_transform textureTransform) - { - var offset = new Vector2(0, 0); - var scale = new Vector2(1, 1); - - if (textureTransform != null) - { - if (textureTransform.offset != null && textureTransform.offset.Length == 2) - { - offset = new Vector2(textureTransform.offset[0], textureTransform.offset[1]); - } - - if (textureTransform.scale != null && textureTransform.scale.Length == 2) - { - scale = new Vector2(textureTransform.scale[0], textureTransform.scale[1]); - } - - // UV Coordinate Conversion: glTF(top-left origin) to Unity(bottom-left origin) - // Formula: https://github.com/vrm-c/UniVRM/issues/930 - offset.y = 1.0f - offset.y - scale.y; - } - - return (offset, scale); - } - - /// /// for unittest /// diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs index d8b1d48f5..0844eda09 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs @@ -49,7 +49,7 @@ namespace UniGLTF public static (SubAssetKey, TextureImportParam Param) BaseColorTexture(GltfParser parser, glTFMaterial src) { - var (offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture); + var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture); return GltfTextureImporter.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale); } @@ -62,7 +62,7 @@ namespace UniGLTF metallicFactor = src.pbrMetallicRoughness.metallicFactor; roughnessFactor = src.pbrMetallicRoughness.roughnessFactor; } - var (offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.metallicRoughnessTexture); + var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.metallicRoughnessTexture); return GltfTextureImporter.CreateStandard(parser, src.pbrMetallicRoughness?.metallicRoughnessTexture?.index, src.occlusionTexture?.index, @@ -73,13 +73,13 @@ namespace UniGLTF public static (SubAssetKey, TextureImportParam Param) NormalTexture(GltfParser parser, glTFMaterial src) { - var (offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(src.normalTexture); + var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.normalTexture); return GltfTextureImporter.CreateNormal(parser, src.normalTexture.index, offset, scale); } public static (SubAssetKey, TextureImportParam Param) EmissiveTexture(GltfParser parser, glTFMaterial src) { - var (offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(src.emissiveTexture); + var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.emissiveTexture); return GltfTextureImporter.CreateSRGB(parser, src.emissiveTexture.index, offset, scale); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs index 18283cf2b..05c335d19 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs @@ -22,13 +22,13 @@ namespace UniGLTF param = default; return false; } - + param = new MaterialImportParam(GltfMaterialImporter.MaterialName(i, src), ShaderName); // texture if (src.pbrMetallicRoughness.baseColorTexture != null) { - var (offset, scale) = GltfMaterialImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture); + var (offset, scale) = GltfTextureImporter.GetTextureOffsetAndScale(src.pbrMetallicRoughness.baseColorTexture); var (key, textureParam) = GltfTextureImporter.CreateSRGB(parser, src.pbrMetallicRoughness.baseColorTexture.index, offset, scale); param.TextureSlots.Add("_MainTex", textureParam); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs index 3a320cf33..12521f27b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs @@ -91,5 +91,39 @@ namespace UniGLTF var param = new TextureImportParam(name, ".png", null, offset, scale, sampler, TextureImportTypes.StandardMap, metallicFactor, roughnessFactor, getMetallicRoughnessAsync, getOcclusionAsync, default, default, default, default); return (param.SubAssetKey, param); } + + public static (Vector2, Vector2) GetTextureOffsetAndScale(glTFTextureInfo textureInfo) + { + if (glTF_KHR_texture_transform.TryGet(textureInfo, out var textureTransform)) + { + return GetTextureOffsetAndScale(textureTransform); + } + return (new Vector2(0, 0), new Vector2(1, 1)); + } + + public static (Vector2, Vector2) GetTextureOffsetAndScale(glTF_KHR_texture_transform textureTransform) + { + var offset = new Vector2(0, 0); + var scale = new Vector2(1, 1); + + if (textureTransform != null) + { + if (textureTransform.offset != null && textureTransform.offset.Length == 2) + { + offset = new Vector2(textureTransform.offset[0], textureTransform.offset[1]); + } + + if (textureTransform.scale != null && textureTransform.scale.Length == 2) + { + scale = new Vector2(textureTransform.scale[0], textureTransform.scale[1]); + } + + // UV Coordinate Conversion: glTF(top-left origin) to Unity(bottom-left origin) + // Formula: https://github.com/vrm-c/UniVRM/issues/930 + offset.y = 1.0f - offset.y - scale.y; + } + + return (offset, scale); + } } } diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialTextureImporter.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialTextureImporter.cs index 5f2f33ddc..586b88ba3 100644 --- a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialTextureImporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialTextureImporter.cs @@ -20,17 +20,17 @@ namespace UniVRM10 { yield return (MToon.Utils.PropEmissionMap, emissiveTex); } - + if (TryGetNormalTexture(parser, material, out var normalTex)) { yield return ("_BumpMap", normalTex); } - + if (TryGetShadeMultiplyTexture(parser, mToon, out var shadeTex)) { yield return (MToon.Utils.PropShadeTexture, shadeTex); } - + if (TryGetShadingShiftTexture(parser, mToon, out var shadeShiftTex)) { Debug.LogWarning("Need VRM 1.0 MToon implementation."); @@ -94,7 +94,7 @@ namespace UniVRM10 pair = default; return false; } - + } private static bool TryGetNormalTexture(GltfParser parser, glTFMaterial src, out (SubAssetKey, TextureImportParam) pair) @@ -189,7 +189,7 @@ namespace UniVRM10 { if (glTF_KHR_texture_transform.TryGet(textureInfo, out var textureTransform)) { - return GltfMaterialImporter.GetTextureOffsetAndScale(textureTransform); + return GltfTextureImporter.GetTextureOffsetAndScale(textureTransform); } return (new Vector2(0, 0), new Vector2(1, 1)); } @@ -202,7 +202,7 @@ namespace UniVRM10 public Vrm10TextureInfo(TextureInfo info) { if (info == null) return; - + index = info.Index ?? -1; texCoord = info.TexCoord ?? -1; extensions = info.Extensions as glTFExtension; @@ -212,7 +212,7 @@ namespace UniVRM10 public Vrm10TextureInfo(ShadingShiftTextureInfo info) { if (info == null) return; - + index = info.Index ?? -1; texCoord = info.TexCoord ?? -1; extensions = info.Extensions as glTFExtension; @@ -220,4 +220,4 @@ namespace UniVRM10 } } } -} \ No newline at end of file +}