VrmLoader.CreateVrmModel use GltfParser

This commit is contained in:
ousttrue
2021-03-29 19:13:21 +09:00
parent d4777e17a4
commit 8629f4df5c
6 changed files with 37 additions and 34 deletions

View File

@@ -306,15 +306,15 @@ namespace UniVRM10.Samples
{
case ".vrm":
{
// var context = new ImporterContext();
var file = File.ReadAllBytes(path);
// context.ParseGlb(file);
var parser =new UniGLTF.GltfParser();
parser.ParsePath(path);
// context.Load();
// context.ShowMeshes();
// context.EnableUpdateWhenOffscreen();
// context.ShowMeshes();
var model = UniVRM10.VrmLoader.CreateVrmModel(file, new FileInfo(path));
var model = UniVRM10.VrmLoader.CreateVrmModel(parser);
// UniVRM-0.XXのコンポーネントを構築する
var assets = UniVRM10.RuntimeUnityBuilder.ToUnityAsset(model, showMesh: false);

View File

@@ -24,10 +24,13 @@ namespace UniVRM10
{
Debug.Log("OnImportAsset to " + ctx.assetPath);
var parser = new UniGLTF.GltfParser();
parser.ParsePath(ctx.assetPath);
try
{
// Create Vrm Model
VrmLib.Model model = VrmLoader.CreateVrmModel(ctx.assetPath);
VrmLib.Model model = VrmLoader.CreateVrmModel(parser);
if (model == null)
{
// maybe VRM-0.X
@@ -158,7 +161,12 @@ namespace UniVRM10
public void ExtractTextures()
{
this.ExtractTextures(TextureDirName, (path) => { return VrmLoader.CreateVrmModel(path); });
this.ExtractTextures(TextureDirName, (path) =>
{
var parser = new UniGLTF.GltfParser();
parser.ParsePath(path);
return VrmLoader.CreateVrmModel(parser);
});
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
}
@@ -170,7 +178,15 @@ namespace UniVRM10
public void ExtractMaterialsAndTextures()
{
this.ExtractTextures(TextureDirName, (path) => { return VrmLoader.CreateVrmModel(path); }, () => { this.ExtractAssets<UnityEngine.Material>(MaterialDirName, ".mat"); });
this.ExtractTextures(TextureDirName, (path) =>
{
var parser = new UniGLTF.GltfParser();
parser.ParsePath(path);
return VrmLoader.CreateVrmModel(parser);
}, () =>
{
this.ExtractAssets<UnityEngine.Material>(MaterialDirName, ".mat");
});
AssetDatabase.ImportAsset(assetPath, ImportAssetOptions.ForceUpdate);
}

View File

@@ -11,7 +11,10 @@ public class Sample : MonoBehaviour
static UniVRM10.ModelAsset Import(byte[] bytes, FileInfo path)
{
var model = UniVRM10.VrmLoader.CreateVrmModel(bytes, path);
var parser = new UniGLTF.GltfParser();
parser.Parse(path.FullName, bytes);
var model = UniVRM10.VrmLoader.CreateVrmModel(parser);
// UniVRM-0.XXのコンポーネントを構築する
var assets = UniVRM10.RuntimeUnityBuilder.ToUnityAsset(model, showMesh: false);

View File

@@ -1,7 +1,5 @@
using System;
using System.IO;
using System.IO;
using VrmLib;
using UniJSON;
using UniGLTF;
namespace UniVRM10
@@ -11,24 +9,10 @@ namespace UniVRM10
/// </summary>
public static class VrmLoader
{
// TODO:
const string VRM0X_LICENSE_URL = "https://vrm-consortium.org/";
/// <summary>
/// Load VRM10 or VRM0x from path
/// </summary>
public static Model CreateVrmModel(string path)
public static Model CreateVrmModel(GltfParser parser)
{
var bytes = File.ReadAllBytes(path);
return CreateVrmModel(bytes, new FileInfo(path));
}
public static Model CreateVrmModel(byte[] bytes, FileInfo path)
{
var parser = new GltfParser();
parser.Parse(path.FullName, bytes);
var storage = new Vrm10Storage(parser);
var model = ModelLoader.Load(storage, path.Name);
var model = ModelLoader.Load(storage, Path.GetFileName(parser.TargetPath));
model.ConvertCoordinate(Coordinates.Unity);
return model;
}

View File

@@ -43,7 +43,10 @@ namespace UniVRM10.Test
private ModelAsset ToUnity(byte[] bytes)
{
// Vrm => Model
var model = VrmLoader.CreateVrmModel(bytes, new FileInfo("tmp.vrm"));
var parser = new UniGLTF.GltfParser();
parser.Parse("tmp.vrm", bytes);
var model = VrmLoader.CreateVrmModel(parser);
model.RemoveSecondary();
return ToUnity(model);

View File

@@ -12,13 +12,10 @@ namespace UniVRM10.Test
{
var bytes = MigrationVrm.Migrate(File.ReadAllBytes(path));
if (!UniGLTF.Glb.TryParse(bytes, out UniGLTF.Glb glb, out Exception ex))
{
Debug.LogError($"fail to Glb.TryParse: {path} => {ex}");
return null;
}
var parser = new UniGLTF.GltfParser();
parser.Parse("migrated", bytes);
var model = UniVRM10.VrmLoader.CreateVrmModel(bytes, new FileInfo(path));
var model = UniVRM10.VrmLoader.CreateVrmModel(parser);
return model;
}