diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs index 294b875a7..0ad9a47bc 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs @@ -51,7 +51,7 @@ namespace UniGLTF s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures"); if (s_foldTextures) { - DrawRemapGUI(importer, parser.EnumerateTextures().Select(x => x.ConvertedName)); + DrawRemapGUI(importer, GltfTextureEnumerator.Enumerate(parser.GLTF).Select(x => x.ConvertedName)); } if (GUILayout.Button("Clear")) @@ -115,6 +115,7 @@ namespace UniGLTF }; TextureExtractor.ExtractTextures(self.assetPath, + GltfTextureEnumerator.Enumerate, self.GetSubAssets(self.assetPath).ToArray(), addRemap, onCompleted diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs index 0a93dff14..a2dba9f0c 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -60,7 +60,7 @@ namespace UniGLTF public static IEnumerable<(string, UnityEngine.Object)> EnumerateTexturesFromUri(Dictionary exclude, GltfParser parser, UnityPath dir) { - foreach (var texParam in parser.EnumerateTextures()) + foreach (var texParam in GltfTextureEnumerator.Enumerate(parser.GLTF)) { switch (texParam.TextureType) { diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index 64db7ab81..dd14e8894 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -116,18 +116,16 @@ namespace UniGLTF /// /// /// - public static void ExtractTextures(string assetPath, Texture2D[] subAssets, Action addRemap, Action> onCompleted = null) + public static void ExtractTextures(string assetPath, TextureEnumerator textureEnumerator, Texture2D[] subAssets, Action addRemap, Action> onCompleted = null) { var extractor = new TextureExtractor(assetPath, subAssets); var normalMaps = new List(); - foreach (var material in extractor.GLTF.materials) + + foreach (var x in textureEnumerator(extractor.GLTF)) { - foreach (var x in extractor.Parser.EnumerateTextures(material)) - { - var gltfTexture = extractor.GLTF.textures[x.Index0.Value]; - var gltfImage = extractor.GLTF.images[gltfTexture.source]; - extractor.Extract(x, !string.IsNullOrEmpty(gltfImage.uri)); - } + var gltfTexture = extractor.GLTF.textures[x.Index0.Value]; + var gltfImage = extractor.GLTF.images[gltfTexture.source]; + extractor.Extract(x, !string.IsNullOrEmpty(gltfImage.uri)); } EditorApplication.delayCall += () => diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs index eeacff864..49a134bf6 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs @@ -334,7 +334,6 @@ namespace UniGLTF } #endregion - public static void AppendImageExtension(glTFImage texture, string extension) { if (!texture.name.EndsWith(extension)) @@ -342,57 +341,5 @@ namespace UniGLTF texture.name = texture.name + extension; } } - - public IEnumerable EnumerateTextures(glTFMaterial m) - { - if (m.pbrMetallicRoughness != null) - { - // base color - if (m.pbrMetallicRoughness?.baseColorTexture != null) - { - yield return PBRMaterialItem.BaseColorTexture(GLTF, m); - } - - // metallic roughness - if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null) - { - yield return PBRMaterialItem.MetallicRoughnessTexture(GLTF, m); - } - } - - // emission - if (m.emissiveTexture != null) - { - yield return GetTextureParam.Create(GLTF, m.emissiveTexture.index); - } - - // normal - if (m.normalTexture != null) - { - yield return PBRMaterialItem.NormalTexture(GLTF, m); - } - - // occlusion - if (m.occlusionTexture != null) - { - yield return PBRMaterialItem.OcclusionTexture(GLTF, m); - } - } - - public IEnumerable EnumerateTextures() - { - var used = new HashSet(); - for (int i = 0; i < GLTF.materials.Count; ++i) - { - var m = GLTF.materials[i]; - foreach (var x in EnumerateTextures(m)) - { - if (used.Add(x.ConvertedName)) - { - yield return x; - } - } - } - } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/GltfTextureEnumerator.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/GltfTextureEnumerator.cs new file mode 100644 index 000000000..b2550d9d4 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/GltfTextureEnumerator.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; + +namespace UniGLTF +{ + public delegate IEnumerable TextureEnumerator(glTF gltf); + + public static class GltfTextureEnumerator + { + public static IEnumerable EnumerateTextures(glTF gltf, glTFMaterial m) + { + if (m.pbrMetallicRoughness != null) + { + // base color + if (m.pbrMetallicRoughness?.baseColorTexture != null) + { + yield return PBRMaterialItem.BaseColorTexture(gltf, m); + } + + // metallic roughness + if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null) + { + yield return PBRMaterialItem.MetallicRoughnessTexture(gltf, m); + } + } + + // emission + if (m.emissiveTexture != null) + { + yield return GetTextureParam.Create(gltf, m.emissiveTexture.index); + } + + // normal + if (m.normalTexture != null) + { + yield return PBRMaterialItem.NormalTexture(gltf, m); + } + + // occlusion + if (m.occlusionTexture != null) + { + yield return PBRMaterialItem.OcclusionTexture(gltf, m); + } + } + + public static IEnumerable Enumerate(glTF gltf) + { + foreach (var material in gltf.materials) + { + foreach (var textureInfo in EnumerateTextures(gltf, material)) + { + yield return textureInfo; + } + } + } + } +} diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/GltfTextureEnumerator.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/GltfTextureEnumerator.cs.meta new file mode 100644 index 000000000..09459eb10 --- /dev/null +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/GltfTextureEnumerator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 205a551be16aede40baa76e1f36e42a2 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs b/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs index 3b0ebcf60..bd8be0424 100644 --- a/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs +++ b/Assets/VRM/Editor/Format/VRMEditorImporterContext.cs @@ -152,7 +152,8 @@ namespace VRM .ToArray(); var prefabParentDir = assetPath.Parent; var folder = assetPath.GetAssetFolder(".Textures"); - TextureExtractor.ExtractTextures(assetPath.Value, subAssets, _ => { }, onTextureReloaded); + var vrmTextures = new VRMTextureEnumerator(m_context.VRM); + TextureExtractor.ExtractTextures(assetPath.Value, vrmTextures.Enumerate, subAssets, _ => { }, onTextureReloaded); } bool SaveAsAsset(UnityEngine.Object o) diff --git a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs new file mode 100644 index 000000000..1a6cc2117 --- /dev/null +++ b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs @@ -0,0 +1,45 @@ +using System.Collections.Generic; +using UniGLTF; + +namespace VRM +{ + public class VRMTextureEnumerator + { + readonly glTF_VRM_extensions m_vrm; + public VRMTextureEnumerator(glTF_VRM_extensions vrm) + { + m_vrm = vrm; + } + + public IEnumerable Enumerate(glTF gltf) + { + for (int i = 0; i < gltf.materials.Count; ++i) + { + var vrmMaterial = m_vrm.materialProperties[i]; + if (vrmMaterial.shader == MToon.Utils.ShaderName) + { + // MToon + foreach (var kv in vrmMaterial.textureProperties) + { + // SRGB color or normalmap + yield return GetTextureParam.Create(gltf, kv.Value, kv.Key); + } + } + else + { + // PBR or Unlit + foreach (var textureInfo in GltfTextureEnumerator.EnumerateTextures(gltf, gltf.materials[i])) + { + yield return textureInfo; + } + } + } + + // thumbnail + if (m_vrm.meta != null && m_vrm.meta.texture != -1) + { + yield return GetTextureParam.Create(gltf, m_vrm.meta.texture); + } + } + } +} diff --git a/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs.meta b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs.meta new file mode 100644 index 000000000..70ddd38c5 --- /dev/null +++ b/Assets/VRM/Runtime/IO/VRMTextureEnumerator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d9b840dcce2a5b94aae00d4bc4bf7e10 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: