mirror of
https://github.com/vrm-c/UniVRM.git
synced 2026-08-25 12:05:08 -05:00
@@ -219,6 +219,11 @@ namespace UniGLTF
|
||||
}
|
||||
|
||||
public static Glb Parse(Byte[] bytes)
|
||||
{
|
||||
return Parse(new ArraySegment<byte>(bytes));
|
||||
}
|
||||
|
||||
public static Glb Parse(ArraySegment<Byte> bytes)
|
||||
{
|
||||
if (TryParse(bytes, out Glb glb, out Exception ex))
|
||||
{
|
||||
|
||||
8
Assets/VRM10/Runtime/Migration.meta
Normal file
8
Assets/VRM10/Runtime/Migration.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 475aa606f7fac0648a504be5102a6f16
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
60
Assets/VRM10/Runtime/Migration/Migration.cs
Normal file
60
Assets/VRM10/Runtime/Migration/Migration.cs
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/VRM10/Runtime/Migration/Migration.cs.meta
Normal file
11
Assets/VRM10/Runtime/Migration/Migration.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ab1292ee15555c249af9108c10133c99
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
47
Assets/VRM10/Tests/MigrationTests.cs
Normal file
47
Assets/VRM10/Tests/MigrationTests.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
11
Assets/VRM10/Tests/MigrationTests.cs.meta
Normal file
11
Assets/VRM10/Tests/MigrationTests.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 856d7b1293642ed4dbce5341b397b74e
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user