From bf3f4416a8ca2a07651659cc3a7e3089c4249929 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 8 Jun 2021 19:21:30 +0900 Subject: [PATCH] Vrm10Importer.OpenOrMigrate --- Assets/VRM10.Samples/Runtime/ViewerUI.cs | 5 +- Assets/VRM10/Runtime/IO/Vrm10Exception.cs | 13 +++ .../VRM10/Runtime/IO/Vrm10Exception.cs.meta | 11 +++ Assets/VRM10/Runtime/IO/Vrm10Importer.cs | 26 +++++- Assets/VRM10/Runtime/Scenes/Sample.cs | 91 ++++++++++--------- 5 files changed, 96 insertions(+), 50 deletions(-) create mode 100644 Assets/VRM10/Runtime/IO/Vrm10Exception.cs create mode 100644 Assets/VRM10/Runtime/IO/Vrm10Exception.cs.meta diff --git a/Assets/VRM10.Samples/Runtime/ViewerUI.cs b/Assets/VRM10.Samples/Runtime/ViewerUI.cs index 70bc91699..d879d40a2 100644 --- a/Assets/VRM10.Samples/Runtime/ViewerUI.cs +++ b/Assets/VRM10.Samples/Runtime/ViewerUI.cs @@ -308,10 +308,7 @@ namespace UniVRM10.Samples { case ".vrm": { - var parser = new UniGLTF.GltfParser(); - parser.ParsePath(path); - - using (var loader = new Vrm10Importer(parser)) + using (var loader = Vrm10Importer.OpenOrMigrate(path)) { loader.Load(); loader.ShowMeshes(); diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exception.cs b/Assets/VRM10/Runtime/IO/Vrm10Exception.cs new file mode 100644 index 000000000..b2728d3eb --- /dev/null +++ b/Assets/VRM10/Runtime/IO/Vrm10Exception.cs @@ -0,0 +1,13 @@ +using System; + +namespace UniVRM10 +{ + public class Vrm10Exception : Exception + { + + } + + public class Vrm10NoExtensionException: Vrm10Exception + { + } +} diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exception.cs.meta b/Assets/VRM10/Runtime/IO/Vrm10Exception.cs.meta new file mode 100644 index 000000000..753965759 --- /dev/null +++ b/Assets/VRM10/Runtime/IO/Vrm10Exception.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d9f647d546d16914d941ce33e1f83ff6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs index c8b2fd5e8..ab19bd18f 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Importer.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Importer.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; using UniGLTF; @@ -39,7 +40,7 @@ namespace UniVRM10 if (!UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.TryGet(parser.GLTF.extensions, out m_vrm)) { - throw new Exception("VRMC_vrm is not found"); + throw new Vrm10NoExtensionException(); } // assign humanoid bones @@ -103,6 +104,29 @@ namespace UniVRM10 } } + public static Vrm10Importer OpenOrMigrate(byte[] bytes, string path) + { + try + { + var parser = new GltfParser(); + parser.Parse(path, bytes); + return new Vrm10Importer(parser); + } + catch (Vrm10NoExtensionException) + { + Debug.Log("vrm1 not found. try migration..."); + bytes = MigrationVrm.Migrate(bytes); + var parser = new GltfParser(); + parser.Parse(path, bytes); + return new Vrm10Importer(parser); + } + } + + public static Vrm10Importer OpenOrMigrate(string path) + { + return OpenOrMigrate(File.ReadAllBytes(path), path); + } + public class ModelMap { public readonly Dictionary Nodes = new Dictionary(); diff --git a/Assets/VRM10/Runtime/Scenes/Sample.cs b/Assets/VRM10/Runtime/Scenes/Sample.cs index cb9367c13..1b509341e 100644 --- a/Assets/VRM10/Runtime/Scenes/Sample.cs +++ b/Assets/VRM10/Runtime/Scenes/Sample.cs @@ -2,58 +2,59 @@ using System.IO; using UnityEngine; using VrmLib; -using UniVRM10; using UniGLTF; -using VRMShaders; -public class Sample : MonoBehaviour + +namespace UniVRM10.Sample { - [SerializeField] - string m_vrmPath = "Tests/Models/Alicia_vrm-0.51/AliciaSolid_vrm-0.51.vrm"; - static GameObject Import(byte[] bytes, FileInfo path) + public class Sample : MonoBehaviour { - var parser = new GltfParser(); - parser.Parse(path.FullName, bytes); + [SerializeField] + string m_vrmPath = "Tests/Models/Alicia_vrm-0.51/AliciaSolid_vrm-0.51.vrm"; - using (var loader = new Vrm10Importer(parser)) + static GameObject Import(byte[] bytes, FileInfo path) { - loader.Load(); - loader.ShowMeshes(); - return loader.DisposeOnGameObjectDestroyed().gameObject; + using (var loader = Vrm10Importer.OpenOrMigrate(bytes, path.FullName)) + { + loader.Load(); + loader.ShowMeshes(); + return loader.DisposeOnGameObjectDestroyed().gameObject; + } + } + + // Start is called before the first frame update + void OnEnable() + { + var src = new FileInfo(m_vrmPath); + var go = Import(File.ReadAllBytes(m_vrmPath), src); + + var exportedBytes = Vrm10Exporter.Export(go); + + // Import 1.0 + var vrm10 = Import(exportedBytes, src); + var pos = vrm10.transform.position; + pos.x += 1.5f; + vrm10.transform.position = pos; + vrm10.name = vrm10.name + "_Imported_v1_0"; + + // write + var path = Path.GetFullPath("vrm10.vrm"); + Debug.Log($"write : {path}"); + File.WriteAllBytes(path, exportedBytes); + } + + static void Printmatrices(Model model) + { + var matrices = model.Skins[0].InverseMatrices.GetSpan(); + var sb = new System.Text.StringBuilder(); + for (int i = 0; i < matrices.Length; ++i) + { + var m = matrices[i]; + sb.AppendLine($"#{i:00}[{m.M11:.00}, {m.M12:.00}, {m.M13:.00}, {m.M14:.00}][{m.M21:.00}, {m.M22:.00}, {m.M23:.00}, {m.M24:.00}][{m.M31:.00}, {m.M32:.00}, {m.M33:.00}, {m.M34:.00}][{m.M41:.00}, {m.M42:.00}, {m.M43:.00}, {m.M44:.00}]"); + } + Debug.Log(sb.ToString()); } } - // Start is called before the first frame update - void OnEnable() - { - var src = new FileInfo(m_vrmPath); - var go = Import(File.ReadAllBytes(m_vrmPath), src); - - var exportedBytes = Vrm10Exporter.Export(go); - - // Import 1.0 - var vrm10 = Import(exportedBytes, src); - var pos = vrm10.transform.position; - pos.x += 1.5f; - vrm10.transform.position = pos; - vrm10.name = vrm10.name + "_Imported_v1_0"; - - // write - var path = Path.GetFullPath("vrm10.vrm"); - Debug.Log($"write : {path}"); - File.WriteAllBytes(path, exportedBytes); - } - - static void Printmatrices(Model model) - { - var matrices = model.Skins[0].InverseMatrices.GetSpan(); - var sb = new System.Text.StringBuilder(); - for (int i = 0; i < matrices.Length; ++i) - { - var m = matrices[i]; - sb.AppendLine($"#{i:00}[{m.M11:.00}, {m.M12:.00}, {m.M13:.00}, {m.M14:.00}][{m.M21:.00}, {m.M22:.00}, {m.M23:.00}, {m.M24:.00}][{m.M31:.00}, {m.M32:.00}, {m.M33:.00}, {m.M34:.00}][{m.M41:.00}, {m.M42:.00}, {m.M43:.00}, {m.M44:.00}]"); - } - Debug.Log(sb.ToString()); - } -} +} \ No newline at end of file