GltfParserの分離と、ImporterContextのインターフェース整理

使い方
* var parser = new GltfParser();
* parser.ParsePath(filePath); // ParseJson, ParseGlb 等パース系の関数
* context = new ImporterContext(parser); // parse から context を作る
* context.Load(); // もしくは LoadAsync で、Unity化する
This commit is contained in:
ousttrue 2021-02-22 15:52:32 +09:00
parent 0f85964a50
commit a5be5e1383
16 changed files with 374 additions and 378 deletions

View File

@ -21,8 +21,10 @@ namespace UniGLTF
//
// load into scene
//
var context = new ImporterContext();
context.Load(path);
var parser = new GltfParser();
parser.ParsePath(path);
var context = new ImporterContext(parser);
context.Load();
context.ShowMeshes();
Selection.activeGameObject = context.Root;
}

View File

@ -44,8 +44,9 @@ namespace UniGLTF
return;
}
var context = new ImporterContext();
context.Parse(src);
var parser = new GltfParser();
parser.ParsePath(src);
var context = new ImporterContext(parser);
// Extract textures to assets folder
context.ExtractImages(prefabPath);

View File

@ -0,0 +1,244 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UniJSON;
using UnityEngine;
namespace UniGLTF
{
public class GltfParser
{
/// <summary>
/// JSON source
/// </summary>
public String Json;
/// <summary>
/// GLTF parsed from JSON
/// </summary>
public glTF GLTF;
/// <summary>
/// URI access
/// </summary>
public IStorage Storage;
public static bool IsGeneratedUniGLTFAndOlderThan(string generatorVersion, int major, int minor)
{
if (string.IsNullOrEmpty(generatorVersion)) return false;
if (generatorVersion == "UniGLTF") return true;
if (!generatorVersion.FastStartsWith("UniGLTF-")) return false;
try
{
var splitted = generatorVersion.Substring(8).Split('.');
var generatorMajor = int.Parse(splitted[0]);
var generatorMinor = int.Parse(splitted[1]);
if (generatorMajor < major)
{
return true;
}
else if (generatorMajor > major)
{
return false;
}
else
{
if (generatorMinor >= minor)
{
return false;
}
else
{
return true;
}
}
}
catch (Exception ex)
{
Debug.LogWarningFormat("{0}: {1}", generatorVersion, ex);
return false;
}
}
public bool IsGeneratedUniGLTFAndOlder(int major, int minor)
{
if (GLTF == null) return false;
if (GLTF.asset == null) return false;
return IsGeneratedUniGLTFAndOlderThan(GLTF.asset.generator, major, minor);
}
#region Parse
public void ParsePath(string path)
{
Parse(path, File.ReadAllBytes(path));
}
/// <summary>
/// Parse gltf json or Parse json chunk of glb
/// </summary>
/// <param name="path"></param>
/// <param name="bytes"></param>
public virtual void Parse(string path, Byte[] bytes)
{
var ext = Path.GetExtension(path).ToLower();
switch (ext)
{
case ".gltf":
ParseJson(Encoding.UTF8.GetString(bytes), new FileSystemStorage(Path.GetDirectoryName(path)));
break;
case ".zip":
{
var zipArchive = Zip.ZipArchiveStorage.Parse(bytes);
var gltf = zipArchive.Entries.FirstOrDefault(x => x.FileName.ToLower().EndsWith(".gltf"));
if (gltf == null)
{
throw new Exception("no gltf in archive");
}
var jsonBytes = zipArchive.Extract(gltf);
var json = Encoding.UTF8.GetString(jsonBytes);
ParseJson(json, zipArchive);
}
break;
default:
ParseGlb(bytes);
break;
}
}
/// <summary>
///
/// </summary>
/// <param name="bytes"></param>
public void ParseGlb(Byte[] bytes)
{
var chunks = glbImporter.ParseGlbChunks(bytes);
if (chunks.Count != 2)
{
throw new Exception("unknown chunk count: " + chunks.Count);
}
if (chunks[0].ChunkType != GlbChunkType.JSON)
{
throw new Exception("chunk 0 is not JSON");
}
if (chunks[1].ChunkType != GlbChunkType.BIN)
{
throw new Exception("chunk 1 is not BIN");
}
try
{
var jsonBytes = chunks[0].Bytes;
ParseJson(Encoding.UTF8.GetString(jsonBytes.Array, jsonBytes.Offset, jsonBytes.Count),
new SimpleStorage(chunks[1].Bytes));
}
catch (StackOverflowException ex)
{
throw new Exception("[UniVRM Import Error] json parsing failed, nesting is too deep.\n" + ex);
}
catch
{
throw;
}
}
public virtual void ParseJson(string json, IStorage storage)
{
Json = json;
Storage = storage;
GLTF = GltfDeserializer.Deserialize(json.ParseAsJson());
if (GLTF.asset.version != "2.0")
{
throw new UniGLTFException("unknown gltf version {0}", GLTF.asset.version);
}
// Version Compatibility
RestoreOlderVersionValues();
FixUnique();
FixNodeName();
// parepare byte buffer
//GLTF.baseDir = System.IO.Path.GetDirectoryName(Path);
foreach (var buffer in GLTF.buffers)
{
buffer.OpenStorage(storage);
}
}
void FixUnique()
{
var used = new HashSet<string>();
foreach (var mesh in GLTF.meshes)
{
if (string.IsNullOrEmpty(mesh.name))
{
mesh.name = "mesh_" + Guid.NewGuid().ToString("N");
used.Add(mesh.name);
}
else
{
var lname = mesh.name.ToLower();
if (used.Contains(lname))
{
// rename
var uname = lname + "_" + Guid.NewGuid().ToString("N");
Debug.LogWarning($"same name: {lname} => {uname}");
mesh.name = uname;
lname = uname;
}
used.Add(lname);
}
}
}
/// <summary>
/// rename empty name to $"{index}"
/// </summary>
void FixNodeName()
{
for (var i = 0; i < GLTF.nodes.Count; ++i)
{
var node = GLTF.nodes[i];
if (string.IsNullOrWhiteSpace(node.name))
{
node.name = $"{i}";
}
}
}
void RestoreOlderVersionValues()
{
var parsed = UniJSON.JsonParser.Parse(Json);
for (int i = 0; i < GLTF.images.Count; ++i)
{
if (string.IsNullOrEmpty(GLTF.images[i].name))
{
try
{
var extraName = parsed["images"][i]["extra"]["name"].Value.GetString();
if (!string.IsNullOrEmpty(extraName))
{
//Debug.LogFormat("restore texturename: {0}", extraName);
GLTF.images[i].name = extraName;
}
}
catch (Exception)
{
// do nothing
}
}
}
}
#endregion
}
}

