From 13c445f8822443324966000a68811a36a14c20ff Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 22 Jun 2021 15:12:41 +0900 Subject: [PATCH] =?UTF-8?q?vrm0=20=E3=81=AE=E6=99=82=E3=81=A0=E3=81=91=20m?= =?UTF-8?q?igration=20checkbox?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/VRM10.Samples/Runtime/ViewerUI.cs | 4 +- .../VrmScriptedImporterEditorGUI.cs | 51 +++++++------ .../VrmScriptedImporterImpl.cs | 2 +- Assets/VRM10/Runtime/IO/Vrm10Parser.cs | 73 +++++++++++-------- Assets/VRM10/Runtime/Scenes/Sample.cs | 2 +- Assets/VRM10/Tests.PlayMode/MaterialTests.cs | 2 +- Assets/VRM10/Tests/ApiSampleTests.cs | 2 +- 7 files changed, 75 insertions(+), 61 deletions(-) diff --git a/Assets/VRM10.Samples/Runtime/ViewerUI.cs b/Assets/VRM10.Samples/Runtime/ViewerUI.cs index fed5b523d..8d908d813 100644 --- a/Assets/VRM10.Samples/Runtime/ViewerUI.cs +++ b/Assets/VRM10.Samples/Runtime/ViewerUI.cs @@ -308,9 +308,9 @@ namespace UniVRM10.Samples { case ".vrm": { - if (!Vrm10Parser.TryParseOrMigrate(path, doMigrate: true, out Vrm10Parser.Result result, out string error)) + if (!Vrm10Parser.TryParseOrMigrate(path, doMigrate: true, out Vrm10Parser.Result result)) { - Debug.LogError(error); + Debug.LogError(result.Message); return; } using (var loader = new Vrm10Importer(result.Parser, result.Vrm)) diff --git a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs index e4d3eebc6..b30965b9c 100644 --- a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs +++ b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterEditorGUI.cs @@ -4,8 +4,6 @@ using UniGLTF; using System.IO; using UniGLTF.MeshUtility; using System.Linq; -using VRMShaders; -using System.Collections.Generic; #if UNITY_2020_2_OR_NEWER using UnityEditor.AssetImporters; #else @@ -19,38 +17,32 @@ namespace UniVRM10 public class VrmScriptedImporterEditorGUI : RemapScriptedImporterEditorBase { VrmScriptedImporter m_importer; - GltfParser m_parser; VrmLib.Model m_model; - UniGLTF.Extensions.VRMC_vrm.VRMC_vrm m_vrm; RemapEditorMaterial m_materialEditor; RemapEditorVrm m_vrmEditor; - string m_message; + Vrm10Parser.Result m_result; public override void OnEnable() { base.OnEnable(); m_importer = target as VrmScriptedImporter; - m_parser = default; - m_message = default; - if (!Vrm10Parser.TryParseOrMigrate(m_importer.assetPath, m_importer.MigrateToVrm1, out Vrm10Parser.Result result, out m_message)) + if (!Vrm10Parser.TryParseOrMigrate(m_importer.assetPath, m_importer.MigrateToVrm1, out m_result)) { // error return; } - m_vrm = result.Vrm; - m_parser = result.Parser; - m_model = ModelReader.Read(result.Parser); + m_model = ModelReader.Read(m_result.Parser); var tmp = m_importer.GetExternalObjectMap(); var generator = new Vrm10MaterialDescriptorGenerator(); - var materialKeys = m_parser.GLTF.materials.Select((x, i) => generator.Get(m_parser, i).SubAssetKey); - var textureKeys = new GltfTextureDescriptorGenerator(m_parser).Get().GetEnumerable().Select(x => x.SubAssetKey); + var materialKeys = m_result.Parser.GLTF.materials.Select((x, i) => generator.Get(m_result.Parser, i).SubAssetKey); + var textureKeys = new GltfTextureDescriptorGenerator(m_result.Parser).Get().GetEnumerable().Select(x => x.SubAssetKey); m_materialEditor = new RemapEditorMaterial(materialKeys.Concat(textureKeys), GetEditorMap, SetEditorMap); - var expressionSubAssetKeys = m_vrm.Expressions.Select(x => ExpressionKey.CreateFromVrm10(x).SubAssetKey); + var expressionSubAssetKeys = m_result.Vrm.Expressions.Select(x => ExpressionKey.CreateFromVrm10(x).SubAssetKey); m_vrmEditor = new RemapEditorVrm(new[] { VRM10Object.SubAssetKey }.Concat(expressionSubAssetKeys), GetEditorMap, SetEditorMap); } @@ -64,24 +56,35 @@ namespace UniVRM10 public override void OnInspectorGUI() { - if (!string.IsNullOrEmpty(m_message)) - { - EditorGUILayout.HelpBox(m_message, MessageType.Error); - } - s_currentTab = TabBar.OnGUI(s_currentTab); GUILayout.Space(10); switch (s_currentTab) { case Tabs.Model: - base.OnInspectorGUI(); + { + switch (m_result.FileType) + { + case Vrm10FileType.Vrm1: + EditorGUILayout.HelpBox(m_result.Message, MessageType.Info); + break; + + case Vrm10FileType.Vrm0: + EditorGUILayout.HelpBox(m_result.Message, m_model != null ? MessageType.Info : MessageType.Warning); + // migration check boxs + base.OnInspectorGUI(); + break; + + default: + break; + } + } break; case Tabs.Materials: - if (m_parser != null && m_vrm != null) + if (m_result.Parser != null && m_result.Vrm != null) { - m_materialEditor.OnGUI(m_importer, m_parser, new Vrm10TextureDescriptorGenerator(m_parser), + m_materialEditor.OnGUI(m_importer, m_result.Parser, new Vrm10TextureDescriptorGenerator(m_result.Parser), assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.vrm1.Textures", assetPath => $"{Path.GetFileNameWithoutExtension(assetPath)}.vrm1.Materials"); RevertApplyRemapGUI(m_importer); @@ -89,9 +92,9 @@ namespace UniVRM10 break; case Tabs.Vrm: - if (m_parser != null && m_vrm != null) + if (m_result.Parser != null && m_result.Vrm != null) { - m_vrmEditor.OnGUI(m_importer, m_parser, m_vrm); + m_vrmEditor.OnGUI(m_importer, m_result.Parser, m_result.Vrm); RevertApplyRemapGUI(m_importer); } break; diff --git a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterImpl.cs b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterImpl.cs index 1df283c37..fa32b565a 100644 --- a/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterImpl.cs +++ b/Assets/VRM10/Editor/ScriptedImporter/VrmScriptedImporterImpl.cs @@ -22,7 +22,7 @@ namespace UniVRM10 Debug.Log("OnImportAsset to " + scriptedImporter.assetPath); #endif - if (!Vrm10Parser.TryParseOrMigrate(scriptedImporter.assetPath, migrateToVrm1, out Vrm10Parser.Result result, out string message)) + if (!Vrm10Parser.TryParseOrMigrate(scriptedImporter.assetPath, migrateToVrm1, out Vrm10Parser.Result result)) { // fail to parse vrm1 return; diff --git a/Assets/VRM10/Runtime/IO/Vrm10Parser.cs b/Assets/VRM10/Runtime/IO/Vrm10Parser.cs index e9e876b03..7c9deff29 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Parser.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Parser.cs @@ -6,32 +6,42 @@ using UniJSON; namespace UniVRM10 { + public enum Vrm10FileType + { + Vrm1, + Vrm0, + Other, + } + public static class Vrm10Parser { public readonly struct Result { public readonly GltfParser Parser; public readonly VRMC_vrm Vrm; - - public Result(GltfParser parser, VRMC_vrm vrm) + public readonly Vrm10FileType FileType; + public readonly String Message; + public Result(GltfParser parser, VRMC_vrm vrm, Vrm10FileType fileType, string message) { Parser = parser; Vrm = vrm; + FileType = fileType; + Message = message; } } - public static bool TryParseOrMigrate(string path, bool doMigrate, out Result result, out string error) + public static bool TryParseOrMigrate(string path, bool doMigrate, out Result result) { - return TryParseOrMigrate(path, File.ReadAllBytes(path), doMigrate, out result, out error); + return TryParseOrMigrate(path, File.ReadAllBytes(path), doMigrate, out result); } /// - /// VRM1 で パースし、失敗したら Migration してから VRM1 でパースする + /// VRM1 でパースし、失敗したら Migration してから VRM1 でパースする /// /// /// /// - public static bool TryParseOrMigrate(string path, byte[] bytes, bool doMigrate, out Result result, out string error) + public static bool TryParseOrMigrate(string path, byte[] bytes, bool doMigrate, out Result result) { // // Parse(parse glb, parser gltf json) @@ -42,50 +52,53 @@ namespace UniVRM10 if (UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(parser.GLTF.extensions, out UniGLTF.Extensions.VRMC_vrm.VRMC_vrm vrm)) { // success - error = default; - result = new Result(parser, vrm); + result = new Result(parser, vrm, Vrm10FileType.Vrm1, "vrm1: loaded"); return true; } } - if (!doMigrate) - { - error = "vrm1 not found"; - result = default; - return false; - } - // try migrateion byte[] migrated = default; try { var glb = UniGLTF.Glb.Parse(bytes); var json = glb.Json.Bytes.ParseAsJson(); - if (!json.TryGet("extensions", out JsonNode extensions)) + + try { - error = "no gltf.extensions"; - result = default; + if (!json.TryGet("extensions", out JsonNode extensions)) + { + result = new Result(default, default, Vrm10FileType.Other, "gltf: no extensions"); + return false; + } + if (!extensions.TryGet("VRM", out JsonNode vrm0)) + { + result = new Result(default, default, Vrm10FileType.Other, "gltf: no vrm0"); + return false; + } + } + catch (Exception ex) + { + result = new Result(default, default, Vrm10FileType.Other, $"error: {ex}"); return false; } - if (!extensions.TryGet("VRM", out JsonNode vrm0)) + + if (!doMigrate) { - error = "vrm0 not found"; - result = default; + result = new Result(default, default, Vrm10FileType.Vrm0, "vrm0: not migrated"); return false; } migrated = MigrationVrm.Migrate(json, glb.Binary.Bytes); if (migrated == null) { - error = "cannot migrate"; - result = default; + result = new Result(default, default, Vrm10FileType.Vrm0, "vrm0: cannot migrate"); return false; } } catch (Exception ex) { - error = $"migration error: {ex}"; - result = default; + result = new Result(default, default, Vrm10FileType.Vrm0, $"vrm0: migration error: {ex}"); return false; } @@ -95,15 +108,13 @@ namespace UniVRM10 if (UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(parser.GLTF.extensions, out VRMC_vrm vrm)) { // success - error = default; - result = new Result(parser, vrm); + result = new Result(parser, vrm, Vrm10FileType.Vrm0, "vrm0: migrated"); return true; } - } - error = "migrate but no vrm1. unknown"; - result = default; - return false; + result = new Result(default, default, Vrm10FileType.Vrm0, "vrm0: migrate but error ?"); + return false; + } } } } diff --git a/Assets/VRM10/Runtime/Scenes/Sample.cs b/Assets/VRM10/Runtime/Scenes/Sample.cs index eba4892f9..a2a292030 100644 --- a/Assets/VRM10/Runtime/Scenes/Sample.cs +++ b/Assets/VRM10/Runtime/Scenes/Sample.cs @@ -15,7 +15,7 @@ namespace UniVRM10.Sample static GameObject Import(byte[] bytes, FileInfo path) { - if (!Vrm10Parser.TryParseOrMigrate(path.FullName, bytes, doMigrate: true, out Vrm10Parser.Result result, out string message)) + if (!Vrm10Parser.TryParseOrMigrate(path.FullName, bytes, doMigrate: true, out Vrm10Parser.Result result)) { return null; } diff --git a/Assets/VRM10/Tests.PlayMode/MaterialTests.cs b/Assets/VRM10/Tests.PlayMode/MaterialTests.cs index 92017283d..9ab8372b7 100644 --- a/Assets/VRM10/Tests.PlayMode/MaterialTests.cs +++ b/Assets/VRM10/Tests.PlayMode/MaterialTests.cs @@ -31,7 +31,7 @@ namespace UniVRM10.Test private (GameObject, IReadOnlyList) ToUnity(byte[] bytes) { // Vrm => Model - if (!Vrm10Parser.TryParseOrMigrate("tpm.vrm", bytes, true, out Vrm10Parser.Result result, out string error)) + if (!Vrm10Parser.TryParseOrMigrate("tpm.vrm", bytes, true, out Vrm10Parser.Result result)) { throw new Exception(); } diff --git a/Assets/VRM10/Tests/ApiSampleTests.cs b/Assets/VRM10/Tests/ApiSampleTests.cs index 79daf7e3d..7b9aa2d25 100644 --- a/Assets/VRM10/Tests/ApiSampleTests.cs +++ b/Assets/VRM10/Tests/ApiSampleTests.cs @@ -40,7 +40,7 @@ namespace UniVRM10.Test var path = "Tests/Models/Alicia_vrm-0.51/AliciaSolid_vrm-0.51.vrm"; Debug.Log($"load: {path}"); - Assert.IsTrue(Vrm10Parser.TryParseOrMigrate(path, true, out Vrm10Parser.Result result, out string error)); + Assert.IsTrue(Vrm10Parser.TryParseOrMigrate(path, true, out Vrm10Parser.Result result)); var go = BuildGameObject(result.Parser, result.Vrm, true); Debug.Log(go);