diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs index 92ebdd232..2b6f98afc 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporter.cs @@ -76,19 +76,19 @@ namespace UniGLTF } } - public void ExtractTextures() - { - // extract textures to files - this.ExtractTextures(TextureDirName); - // reimport - AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); - } + // public void ExtractTextures() + // { + // // extract textures to files + // this.ExtractTextures(TextureDirName); + // // reimport + // AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); + // } - public void ExtractMaterials() - { - this.ExtractAssets(MaterialDirName, ".mat"); - AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); - } + // public void ExtractMaterials() + // { + // this.ExtractAssets(MaterialDirName, ".mat"); + // AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); + // } public void ExtractMaterialsAndTextures() { @@ -99,10 +99,10 @@ namespace UniGLTF }); } - public Dictionary GetExternalUnityObjects() where T : UnityEngine.Object - { - return this.GetExternalObjectMap().Where(x => x.Key.type == typeof(T)).ToDictionary(x => x.Key.name, x => (T)x.Value); - } + // public Dictionary GetExternalUnityObjects() where T : UnityEngine.Object + // { + // return this.GetExternalObjectMap().Where(x => x.Key.type == typeof(T)).ToDictionary(x => x.Key.name, x => (T)x.Value); + // } public void SetExternalUnityObject(UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object { diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs index 20201740b..bf8f474d7 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GltfScriptedImporterEditorGUI.cs @@ -85,10 +85,14 @@ namespace UniGLTF { throw new System.ArgumentNullException(); } + EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel(name); - if (map.TryGetValue(name, out T value)) + map.TryGetValue(name, out T value); + if (map.Any() && value == null) { + var a = 0; + // Debug.Log($"{name}: {value}"); } var asset = EditorGUILayout.ObjectField(value, typeof(T), true) as T; if (asset != value) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs index 883da2162..a4b27162d 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs @@ -96,6 +96,7 @@ namespace UniGLTF class TextureExtractor { GltfParser m_parser; + public GltfParser Parser => m_parser; public glTF GLTF => m_parser.GLTF; @@ -115,59 +116,17 @@ namespace UniGLTF static Regex s_mimeTypeReg = new Regex("image/(?.*)$"); - public void Extract(int? index, string prop = default) + public void Extract(GetTextureParam param) { - if (!index.HasValue) - { - return; - } - - var gltfTexture = GLTF.textures[index.Value]; - var gltfImage = GLTF.images[gltfTexture.source]; - var mimeType = s_mimeTypeReg.Match(gltfImage.mimeType); - var ext = ""; - switch (mimeType.Groups["mime"].Value) - { - case "jpeg": - ext = ".jpg"; - break; - - case "png": - ext = ".png"; - break; - - default: - throw new NotImplementedException(); - - } - - var assetName = gltfImage.name; - string targetPath = default; - switch (prop) - { - case GetTextureParam.NORMAL_PROP: - case GetTextureParam.METALLIC_GLOSS_PROP: - case GetTextureParam.OCCLUSION_PROP: - // File.WriteAllBytes(targetPath, subAsset.EncodeToPNG()); - throw new NotImplementedException(); - - default: - { - var name = ""; - var bytes = GLTF.GetImageBytes(m_parser.Storage, gltfTexture.source, out name); - targetPath = string.Format("{0}/{1}{2}", - m_path, - assetName, - ext - ); - File.WriteAllBytes(targetPath, bytes.ToArray()); - } - break; - } - + var subAsset = m_subAssets.FirstOrDefault(x => x.name == param.Name); + var targetPath = string.Format("{0}/{1}{2}", + m_path, + param.Name, + ".png" + ); + File.WriteAllBytes(targetPath, subAsset.EncodeToPNG().ToArray()); AssetDatabase.ImportAsset(targetPath); - var subAsset = m_subAssets.FirstOrDefault(x => x.name == assetName); Textures.Add(new TextureInfo { Path = targetPath, @@ -195,11 +154,9 @@ namespace UniGLTF foreach (var material in extractor.GLTF.materials) { - // standard or unlit - extractor.Extract(material.pbrMetallicRoughness?.baseColorTexture?.index); - if (!glTF_KHR_materials_unlit.IsEnable(material)) + foreach (var x in extractor.Parser.EnumerateTextures(material)) { - // standard + extractor.Extract(x); } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs index 020337717..514055d59 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfParser.cs @@ -257,6 +257,12 @@ namespace UniGLTF } used.Add(lower); } + + var ext = GetTextureExtension(i); + if (!string.IsNullOrEmpty(ext)) + { + AppendImageExtension(image, ext); + } } } @@ -313,14 +319,117 @@ namespace UniGLTF } #endregion - public IEnumerable EnumerateTextures() + + public static void AppendImageExtension(glTFImage texture, string extension) { - for (int i = 0; i < GLTF.textures.Count; ++i) + if (!texture.name.EndsWith(extension)) { - yield return GetTextureParam.Create(GLTF, i); + texture.name = texture.name + extension; + } + } + + public string GetTextureExtension(int imageIndex) + { + foreach (var m in GLTF.materials) + { + if (m.pbrMetallicRoughness != null) + { + // base color + if (m.pbrMetallicRoughness?.baseColorTexture != null) + { + if (m.pbrMetallicRoughness.baseColorTexture.index == imageIndex) + { + return ""; + } + } + + // metallic roughness + if (m.pbrMetallicRoughness?.metallicRoughnessTexture != null) + { + if (m.pbrMetallicRoughness.metallicRoughnessTexture.index == imageIndex) + { + return ".metallicRoughness"; + } + } + } + + // emission + if (m.emissiveTexture != null) + { + if (m.emissiveTexture.index == imageIndex) + { + return ""; + } + } + + // normal + if (m.normalTexture != null) + { + if (m.normalTexture.index == imageIndex) + { + return ""; + } + } + + // occlusion + if (m.occlusionTexture != null) + { + if (m.occlusionTexture.index == imageIndex) + { + return ".occlusion"; + } + } } - // TODO: converted textures + return ""; + } + + 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() + { + for (int i = 0; i < GLTF.materials.Count; ++i) + { + var m = GLTF.materials[i]; + foreach (var x in EnumerateTextures(m)) + { + yield return x; + } + } } } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs index 82edfd45b..cf3605552 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialLoader/PBRMaterialItem.cs @@ -43,6 +43,28 @@ namespace UniGLTF Transparent } + public static GetTextureParam BaseColorTexture(glTF gltf, glTFMaterial src) + { + return GetTextureParam.Create(gltf, src.pbrMetallicRoughness.baseColorTexture.index); + } + + public static GetTextureParam MetallicRoughnessTexture(glTF gltf, glTFMaterial src) + { + return GetTextureParam.CreateMetallic(gltf, + src.pbrMetallicRoughness.metallicRoughnessTexture.index, + src.pbrMetallicRoughness.metallicFactor); + } + + public static GetTextureParam OcclusionTexture(glTF gltf, glTFMaterial src) + { + return GetTextureParam.CreateOcclusion(gltf, src.occlusionTexture.index); + } + + public static GetTextureParam NormalTexture(glTF gltf, glTFMaterial src) + { + return GetTextureParam.CreateNormal(gltf, src.normalTexture.index); + } + public static async Awaitable CreateAsync(glTF gltf, int i, GetTextureAsyncFunc getTexture) { if (getTexture == null) @@ -66,7 +88,7 @@ namespace UniGLTF if (src.pbrMetallicRoughness.baseColorTexture != null && src.pbrMetallicRoughness.baseColorTexture.index != -1) { - material.mainTexture = await getTexture(gltf, GetTextureParam.Create(gltf, src.pbrMetallicRoughness.baseColorTexture.index)); + material.mainTexture = await getTexture(gltf, BaseColorTexture(gltf, src)); // Texture Offset and Scale MaterialFactory.SetTextureOffsetAndScale(material, src.pbrMetallicRoughness.baseColorTexture, "_MainTex"); @@ -76,9 +98,7 @@ namespace UniGLTF { material.EnableKeyword("_METALLICGLOSSMAP"); - var texture = await getTexture(gltf, GetTextureParam.CreateMetallic(gltf, - src.pbrMetallicRoughness.metallicRoughnessTexture.index, - src.pbrMetallicRoughness.metallicFactor)); + var texture = await getTexture(gltf, MetallicRoughnessTexture(gltf, src)); if (texture != null) { material.SetTexture(GetTextureParam.METALLIC_GLOSS_PROP, texture); @@ -101,7 +121,7 @@ namespace UniGLTF if (src.normalTexture != null && src.normalTexture.index != -1) { material.EnableKeyword("_NORMALMAP"); - var texture = await getTexture(gltf, GetTextureParam.CreateNormal(gltf, src.normalTexture.index)); + var texture = await getTexture(gltf, NormalTexture(gltf, src)); if (texture != null) { material.SetTexture(GetTextureParam.NORMAL_PROP, texture); @@ -114,7 +134,7 @@ namespace UniGLTF if (src.occlusionTexture != null && src.occlusionTexture.index != -1) { - var texture = await getTexture(gltf, GetTextureParam.CreateOcclusion(gltf, src.occlusionTexture.index)); + var texture = await getTexture(gltf, OcclusionTexture(gltf, src)); if (texture != null) { material.SetTexture(GetTextureParam.OCCLUSION_PROP, texture); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter.cs index 3b400d799..52270561b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureConverter.cs @@ -31,22 +31,6 @@ namespace UniGLTF return copyTexture; } - public static void AppendTextureExtension(Texture texture, string extension) - { - if (!texture.name.EndsWith(extension)) - { - texture.name = texture.name + extension; - } - } - - public static void RemoveTextureExtension(Texture texture, string extension) - { - if (texture.name.EndsWith(extension)) - { - texture.name = texture.name.Replace(extension, ""); - } - } - struct ColorSpaceScope : IDisposable { bool m_sRGBWrite; @@ -194,8 +178,6 @@ namespace UniGLTF public class MetallicRoughnessConverter : ITextureConverter { - private const string m_extension = ".metallicRoughness"; - private float _smoothnessOrRoughness; public MetallicRoughnessConverter(float smoothnessOrRoughness) @@ -206,14 +188,12 @@ namespace UniGLTF public Texture2D GetImportTexture(Texture2D texture) { var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Import, null); - TextureConverter.AppendTextureExtension(converted, m_extension); return converted; } public Texture2D GetExportTexture(Texture2D texture) { var converted = TextureConverter.Convert(texture, glTFTextureTypes.Metallic, Export, null); - TextureConverter.RemoveTextureExtension(converted, m_extension); return converted; } @@ -260,8 +240,6 @@ namespace UniGLTF public class NormalConverter : ITextureConverter { - private const string m_extension = ".normal"; - private Material m_decoder; private Material GetDecoder() { @@ -288,7 +266,6 @@ namespace UniGLTF { var mat = GetEncoder(); var converted = TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, mat); - TextureConverter.AppendTextureExtension(converted, m_extension); return converted; } @@ -298,26 +275,22 @@ namespace UniGLTF { var mat = GetDecoder(); var converted = TextureConverter.Convert(texture, glTFTextureTypes.Normal, null, mat); - TextureConverter.RemoveTextureExtension(converted, m_extension); return converted; } } public class OcclusionConverter : ITextureConverter { - private const string m_extension = ".occlusion"; public Texture2D GetImportTexture(Texture2D texture) { var converted = TextureConverter.Convert(texture, glTFTextureTypes.Occlusion, Import, null); - TextureConverter.AppendTextureExtension(converted, m_extension); return converted; } public Texture2D GetExportTexture(Texture2D texture) { var converted = TextureConverter.Convert(texture, glTFTextureTypes.Occlusion, Export, null); - TextureConverter.RemoveTextureExtension(converted, m_extension); return converted; } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs index a4872d261..8d149c143 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureLoader/TextureFactory.cs @@ -148,7 +148,6 @@ namespace UniGLTF { var baseTexture = await GetOrCreateBaseTexture(gltf, param.Index0.Value, false); var converted = new NormalConverter().GetImportTexture(baseTexture); - converted.name = $"{converted.name}.{GetTextureParam.NORMAL_PROP}"; var info = new TextureLoadInfo(converted, true, false); m_textureCache.Add(param, info); return info.Texture; @@ -161,7 +160,6 @@ namespace UniGLTF var textureAssetPath = AssetDatabase.GetAssetPath(info.Texture); TextureIO.MarkTextureAssetAsNormalMap(textureAssetPath); - info.Texture.name = $"{info.Texture.name}.{GetTextureParam.NORMAL_PROP}"; #endif return info.Texture; } @@ -172,7 +170,6 @@ namespace UniGLTF // Bake roughnessFactor values into a texture. var baseTexture = await GetOrCreateBaseTexture(gltf, param.Index0.Value, false); var converted = new MetallicRoughnessConverter(param.MetallicFactor).GetImportTexture(baseTexture); - converted.name = $"{converted.name}.{GetTextureParam.METALLIC_GLOSS_PROP}"; var info = new TextureLoadInfo(converted, true, false); m_textureCache.Add(param, info); return info.Texture; @@ -182,7 +179,6 @@ namespace UniGLTF { var baseTexture = await GetOrCreateBaseTexture(gltf, param.Index0.Value, false); var converted = new OcclusionConverter().GetImportTexture(baseTexture); - converted.name = $"{converted.name}.{GetTextureParam.OCCLUSION_PROP}"; var info = new TextureLoadInfo(converted, true, false); m_textureCache.Add(param, info); return info.Texture;