diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glbTypes.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glbTypes.cs index cb10dd6df..2b8e0bf4a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glbTypes.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glbTypes.cs @@ -219,6 +219,11 @@ namespace UniGLTF } public static Glb Parse(Byte[] bytes) + { + return Parse(new ArraySegment(bytes)); + } + + public static Glb Parse(ArraySegment bytes) { if (TryParse(bytes, out Glb glb, out Exception ex)) { diff --git a/Assets/VRM10/Runtime/Migration.meta b/Assets/VRM10/Runtime/Migration.meta new file mode 100644 index 000000000..26398a409 --- /dev/null +++ b/Assets/VRM10/Runtime/Migration.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 475aa606f7fac0648a504be5102a6f16 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Runtime/Migration/Migration.cs b/Assets/VRM10/Runtime/Migration/Migration.cs new file mode 100644 index 000000000..8f15afb7b --- /dev/null +++ b/Assets/VRM10/Runtime/Migration/Migration.cs @@ -0,0 +1,60 @@ +using System; +using System.Collections.Generic; +using UniGLTF.Extensions.VRMC_vrm; +using UniJSON; + +namespace UniVRM10 +{ + /// + /// Convert vrm0 binary to vrm1 binary. Json processing + /// + public static class Migration + { + static bool TryGet(this UniGLTF.glTFExtensionImport extensions, string key, out ListTreeNode value) + { + foreach (var kv in extensions.ObjectItems()) + { + if (kv.Key.GetString() == key) + { + value = kv.Value; + return true; + } + } + + value = default; + return false; + } + + public static byte[] Migrate(byte[] src) + { + var glb = UniGLTF.Glb.Parse(src); + var json = glb.Json.Bytes.ParseAsJson(); + + var gltf = UniGLTF.GltfDeserializer.Deserialize(json); + if (!(gltf.extensions is UniGLTF.glTFExtensionImport import)) + { + throw new Exception("not extensions"); + } + if (!import.TryGet("VRM", out ListTreeNode vrm)) + { + throw new Exception("no vrm"); + } + + { + var vrm1 = new VRMC_vrm(); + var f = new JsonFormatter(); + GltfSerializer.Serialize(f, vrm1); + gltf.extensions = new UniGLTF.glTFExtensionExport().Add(VRMC_vrm.ExtensionName, f.GetStoreBytes()); + } + + ArraySegment vrm1Json = default; + { + var f = new JsonFormatter(); + UniGLTF.GltfSerializer.Serialize(f, gltf); + vrm1Json = f.GetStoreBytes(); + } + + return UniGLTF.Glb.Create(vrm1Json, glb.Binary.Bytes).ToBytes(); + } + } +} diff --git a/Assets/VRM10/Runtime/Migration/Migration.cs.meta b/Assets/VRM10/Runtime/Migration/Migration.cs.meta new file mode 100644 index 000000000..09c343804 --- /dev/null +++ b/Assets/VRM10/Runtime/Migration/Migration.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ab1292ee15555c249af9108c10133c99 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRM10/Tests/MigrationTests.cs b/Assets/VRM10/Tests/MigrationTests.cs new file mode 100644 index 000000000..fd70d6b45 --- /dev/null +++ b/Assets/VRM10/Tests/MigrationTests.cs @@ -0,0 +1,47 @@ +using System.IO; +using NUnit.Framework; +using UnityEngine; +using UniJSON; + +namespace UniVRM10 +{ + public class MigrationTests + { + static string AliciaPath + { + get + { + return Path.GetFullPath(Application.dataPath + "/../Tests/Models/Alicia_vrm-0.51/AliciaSolid_vrm-0.51.vrm") + .Replace("\\", "/"); + } + } + + UniGLTF.Extensions.VRMC_vrm.VRMC_vrm GetVRM(UniGLTF.glTFExtension extensions) + { + if (extensions is UniGLTF.glTFExtensionImport import) + { + foreach (var kv in import.ObjectItems()) + { + if (kv.Key.GetUtf8String() == UniGLTF.Extensions.VRMC_vrm.VRMC_vrm.ExtensionNameUtf8) + { + return UniGLTF.Extensions.VRMC_vrm.GltfDeserializer.Deserialize(kv.Value); + } + } + } + + return default; + } + + [Test] + public void Migrate0to1() + { + var vrm0 = File.ReadAllBytes(AliciaPath); + var vrm1 = Migration.Migrate(vrm0); + var glb = UniGLTF.Glb.Parse(vrm1); + var json = glb.Json.Bytes.ParseAsJson(); + var gltf = UniGLTF.GltfDeserializer.Deserialize(json); + var vrm = GetVRM(gltf.extensions); + Assert.NotNull(vrm); + } + } +} diff --git a/Assets/VRM10/Tests/MigrationTests.cs.meta b/Assets/VRM10/Tests/MigrationTests.cs.meta new file mode 100644 index 000000000..2bfe03e08 --- /dev/null +++ b/Assets/VRM10/Tests/MigrationTests.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 856d7b1293642ed4dbce5341b397b74e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: