From b62c123b66e2a8f54bfbb907e7723ddeb9b7649e Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 6 Apr 2021 15:08:37 +0900 Subject: [PATCH] =?UTF-8?q?meta,=20expressionAvatar,=20expression=20?= =?UTF-8?q?=E3=81=AE=20extract=20=E3=82=92=E5=BE=A9=E6=97=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ScriptedImporter/EditorMaterial.cs | 87 ++++--------- .../GlbScriptedImporterEditorGUI.cs | 2 +- .../GltfScriptedImporterEditorGUI.cs | 2 +- .../ScriptedImporterExtension.cs | 55 ++++++++ .../ScriptedImporter/TextureExtractor.cs | 9 -- .../Editor/ScriptedImporter/EditorVrm.cs | 110 ++++++++++++++++ .../Editor/ScriptedImporter/EditorVrm.cs.meta | 11 ++ .../VrmScriptedImporterEditorGUI.cs | 102 ++------------- .../VrmScriptedImporterImpl.cs | 119 ------------------ Assets/VRM10/Editor/VRM10MetaObjectEditor.cs | 5 + .../Components/Expression/ExpressionKey.cs | 13 +- .../Expression/VRM10ExpressionAvatar.cs | 2 +- .../Components/Meta/VRM10MetaObject.cs | 2 +- .../VRM10/Runtime/IO/ExpressionExtensions.cs | 4 +- .../VRM10/Runtime/IO/RuntimeUnityBuilder.cs | 50 ++++++-- 15 files changed, 267 insertions(+), 306 deletions(-) create mode 100644 Assets/VRM10/Editor/ScriptedImporter/EditorVrm.cs create mode 100644 Assets/VRM10/Editor/ScriptedImporter/EditorVrm.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs index 22ab1a442..4de9632ea 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/EditorMaterial.cs @@ -14,30 +14,29 @@ using UnityEditor.Experimental.AssetImporters; namespace UniGLTF { - public static class EditorMaterial + public class TmpGuiEnable : IDisposable { - class TmpGuiEnable : IDisposable + bool m_backup; + public TmpGuiEnable(bool enable) { - bool m_backup; - public TmpGuiEnable(bool enable) - { - m_backup = GUI.enabled; - GUI.enabled = enable; - } - - public void Dispose() - { - GUI.enabled = m_backup; - } + m_backup = GUI.enabled; + GUI.enabled = enable; } + public void Dispose() + { + GUI.enabled = m_backup; + } + } + public static class EditorMaterial + { static bool s_foldMaterials = true; static bool s_foldTextures = true; - public static void OnGUIMaterial(ScriptedImporter importer, GltfParser parser, EnumerateAllTexturesDistinctFunc enumTextures) + public static void OnGUI(ScriptedImporter importer, GltfParser parser, EnumerateAllTexturesDistinctFunc enumTextures) { - var canExtract = !importer.GetExternalObjectMap().Any(x => x.Value is Material || x.Value is Texture2D); - using (new TmpGuiEnable(canExtract)) + var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is Material || x.Value is Texture2D); + using (new TmpGuiEnable(!hasExternal)) { if (GUILayout.Button("Extract Materials And Textures ...")) { @@ -51,7 +50,7 @@ namespace UniGLTF s_foldMaterials = EditorGUILayout.Foldout(s_foldMaterials, "Remapped Materials"); if (s_foldMaterials) { - DrawRemapGUI(importer, parser.GLTF.materials.Select(x => x.name)); + importer.DrawRemapGUI(parser.GLTF.materials.Select(x => x.name)); } s_foldTextures = EditorGUILayout.Foldout(s_foldTextures, "Remapped Textures"); @@ -77,7 +76,7 @@ namespace UniGLTF }) .Where(x => !string.IsNullOrEmpty(x)) ; - DrawRemapGUI(importer, names); + importer.DrawRemapGUI(names); } if (GUILayout.Button("Clear")) @@ -87,34 +86,6 @@ namespace UniGLTF } } - static void DrawRemapGUI(ScriptedImporter importer, IEnumerable names) where T : UnityEngine.Object - { - EditorGUI.indentLevel++; - var map = importer.GetExternalObjectMap() - .Select(x => (x.Key.name, x.Value as T)) - .Where(x => x.Item2 != null) - .ToDictionary(x => x.Item1, x => x.Item2) - ; - foreach (var name in names) - { - if (string.IsNullOrEmpty(name)) - { - throw new System.ArgumentNullException(); - } - - EditorGUILayout.BeginHorizontal(); - EditorGUILayout.PrefixLabel(name); - map.TryGetValue(name, out T value); - var asset = EditorGUILayout.ObjectField(value, typeof(T), true) as T; - if (asset != value) - { - importer.SetExternalUnityObject(new AssetImporter.SourceAssetIdentifier(value), asset); - } - EditorGUILayout.EndHorizontal(); - } - EditorGUI.indentLevel--; - } - public static void SetExternalUnityObject(this ScriptedImporter self, UnityEditor.AssetImporter.SourceAssetIdentifier sourceAssetIdentifier, T obj) where T : UnityEngine.Object { self.AddRemap(sourceAssetIdentifier, obj); @@ -157,28 +128,14 @@ namespace UniGLTF return; } var path = $"{Path.GetDirectoryName(importer.assetPath)}/{Path.GetFileNameWithoutExtension(importer.assetPath)}.Materials"; - var info = TextureExtractor.SafeCreateDirectory(path); + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } foreach (var asset in importer.GetSubAssets(importer.assetPath)) { - ExtractSubAsset(asset, $"{path}/{asset.name}.mat", false); - } - } - - private static void ExtractSubAsset(UnityEngine.Object subAsset, string destinationPath, bool isForceUpdate) - { - string assetPath = AssetDatabase.GetAssetPath(subAsset); - - var clone = UnityEngine.Object.Instantiate(subAsset); - AssetDatabase.CreateAsset(clone, destinationPath); - - var assetImporter = AssetImporter.GetAtPath(assetPath); - assetImporter.AddRemap(new AssetImporter.SourceAssetIdentifier(clone), clone); - - if (isForceUpdate) - { - AssetDatabase.WriteImportSettingsIfDirty(assetPath); - AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); + asset.ExtractSubAsset($"{path}/{asset.name}.mat", false); } } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/GlbScriptedImporterEditorGUI.cs index de7544c95..08f38013c 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, GltfTextureEnumerator.EnumerateAllTexturesDistinct); + EditorMaterial.OnGUI(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 dc9e56cd5..8e90514ab 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, GltfTextureEnumerator.EnumerateAllTexturesDistinct); + EditorMaterial.OnGUI(m_importer, m_parser, GltfTextureEnumerator.EnumerateAllTexturesDistinct); break; } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs index 4d64dbc9f..5a34d56e9 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/ScriptedImporterExtension.cs @@ -31,5 +31,60 @@ namespace UniGLTF .Where(x => x is T) .Select(x => x as T); } + + public static void DrawRemapGUI(this ScriptedImporter importer, IEnumerable names) where T : UnityEngine.Object + { + EditorGUI.indentLevel++; + { + var map = importer.GetExternalObjectMap() + .Select(x => (x.Key.name, x.Value as T)) + .Where(x => x.Item2 != null) + .ToDictionary(x => x.Item1, x => x.Item2) + ; + foreach (var name in names) + { + if (string.IsNullOrEmpty(name)) + { + throw new System.ArgumentNullException(); + } + + EditorGUILayout.BeginHorizontal(); + EditorGUILayout.PrefixLabel(name); + map.TryGetValue(name, out T value); + var asset = EditorGUILayout.ObjectField(value, typeof(T), true) as T; + if (asset != value) + { + importer.SetExternalUnityObject(new AssetImporter.SourceAssetIdentifier(value), asset); + } + EditorGUILayout.EndHorizontal(); + } + } + EditorGUI.indentLevel--; + } + + /// + /// subAsset を 指定された path に extract する + /// + /// + /// + /// + public static void ExtractSubAsset(this UnityEngine.Object subAsset, string destinationPath, bool isForceUpdate) + { + string assetPath = AssetDatabase.GetAssetPath(subAsset); + + // clone を path に出力 + var clone = UnityEngine.Object.Instantiate(subAsset); + AssetDatabase.CreateAsset(clone, destinationPath); + + // subAsset を clone に対して remap する + var assetImporter = AssetImporter.GetAtPath(assetPath); + assetImporter.AddRemap(new AssetImporter.SourceAssetIdentifier(clone), clone); + + if (isForceUpdate) + { + AssetDatabase.WriteImportSettingsIfDirty(assetPath); + AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); + } + } } } diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index 44ed3f36c..2fff2301a 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -82,15 +82,6 @@ namespace UniGLTF Textures.Add(targetPath, param); } - public static DirectoryInfo SafeCreateDirectory(string path) - { - if (Directory.Exists(path)) - { - return null; - } - return Directory.CreateDirectory(path); - } - /// /// /// * Texture(.png etc...)をディスクに書き出す diff --git a/Assets/VRM10/Editor/ScriptedImporter/EditorVrm.cs b/Assets/VRM10/Editor/ScriptedImporter/EditorVrm.cs new file mode 100644 index 000000000..f1aff215e --- /dev/null +++ b/Assets/VRM10/Editor/ScriptedImporter/EditorVrm.cs @@ -0,0 +1,110 @@ +using UniGLTF; +using UnityEngine; +using System.Linq; +using System.IO; +using UnityEditor; +using VRMShaders; +#if UNITY_2020_2_OR_NEWER +using UnityEditor.AssetImporters; +#else +using UnityEditor.Experimental.AssetImporters; +#endif + + +namespace UniVRM10 +{ + public static class EditorVrm + { + static ExpressionKey CreateKey(UniGLTF.Extensions.VRMC_vrm.Expression expression) + { + if (expression.Preset == UniGLTF.Extensions.VRMC_vrm.ExpressionPreset.custom) + { + return ExpressionKey.CreateCustom(expression.Name); + } + else + { + return ExpressionKey.CreateFromPreset(expression.Preset); + } + } + + public static void OnGUI(ScriptedImporter importer, GltfParser parser, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm) + { + var hasExternal = importer.GetExternalObjectMap().Any(x => x.Value is VRM10MetaObject || x.Value is VRM10ExpressionAvatar || x.Value is VRM10Expression); + using (new TmpGuiEnable(!hasExternal)) + { + if (GUILayout.Button("Extract Meta And Expressions ...")) + { + Extract(importer, parser); + } + } + + // meta + importer.DrawRemapGUI(new string[] { VRM10MetaObject.ExtractKey }); + + // expression avatar + importer.DrawRemapGUI(new string[] { VRM10ExpressionAvatar.ExtractKey }); + + // expressions + importer.DrawRemapGUI(vrm.Expressions.Select(x => CreateKey(x).ExtractKey)); + + if (GUILayout.Button("Clear")) + { + importer.ClearExternalObjects(); + importer.ClearExternalObjects(); + importer.ClearExternalObjects(); + } + } + + /// + /// SubAssetを外部ファイルに展開する + /// + public static void Extract(ScriptedImporter importer, GltfParser parser) + { + if (string.IsNullOrEmpty(importer.assetPath)) + { + return; + } + + var path = $"{Path.GetDirectoryName(importer.assetPath)}/{Path.GetFileNameWithoutExtension(importer.assetPath)}.Extracted"; + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + + // meta + { + foreach (var asset in importer.GetSubAssets(importer.assetPath)) + { + asset.ExtractSubAsset($"{path}/{asset.name}.asset", false); + } + } + + { + // expressions + foreach (var asset in importer.GetSubAssets(importer.assetPath)) + { + asset.ExtractSubAsset($"{path}/{asset.name}.asset", false); + } + + // expressions + foreach (var asset in importer.GetSubAssets(importer.assetPath)) + { + asset.ExtractSubAsset($"{path}/{asset.name}.asset", false); + } + + // external な expressionAvatar.Clips に 再代入する + var expressionAvatar = importer.GetExternalObjectMap().Select(x => x.Value as VRM10ExpressionAvatar).FirstOrDefault(x => x != null); + var expressions = importer.GetExternalObjectMap().Select(x => x.Value as VRM10Expression).Where(x => x != null).ToList(); + expressionAvatar.Clips = expressions; + var avatarPath = AssetDatabase.GetAssetPath(expressionAvatar); + if (!string.IsNullOrEmpty(avatarPath)) + { + EditorUtility.SetDirty(expressionAvatar); + AssetDatabase.WriteImportSettingsIfDirty(avatarPath); + } + } + + AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); + } + } +} diff --git a/Assets/VRM10/Editor/ScriptedImporter/EditorVrm.cs.meta b/Assets/VRM10/Editor/ScriptedImporter/EditorVrm.cs.meta new file mode 100644 index 000000000..d523d2332 --- /dev/null +++ b/Assets/VRM10/Editor/ScriptedImporter/EditorVrm.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: a15559f8a5ba86841a1ef6e08d37569a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs index f89d8be68..5a627919c 100644 --- a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs +++ b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs @@ -1,5 +1,4 @@ -using System.Linq; -using UnityEditor; +using UnityEditor; using UnityEngine; using UniGLTF; #if UNITY_2020_2_OR_NEWER @@ -14,14 +13,10 @@ namespace UniVRM10 [CustomEditor(typeof(VrmScriptedImporter))] public class VrmScriptedImporterEditorGUI : ScriptedImporterEditor { - // const string TextureDirName = "Textures"; - // const string MaterialDirName = "Materials"; - // const string MetaDirName = "MetaObjects"; - // const string ExpressionDirName = "Expressions"; - VrmScriptedImporter m_importer; GltfParser m_parser; VrmLib.Model m_model; + UniGLTF.Extensions.VRMC_vrm.VRMC_vrm m_vrm; public override void OnEnable() { @@ -33,7 +28,10 @@ namespace UniVRM10 { return; } - + if (!UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(m_parser.GLTF.extensions, out m_vrm)) + { + return; + } m_model = VrmLoader.CreateVrmModel(m_parser); } @@ -59,95 +57,17 @@ namespace UniVRM10 case Tabs.Materials: if (m_parser != null) { - EditorMaterial.OnGUIMaterial(m_importer, m_parser, Vrm10MToonMaterialImporter.EnumerateAllTexturesDistinct); + EditorMaterial.OnGUI(m_importer, m_parser, Vrm10MToonMaterialImporter.EnumerateAllTexturesDistinct); } break; case Tabs.Vrm: + if (m_parser != null) + { + EditorVrm.OnGUI(m_importer, m_parser, m_vrm); + } break; } } - - // var importer = target as VrmScriptedImporter; - - // EditorGUILayout.LabelField("Extract settings"); - - // EditorGUILayout.BeginHorizontal(); - // EditorGUILayout.PrefixLabel("Materials And Textures"); - // GUI.enabled = !(importer.GetExternalUnityObjects().Any() - // && importer.GetExternalUnityObjects().Any()); - // if (GUILayout.Button("Extract")) - // { - // importer.ExtractMaterialsAndTextures(); - // } - // GUI.enabled = !GUI.enabled; - // if (GUILayout.Button("Clear")) - // { - // importer.ClearExternalObjects(); - // importer.ClearExternalObjects(); - // } - // GUI.enabled = true; - // EditorGUILayout.EndHorizontal(); - - // EditorGUILayout.BeginHorizontal(); - // EditorGUILayout.PrefixLabel("Meta"); - // GUI.enabled = !importer.GetExternalUnityObjects().Any(); - // if (GUILayout.Button("Extract")) - // { - // importer.ExtractMeta(); - // } - // GUI.enabled = !GUI.enabled; - // if (GUILayout.Button("Clear")) - // { - // importer.ClearExternalObjects(); - // } - // GUI.enabled = true; - // EditorGUILayout.EndHorizontal(); - - // EditorGUILayout.BeginHorizontal(); - // EditorGUILayout.PrefixLabel("Expressions"); - // GUI.enabled = !(importer.GetExternalUnityObjects().Any() - // && importer.GetExternalUnityObjects().Any()); - // if (GUILayout.Button("Extract")) - // { - // importer.ExtractExpressions(); - // } - // GUI.enabled = !GUI.enabled; - // if (GUILayout.Button("Clear")) - // { - // importer.ClearExternalObjects(); - // importer.ClearExternalObjects(); - // } - // GUI.enabled = true; - // EditorGUILayout.EndHorizontal(); - - // // ObjectMap - // DrawRemapGUI("Material Remap", importer); - // DrawRemapGUI("Texture Remap", importer); - // DrawRemapGUI("Meta Remap", importer); - // DrawRemapGUI("ExpressionAvatar Remap", importer); - // DrawRemapGUI("Expression Remap", importer); - - // base.OnInspectorGUI(); - // } - - // private void DrawRemapGUI(string title, VrmScriptedImporter importer) where T : UnityEngine.Object - // { - // EditorGUILayout.Foldout(_isOpen, title); - // EditorGUI.indentLevel++; - // var objects = importer.GetExternalObjectMap().Where(x => x.Key.type == typeof(T)); - // foreach (var obj in objects) - // { - // EditorGUILayout.BeginHorizontal(); - // EditorGUILayout.PrefixLabel(obj.Key.name); - // var asset = EditorGUILayout.ObjectField(obj.Value, obj.Key.type, true) as T; - // if (asset != obj.Value) - // { - // importer.SetExternalUnityObject(obj.Key, asset); - // } - // EditorGUILayout.EndHorizontal(); - // } - // EditorGUI.indentLevel--; - // } } } diff --git a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterImpl.cs b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterImpl.cs index 53a1b35de..93eae8c74 100644 --- a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterImpl.cs +++ b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterImpl.cs @@ -85,124 +85,5 @@ namespace UniVRM10 }); } } - - // public void ExtractMeta() - // { - // this.ExtractAssets(MetaDirName, ".asset"); - // var metaObject = this.GetExternalUnityObjects().FirstOrDefault(); - // var metaObjectPath = AssetDatabase.GetAssetPath(metaObject.Value); - // if (!string.IsNullOrEmpty(metaObjectPath)) - // { - // EditorUtility.SetDirty(metaObject.Value); - // AssetDatabase.WriteImportSettingsIfDirty(metaObjectPath); - // } - // AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); - // } - - // public void ExtractExpressions() - // { - // this.ExtractAssets(ExpressionDirName, ".asset"); - // this.ExtractAssets(ExpressionDirName, ".asset"); - - // var expressionAvatar = this.GetExternalUnityObjects().FirstOrDefault(); - // var expressions = this.GetExternalUnityObjects(); - - // expressionAvatar.Value.Clips = expressions.Select(x => x.Value).ToList(); - // var avatarPath = AssetDatabase.GetAssetPath(expressionAvatar.Value); - // if (!string.IsNullOrEmpty(avatarPath)) - // { - // EditorUtility.SetDirty(expressionAvatar.Value); - // AssetDatabase.WriteImportSettingsIfDirty(avatarPath); - // } - - // AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); - // } - - // 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 - // { - // this.AddRemap(sourceAssetIdentifier, obj); - // AssetDatabase.WriteImportSettingsIfDirty(this.assetPath); - // AssetDatabase.ImportAsset(this.assetPath, ImportAssetOptions.ForceUpdate); - // } - - // public static void ClearExternalObjects(this ScriptedImporter importer) where T : UnityEngine.Object - // { - // foreach (var extarnalObject in importer.GetExternalObjectMap().Where(x => x.Key.type == typeof(T))) - // { - // importer.RemoveRemap(extarnalObject.Key); - // } - - // AssetDatabase.WriteImportSettingsIfDirty(importer.assetPath); - // AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); - // } - - // public static void ClearExtarnalObjects(this ScriptedImporter importer) - // { - // foreach (var extarnalObject in importer.GetExternalObjectMap()) - // { - // importer.RemoveRemap(extarnalObject.Key); - // } - - // AssetDatabase.WriteImportSettingsIfDirty(importer.assetPath); - // AssetDatabase.ImportAsset(importer.assetPath, ImportAssetOptions.ForceUpdate); - // } - - // private static T GetSubAsset(this ScriptedImporter importer, string assetPath) where T : UnityEngine.Object - // { - // return importer.GetSubAssets(assetPath) - // .FirstOrDefault(); - // } - - // public static IEnumerable GetSubAssets(this ScriptedImporter importer, string assetPath) where T : UnityEngine.Object - // { - // return AssetDatabase - // .LoadAllAssetsAtPath(assetPath) - // .Where(x => AssetDatabase.IsSubAsset(x)) - // .Where(x => x is T) - // .Select(x => x as T); - // } - - // private static void ExtractFromAsset(UnityEngine.Object subAsset, string destinationPath, bool isForceUpdate) - // { - // string assetPath = AssetDatabase.GetAssetPath(subAsset); - - // var clone = UnityEngine.Object.Instantiate(subAsset); - // AssetDatabase.CreateAsset(clone, destinationPath); - - // var assetImporter = AssetImporter.GetAtPath(assetPath); - // assetImporter.AddRemap(new AssetImporter.SourceAssetIdentifier(subAsset), clone); - - // if (isForceUpdate) - // { - // AssetDatabase.WriteImportSettingsIfDirty(assetPath); - // AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate); - // } - // } - - // public static void ExtractAssets(this ScriptedImporter importer, string dirName, string extension) where T : UnityEngine.Object - // { - // if (string.IsNullOrEmpty(importer.assetPath)) - // return; - - // var subAssets = importer.GetSubAssets(importer.assetPath); - - // var path = string.Format("{0}/{1}.{2}", - // Path.GetDirectoryName(importer.assetPath), - // Path.GetFileNameWithoutExtension(importer.assetPath), - // dirName - // ); - - // var info = importer.SafeCreateDirectory(path); - - // foreach (var asset in subAssets) - // { - // ExtractFromAsset(asset, string.Format("{0}/{1}{2}", path, asset.name, extension), false); - // } - // } } } diff --git a/Assets/VRM10/Editor/VRM10MetaObjectEditor.cs b/Assets/VRM10/Editor/VRM10MetaObjectEditor.cs index b5a906de5..fc568c025 100644 --- a/Assets/VRM10/Editor/VRM10MetaObjectEditor.cs +++ b/Assets/VRM10/Editor/VRM10MetaObjectEditor.cs @@ -193,6 +193,11 @@ namespace UniVRM10 public override void OnInspectorGUI() { + if (target == null) + { + return; + } + serializedObject.Update(); if (VRMVersion.IsNewer(m_exporterVersion.stringValue)) diff --git a/Assets/VRM10/Runtime/Components/Expression/ExpressionKey.cs b/Assets/VRM10/Runtime/Components/Expression/ExpressionKey.cs index 70656d225..78df93c5f 100644 --- a/Assets/VRM10/Runtime/Components/Expression/ExpressionKey.cs +++ b/Assets/VRM10/Runtime/Components/Expression/ExpressionKey.cs @@ -21,13 +21,13 @@ namespace UniVRM10 /// Preset of this ExpressionKey. /// public readonly UniGLTF.Extensions.VRMC_vrm.ExpressionPreset Preset; - + /// /// Custom Name of this ExpressionKey. /// This works if Preset was Custom. /// public readonly string Name; - + /// /// Id for comparison of ExpressionKey. /// @@ -172,6 +172,13 @@ namespace UniVRM10 return 0; } - } + public string ExtractKey + { + get + { + return $"Expression.{this}"; + } + } + } } diff --git a/Assets/VRM10/Runtime/Components/Expression/VRM10ExpressionAvatar.cs b/Assets/VRM10/Runtime/Components/Expression/VRM10ExpressionAvatar.cs index 61dc328ca..51eb21c70 100644 --- a/Assets/VRM10/Runtime/Components/Expression/VRM10ExpressionAvatar.cs +++ b/Assets/VRM10/Runtime/Components/Expression/VRM10ExpressionAvatar.cs @@ -13,7 +13,7 @@ namespace UniVRM10 [CreateAssetMenu(menuName = "VRM10/ExpressionAvatar")] public sealed class VRM10ExpressionAvatar : ScriptableObject { - public const string ExtractKey = ".ExpressionAvatar"; + public const string ExtractKey = "ExpressionAvatar"; [SerializeField] public List Clips = new List(); diff --git a/Assets/VRM10/Runtime/Components/Meta/VRM10MetaObject.cs b/Assets/VRM10/Runtime/Components/Meta/VRM10MetaObject.cs index 8851ccc26..a3a097a6c 100644 --- a/Assets/VRM10/Runtime/Components/Meta/VRM10MetaObject.cs +++ b/Assets/VRM10/Runtime/Components/Meta/VRM10MetaObject.cs @@ -10,7 +10,7 @@ namespace UniVRM10 [CreateAssetMenu(menuName = "VRM10/MetaObject")] public class VRM10MetaObject : ScriptableObject { - public const string ExtractKey = ".Meta"; + public const string ExtractKey = "Meta"; [SerializeField] public string ExporterVersion; diff --git a/Assets/VRM10/Runtime/IO/ExpressionExtensions.cs b/Assets/VRM10/Runtime/IO/ExpressionExtensions.cs index 939207832..a116a9172 100644 --- a/Assets/VRM10/Runtime/IO/ExpressionExtensions.cs +++ b/Assets/VRM10/Runtime/IO/ExpressionExtensions.cs @@ -11,14 +11,14 @@ namespace UniVRM10 /// for SubAssetName /// /// - public static string ExtractName(this Expression expression) + public static string ExtractKey(this Expression expression) { ExpressionKey key = (expression.Preset == ExpressionPreset.custom) ? ExpressionKey.CreateCustom(expression.Name) : ExpressionKey.CreateFromPreset(expression.Preset) ; - return $"Expression.{key}"; + return key.ExtractKey; } public static UniVRM10.MorphTargetBinding Build10(this MorphTargetBind bind, GameObject root, RuntimeUnityBuilder.ModelMap loader, VrmLib.Model model) diff --git a/Assets/VRM10/Runtime/IO/RuntimeUnityBuilder.cs b/Assets/VRM10/Runtime/IO/RuntimeUnityBuilder.cs index 83ba9f8f9..486390cc1 100644 --- a/Assets/VRM10/Runtime/IO/RuntimeUnityBuilder.cs +++ b/Assets/VRM10/Runtime/IO/RuntimeUnityBuilder.cs @@ -18,10 +18,17 @@ namespace UniVRM10 UniGLTF.Extensions.VRMC_vrm.VRMC_vrm m_vrm; + List<(string, UnityEngine.Object)> m_external = new List<(string, UnityEngine.Object)>(); + public RuntimeUnityBuilder(UniGLTF.GltfParser parser, IEnumerable<(string, UnityEngine.Object)> externalObjectMap = null) : base(parser, externalObjectMap) { m_model = VrmLoader.CreateVrmModel(parser); + if (externalObjectMap != null) + { + m_external.AddRange(externalObjectMap); + } + // for `VRMC_materials_mtoon` this.GltfMaterialImporter.GltfMaterialParamProcessors.Insert(0, Vrm10MToonMaterialImporter.TryCreateParam); @@ -229,7 +236,17 @@ namespace UniVRM10 async Task LoadVrmAsync(IAwaitCaller awaitCaller, VRM10Controller controller, UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm) { // meta - if (vrm.Meta != null) + controller.Meta = m_external.Select(x => x.Item2 as VRM10MetaObject).FirstOrDefault(x => x != null); + // external meta != null のとき m_meta は null になる + if (controller.Meta != null) + { + // texture の 取得 + if (Vrm10MToonMaterialImporter.TryGetMetaThumbnailTextureImportParam(Parser, vrm, out VRMShaders.TextureImportParam param)) + { + var texture = await TextureFactory.GetTextureAsync(param); + } + } + else if (vrm.Meta != null) { var src = vrm.Meta; m_meta = ScriptableObject.CreateInstance(); @@ -269,7 +286,8 @@ namespace UniVRM10 } // expression - if (vrm.Expressions != null) + controller.Expression.ExpressionAvatar = m_external.Select(x => x.Item2 as VRM10ExpressionAvatar).FirstOrDefault(x => x != null); + if (controller.Expression.ExpressionAvatar == null && vrm.Expressions != null) { controller.Expression.ExpressionAvatar = ScriptableObject.CreateInstance(); @@ -281,7 +299,7 @@ namespace UniVRM10 var clip = ScriptableObject.CreateInstance(); clip.Preset = expression.Preset; clip.ExpressionName = expression.Name; - clip.name = expression.ExtractName(); + clip.name = expression.ExtractKey(); clip.IsBinary = expression.IsBinary.GetValueOrDefault(); clip.OverrideBlink = expression.OverrideBlink; clip.OverrideLookAt = expression.OverrideLookAt; @@ -555,22 +573,28 @@ namespace UniVRM10 m_humanoid = null; } - if (take(m_meta)) + if (m_meta != null) { - m_meta = null; - } - - foreach (var x in m_exressionAvatar.Clips) - { - if (take(x)) + if (take(m_meta)) { - // do nothing + m_meta = null; } } - if (take(m_exressionAvatar)) + if (m_exressionAvatar != null) { - m_exressionAvatar = null; + foreach (var x in m_exressionAvatar.Clips) + { + if (take(x)) + { + // do nothing + } + } + + if (take(m_exressionAvatar)) + { + m_exressionAvatar = null; + } } // GLTF のリソース