View File

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

View File

@ -3,8 +3,6 @@ using System.Linq;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text;
using UniJSON;
using UniGLTF.AltTask;
#if UNITY_EDITOR
using UnityEditor;
@ -43,247 +41,19 @@ namespace UniGLTF
TextureFactory m_textureFactory;
public TextureFactory TextureFactory => m_textureFactory;
public ImporterContext()
public ImporterContext(GltfParser parser)
{
}
#region Source
/// <summary>
/// JSON source
/// </summary>
public String Json;
/// <summary>
/// GLTF parsed from JSON
/// </summary>
public glTF GLTF; // parsed
public static bool IsGeneratedUniGLTFAndOlderThan(string generatorVersion, int major, int minor)
{
if (string.IsNullOrEmpty(generatorVersion)) return false;
if (generatorVersion == "UniGLTF") return true;
if (!generatorVersion.FastStartsWith("UniGLTF-")) return false;
try
{
var splitted = generatorVersion.Substring(8).Split('.');
var generatorMajor = int.Parse(splitted[0]);
var generatorMinor = int.Parse(splitted[1]);
if (generatorMajor < major)
{
return true;
}
else if (generatorMajor > major)
{
return false;
}
else
{
if (generatorMinor >= minor)
{
return false;
}
else
{
return true;
}
}
}
catch (Exception ex)
{
Debug.LogWarningFormat("{0}: {1}", generatorVersion, ex);
return false;
}
}
public bool IsGeneratedUniGLTFAndOlder(int major, int minor)
{
if (GLTF == null) return false;
if (GLTF.asset == null) return false;
return IsGeneratedUniGLTFAndOlderThan(GLTF.asset.generator, major, minor);
}
/// <summary>
/// URI access
/// </summary>
public IStorage Storage;
#endregion
#region Parse
public void Parse(string path)
{
Parse(path, File.ReadAllBytes(path));
}
/// <summary>
/// Parse gltf json or Parse json chunk of glb
/// </summary>
/// <param name="path"></param>
/// <param name="bytes"></param>
public virtual void Parse(string path, Byte[] bytes)
{
var ext = Path.GetExtension(path).ToLower();
switch (ext)
{
case ".gltf":
ParseJson(Encoding.UTF8.GetString(bytes), new FileSystemStorage(Path.GetDirectoryName(path)));
break;
case ".zip":
{
var zipArchive = Zip.ZipArchiveStorage.Parse(bytes);
var gltf = zipArchive.Entries.FirstOrDefault(x => x.FileName.ToLower().EndsWith(".gltf"));
if (gltf == null)
{
throw new Exception("no gltf in archive");
}
var jsonBytes = zipArchive.Extract(gltf);
var json = Encoding.UTF8.GetString(jsonBytes);
ParseJson(json, zipArchive);
}
break;
case ".glb":
ParseGlb(bytes);
break;
default:
throw new NotImplementedException();
}
}
/// <summary>
///
/// </summary>
/// <param name="bytes"></param>
public void ParseGlb(Byte[] bytes)
{
var chunks = glbImporter.ParseGlbChunks(bytes);
if (chunks.Count != 2)
{
throw new Exception("unknown chunk count: " + chunks.Count);
}
if (chunks[0].ChunkType != GlbChunkType.JSON)
{
throw new Exception("chunk 0 is not JSON");
}
if (chunks[1].ChunkType != GlbChunkType.BIN)
{
throw new Exception("chunk 1 is not BIN");
}
try
{
var jsonBytes = chunks[0].Bytes;
ParseJson(Encoding.UTF8.GetString(jsonBytes.Array, jsonBytes.Offset, jsonBytes.Count),
new SimpleStorage(chunks[1].Bytes));
}
catch (StackOverflowException ex)
{
throw new Exception("[UniVRM Import Error] json parsing failed, nesting is too deep.\n" + ex);
}
catch
{
throw;
}
}
public virtual void ParseJson(string json, IStorage storage)
{
Json = json;
Storage = storage;
GLTF = GltfDeserializer.Deserialize(json.ParseAsJson());
if (GLTF.asset.version != "2.0")
{
throw new UniGLTFException("unknown gltf version {0}", GLTF.asset.version);
}
m_parser = parser;
m_textureFactory = new TextureFactory(GLTF, Storage);
m_materialFactory = new MaterialFactory(GLTF, Storage);
// Version Compatibility
RestoreOlderVersionValues();
FixUnique();
FixNodeName();
// parepare byte buffer
//GLTF.baseDir = System.IO.Path.GetDirectoryName(Path);
foreach (var buffer in GLTF.buffers)
{
buffer.OpenStorage(storage);
}
}
void FixUnique()
{
var used = new HashSet<string>();
foreach (var mesh in GLTF.meshes)
{
if (string.IsNullOrEmpty(mesh.name))
{
mesh.name = "mesh_" + Guid.NewGuid().ToString("N");
used.Add(mesh.name);
}
else
{
var lname = mesh.name.ToLower();
if (used.Contains(lname))
{
// rename
var uname = lname + "_" + Guid.NewGuid().ToString("N");
Debug.LogWarning($"same name: {lname} => {uname}");
mesh.name = uname;
lname = uname;
}
used.Add(lname);
}
}
}
/// <summary>
/// rename empty name to $"{index}"
/// </summary>
void FixNodeName()
{
for (var i = 0; i < GLTF.nodes.Count; ++i)
{
var node = GLTF.nodes[i];
if (string.IsNullOrWhiteSpace(node.name))
{
node.name = $"{i}";
}
}
}
void RestoreOlderVersionValues()
{
var parsed = UniJSON.JsonParser.Parse(Json);
for (int i = 0; i < GLTF.images.Count; ++i)
{
if (string.IsNullOrEmpty(GLTF.images[i].name))
{
try
{
var extraName = parsed["images"][i]["extra"]["name"].Value.GetString();
if (!string.IsNullOrEmpty(extraName))
{
//Debug.LogFormat("restore texturename: {0}", extraName);
GLTF.images[i].name = extraName;
}
}
catch (Exception)
{
// do nothing
}
}
}
}
#region Source
GltfParser m_parser;
public GltfParser Parser => m_parser;
public String Json => m_parser.Json;
public glTF GLTF => m_parser.GLTF;
public IStorage Storage => m_parser.Storage;
#endregion
#region Load. Build unity objects

