From 5664c7586ae3fa6e7136ee0b9bf65be31ebb6a47 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 26 May 2021 16:31:11 +0900 Subject: [PATCH 1/6] mv func about importing texture --- .../IO/MaterialIO/GltfMaterialImporter.cs | 52 ------------------- .../UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs | 8 +-- .../IO/MaterialIO/GltfUnlitMaterial.cs | 4 +- .../IO/TextureIO/GltfTextureImporter.cs | 34 ++++++++++++ .../Vrm10MToonMaterialTextureImporter.cs | 16 +++--- 5 files changed, 48 insertions(+), 66 deletions(-) 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 +} From e5d0e927b400a3cc0e8609725fb773fa41f1da2e Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 26 May 2021 17:19:17 +0900 Subject: [PATCH 2/6] Define IMaterialExporter, IMaterialImporter --- .../Runtime/UniGLTF/IO/ImporterContext.cs | 37 ++++++++---- .../IO/MaterialIO/GltfMaterialImporter.cs | 60 ++++++------------- .../UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs | 2 +- .../IO/MaterialIO/GltfUnlitMaterial.cs | 2 +- .../IO/MaterialIO/IMaterialExporter.cs | 13 ++++ .../IO/MaterialIO/IMaterialExporter.cs.meta | 3 + .../IO/MaterialIO/IMaterialImporter.cs | 13 ++++ .../IO/MaterialIO/IMaterialImporter.cs.meta | 3 + .../UniGLTF/IO/MaterialIO/MaterialExporter.cs | 5 -- Assets/VRM/Runtime/IO/VRMImporterContext.cs | 3 +- Assets/VRM/Runtime/IO/VRMMaterialImporter.cs | 11 +++- Assets/VRM10/Runtime/IO/Vrm10Importer.cs | 5 +- .../VRM10/Runtime/IO/Vrm10MaterialImporter.cs | 47 ++++++++------- Assets/VRM10/Tests/MaterialImportTests.cs | 2 +- 14 files changed, 115 insertions(+), 91 deletions(-) create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs.meta create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs create mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 39a7b4cd5..22051d931 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -32,23 +32,21 @@ namespace UniGLTF #endregion - MaterialFactory m_materialFactory; - public MaterialFactory MaterialFactory => m_materialFactory; - - public readonly GltfMaterialImporter GltfMaterialImporter = new GltfMaterialImporter(); - - TextureFactory m_textureFactory; - public TextureFactory TextureFactory => m_textureFactory; + public IMaterialImporter MaterialImporter { get; protected set; } + public TextureFactory TextureFactory { get; } + public MaterialFactory MaterialFactory { get; } public ImporterContext(GltfParser parser, IReadOnlyDictionary externalObjectMap = null) { m_parser = parser; + MaterialImporter = new GltfMaterialImporter(); + externalObjectMap = externalObjectMap ?? new Dictionary(); - m_textureFactory = new TextureFactory(externalObjectMap + TextureFactory = new TextureFactory(externalObjectMap .Where(x => x.Value is Texture) .ToDictionary(x => x.Key, x => (Texture) x.Value)); - m_materialFactory = new MaterialFactory(externalObjectMap + MaterialFactory = new MaterialFactory(externalObjectMap .Where(x => x.Value is Material) .ToDictionary(x => x.Key, x => (Material) x.Value)); } @@ -105,6 +103,14 @@ namespace UniGLTF } } + using (MeasureTime("LoadTextures")) + { + foreach (var (key, param) in GltfTextureEnumerator.EnumerateAllTexturesDistinct(m_parser)) + { + + } + } + using (MeasureTime("LoadMaterials")) { await LoadMaterialsAsync(); @@ -178,19 +184,24 @@ namespace UniGLTF await awaitCaller.NextFrame(); } + private async Task LoadTexturesAsync() + { + + } + public async Task LoadMaterialsAsync() { if (m_parser.GLTF.materials == null || m_parser.GLTF.materials.Count == 0) { // no material. work around. - var param = GltfMaterialImporter.GetMaterialParam(m_parser, 0); + var param = MaterialImporter.GetMaterialParam(m_parser, 0); var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync); } else { for (int i = 0; i < m_parser.GLTF.materials.Count; ++i) { - var param = GltfMaterialImporter.GetMaterialParam(m_parser, i); + var param = MaterialImporter.GetMaterialParam(m_parser, i); var material = await MaterialFactory.LoadAsync(param, TextureFactory.GetTextureAsync); } } @@ -293,8 +304,8 @@ namespace UniGLTF } Meshes.Clear(); - m_materialFactory.Dispose(); - m_textureFactory.Dispose(); + MaterialFactory?.Dispose(); + TextureFactory?.Dispose(); if (m_ownRoot && Root != null) { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs index 18a15adfa..7b284db05 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs @@ -5,27 +5,29 @@ using VRMShaders; namespace UniGLTF { - public delegate bool TryCreateMaterialParamFromGltf(GltfParser parser, int i, out MaterialImportParam param); - - public class GltfMaterialImporter + /// + /// GLTF の MaterialImporter + /// + public sealed class GltfMaterialImporter : IMaterialImporter { - /// - /// gltfMaterialを解釈する関数。 - /// 拡張するには、先頭に挿入するべし。 - /// - /// - /// - public readonly List GltfMaterialParamProcessors = new List(); - - public GltfMaterialImporter() + public MaterialImportParam GetMaterialParam(GltfParser parser, int i) { - // unlit を試し - GltfMaterialParamProcessors.Add(GltfUnlitMaterial.TryCreateParam); - // PBR を作成する(失敗しない) - GltfMaterialParamProcessors.Add(GltfPBRMaterial.TryCreateParam); + if (!GltfUnlitMaterial.TryCreateParam(parser, i, out var param)) + { + if (!GltfPBRMaterial.TryCreateParam(parser, i, out param)) + { + // fallback +#if VRM_DEVELOP + Debug.LogWarning($"material: {i} out of range. fallback"); +#endif + return new MaterialImportParam(GetMaterialName(i, null), GltfPBRMaterial.ShaderName); + } + } + + return param; } - public static string MaterialName(int index, glTFMaterial src) + public static string GetMaterialName(int index, glTFMaterial src) { if (src != null && !string.IsNullOrEmpty(src.name)) { @@ -33,29 +35,5 @@ namespace UniGLTF } return $"material_{index:00}"; } - - /// - /// for unittest - /// - public static glTF CreateMaterialForTest(glTFMaterial material) - { - return new glTF - { - materials = new System.Collections.Generic.List { - material - }, - textures = new List{ - new glTFTexture{ - name = "texture_0" - } - }, - images = new List{ - new glTFImage{ - name = "image_0", - mimeType = "image/png", - } - }, - }; - } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs index 0844eda09..a9ff09904 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs @@ -92,7 +92,7 @@ namespace UniGLTF } var src = parser.GLTF.materials[i]; - param = new MaterialImportParam(GltfMaterialImporter.MaterialName(i, src), ShaderName); + param = new MaterialImportParam(GltfMaterialImporter.GetMaterialName(i, src), ShaderName); var standardParam = default(TextureImportParam); if (src.pbrMetallicRoughness != null || src.occlusionTexture != null) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs index 05c335d19..ea833944d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs @@ -23,7 +23,7 @@ namespace UniGLTF return false; } - param = new MaterialImportParam(GltfMaterialImporter.MaterialName(i, src), ShaderName); + param = new MaterialImportParam(GltfMaterialImporter.GetMaterialName(i, src), ShaderName); // texture if (src.pbrMetallicRoughness.baseColorTexture != null) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs new file mode 100644 index 000000000..be4270182 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs @@ -0,0 +1,13 @@ +using UnityEngine; +using VRMShaders; + +namespace UniGLTF +{ + /// + /// 指定の Unity Material から glTFMaterial を生成する。 + /// + public interface IMaterialExporter + { + glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter); + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs.meta new file mode 100644 index 000000000..c8cff3c4c --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: afbe933c51a9404199ba1efe6d2f8c62 +timeCreated: 1622015749 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs new file mode 100644 index 000000000..9919e17c0 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs @@ -0,0 +1,13 @@ +using VRMShaders; + +namespace UniGLTF +{ + /// + /// 指定の index の glTFMaterial から Import できる Material の生成情報を生成する。 + /// Material の Import は glTFMaterial と 1:1 対応する。 + /// + public interface IMaterialImporter + { + MaterialImportParam GetMaterialParam(GltfParser parser, int i); + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs.meta new file mode 100644 index 000000000..386a8bc44 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: c309fb8ee8534a54b291d408511c9331 +timeCreated: 1622015740 \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs index fcb75526f..b8c6b3216 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/MaterialExporter.cs @@ -13,11 +13,6 @@ namespace UniGLTF BLEND } - public interface IMaterialExporter - { - glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter); - } - public class MaterialExporter : IMaterialExporter { public virtual glTFMaterial ExportMaterial(Material m, ITextureExporter textureExporter) diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index ad0951171..aaef4f6d6 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -27,8 +27,7 @@ namespace VRM if (glTF_VRM_extensions.TryDeserialize(GLTF.extensions, out glTF_VRM_extensions vrm)) { VRM = vrm; - // override material importer - GltfMaterialImporter.GltfMaterialParamProcessors.Insert(0, new VRMMaterialImporter(VRM).TryCreateParam); + MaterialImporter = new VRMMaterialImporter(VRM); } else { diff --git a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs index d6b69d1de..ed25512e3 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs @@ -6,7 +6,7 @@ using VRMShaders; namespace VRM { - public class VRMMaterialImporter + public sealed class VRMMaterialImporter : IMaterialImporter { readonly glTF_VRM_extensions m_vrm; public VRMMaterialImporter(glTF_VRM_extensions vrm) @@ -103,7 +103,14 @@ namespace VRM if (!GltfUnlitMaterial.TryCreateParam(parser, i, out param)) { // pbr - GltfPBRMaterial.TryCreateParam(parser, i, out param); + if (!GltfPBRMaterial.TryCreateParam(parser, i, out param)) + { + // fallback +#if VRM_DEVELOP + Debug.LogWarning($"material: {i} out of range. fallback"); +#endif + return new MaterialImportParam(GltfMaterialImporter.GetMaterialName(i, null), GltfPBRMaterial.ShaderName); + } } } return param; diff --git a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs index 51329db12..8094490c3 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs @@ -24,6 +24,8 @@ namespace UniVRM10 public Vrm10Importer(UniGLTF.GltfParser parser, IReadOnlyDictionary externalObjectMap = null) : base(parser, externalObjectMap) { + MaterialImporter = new Vrm10MaterialImporter(); + m_externalMap = externalObjectMap; if (m_externalMap == null) { @@ -31,9 +33,6 @@ namespace UniVRM10 } m_model = ModelReader.Read(parser); - // for `VRMC_materials_mtoon` - this.GltfMaterialImporter.GltfMaterialParamProcessors.Insert(0, Vrm10MaterialImporter.TryCreateParam); - if (!UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(parser.GLTF.extensions, out m_vrm)) { throw new Exception("VRMC_vrm is not found"); diff --git a/Assets/VRM10/Runtime/IO/Vrm10MaterialImporter.cs b/Assets/VRM10/Runtime/IO/Vrm10MaterialImporter.cs index 16eede2f8..b9196f20b 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10MaterialImporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10MaterialImporter.cs @@ -4,16 +4,34 @@ using VRMShaders; namespace UniVRM10 { - public static class Vrm10MaterialImporter + public sealed class Vrm10MaterialImporter : IMaterialImporter { + public MaterialImportParam GetMaterialParam(GltfParser parser, int i) + { + // mtoon + if (!TryCreateMToonParam(parser, i, out MaterialImportParam param)) + { + // unlit + if (!GltfUnlitMaterial.TryCreateParam(parser, i, out param)) + { + // pbr + if (!GltfPBRMaterial.TryCreateParam(parser, i, out param)) + { + // fallback +#if VRM_DEVELOP + Debug.LogWarning($"material: {i} out of range. fallback"); +#endif + return new MaterialImportParam(GltfMaterialImporter.GetMaterialName(i, null), GltfPBRMaterial.ShaderName); + } + } + } + return param; + } + /// /// VMRC_materials_mtoon の場合にマテリアル生成情報を作成する /// - /// - /// - /// - /// - public static bool TryCreateParam(GltfParser parser, int i, out MaterialImportParam param) + public bool TryCreateMToonParam(GltfParser parser, int i, out MaterialImportParam param) { var m = parser.GLTF.materials[i]; if (!UniGLTF.Extensions.VRMC_materials_mtoon.GltfDeserializer.TryGet(m.extensions, @@ -48,7 +66,7 @@ namespace UniVRM10 } param.RenderQueue = Vrm10MToonMaterialParameterImporter.TryGetRenderQueue(m, mtoon); - + param.Actions.Add(material => { // Set hidden properties, keywords from float properties. @@ -57,20 +75,5 @@ namespace UniVRM10 return true; } - - public static MaterialImportParam GetMaterialParam(GltfParser parser, int i) - { - // mtoon - if (!TryCreateParam(parser, i, out MaterialImportParam param)) - { - // unlit - if (!GltfUnlitMaterial.TryCreateParam(parser, i, out param)) - { - // pbr - GltfPBRMaterial.TryCreateParam(parser, i, out param); - } - } - return param; - } } } diff --git a/Assets/VRM10/Tests/MaterialImportTests.cs b/Assets/VRM10/Tests/MaterialImportTests.cs index 4568eeb7a..6eeca4155 100644 --- a/Assets/VRM10/Tests/MaterialImportTests.cs +++ b/Assets/VRM10/Tests/MaterialImportTests.cs @@ -25,7 +25,7 @@ namespace UniVRM10 var parser = new GltfParser(); parser.Parse(AliciaPath, migratedBytes); - var materialParam = Vrm10MaterialImporter.GetMaterialParam(parser, 0); + var materialParam = new Vrm10MaterialImporter().GetMaterialParam(parser, 0); Assert.AreEqual("VRM/MToon", materialParam.ShaderName); Assert.AreEqual("Alicia_body", materialParam.TextureSlots["_MainTex"].UnityObjectName); From fda6b144661a6e0061ebd929123be6a998e2e07c Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 26 May 2021 17:23:37 +0900 Subject: [PATCH 3/6] Remove unused --- .../UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 22051d931..60f2c6c90 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -103,14 +103,6 @@ namespace UniGLTF } } - using (MeasureTime("LoadTextures")) - { - foreach (var (key, param) in GltfTextureEnumerator.EnumerateAllTexturesDistinct(m_parser)) - { - - } - } - using (MeasureTime("LoadMaterials")) { await LoadMaterialsAsync(); @@ -184,11 +176,6 @@ namespace UniGLTF await awaitCaller.NextFrame(); } - private async Task LoadTexturesAsync() - { - - } - public async Task LoadMaterialsAsync() { if (m_parser.GLTF.materials == null || m_parser.GLTF.materials.Count == 0) From f1a640be6171b717773045cd48af07a593729cb3 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 26 May 2021 17:28:08 +0900 Subject: [PATCH 4/6] Rename --- .../Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs | 6 +++--- .../{GltfPBRMaterial.cs => GltfPbrMaterialImporter.cs} | 2 +- ...BRMaterial.cs.meta => GltfPbrMaterialImporter.cs.meta} | 0 .../UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs | 8 ++++---- ...{GltfUnlitMaterial.cs => GltfUnlitMaterialImporter.cs} | 2 +- ...Material.cs.meta => GltfUnlitMaterialImporter.cs.meta} | 0 Assets/VRM/Runtime/IO/VRMMaterialImporter.cs | 6 +++--- .../IO/Material/Vrm10MToonMaterialTextureImporter.cs | 6 +++--- Assets/VRM10/Runtime/IO/Vrm10MaterialImporter.cs | 6 +++--- 9 files changed, 18 insertions(+), 18 deletions(-) rename Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/{GltfPBRMaterial.cs => GltfPbrMaterialImporter.cs} (99%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/{GltfPBRMaterial.cs.meta => GltfPbrMaterialImporter.cs.meta} (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/{GltfUnlitMaterial.cs => GltfUnlitMaterialImporter.cs} (98%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/{GltfUnlitMaterial.cs.meta => GltfUnlitMaterialImporter.cs.meta} (100%) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs index 7b284db05..012993ced 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfMaterialImporter.cs @@ -12,15 +12,15 @@ namespace UniGLTF { public MaterialImportParam GetMaterialParam(GltfParser parser, int i) { - if (!GltfUnlitMaterial.TryCreateParam(parser, i, out var param)) + if (!GltfUnlitMaterialImporter.TryCreateParam(parser, i, out var param)) { - if (!GltfPBRMaterial.TryCreateParam(parser, i, out param)) + if (!GltfPbrMaterialImporter.TryCreateParam(parser, i, out param)) { // fallback #if VRM_DEVELOP Debug.LogWarning($"material: {i} out of range. fallback"); #endif - return new MaterialImportParam(GetMaterialName(i, null), GltfPBRMaterial.ShaderName); + return new MaterialImportParam(GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName); } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs similarity index 99% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs index a9ff09904..99e98813a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs @@ -35,7 +35,7 @@ namespace UniGLTF /// _ZWrite /// /// - public static class GltfPBRMaterial + public static class GltfPbrMaterialImporter { public const string ShaderName = "Standard"; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPBRMaterial.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfPbrMaterialImporter.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs index c72081b79..5f2baecc9 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs @@ -38,7 +38,7 @@ namespace UniGLTF // base color if (m.pbrMetallicRoughness?.baseColorTexture != null) { - yield return GltfPBRMaterial.BaseColorTexture(parser, m); + yield return GltfPbrMaterialImporter.BaseColorTexture(parser, m); } // metallic roughness @@ -51,13 +51,13 @@ namespace UniGLTF // emission if (m.emissiveTexture != null) { - yield return GltfPBRMaterial.EmissiveTexture(parser, m); + yield return GltfPbrMaterialImporter.EmissiveTexture(parser, m); } // normal if (m.normalTexture != null) { - yield return GltfPBRMaterial.NormalTexture(parser, m); + yield return GltfPbrMaterialImporter.NormalTexture(parser, m); } // occlusion @@ -70,7 +70,7 @@ namespace UniGLTF // metallicSmooth and occlusion if (metallicRoughnessTexture.HasValue || occlusionTexture.HasValue) { - yield return GltfPBRMaterial.StandardTexture(parser, m); + yield return GltfPbrMaterialImporter.StandardTexture(parser, m); } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs similarity index 98% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs index ea833944d..149570a6a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs @@ -4,7 +4,7 @@ using VRMShaders; namespace UniGLTF { - public static class GltfUnlitMaterial + public static class GltfUnlitMaterialImporter { public const string ShaderName = "UniGLTF/UniUnlit"; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterial.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfUnlitMaterialImporter.cs.meta diff --git a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs index ed25512e3..2032d1c8b 100644 --- a/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMaterialImporter.cs @@ -100,16 +100,16 @@ namespace VRM if (!TryCreateParam(parser, i, out MaterialImportParam param)) { // unlit - if (!GltfUnlitMaterial.TryCreateParam(parser, i, out param)) + if (!GltfUnlitMaterialImporter.TryCreateParam(parser, i, out param)) { // pbr - if (!GltfPBRMaterial.TryCreateParam(parser, i, out param)) + if (!GltfPbrMaterialImporter.TryCreateParam(parser, i, out param)) { // fallback #if VRM_DEVELOP Debug.LogWarning($"material: {i} out of range. fallback"); #endif - return new MaterialImportParam(GltfMaterialImporter.GetMaterialName(i, null), GltfPBRMaterial.ShaderName); + return new MaterialImportParam(GltfMaterialImporter.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName); } } } diff --git a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialTextureImporter.cs b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialTextureImporter.cs index 586b88ba3..127f9a7e7 100644 --- a/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialTextureImporter.cs +++ b/Assets/VRM10/Runtime/IO/Material/Vrm10MToonMaterialTextureImporter.cs @@ -62,7 +62,7 @@ namespace UniVRM10 { try { - pair = GltfPBRMaterial.BaseColorTexture(parser, src); + pair = GltfPbrMaterialImporter.BaseColorTexture(parser, src); return true; } catch (NullReferenceException) @@ -81,7 +81,7 @@ namespace UniVRM10 { try { - pair = GltfPBRMaterial.EmissiveTexture(parser, src); + pair = GltfPbrMaterialImporter.EmissiveTexture(parser, src); return true; } catch (NullReferenceException) @@ -101,7 +101,7 @@ namespace UniVRM10 { try { - pair = GltfPBRMaterial.NormalTexture(parser, src); + pair = GltfPbrMaterialImporter.NormalTexture(parser, src); return true; } catch (NullReferenceException) diff --git a/Assets/VRM10/Runtime/IO/Vrm10MaterialImporter.cs b/Assets/VRM10/Runtime/IO/Vrm10MaterialImporter.cs index b9196f20b..384fa7902 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10MaterialImporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10MaterialImporter.cs @@ -12,16 +12,16 @@ namespace UniVRM10 if (!TryCreateMToonParam(parser, i, out MaterialImportParam param)) { // unlit - if (!GltfUnlitMaterial.TryCreateParam(parser, i, out param)) + if (!GltfUnlitMaterialImporter.TryCreateParam(parser, i, out param)) { // pbr - if (!GltfPBRMaterial.TryCreateParam(parser, i, out param)) + if (!GltfPbrMaterialImporter.TryCreateParam(parser, i, out param)) { // fallback #if VRM_DEVELOP Debug.LogWarning($"material: {i} out of range. fallback"); #endif - return new MaterialImportParam(GltfMaterialImporter.GetMaterialName(i, null), GltfPBRMaterial.ShaderName); + return new MaterialImportParam(GltfMaterialImporter.GetMaterialName(i, null), GltfPbrMaterialImporter.ShaderName); } } } From 619c3cb57acb9f9173c6a8a234818e0a33fc7799 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 26 May 2021 17:33:59 +0900 Subject: [PATCH 5/6] Add comment --- .../UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs | 1 + .../UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs index be4270182..a458a9283 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialExporter.cs @@ -5,6 +5,7 @@ namespace UniGLTF { /// /// 指定の Unity Material から glTFMaterial を生成する。 + /// glTFMaterial と Unity Material は 1:1 対応する。 /// public interface IMaterialExporter { diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs index 9919e17c0..d4e45d63a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/IMaterialImporter.cs @@ -4,7 +4,7 @@ namespace UniGLTF { /// /// 指定の index の glTFMaterial から Import できる Material の生成情報を生成する。 - /// Material の Import は glTFMaterial と 1:1 対応する。 + /// glTFMaterial と Unity Material は 1:1 対応する。 /// public interface IMaterialImporter { From c02ae21f70befe7523f72c8331d515e8c5d058f0 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 26 May 2021 17:37:55 +0900 Subject: [PATCH 6/6] mv --- .../UniGLTF/IO/{MaterialIO => TextureIO}/GltfTextureEnumerator.cs | 0 .../IO/{MaterialIO => TextureIO}/GltfTextureEnumerator.cs.meta | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialIO => TextureIO}/GltfTextureEnumerator.cs (100%) rename Assets/UniGLTF/Runtime/UniGLTF/IO/{MaterialIO => TextureIO}/GltfTextureEnumerator.cs.meta (100%) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureEnumerator.cs similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureEnumerator.cs diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureEnumerator.cs.meta similarity index 100% rename from Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureEnumerator.cs.meta