Vrm10Importer.OpenOrMigrate

This commit is contained in:
ousttrue
2021-06-08 19:21:30 +09:00
parent 831b153a7a
commit bf3f4416a8
5 changed files with 96 additions and 50 deletions

View File

@@ -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();

View File

@@ -0,0 +1,13 @@
using System;
namespace UniVRM10
{
public class Vrm10Exception : Exception
{
}
public class Vrm10NoExtensionException: Vrm10Exception
{
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d9f647d546d16914d941ce33e1f83ff6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -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<VrmLib.Node, GameObject> Nodes = new Dictionary<VrmLib.Node, GameObject>();

View File

@@ -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<System.Numerics.Matrix4x4>();
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<System.Numerics.Matrix4x4>();
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());
}
}
}