diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs index faed324c7..f4d483684 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs @@ -185,6 +185,8 @@ namespace UniGLTF return f.ToString(); } + public ArraySegment BinBytes => _gltf.buffers[0].GetBytes(); + /// /// GLBバイト列 /// @@ -197,7 +199,7 @@ namespace UniGLTF // remove unused extenions var json = f.ToString().ParseAsJson().ToString(" "); RemoveUnusedExtensions(_gltf, json); - return Glb.Create(json, _gltf.buffers[0].GetBytes()).ToBytes(); + return Glb.Create(json, BinBytes).ToBytes(); } /// diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index 64c35f7ee..ffb08e768 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -42,7 +42,21 @@ namespace UniGLTF /// > This chunk MUST be the second chunk of the Binary glTF asset /// /// - public ArraySegment Bin => Chunks[1].Bytes; + public ArraySegment Bin + { + get + { + if (Chunks == null) + { + return default; + } + if (Chunks.Count < 2) + { + return default; + } + return Chunks[1].Bytes; + } + } /// /// URI access @@ -64,7 +78,12 @@ namespace UniGLTF MigrationFlags = migrationFlags; } - public static GltfData CreateFromGltfDataForTest(glTF gltf, ArraySegment bytes = default) + public static GltfData CreateFromExport(ExportingGltfData data) + { + return CreateFromGltfDataForTest(data.GLTF, data.BinBytes); + } + + public static GltfData CreateFromGltfDataForTest(glTF gltf, ArraySegment bytes) { IStorage storage = null; if (bytes.Array != null) @@ -79,17 +98,41 @@ namespace UniGLTF string.Empty, string.Empty, gltf, - new List(), + new List{ + new GlbChunk(), // json + GlbChunk.CreateBin(bytes), + }, storage, new MigrationFlags() ); } + public ArraySegment GetBytesFromUri(string uri) + { + if (string.IsNullOrEmpty(uri)) + { + throw new ArgumentNullException(); + } + return _storage.Get(uri); + } + public ArraySegment GetBytes(int bufferIndex) { - // TODO: var buffer = GLTF.buffers[bufferIndex]; - return _storage.Get(buffer.uri); + if (bufferIndex == 0 && string.IsNullOrEmpty(buffer.uri)) + { + // https://www.khronos.org/registry/glTF/specs/2.0/glTF-2.0.html#glb-stored-buffer + // this buffer is reference bin chunk + if (Bin.Array == null) + { + throw new NullReferenceException(); + } + return Bin; + } + else + { + return GetBytesFromUri(buffer.uri); + } } public ArraySegment GetViewBytes(int bufferView) diff --git a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs index 463da1c2e..50a969137 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs @@ -143,7 +143,7 @@ namespace UniGLTF UnityEngine.Object.DestroyImmediate(mat); UnityEngine.Object.DestroyImmediate(root); - var parsed = GltfData.CreateFromGltfDataForTest(gltf); + var parsed = GltfData.CreateFromGltfDataForTest(gltf, data.BinBytes); // Extract Image to Texture2D var exportedBytes = parsed.GetViewBytes(exportedImage.bufferView).ToArray(); diff --git a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs index 7d75ea89f..0337f0bd8 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs @@ -137,7 +137,7 @@ namespace UniGLTF : MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings) ; - var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF); + var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes); { var indices = parsed.GetIndices(gltfMesh.primitives[0].indices); @@ -186,7 +186,7 @@ namespace UniGLTF : MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings) ; - var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF); + var parsed = GltfData.CreateFromGltfDataForTest(data.GLTF, data.BinBytes); { var indices = parsed.GetIndices(gltfMesh.primitives[0].indices); Assert.AreEqual(0, indices[0]); diff --git a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs index d521af24b..2ed1ed1a5 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs @@ -553,9 +553,7 @@ namespace UniGLTF // import { - var storage = new SimpleStorage(new ArraySegment(new byte[1024 * 1024])); - var parsed = new JsonWithStorageParser(json, storage).Parse(); - + var parsed = GltfData.CreateFromExport(data); using (var context = new ImporterContext(parsed)) using (var loaded = context.Load()) { @@ -573,10 +571,7 @@ namespace UniGLTF // import new version { - var storage = new SimpleStorage(new ArraySegment(new byte[1024 * 1024])); - var parsed = new JsonWithStorageParser(json, storage).Parse(); - - //Debug.LogFormat("{0}", context.Json); + var parsed = GltfData.CreateFromExport(data); using (var context = new ImporterContext(parsed)) using (var loaded = context.Load()) { diff --git a/Assets/VRM/Tests/VRMTextureEnumerateTests.cs b/Assets/VRM/Tests/VRMTextureEnumerateTests.cs index d7fad4150..760d6106f 100644 --- a/Assets/VRM/Tests/VRMTextureEnumerateTests.cs +++ b/Assets/VRM/Tests/VRMTextureEnumerateTests.cs @@ -58,7 +58,8 @@ namespace VRM } }, } - } + }, + default ); var vrm = new glTF_VRM_extensions { @@ -119,7 +120,8 @@ namespace VRM } }, } - } + }, + default ); var vrm = new glTF_VRM_extensions {