From 18fd38182d3cd104e5c14b6a0bad8b53ca6644d2 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Mon, 15 Mar 2021 20:01:20 +0900 Subject: [PATCH] TextureImporterConfigurator --- .../ScriptedImporter/ScriptedImporterImpl.cs | 7 ++ .../ScriptedImporter/TextureExtractor.cs | 30 +------- .../UniGLTF/TextureImporterConfigurator.cs | 74 +++++++++++++++++++ .../TextureImporterConfigurator.cs.meta | 11 +++ .../Runtime/UniGLTF/IO/ImporterContext.cs | 1 + .../UniGLTF/IO/MaterialIO/PBRMaterialItem.cs | 5 +- .../UniGLTF/IO/TextureIO/TextureFactory.cs | 9 ++- Assets/VRM/Editor/Format/VRMImporterMenu.cs | 13 +++- .../Editor/Format/vrmAssetPostprocessor.cs | 5 ++ 9 files changed, 120 insertions(+), 35 deletions(-) create mode 100644 Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs create mode 100644 Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs index 666defaaf..34b1ed741 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterImpl.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using UnityEditor; @@ -35,6 +36,12 @@ namespace UniGLTF externalObjectMap.Where(x => x.Value != null).Select(x => (x.Value.name, x.Value)).Concat( EnumerateTexturesFromUri(externalObjectMap, parser, UnityPath.FromUnityPath(scriptedImporter.assetPath).Parent)))) { + // settings TextureImporters + foreach (var textureInfo in parser.EnumerateTextures()) + { + TextureImporterConfigurator.Configure(textureInfo, loaded.TextureFactory.ExternalMap); + } + loaded.InvertAxis = reverseAxis; loaded.Load(); loaded.ShowMeshes(); diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index a74736131..3aa84ac95 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -129,39 +129,13 @@ namespace UniGLTF EditorApplication.delayCall += () => { + // Wait for the texture assets to be imported + foreach (var kv in extractor.Textures) { var targetPath = kv.Key; var param = kv.Value; - // TextureImporter - var targetTextureImporter = AssetImporter.GetAtPath(targetPath) as TextureImporter; - if (targetTextureImporter != null) - { - switch (param.TextureType) - { - case GetTextureParam.TextureTypes.StandardMap: -#if VRM_DEVELOP - Debug.Log($"{targetPath} => linear"); -#endif - targetTextureImporter.sRGBTexture = false; - targetTextureImporter.SaveAndReimport(); - break; - - case GetTextureParam.TextureTypes.NormalMap: -#if VRM_DEVELOP - Debug.Log($"{targetPath} => normalmap"); -#endif - targetTextureImporter.textureType = TextureImporterType.NormalMap; - targetTextureImporter.SaveAndReimport(); - break; - } - } - else - { - throw new FileNotFoundException(targetPath); - } - // remap var externalObject = AssetDatabase.LoadAssetAtPath(targetPath); if (externalObject != null) diff --git a/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs b/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs new file mode 100644 index 000000000..8dca3330d --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + +namespace UniGLTF +{ + public static class TextureImporterConfigurator + { + public static void ConfigureNormalMap(Texture2D texture) + { + var path = UnityPath.FromAsset(texture); + if (AssetImporter.GetAtPath(path.Value) is TextureImporter textureImporter) + { +#if VRM_DEVELOP + Debug.Log($"{path} => normalmap"); +#endif + textureImporter.textureType = TextureImporterType.NormalMap; + textureImporter.SaveAndReimport(); + } + else + { + throw new System.IO.FileNotFoundException($"{path}"); + } + } + + public static void ConfigureLinear(Texture2D texture) + { + var path = UnityPath.FromAsset(texture); + if (AssetImporter.GetAtPath(path.Value) is TextureImporter textureImporter) + { +#if VRM_DEVELOP + Debug.Log($"{path} => linear"); +#endif + textureImporter.sRGBTexture = false; + textureImporter.SaveAndReimport(); + } + else + { + throw new System.IO.FileNotFoundException($"{path}"); + } + } + + public static void Configure(GetTextureParam textureInfo, IDictionary ExternalMap) + { + switch (textureInfo.TextureType) + { + case GetTextureParam.TextureTypes.NormalMap: + { + if (ExternalMap.TryGetValue(textureInfo.GltflName, out Texture2D external)) + { + ConfigureNormalMap(external); + } + } + break; + + case GetTextureParam.TextureTypes.StandardMap: + { + if (ExternalMap.TryGetValue(textureInfo.ConvertedName, out Texture2D external)) + { + ConfigureLinear(external); + } + } + break; + + case GetTextureParam.TextureTypes.sRGB: + break; + + default: + throw new NotImplementedException(); + } + } + } +} diff --git a/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs.meta b/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs.meta new file mode 100644 index 000000000..b14b582e7 --- /dev/null +++ b/Assets/UniGLTF/Editor/UniGLTF/TextureImporterConfigurator.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 193b7c6393807a04f8aafabc23478f8e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 8e05108df..503d77127 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -56,6 +56,7 @@ namespace UniGLTF }; #endif } + m_textureFactory = new TextureFactory(loadTextureAsync, externalObjectMap); m_materialFactory = new MaterialFactory(GLTF, Storage, externalObjectMap); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs index 38968b4e6..55de4ddee 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MaterialIO/PBRMaterialItem.cs @@ -83,9 +83,10 @@ namespace UniGLTF var src = gltf.materials[i]; var material = MaterialFactory.CreateMaterial(i, src, ShaderName); - var standardParam = StandardTexture(gltf, src); - if (src.pbrMetallicRoughness != null) + var standardParam = default(GetTextureParam); + if (src.pbrMetallicRoughness != null || src.occlusionTexture != null) { + standardParam = StandardTexture(gltf, src); if (src.pbrMetallicRoughness.baseColorFactor != null && src.pbrMetallicRoughness.baseColorFactor.Length == 4) { var color = src.pbrMetallicRoughness.baseColorFactor; diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs index af060fa95..70872c48c 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/TextureFactory.cs @@ -46,10 +46,11 @@ namespace UniGLTF public delegate Task GetTextureAsyncFunc(IAwaitCaller awaitCaller, glTF gltf, GetTextureParam param); public class TextureFactory : IDisposable { - Dictionary m_externalMap; + public readonly Dictionary ExternalMap; + public bool TryGetExternal(GetTextureParam param, bool used, out Texture2D external) { - if (param.Index0.HasValue && m_externalMap != null) + if (param.Index0.HasValue && ExternalMap != null) { var cacheName = param.ConvertedName; if (param.TextureType == GetTextureParam.TextureTypes.NormalMap) @@ -61,7 +62,7 @@ namespace UniGLTF return true; } } - if (m_externalMap.TryGetValue(cacheName, out external)) + if (ExternalMap.TryGetValue(cacheName, out external)) { m_textureCache.Add(cacheName, new TextureLoadInfo(external, used, true)); return true; @@ -79,7 +80,7 @@ namespace UniGLTF LoadTextureAsync = loadTextureAsync; if (externalMap != null) { - m_externalMap = externalMap + ExternalMap = externalMap .Select(kv => (kv.Item1, kv.Item2 as Texture2D)) .Where(kv => kv.Item2 != null) .ToDictionary(kv => kv.Item1, kv => kv.Item2); diff --git a/Assets/VRM/Editor/Format/VRMImporterMenu.cs b/Assets/VRM/Editor/Format/VRMImporterMenu.cs index 61be120de..9090698c4 100644 --- a/Assets/VRM/Editor/Format/VRMImporterMenu.cs +++ b/Assets/VRM/Editor/Format/VRMImporterMenu.cs @@ -4,6 +4,7 @@ using UnityEngine; using UniGLTF; using System; using System.Collections.Generic; +using System.Linq; namespace VRM { @@ -58,14 +59,24 @@ namespace VRM var parser = new GltfParser(); parser.ParseGlb(File.ReadAllBytes(path)); - Action> onCompleted = _ => + Action> onCompleted = texturePaths => { // // after textures imported // + var map = texturePaths.Select(x => + { + var texture = AssetDatabase.LoadAssetAtPath(x, typeof(Texture2D)); + return (texture.name, texture); + }).ToArray(); + using (var context = new VRMImporterContext(parser)) { var editor = new VRMEditorImporterContext(context, prefabPath); + foreach (var textureInfo in parser.EnumerateTextures()) + { + TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D)); + } context.Load(); editor.SaveAsAsset(); } diff --git a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs index 630bffa37..264bf69a6 100644 --- a/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs +++ b/Assets/VRM/Editor/Format/vrmAssetPostprocessor.cs @@ -49,9 +49,14 @@ namespace VRM var texture = AssetDatabase.LoadAssetAtPath(x, typeof(Texture2D)); return (texture.name, texture); }).ToArray(); + using (var context = new VRMImporterContext(parser, null, map)) { var editor = new VRMEditorImporterContext(context, prefabPath); + foreach (var textureInfo in parser.EnumerateTextures()) + { + TextureImporterConfigurator.Configure(textureInfo, map.ToDictionary(x => x.name, x => x.texture as Texture2D)); + } context.Load(); editor.SaveAsAsset(); }