-
Notifications
You must be signed in to change notification settings - Fork 428
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using UniGLTF.Extensions.VRMC_vrm; | ||
using UniJSON; | ||
|
||
namespace UniVRM10 | ||
{ | ||
/// <summary> | ||
/// Convert vrm0 binary to vrm1 binary. Json processing | ||
/// </summary> | ||
public static class Migration | ||
{ | ||
static bool TryGet(this UniGLTF.glTFExtensionImport extensions, string key, out ListTreeNode<JsonValue> 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<JsonValue> 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<byte> vrm1Json = default; | ||
{ | ||
var f = new JsonFormatter(); | ||
UniGLTF.GltfSerializer.Serialize(f, gltf); | ||
vrm1Json = f.GetStoreBytes(); | ||
} | ||
|
||
return UniGLTF.Glb.Create(vrm1Json, glb.Binary.Bytes).ToBytes(); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.