diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs index 6f0d0114b..22ab1a442 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs @@ -34,14 +34,14 @@ namespace UniGLTF static bool s_foldMaterials = true; static bool s_foldTextures = true; - public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser) + public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser, EnumerateAllTexturesDistinctFunc enumTextures) { var canExtract = !importer.GetExternalObjectMap().Any(x => x.Value is Material || x.Value is Texture2D); using (new TmpGuiEnable(canExtract)) { if (GUILayout.Button("Extract Materials And Textures ...")) { - ExtractMaterialsAndTextures(importer, parser); + ExtractMaterialsAndTextures(importer, parser, enumTextures); } } @@ -57,7 +57,7 @@ namespace UniGLTF s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures"); if (s_foldTextures) { - var names = GltfTextureEnumerator.Enumerate(parser) + var names = enumTextures(parser) .Select(x => { if (x.TextureType != TextureImportTypes.StandardMap && !string.IsNullOrEmpty(x.Uri)) @@ -122,7 +122,7 @@ namespace UniGLTF AssetDatabase.ImportAsset(self.assetPath, ImportAssetOptions.ForceUpdate); } - static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser) + static void ExtractMaterialsAndTextures(ScriptedImporter self, GltfParser parser, EnumerateAllTexturesDistinctFunc enumTextures) { if (string.IsNullOrEmpty(self.assetPath)) { @@ -143,7 +143,7 @@ namespace UniGLTF var assetPath = UnityPath.FromFullpath(parser.TargetPath); var dirName = $"{assetPath.FileNameWithoutExtension}.Textures"; TextureExtractor.ExtractTextures(parser, assetPath.Parent.Child(dirName), - GltfTextureEnumerator.Enumerate, + enumTextures, self.GetSubAssets(self.assetPath).ToArray(), addRemap, onCompleted diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs index f80403f2e..de7544c95 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs @@ -48,7 +48,7 @@ namespace UniGLTF break; case Tabs.Materials: - EditorMaterial.OnGUIMaterial(m_importer, m_parser); + EditorMaterial.OnGUIMaterial(m_importer, m_parser, GltfTextureEnumerator.EnumerateAllTexturesDistinct); break; } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs index 0afc418c8..dc9e56cd5 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs @@ -48,7 +48,7 @@ namespace UniGLTF break; case Tabs.Materials: - EditorMaterial.OnGUIMaterial(m_importer, m_parser); + EditorMaterial.OnGUIMaterial(m_importer, m_parser, GltfTextureEnumerator.EnumerateAllTexturesDistinct); break; } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs index d3ed16e3a..e6018af21 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -42,7 +42,7 @@ namespace UniGLTF using (var loader = new ImporterContext(parser, externalObjectMap.Concat(externalTextures))) { // settings TextureImporters - foreach (var textureInfo in GltfTextureEnumerator.Enumerate(parser)) + foreach (var textureInfo in GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser)) { TextureImporterConfigurator.Configure(textureInfo, loader.TextureFactory.ExternalMap); } @@ -69,7 +69,7 @@ namespace UniGLTF GltfParser parser, UnityPath dir) { var used = new HashSet(); - foreach (var texParam in GltfTextureEnumerator.Enumerate(parser)) + foreach (var texParam in GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser)) { switch (texParam.TextureType) { diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index ebbf35db8..44ed3f36c 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -102,7 +102,7 @@ namespace UniGLTF /// /// public static void ExtractTextures(GltfParser parser, UnityPath textureDirectory, - TextureEnumerator textureEnumerator, Texture2D[] subAssets, Action addRemap, + EnumerateAllTexturesDistinctFunc textureEnumerator, Texture2D[] subAssets, Action addRemap, Action> onCompleted = null) { var extractor = new TextureExtractor(parser, textureDirectory, subAssets); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs index 3fb2e059c..52511e709 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/GltfTextureEnumerator.cs @@ -4,7 +4,7 @@ using VRMShaders; namespace UniGLTF { - public delegate IEnumerable TextureEnumerator(GltfParser parser); + public delegate IEnumerable EnumerateAllTexturesDistinctFunc(GltfParser parser); /// /// Texture 生成に関して @@ -34,8 +34,10 @@ namespace UniGLTF /// public static class GltfTextureEnumerator { - public static IEnumerable EnumerateTextures(GltfParser parser, glTFMaterial m) + public static IEnumerable EnumerateTexturesForMaterial(GltfParser parser, int i) { + var m = parser.GLTF.materials[i]; + int? metallicRoughnessTexture = default; if (m.pbrMetallicRoughness != null) { @@ -79,14 +81,20 @@ namespace UniGLTF } } - public static IEnumerable Enumerate(GltfParser parser) + /// + /// glTF 全体で使うテクスチャーをユニークになるように列挙する + /// + /// + /// + public static IEnumerable EnumerateAllTexturesDistinct(GltfParser parser) { var used = new HashSet(); - foreach (var material in parser.GLTF.materials) + for (int i = 0; i < parser.GLTF.materials.Count; ++i) { - foreach (var textureInfo in EnumerateTextures(parser, material)) + foreach (var textureInfo in EnumerateTexturesForMaterial(parser, i)) { - if(used.Add(textureInfo.ExtractKey)){ + if (used.Add(textureInfo.ExtractKey)) + { yield return textureInfo; } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs index c23c4cd93..cc70367ba 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs @@ -13,7 +13,7 @@ namespace UniGLTF /// public static class GltfTextureImporter { - static Byte[] ToArray(ArraySegment bytes) + public static Byte[] ToArray(ArraySegment bytes) { if (bytes.Array == null) { diff --git a/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs b/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs index e0f42e241..7ee3382d7 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/GltfLoadTests.cs @@ -99,7 +99,7 @@ namespace UniGLTF } // should unique - var gltfTextures = GltfTextureEnumerator.Enumerate(parser).ToArray(); + var gltfTextures = GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser).ToArray(); var distinct = gltfTextures.Distinct().ToArray(); Assert.True(gltfTextures.SequenceEqual(distinct)); } diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureEnumerateTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureEnumerateTests.cs index 403638bb5..938a78118 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureEnumerateTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureEnumerateTests.cs @@ -207,7 +207,7 @@ namespace UniGLTF { GLTF = TwoTexture(), }; - var items = GltfTextureEnumerator.Enumerate(parser).ToArray(); + var items = GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser).ToArray(); Assert.AreEqual(2, items.Length); } @@ -216,7 +216,7 @@ namespace UniGLTF { GLTF = TwoTextureOneUri(), }; - var items = GltfTextureEnumerator.Enumerate(parser).ToArray(); + var items = GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser).ToArray(); Assert.AreEqual(1, items.Length); } @@ -225,7 +225,7 @@ namespace UniGLTF { GLTF = TwoTextureOneImage(), }; - var items = GltfTextureEnumerator.Enumerate(parser).ToArray(); + var items = GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser).ToArray(); Assert.AreEqual(1, items.Length); } @@ -234,7 +234,7 @@ namespace UniGLTF { GLTF = CombineMetallicSmoothOcclusion(), }; - var items = GltfTextureEnumerator.Enumerate(parser).ToArray(); + var items = GltfTextureEnumerator.EnumerateAllTexturesDistinct(parser).ToArray(); Assert.AreEqual(1, items.Length); } } diff --git a/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs b/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs index 4ef829449..66d316a1b 100644 --- a/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs +++ b/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs @@ -149,9 +149,9 @@ namespace VRM .Where(x => x.IsUsed) .Select(x => x.Texture) .ToArray(); - var vrmTextures = new VRMTextureEnumerator(m_context.VRM); + var vrmTextures = new VRMMtoonMaterialImporter(m_context.VRM); var dirName = $"{m_prefabPath.FileNameWithoutExtension}.Textures"; - TextureExtractor.ExtractTextures(m_context.Parser, m_prefabPath.Parent.Child(dirName), vrmTextures.Enumerate, subAssets, _ => { }, onTextureReloaded); + TextureExtractor.ExtractTextures(m_context.Parser, m_prefabPath.Parent.Child(dirName), vrmTextures.EnumerateAllTexturesDistinct, subAssets, _ => { }, onTextureReloaded); } bool SaveAsAsset(UnityEngine.Object o) diff --git a/Assets/VRM/Editor/Format/VRMImporterMenu.cs b/Assets/VRM/Editor/Format/VRMImporterMenu.cs index 97d26f9f6..d45358480 100644 --- a/Assets/VRM/Editor/Format/VRMImporterMenu.cs +++ b/Assets/VRM/Editor/Format/VRMImporterMenu.cs @@ -83,7 +83,7 @@ namespace VRM using (var context = new VRMImporterContext(parser, map)) { var editor = new VRMEditorImporterContext(context, prefabPath); - foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser)) + foreach (var textureInfo in new VRMMtoonMaterialImporter(context.VRM).EnumerateAllTexturesDistinct(parser)) { TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D)); } diff --git a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs index 218057dd6..cbc19a7ce 100644 --- a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs +++ b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs @@ -60,7 +60,7 @@ namespace VRM using (var context = new VRMImporterContext(parser, map)) { var editor = new VRMEditorImporterContext(context, prefabPath); - foreach (var textureInfo in new VRMTextureEnumerator(context.VRM).Enumerate(parser)) + foreach (var textureInfo in new VRMMtoonMaterialImporter(context.VRM).EnumerateAllTexturesDistinct(parser)) { TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D)); } diff --git a/Assets/VRM/Runtime/IO/VRMImporterContext.cs b/Assets/VRM/Runtime/IO/VRMImporterContext.cs index ef9d9c841..58eb5a52d 100644 --- a/Assets/VRM/Runtime/IO/VRMImporterContext.cs +++ b/Assets/VRM/Runtime/IO/VRMImporterContext.cs @@ -26,7 +26,7 @@ namespace VRM { VRM = vrm; // override material importer - GltfMaterialImporter.GltfMaterialParamProcessors.Insert(0, new MToonMaterialImporter(VRM.materialProperties).TryCreateParam); + GltfMaterialImporter.GltfMaterialParamProcessors.Insert(0, new VRMMtoonMaterialImporter(VRM).TryCreateParam); } else { diff --git a/Assets/VRM/Runtime/IO/MToonMaterialImporter.cs b/Assets/VRM/Runtime/IO/VRMMtoonMaterialImporter.cs similarity index 57% rename from Assets/VRM/Runtime/IO/MToonMaterialImporter.cs rename to Assets/VRM/Runtime/IO/VRMMtoonMaterialImporter.cs index ae6bfaa5b..c2ca44185 100644 --- a/Assets/VRM/Runtime/IO/MToonMaterialImporter.cs +++ b/Assets/VRM/Runtime/IO/VRMMtoonMaterialImporter.cs @@ -3,13 +3,19 @@ using UniGLTF; using UnityEngine; using VRMShaders; - namespace VRM { - public class MToonMaterialImporter + public class VRMMtoonMaterialImporter { - public static bool TryCreateParam(GltfParser parser, int i, glTF_VRM_Material vrmMaterial, out MaterialImportParam param) + readonly glTF_VRM_extensions m_vrm; + public VRMMtoonMaterialImporter(glTF_VRM_extensions vrm) { + m_vrm = vrm; + } + + public bool TryCreateParam(GltfParser parser, int i, out MaterialImportParam param) + { + var vrmMaterial = m_vrm.materialProperties[i]; if (vrmMaterial.shader == VRM.glTF_VRM_Material.VRM_USE_GLTFSHADER) { // fallback to gltf @@ -87,21 +93,64 @@ namespace VRM return true; } - List m_materials; - public MToonMaterialImporter(List materials) + public IEnumerable EnumerateTexturesForMaterial(GltfParser parser, int i) { - m_materials = materials; - } - - public bool TryCreateParam(GltfParser parser, int i, out MaterialImportParam param) - { - if (TryCreateParam(parser, i, m_materials[i], out param)) + // mtoon + if (!TryCreateParam(parser, i, out MaterialImportParam param)) { - return true; + // unlit + if (!GltfUnlitMaterial.TryCreateParam(parser, i, out param)) + { + // pbr + GltfPBRMaterial.TryCreateParam(parser, i, out param); + } } - param = default; - return false; + foreach (var kv in param.TextureSlots) + { + yield return kv.Value; + } + } + + public IEnumerable EnumerateAllTexturesDistinct(GltfParser parser) + { + var used = new HashSet(); + for (int i = 0; i < parser.GLTF.materials.Count; ++i) + { + var vrmMaterial = m_vrm.materialProperties[i]; + if (vrmMaterial.shader == MToon.Utils.ShaderName) + { + // MToon + foreach (var textureInfo in EnumerateTexturesForMaterial(parser, i)) + { + if (used.Add(textureInfo.ExtractKey)) + { + yield return textureInfo; + } + } + } + else + { + // PBR or Unlit + foreach (var textureInfo in GltfTextureEnumerator.EnumerateTexturesForMaterial(parser, i)) + { + if (used.Add(textureInfo.ExtractKey)) + { + yield return textureInfo; + } + } + } + } + + // thumbnail + if (m_vrm.meta != null && m_vrm.meta.texture != -1) + { + var textureInfo = GltfTextureImporter.CreateSRGB(parser, m_vrm.meta.texture, Vector2.zero, Vector2.one); + if (used.Add(textureInfo.ExtractKey)) + { + yield return textureInfo; + } + } } } } diff --git a/Assets/VRM/Runtime/IO/MToonMaterialImporter.cs.meta b/Assets/VRM/Runtime/IO/VRMMtoonMaterialImporter.cs.meta similarity index 83% rename from Assets/VRM/Runtime/IO/MToonMaterialImporter.cs.meta rename to Assets/VRM/Runtime/IO/VRMMtoonMaterialImporter.cs.meta index cfac19cb9..ea615db5e 100644 --- a/Assets/VRM/Runtime/IO/MToonMaterialImporter.cs.meta +++ b/Assets/VRM/Runtime/IO/VRMMtoonMaterialImporter.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 35c90d5d3fa706b4f87a92ce4dc59008 +guid: bad75b40d017eb74ba79f561d22dc372 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs deleted file mode 100644 index 20ed7f39d..000000000 --- a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs +++ /dev/null @@ -1,83 +0,0 @@ -using System.Collections.Generic; -using UniGLTF; -using UnityEngine; -using VRMShaders; - -namespace VRM -{ - public class VRMTextureEnumerator - { - readonly glTF_VRM_extensions m_vrm; - public VRMTextureEnumerator(glTF_VRM_extensions vrm) - { - m_vrm = vrm; - } - - public IEnumerable EnumerateMaterial(GltfParser parser, glTF_VRM_Material vrmMaterial) - { - // MToon - var offsetScaleMap = new Dictionary(); - foreach (var kv in vrmMaterial.vectorProperties) - { - if (vrmMaterial.textureProperties.ContainsKey(kv.Key)) - { - // texture offset & scale - offsetScaleMap.Add(kv.Key, kv.Value); - } - } - foreach (var kv in vrmMaterial.textureProperties) - { - var (offset, scale) = (Vector2.zero, Vector2.one); - if (offsetScaleMap.TryGetValue(kv.Key, out float[] value)) - { - offset = new Vector2(value[0], value[1]); - scale = new Vector2(value[2], value[3]); - } - - // SRGB color or normalmap - yield return MToonTextureParam.Create(parser, kv.Value, offset, scale, kv.Key, default, default); - } - } - - public IEnumerable Enumerate(GltfParser parser) - { - var used = new HashSet(); - for (int i = 0; i < parser.GLTF.materials.Count; ++i) - { - var vrmMaterial = m_vrm.materialProperties[i]; - if (vrmMaterial.shader == MToon.Utils.ShaderName) - { - // MToon - foreach(var textureInfo in EnumerateMaterial(parser, vrmMaterial)) - { - if (used.Add(textureInfo.ExtractKey)) - { - yield return textureInfo; - } - } - } - else - { - // PBR or Unlit - foreach (var textureInfo in GltfTextureEnumerator.EnumerateTextures(parser, parser.GLTF.materials[i])) - { - if (used.Add(textureInfo.ExtractKey)) - { - yield return textureInfo; - } - } - } - } - - // thumbnail - if (m_vrm.meta != null && m_vrm.meta.texture != -1) - { - var textureInfo = GltfTextureImporter.CreateSRGB(parser, m_vrm.meta.texture, Vector2.zero, Vector2.one); - if (used.Add(textureInfo.ExtractKey)) - { - yield return textureInfo; - } - } - } - } -} diff --git a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs.meta b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs.meta deleted file mode 100644 index 70ddd38c5..000000000 --- a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: d9b840dcce2a5b94aae00d4bc4bf7e10 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRM/Tests/MToonTest.cs b/Assets/VRM/Tests/MToonTest.cs index 551b29e5b..b8326d7e0 100644 --- a/Assets/VRM/Tests/MToonTest.cs +++ b/Assets/VRM/Tests/MToonTest.cs @@ -24,12 +24,15 @@ namespace VRM srcMaterial.mainTexture = tex0; srcMaterial.mainTextureOffset = offset; srcMaterial.mainTextureScale = scale; - + var materialExporter = new VRMMaterialExporter(); var vrmMaterial = VRMMaterialExporter.CreateFromMaterial(srcMaterial, textureManager); - Assert.AreEqual(vrmMaterial.vectorProperties["_MainTex"], new float[]{0.3f, 0.2f, 0.5f, 0.6f}); - - var materialImporter = new MToonMaterialImporter(new System.Collections.Generic.List{ vrmMaterial }); + Assert.AreEqual(vrmMaterial.vectorProperties["_MainTex"], new float[] { 0.3f, 0.2f, 0.5f, 0.6f }); + + var materialImporter = new VRMMtoonMaterialImporter(new glTF_VRM_extensions + { + materialProperties = new System.Collections.Generic.List { vrmMaterial } + }); } } } diff --git a/Assets/VRM/Tests/VRMTextureEnumerateTests.cs b/Assets/VRM/Tests/VRMTextureEnumerateTests.cs index 5964a528c..478466fba 100644 --- a/Assets/VRM/Tests/VRMTextureEnumerateTests.cs +++ b/Assets/VRM/Tests/VRMTextureEnumerateTests.cs @@ -71,7 +71,7 @@ namespace VRM }, } }; - var items = new VRMTextureEnumerator(vrm).Enumerate(parser).ToArray(); + var items = new VRMMtoonMaterialImporter(vrm).EnumerateAllTexturesDistinct(parser).ToArray(); Assert.AreEqual(1, items.Length); } } diff --git a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs index a7d07ec66..5cbf1b99e 100644 --- a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs +++ b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs @@ -48,7 +48,7 @@ namespace UniVRM10 break; case Tabs.Materials: - EditorMaterial.OnGUIMaterial(m_importer, m_parser); + EditorMaterial.OnGUIMaterial(m_importer, m_parser, Vrm10MToonMaterialImporter.EnumerateAllTexturesDistinct); break; case Tabs.Vrm: diff --git a/Assets/VRM10/Editor/VRM10.Editor.asmdef b/Assets/VRM10/Editor/VRM10.Editor.asmdef index 9d1baa8c8..ad11988fe 100644 --- a/Assets/VRM10/Editor/VRM10.Editor.asmdef +++ b/Assets/VRM10/Editor/VRM10.Editor.asmdef @@ -6,7 +6,8 @@ "MeshUtility", "MeshUtility.Editor", "UniGLTF.Editor", - "UniGLTF" + "UniGLTF", + "VRMShaders" ], "optionalUnityReferences": [], "includePlatforms": [ diff --git a/Assets/VRM10/Runtime/IO/RuntimeUnityBuilder.cs b/Assets/VRM10/Runtime/IO/RuntimeUnityBuilder.cs index 36e69eab3..26b236441 100644 --- a/Assets/VRM10/Runtime/IO/RuntimeUnityBuilder.cs +++ b/Assets/VRM10/Runtime/IO/RuntimeUnityBuilder.cs @@ -29,7 +29,7 @@ namespace UniVRM10 m_model = VrmLoader.CreateVrmModel(parser); // for `VRMC_materials_mtoon` - // this.GltfMaterialImporter.GltfMaterialParamProcessors.Insert(0, VrmMToonMaterialImporter.TryCreateParam); + this.GltfMaterialImporter.GltfMaterialParamProcessors.Insert(0, Vrm10MToonMaterialImporter.TryCreateParam); } /// diff --git a/Assets/VRM10/Runtime/IO/Vrm10MToonMaterialImporter.cs b/Assets/VRM10/Runtime/IO/Vrm10MToonMaterialImporter.cs index b5055fbd3..d8a32713e 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10MToonMaterialImporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10MToonMaterialImporter.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Threading.Tasks; using UniGLTF; using UnityEngine; using VRMShaders; @@ -6,11 +7,19 @@ using VRMShaders; namespace UniVRM10 { - public class VrmMToonMaterialImporter + public static class Vrm10MToonMaterialImporter { + /// + /// VMRC_materials_mtoon の場合にマテリアル生成情報を作成する + /// + /// + /// + /// + /// public static bool TryCreateParam(GltfParser parser, int i, out MaterialImportParam param) { - if (!UniGLTF.Extensions.VRMC_materials_mtoon.GltfDeserializer.TryGet(parser.GLTF.materials[i].extensions, + var m = parser.GLTF.materials[i]; + if (!UniGLTF.Extensions.VRMC_materials_mtoon.GltfDeserializer.TryGet(m.extensions, out UniGLTF.Extensions.VRMC_materials_mtoon.VRMC_materials_mtoon mtoon)) { // fallback to gltf @@ -19,12 +28,89 @@ namespace UniVRM10 } // use material.name, because material name may renamed in GltfParser. - var name = parser.GLTF.materials[i].name; + var name = m.name; param = new MaterialImportParam(name, MToon.Utils.ShaderName); - // + if (m.pbrMetallicRoughness != null) + { + // base color + if (m.pbrMetallicRoughness?.baseColorTexture != null) + { + param.TextureSlots.Add("_MainTex", GltfPBRMaterial.BaseColorTexture(parser, m)); + } + } + + // TODO: return true; } + + /// + /// Material一つ分のテクスチャーを列挙する。重複する場合がある + /// + /// + /// + /// + public static IEnumerable EnumerateTexturesForMaterial(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); + } + } + + foreach (var kv in param.TextureSlots) + { + yield return kv.Value; + } + } + + /// + /// glTF 全体で使うテクスチャーをユニークになるように列挙する + /// + /// + /// + public static IEnumerable EnumerateAllTexturesDistinct(GltfParser parser) + { + if (!UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(parser.GLTF.extensions, out UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm)) + { + throw new System.Exception("not vrm"); + } + if (vrm.Meta.ThumbnailImage.HasValue) + { + // thumbnail + var imageIndex = vrm.Meta.ThumbnailImage.Value; + var gltfImage = parser.GLTF.images[imageIndex]; + var name = new TextureImportName(TextureImportTypes.sRGB, gltfImage.name, gltfImage.GetExt(), ""); + + GetTextureBytesAsync getBytesAsync = () => + { + var bytes = parser.GLTF.GetImageBytes(parser.Storage, imageIndex); + return Task.FromResult(GltfTextureImporter.ToArray(bytes)); + }; + yield return new TextureImportParam(name, Vector2.zero, Vector2.one, default, TextureImportTypes.sRGB, default, default, + getBytesAsync, default, default, + default, default, default + ); + } + + + var used = new HashSet(); + for (int i = 0; i < parser.GLTF.materials.Count; ++i) + { + foreach (var textureInfo in EnumerateTexturesForMaterial(parser, i)) + { + if (used.Add(textureInfo.ExtractKey)) + { + yield return textureInfo; + } + } + } + } } }