View File

@ -6,27 +6,28 @@ namespace UniGLTF
{
public static class ImporterContextExtensions
{
/// <summary>
/// ReadAllBytes, Parse, Create GameObject
/// </summary>
/// <param name="path">allbytes</param>
public static void Load(this ImporterContext self, string path)
{
var bytes = File.ReadAllBytes(path);
self.Load(path, bytes);
}
// /// <summary>
// /// ReadAllBytes, Parse, Create GameObject
// /// </summary>
// /// <param name="path">allbytes</param>
// public static void Load(this ImporterContext self, string path)
// {
// var bytes = File.ReadAllBytes(path);
// self.Load(path, bytes);
// }
/// <summary>
/// Parse, Create GameObject
/// </summary>
/// <param name="path">gltf or glb path</param>
/// <param name="bytes">allbytes</param>
public static void Load(this ImporterContext self, string path, byte[] bytes)
{
self.Parse(path, bytes);
self.Load();
self.Root.name = Path.GetFileNameWithoutExtension(path);
}
// /// <summary>
// /// Parse, Create GameObject
// /// </summary>
// /// <param name="path">gltf or glb path</param>
// /// <param name="bytes">allbytes</param>
// public static void Load(this ImporterContext self, string path, byte[] bytes)
// {
// var parser = new GltfParser();
// parser.Parse(path, bytes);
// self.Load(parser);
// self.Root.name = Path.GetFileNameWithoutExtension(path);
// }
/// <summary>
/// Build unity objects from parsed gltf

View File

@ -154,7 +154,7 @@ namespace UniGLTF
{
throw new Exception("different length");
}
if (ctx.IsGeneratedUniGLTFAndOlder(1, 16))
if (ctx.Parser.IsGeneratedUniGLTFAndOlder(1, 16))
{
#pragma warning disable 0612
// backward compatibility
@ -317,7 +317,7 @@ namespace UniGLTF
// uv
if (prim.attributes.TEXCOORD_0 != -1)
{
if (ctx.IsGeneratedUniGLTFAndOlder(1, 16))
if (ctx.Parser.IsGeneratedUniGLTFAndOlder(1, 16))
{
#pragma warning disable 0612
// backward compatibility

View File

@ -98,7 +98,7 @@ namespace UniGLTF
public void UniGLTFSimpleSceneTest()
{
var go = CreateSimpleScene();
var context = new ImporterContext();
ImporterContext context = default;
try
{
@ -117,9 +117,12 @@ namespace UniGLTF
json = gltf.ToJson();
}
// parse
var parser = new GltfParser();
parser.ParseJson(json, new SimpleStorage(new ArraySegment<byte>()));
// import
context.ParseJson(json, new SimpleStorage(new ArraySegment<byte>()));
//Debug.LogFormat("{0}", context.Json);
context = new ImporterContext(parser);
context.Load();
AssertAreEqual(go.transform, context.Root.transform);
@ -128,7 +131,10 @@ namespace UniGLTF
{
//Debug.LogFormat("Destroy, {0}", go.name);
GameObject.DestroyImmediate(go);
context.EditorDestroyRootAndAssets();
if (context != null)
{
context.EditorDestroyRootAndAssets();
}
}
}
@ -191,12 +197,12 @@ namespace UniGLTF
[Test]
public void VersionChecker()
{
Assert.False(ImporterContext.IsGeneratedUniGLTFAndOlderThan("hoge", 1, 16));
Assert.False(ImporterContext.IsGeneratedUniGLTFAndOlderThan("UniGLTF-1.16", 1, 16));
Assert.True(ImporterContext.IsGeneratedUniGLTFAndOlderThan("UniGLTF-1.15", 1, 16));
Assert.False(ImporterContext.IsGeneratedUniGLTFAndOlderThan("UniGLTF-11.16", 1, 16));
Assert.True(ImporterContext.IsGeneratedUniGLTFAndOlderThan("UniGLTF-0.16", 1, 16));
Assert.True(ImporterContext.IsGeneratedUniGLTFAndOlderThan("UniGLTF", 1, 16));
Assert.False(GltfParser.IsGeneratedUniGLTFAndOlderThan("hoge", 1, 16));
Assert.False(GltfParser.IsGeneratedUniGLTFAndOlderThan("UniGLTF-1.16", 1, 16));
Assert.True(GltfParser.IsGeneratedUniGLTFAndOlderThan("UniGLTF-1.15", 1, 16));
Assert.False(GltfParser.IsGeneratedUniGLTFAndOlderThan("UniGLTF-11.16", 1, 16));
Assert.True(GltfParser.IsGeneratedUniGLTFAndOlderThan("UniGLTF-0.16", 1, 16));
Assert.True(GltfParser.IsGeneratedUniGLTFAndOlderThan("UniGLTF", 1, 16));
}
[Test]
@ -439,7 +445,7 @@ namespace UniGLTF
public void SkinTestEmptyName()
{
var model = new glTFSkin()
{
{
name = "",
inverseBindMatrices = 4,
joints = new int[] { 1 },
@ -559,8 +565,9 @@ namespace UniGLTF
// import
{
var context = new ImporterContext();
context.ParseJson(json, new SimpleStorage(new ArraySegment<byte>(new byte[1024 * 1024])));
var parser = new GltfParser();
parser.ParseJson(json, new SimpleStorage(new ArraySegment<byte>(new byte[1024 * 1024])));
var context = new ImporterContext(parser);
//Debug.LogFormat("{0}", context.Json);
context.Load();
@ -577,9 +584,10 @@ namespace UniGLTF
// import new version
{
var context = new ImporterContext();
context.ParseJson(json, new SimpleStorage(new ArraySegment<byte>(new byte[1024 * 1024])));
var parser = new GltfParser();
parser.ParseJson(json, new SimpleStorage(new ArraySegment<byte>(new byte[1024 * 1024])));
//Debug.LogFormat("{0}", context.Json);
var context = new ImporterContext(parser);
context.Load();
var importedRed = context.Root.transform.GetChild(0);

View File

@ -42,8 +42,10 @@ namespace VRM.Samples
public void ImportExportTest()
{
var path = AliciaPath;
var context = new VRMImporterContext();
context.ParseGlb(File.ReadAllBytes(path));
var parser = new GltfParser();
parser.ParseGlb(File.ReadAllBytes(path));
var context = new VRMImporterContext(parser);
context.Load();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
@ -114,8 +116,9 @@ namespace VRM.Samples
public void MeshCopyTest()
{
var path = AliciaPath;
var context = new VRMImporterContext();
context.ParseGlb(File.ReadAllBytes(path));
var parser = new GltfParser();
parser.ParseGlb(File.ReadAllBytes(path));
var context = new VRMImporterContext(parser);
context.Load();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
@ -133,8 +136,9 @@ namespace VRM.Samples
{
// Aliciaを古いデシリアライザでロードする
var path = AliciaPath;
var context = new VRMImporterContext();
context.ParseGlb(File.ReadAllBytes(path));
var parser = new GltfParser();
parser.ParseGlb(File.ReadAllBytes(path));
var context = new VRMImporterContext(parser);
var oldJson = context.GLTF.ToJson().ParseAsJson().ToString(" ");
// 生成シリアライザでJSON化する

View File

@ -46,11 +46,11 @@ namespace VRM.Samples
var bytes = File.ReadAllBytes(path);
// なんらかの方法でByte列を得た
var context = new VRMImporterContext();
// GLB形式でJSONを取得しParseします
context.ParseGlb(bytes);
var parser = new GltfParser();
parser.ParseGlb(bytes);
var context = new VRMImporterContext(parser);
// metaを取得(todo: thumbnailテクスチャのロード)
var meta = await context.ReadMetaAsync();

View File

@ -1,14 +1,11 @@
#pragma warning disable 0414
using System.IO;
using UniGLTF;
using UniGLTF.AltTask;
using UnityEngine;
namespace VRM.Samples
{
public class VRMRuntimeLoader : MonoBehaviour
{
[SerializeField]
@ -89,33 +86,33 @@ namespace VRM.Samples
var bytes = File.ReadAllBytes(path);
// なんらかの方法でByte列を得た
var context = new VRMImporterContext();
// GLB形式でJSONを取得しParseします
context.ParseGlb(bytes);
var parser = new GltfParser();
parser.Parse(path, bytes);
var context = new VRMImporterContext(parser);
// metaを取得(todo: thumbnailテクスチャのロード)
var meta = await context.ReadMetaAsync();
Debug.LogFormat("meta: title:{0}", meta.Title);
// ParseしたJSONをシーンオブジェクトに変換していく
if (m_loadAsync)
{
await LoadAsync(context);
await context.LoadAsync();
}
else
{
context.LoadAsync();
OnLoaded(context);
context.Load();
}
OnLoaded(context);
}
/// <summary>
/// メタが不要な場合のローダー
/// </summary>
void LoadVRMClicked_without_meta()
async void LoadVRMClicked_without_meta()
{
#if UNITY_STANDALONE_WIN
var path = FileDialogForWindows.FileDialog("open VRM", ".vrm");
@ -129,55 +126,23 @@ namespace VRM.Samples
return;
}
#if true
var bytes = File.ReadAllBytes(path);
// なんらかの方法でByte列を得た
var context = new VRMImporterContext();
// GLB形式でJSONを取得しParseします
context.ParseGlb(bytes);
var parser = new GltfParser();
parser.ParseGlb(bytes);
var context = new VRMImporterContext(parser);
if (m_loadAsync)
{
// ローカルファイルシステムからロードします
LoadAsync(context);
await context.LoadAsync();
}
else
{
context.Load();
OnLoaded(context);
}
#else
// ParseしたJSONをシーンオブジェクトに変換していく
if (m_loadAsync)
{
// ローカルファイルシステムからロードします
VRMImporter.LoadVrmAsync(path, OnLoaded);
}
else
{
var root=VRMImporter.LoadFromPath(path);
OnLoaded(root);
}
#endif
}
async Awaitable LoadAsync(VRMImporterContext context)
{
#if true
var now = Time.time;
await context.LoadAsync();
var delta = Time.time - now;
Debug.LogFormat("LoadAsync {0:0.0} seconds", delta);
OnLoaded(context);
#else
// ローカルファイルシステムからロードします
VRMImporter.LoadVrmAsync(path, OnLoaded);
#endif
}
void LoadBVHClicked()

View File

@ -309,15 +309,15 @@ namespace VRM.Samples
{
case ".vrm":
{
var context = new VRMImporterContext();
var file = File.ReadAllBytes(path);
context.ParseGlb(file);
var parser = new GltfParser();
parser.ParseGlb(file);
var context = new VRMImporterContext(parser);
await m_texts.UpdateMetaAsync(context);
#if true
await context.LoadAsync();
#else
context.Load();
#endif
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
context.ShowMeshes();
@ -327,9 +327,11 @@ namespace VRM.Samples
case ".glb":
{
var context = new UniGLTF.ImporterContext();
var file = File.ReadAllBytes(path);
context.ParseGlb(file);
var parser = new GltfParser();
parser.ParseGlb(file);
var context = new UniGLTF.ImporterContext(parser);
context.Load();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
@ -341,8 +343,10 @@ namespace VRM.Samples
case ".gltf":
case ".zip":
{
var context = new UniGLTF.ImporterContext();
context.Parse(path);
var parser = new GltfParser();
parser.ParsePath(path);
var context = new UniGLTF.ImporterContext(parser);
context.Load();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();

View File

@ -20,8 +20,10 @@ namespace VRM
if (Application.isPlaying)
{
// load into scene
var context = new VRMImporterContext();
context.Load(path);
var parser = new GltfParser();
parser.ParsePath(path);
var context = new VRMImporterContext(parser);
context.Load();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
Selection.activeGameObject = context.Root;
@ -48,8 +50,9 @@ namespace VRM
// import as asset
var prefabPath = UnityPath.FromUnityPath(assetPath);
var context = new VRMImporterContext();
context.ParseGlb(File.ReadAllBytes(path));
var parser = new GltfParser();
parser.ParseGlb(File.ReadAllBytes(path));
var context = new VRMImporterContext(parser);
context.ExtractImages(prefabPath);
EditorApplication.delayCall += () =>

View File

@ -36,11 +36,11 @@ namespace VRM
{
throw new Exception();
}
var context = new VRMImporterContext();
var parser = new GltfParser();
try
{
context.ParseGlb(File.ReadAllBytes(path.FullPath));
parser.ParseGlb(File.ReadAllBytes(path.FullPath));
}
catch (KeyNotFoundException)
{
@ -52,6 +52,7 @@ namespace VRM
var prefabPath = path.Parent.Child(path.FileNameWithoutExtension + ".prefab");
// save texture assets !
var context = new VRMImporterContext(parser);
context.ExtractImages(prefabPath);
EditorApplication.delayCall += () =>

View File

@ -15,30 +15,8 @@ namespace VRM
{
public VRM.glTF_VRM_extensions VRM { get; private set; }
public VRMImporterContext()
public VRMImporterContext(GltfParser parser) : base(parser)
{
}
public override void Parse(string path, byte[] bytes)
{
var ext = Path.GetExtension(path).ToLower();
switch (ext)
{
case ".vrm":
ParseGlb(bytes);
break;
default:
base.Parse(path, bytes);
break;
}
}
public override void ParseJson(string json, IStorage storage)
{
// parse GLTF part(core + unlit, textureTransform, targetNames)
base.ParseJson(json, storage);
// parse VRM part
if (glTF_VRM_extensions.TryDeserilize(GLTF.extensions, out glTF_VRM_extensions vrm))
{

View File

@ -333,9 +333,11 @@ namespace UniVRM10.Samples
case ".glb":
{
var context = new UniGLTF.ImporterContext();
var file = File.ReadAllBytes(path);
context.ParseGlb(file);
var parser = new GltfParser();
parser.ParseGlb(file);
var context = new UniGLTF.ImporterContext(parser);
context.Load();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();
@ -347,8 +349,10 @@ namespace UniVRM10.Samples
case ".gltf":
case ".zip":
{
var context = new UniGLTF.ImporterContext();
context.Parse(path);
var parser = new GltfParser();
parser.ParsePath(path);
var context = new UniGLTF.ImporterContext(parser);
context.Load();
context.ShowMeshes();
context.EnableUpdateWhenOffscreen();