From fecbf3f0de5b3a43ec128ac3f52b8e98b849e5d5 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 27 Oct 2021 17:31:32 +0900 Subject: [PATCH 1/9] GltfData.GetBytesFromUri --- .../Runtime/UniGLTF/IO/ExportingGltfData.cs | 4 +- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 53 +++++++++++++++++-- .../UniGLTF/EditorTextureSerializerTests.cs | 2 +- Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs | 4 +- Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs | 9 +--- Assets/VRM/Tests/VRMTextureEnumerateTests.cs | 6 ++- 6 files changed, 60 insertions(+), 18 deletions(-) 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 { From 4f7043ffddce3cede5a372c67e0cb2986495d0fc Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 27 Oct 2021 17:58:35 +0900 Subject: [PATCH 2/9] Implement `data:` uri dispatch --- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index ffb08e768..5130311af 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -107,13 +107,28 @@ namespace UniGLTF ); } + Dictionary> _dataCache = new Dictionary>(); + public ArraySegment GetBytesFromUri(string uri) { if (string.IsNullOrEmpty(uri)) { throw new ArgumentNullException(); } - return _storage.Get(uri); + if (uri.StartsWith("data:", StringComparison.Ordinal)) + { + if (_dataCache.TryGetValue(uri, out ArraySegment data)) + { + return data; + } + data = new ArraySegment(UriByteBuffer.ReadEmbedded(uri)); + _dataCache.Add(uri, data); + return data; + } + else + { + return _storage.Get(uri); + } } public ArraySegment GetBytes(int bufferIndex) From 95c3c7c5d4b7793548cb549be4ab28f5390070f6 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 27 Oct 2021 18:08:32 +0900 Subject: [PATCH 3/9] rename GltfData buffer access methods --- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 60 +++++++++++-------- .../UniGLTF/EditorTextureSerializerTests.cs | 2 +- .../Vrm10TextureDescriptorGenerator.cs | 2 +- Assets/VRM10/Runtime/Migration/MeshUpdater.cs | 2 +- Assets/VRM10/Runtime/Migration/RotateY180.cs | 4 +- 5 files changed, 39 insertions(+), 31 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index 5130311af..1ced8012f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -131,7 +131,7 @@ namespace UniGLTF } } - public ArraySegment GetBytes(int bufferIndex) + public ArraySegment GetBytesFromBuffer(int bufferIndex) { var buffer = GLTF.buffers[bufferIndex]; if (bufferIndex == 0 && string.IsNullOrEmpty(buffer.uri)) @@ -150,44 +150,52 @@ namespace UniGLTF } } - public ArraySegment GetViewBytes(int bufferView) + public ArraySegment GetBytesFromBufferView(int bufferView) { var view = GLTF.bufferViews[bufferView]; - var segment = GetBytes(view.buffer); + var segment = GetBytesFromBuffer(view.buffer); return new ArraySegment(segment.Array, segment.Offset + view.byteOffset, view.byteLength); } - T[] GetAttrib(int count, int byteOffset, glTFBufferView view) where T : struct + T[] GetTypedFromBufferView(int count, int byteOffset, glTFBufferView view) where T : struct { - var segment = GetBytes(view.buffer); + var segment = GetBytesFromBuffer(view.buffer); var attrib = new T[count]; var bytes = new ArraySegment(segment.Array, segment.Offset + view.byteOffset + byteOffset, count * view.byteStride); bytes.MarshalCopyTo(attrib); return attrib; } - T[] GetAttrib(glTFAccessor accessor, glTFBufferView view) where T : struct + T[] GetTypedFromAccessor(glTFAccessor accessor, glTFBufferView view) where T : struct { - return GetAttrib(accessor.count, accessor.byteOffset, view); + return GetTypedFromBufferView(accessor.count, accessor.byteOffset, view); } - IEnumerable _GetIndices(glTFBufferView view, int count, int byteOffset, glComponentType componentType) + /// + /// for sparse + /// + /// + /// + /// + /// + /// + IEnumerable GetIntIndicesFromView(glTFBufferView view, int count, int byteOffset, glComponentType componentType) { switch (componentType) { case glComponentType.UNSIGNED_BYTE: { - return GetAttrib(count, byteOffset, view).Select(x => (int)(x)); + return GetTypedFromBufferView(count, byteOffset, view).Select(x => (int)(x)); } case glComponentType.UNSIGNED_SHORT: { - return GetAttrib(count, byteOffset, view).Select(x => (int)(x)); + return GetTypedFromBufferView(count, byteOffset, view).Select(x => (int)(x)); } case glComponentType.UNSIGNED_INT: { - return GetAttrib(count, byteOffset, view).Select(x => (int)(x)); + return GetTypedFromBufferView(count, byteOffset, view).Select(x => (int)(x)); } } throw new NotImplementedException("GetIndices: unknown componenttype: " + componentType); @@ -199,7 +207,7 @@ namespace UniGLTF /// /// /// - IEnumerable _GetIndices(glTFAccessor accessor, out int count) + IEnumerable GetIntIndicesFromAccessor(glTFAccessor accessor, out int count) { count = accessor.count; var view = GLTF.bufferViews[accessor.bufferView]; @@ -207,17 +215,17 @@ namespace UniGLTF { case glComponentType.UNSIGNED_BYTE: { - return GetAttrib(accessor, view).Select(x => (int)(x)); + return GetTypedFromAccessor(accessor, view).Select(x => (int)(x)); } case glComponentType.UNSIGNED_SHORT: { - return GetAttrib(accessor, view).Select(x => (int)(x)); + return GetTypedFromAccessor(accessor, view).Select(x => (int)(x)); } case glComponentType.UNSIGNED_INT: { - return GetAttrib(accessor, view).Select(x => (int)(x)); + return GetTypedFromAccessor(accessor, view).Select(x => (int)(x)); } } throw new NotImplementedException("GetIndices: unknown componenttype: " + accessor.componentType); @@ -226,7 +234,7 @@ namespace UniGLTF public int[] GetIndices(int accessorIndex) { int count; - var result = _GetIndices(GLTF.accessors[accessorIndex], out count); + var result = GetIntIndicesFromAccessor(GLTF.accessors[accessorIndex], out count); var indices = new int[count]; // flip triangles @@ -250,7 +258,7 @@ namespace UniGLTF if (vertexAccessor.count <= 0) return new T[] { }; var result = (vertexAccessor.bufferView != -1) - ? GetAttrib(vertexAccessor, GLTF.bufferViews[vertexAccessor.bufferView]) + ? GetTypedFromAccessor(vertexAccessor, GLTF.bufferViews[vertexAccessor.bufferView]) : new T[vertexAccessor.count] ; @@ -258,8 +266,8 @@ namespace UniGLTF if (sparse != null && sparse.count > 0) { // override sparse values - var indices = _GetIndices(GLTF.bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType); - var values = GetAttrib(sparse.count, sparse.values.byteOffset, GLTF.bufferViews[sparse.values.bufferView]); + var indices = GetIntIndicesFromView(GLTF.bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType); + var values = GetTypedFromBufferView(sparse.count, sparse.values.byteOffset, GLTF.bufferViews[sparse.values.bufferView]); var it = indices.GetEnumerator(); for (int i = 0; i < sparse.count; ++i) @@ -284,7 +292,7 @@ namespace UniGLTF { var attrib = new float[vertexAccessor.count * vertexAccessor.TypeCount]; var view = GLTF.bufferViews[vertexAccessor.bufferView]; - var segment = GetBytes(view.buffer); + var segment = GetBytesFromBuffer(view.buffer); var bytes = new ArraySegment(segment.Array, segment.Offset + view.byteOffset + vertexAccessor.byteOffset, vertexAccessor.count * view.byteStride); bytes.MarshalCopyTo(attrib); result = attrib; @@ -298,8 +306,8 @@ namespace UniGLTF if (sparse != null && sparse.count > 0) { // override sparse values - var indices = _GetIndices(GLTF.bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType); - var values = GetAttrib(sparse.count * vertexAccessor.TypeCount, sparse.values.byteOffset, GLTF.bufferViews[sparse.values.bufferView]); + var indices = GetIntIndicesFromView(GLTF.bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType); + var values = GetTypedFromBufferView(sparse.count * vertexAccessor.TypeCount, sparse.values.byteOffset, GLTF.bufferViews[sparse.values.bufferView]); var it = indices.GetEnumerator(); for (int i = 0; i < sparse.count; ++i) @@ -311,23 +319,23 @@ namespace UniGLTF return result; } - public ArraySegment GetImageBytes(int imageIndex) + public ArraySegment GetBytesFromImage(int imageIndex) { var image = GLTF.images[imageIndex]; if (string.IsNullOrEmpty(image.uri)) { - return GetViewBytes(image.bufferView); + return GetBytesFromBufferView(image.bufferView); } else { - return _storage.Get(image.uri); + return GetBytesFromUri(image.uri); } } public ArraySegment GetImageBytesFromTextureIndex(int textureIndex) { var imageIndex = GLTF.textures[textureIndex].source; - return GetImageBytes(imageIndex); + return GetBytesFromImage(imageIndex); } } } diff --git a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs index 50a969137..6580dbe54 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs @@ -146,7 +146,7 @@ namespace UniGLTF var parsed = GltfData.CreateFromGltfDataForTest(gltf, data.BinBytes); // Extract Image to Texture2D - var exportedBytes = parsed.GetViewBytes(exportedImage.bufferView).ToArray(); + var exportedBytes = parsed.GetBytesFromBufferView(exportedImage.bufferView).ToArray(); var exportedTexture = new Texture2D(2, 2, TextureFormat.ARGB32, mipChain: false, linear: false); Assert.IsTrue(exportedTexture.LoadImage(exportedBytes)); // Always true ? Assert.AreEqual(srcTex.width, exportedTexture.width); diff --git a/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs b/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs index ea1aaf321..c9de0bd5e 100644 --- a/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs +++ b/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs @@ -110,7 +110,7 @@ namespace UniVRM10 GetTextureBytesAsync getThumbnailImageBytesAsync = () => { - var bytes = data.GetImageBytes(imageIndex); + var bytes = data.GetBytesFromImage(imageIndex); return Task.FromResult(GltfTextureImporter.ToArray(bytes)); }; var texDesc = new TextureDescriptor(objectName, gltfImage.GetExt(), gltfImage.uri, Vector2.zero, Vector2.one, default, TextureImportTypes.sRGB, default, default, diff --git a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs index 86b6c5f66..b97c92740 100644 --- a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs +++ b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs @@ -69,7 +69,7 @@ namespace UniVRM10 // copy images foreach (var image in gltf.images) { - var bytes = _data.GetViewBytes(image.bufferView); + var bytes = _data.GetBytesFromBufferView(image.bufferView); image.bufferView = AddBuffer(bytes); } diff --git a/Assets/VRM10/Runtime/Migration/RotateY180.cs b/Assets/VRM10/Runtime/Migration/RotateY180.cs index fb7ca77c2..4ada62c1c 100644 --- a/Assets/VRM10/Runtime/Migration/RotateY180.cs +++ b/Assets/VRM10/Runtime/Migration/RotateY180.cs @@ -72,7 +72,7 @@ namespace UniVRM10 if (bufferViewIndex != -1) { - var buffer = data.GetViewBytes(bufferViewIndex); + var buffer = data.GetBytesFromBufferView(bufferViewIndex); var span = SpanLike.Wrap(buffer); for (int i = 0; i < span.Length; ++i) { @@ -113,7 +113,7 @@ namespace UniVRM10 if (used.Add(skin.inverseBindMatrices)) { var accessor = data.GLTF.accessors[skin.inverseBindMatrices]; - var buffer = data.GetViewBytes(accessor.bufferView); + var buffer = data.GetBytesFromBufferView(accessor.bufferView); var span = SpanLike.Wrap(buffer); for (int i = 0; i < span.Length; ++i) { From b8aad0cc63d9fa56ac20d1cbdaef7d773b831271 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 27 Oct 2021 19:01:42 +0900 Subject: [PATCH 4/9] IByteBuffer and glTFBuffer decoupling. --- .../UniGLTF/ExportDialog/GltfExportWindow.cs | 21 +++++---- .../Runtime/UniGLTF/Format/IStorage.cs | 4 -- .../Runtime/UniGLTF/Format/glTFBuffer.cs | 34 -------------- .../Runtime/UniGLTF/IO/ExportingGltfData.cs | 32 ++++++++++--- .../Runtime/UniGLTF/IO/FileSystemStorage.cs | 47 +------------------ Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 11 +---- .../UniGLTF/IO/Parser/GlbLowLevelParser.cs | 9 +--- .../IO/Parser/JsonWithStorageParser.cs | 32 ------------- .../IO/Parser/JsonWithStorageParser.cs.meta | 3 -- .../UniGLTF/Tests/UniGLTF/GlbParserTests.cs | 1 - .../Tests/UniGLTF/TextureEnumerateTests.cs | 2 +- Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs | 19 ++++---- .../VRM10/Runtime/IO/BufferAccessorAdapter.cs | 6 +-- Assets/VRM10/Runtime/IO/Vrm10Storage.cs | 31 +++++------- 14 files changed, 65 insertions(+), 187 deletions(-) delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/JsonWithStorageParser.cs delete mode 100644 Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/JsonWithStorageParser.cs.meta diff --git a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs index d567ef00a..c525a036b 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ExportDialog/GltfExportWindow.cs @@ -114,16 +114,19 @@ namespace UniGLTF } else { - var (json, buffers) = data.ToGltf(path); - // without BOM - var encoding = new System.Text.UTF8Encoding(false); - File.WriteAllText(path, json, encoding); - // write to local folder - var dir = Path.GetDirectoryName(path); - foreach (var b in buffers) + var (json, buffer0) = data.ToGltf(path); + { - var bufferPath = Path.Combine(dir, b.uri); - File.WriteAllBytes(bufferPath, b.GetBytes().ToArray()); + // write JSON without BOM + var encoding = new System.Text.UTF8Encoding(false); + File.WriteAllText(path, json, encoding); + } + + { + // write to buffer0 local folder + var dir = Path.GetDirectoryName(path); + var bufferPath = Path.Combine(dir, buffer0.uri); + File.WriteAllBytes(bufferPath, data.BinBytes.ToArray()); } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/IStorage.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/IStorage.cs index 4d4d65774..c215ccfcb 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/IStorage.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/IStorage.cs @@ -12,10 +12,6 @@ namespace UniGLTF /// 1. url による相対パス /// 2. url によるbase64 encoding /// 3. url がnullのときに bin chunk(buffers[0]) にアクセスする - /// - /// TODO: - /// 1. url による相対パス - /// 以外をやめて、呼び出し側で分岐させる。 /// /// /// diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs index ed5dbb166..1721a95b8 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTFBuffer.cs @@ -6,24 +6,6 @@ namespace UniGLTF [Serializable] public class glTFBuffer { - IBytesBuffer m_buffer; - public IBytesBuffer Buffer => m_buffer; - - public void OpenStorage(IStorage storage) - { - m_buffer = new ArraySegmentByteBuffer(storage.Get(uri)); - } - - public glTFBuffer() - { - - } - - public glTFBuffer(IBytesBuffer storage) - { - m_buffer = storage; - } - public string uri; [JsonSchema(Required = true, Minimum = 1)] @@ -33,22 +15,6 @@ namespace UniGLTF public glTFExtension extensions; public glTFExtension extras; public string name; - - public glTFBufferView Append(T[] array, glBufferTarget target) where T : struct - { - return Append(new ArraySegment(array), target); - } - public glTFBufferView Append(ArraySegment segment, glBufferTarget target) where T : struct - { - var view = m_buffer.Extend(segment, target); - byteLength = m_buffer.Bytes.Count; - return view; - } - - public ArraySegment GetBytes() - { - return m_buffer.Bytes; - } } [Serializable] diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs index f4d483684..b853835f4 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs @@ -11,17 +11,37 @@ namespace UniGLTF public glTF GLTF => _gltf; + protected IBytesBuffer _buffer; + /// + /// bin chunk + /// + public ArraySegment BinBytes => _buffer.Bytes; + public ExportingGltfData(int reserved = default) { if (reserved == 0) { reserved = 50 * 1024 * 1024; } - // glb body と gltf の bin 兼用 - _gltf.buffers.Add(new glTFBuffer(new ArrayByteBuffer(new byte[reserved]))); + + // buffers[0] is export target + _gltf.buffers.Add(new glTFBuffer()); + _buffer = new ArrayByteBuffer(new byte[reserved]); } #region Buffer management for export + public glTFBufferView Append(ArraySegment segment, glBufferTarget target) where T : struct + { + var view = _buffer.Extend(segment, target); + GLTF.buffers[0].byteLength = _buffer.Bytes.Count; + return view; + } + + public glTFBufferView Append(T[] array, glBufferTarget target) where T : struct + { + return Append(new ArraySegment(array), target); + } + public int ExtendBufferAndGetViewIndex( ArraySegment array, glBufferTarget target = glBufferTarget.NONE) where T : struct @@ -30,7 +50,7 @@ namespace UniGLTF { return -1; } - var view = _gltf.buffers[0].Append(array, target); + var view = Append(array, target); var viewIndex = _gltf.bufferViews.Count; _gltf.bufferViews.Add(view); return viewIndex; @@ -185,8 +205,6 @@ namespace UniGLTF return f.ToString(); } - public ArraySegment BinBytes => _gltf.buffers[0].GetBytes(); - /// /// GLBバイト列 /// @@ -207,7 +225,7 @@ namespace UniGLTF /// /// /// - public (string, List) ToGltf(string gltfPath) + public (string, glTFBuffer) ToGltf(string gltfPath) { // fix buffer path if (_gltf.buffers.Count == 1) @@ -224,7 +242,7 @@ namespace UniGLTF GltfSerializer.Serialize(f, _gltf); var json = f.ToString().ParseAsJson().ToString(" "); RemoveUnusedExtensions(_gltf, json); - return (json, _gltf.buffers); + return (json, _gltf.buffers[0]); } #endregion } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/FileSystemStorage.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/FileSystemStorage.cs index 748714d8b..285754ff9 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/FileSystemStorage.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/FileSystemStorage.cs @@ -1,31 +1,8 @@ using System; -using System.Collections.Generic; using System.IO; namespace UniGLTF { - /// - /// Implement bin chunk access - /// - public class SimpleStorage : IStorage - { - ArraySegment m_bytes; - - public SimpleStorage() : this(new ArraySegment()) - { - } - - public SimpleStorage(ArraySegment bytes) - { - m_bytes = bytes; - } - - public ArraySegment Get(string url) - { - return m_bytes; - } - } - /// /// Implement url that represnet relative path /// @@ -40,30 +17,8 @@ namespace UniGLTF public ArraySegment Get(string url) { - var bytes = - (url.FastStartsWith("data:")) - ? UriByteBuffer.ReadEmbedded(url) - : File.ReadAllBytes(Path.Combine(m_root, url)) - ; + var bytes = File.ReadAllBytes(Path.Combine(m_root, url)); return new ArraySegment(bytes); } } - - /// - /// for UnitTest - /// - public sealed class GltfStorage : IStorage - { - glTF _gltf; - - public GltfStorage(glTF gltf) - { - _gltf = gltf; - } - - public ArraySegment Get(string url) - { - return _gltf.buffers[0].GetBytes(); - } - } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index 1ced8012f..101fa5f45 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -85,15 +85,6 @@ namespace UniGLTF public static GltfData CreateFromGltfDataForTest(glTF gltf, ArraySegment bytes) { - IStorage storage = null; - if (bytes.Array != null) - { - storage = new SimpleStorage(bytes); - } - else - { - storage = new GltfStorage(gltf); - } return new GltfData( string.Empty, string.Empty, @@ -102,7 +93,7 @@ namespace UniGLTF new GlbChunk(), // json GlbChunk.CreateBin(bytes), }, - storage, + default, new MigrationFlags() ); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs index a0ecdfd51..bdb31f01d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/GlbLowLevelParser.cs @@ -35,7 +35,7 @@ namespace UniGLTF _path, Encoding.UTF8.GetString(jsonBytes.Array, jsonBytes.Offset, jsonBytes.Count), chunks, - new SimpleStorage(chunks[1].Bytes), + default, new MigrationFlags() ); } @@ -92,13 +92,6 @@ namespace UniGLTF FixNodeName(GLTF); FixAnimationNameUnique(GLTF); - // parepare byte buffer - //GLTF.baseDir = System.IO.Path.GetDirectoryName(Path); - foreach (var buffer in GLTF.buffers) - { - buffer.OpenStorage(storage); - } - return new GltfData(path, json, GLTF, chunks, storage, migrationFlags); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/JsonWithStorageParser.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/JsonWithStorageParser.cs deleted file mode 100644 index 5037dd55b..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/JsonWithStorageParser.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.Collections.Generic; - -namespace UniGLTF -{ - /// - /// For unit tests. - /// JSON string with storage parser. - /// - public sealed class JsonWithStorageParser - { - private readonly string _json; - private readonly IStorage _storage; - - public JsonWithStorageParser(string json, IStorage storage = null) - { - _json = json; - _storage = storage ?? new SimpleStorage(new ArraySegment()); - } - - public GltfData Parse() - { - return GlbLowLevelParser.ParseGltf( - string.Empty, - _json, - new List(), - _storage, - new MigrationFlags() - ); - } - } -} \ No newline at end of file diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/JsonWithStorageParser.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/JsonWithStorageParser.cs.meta deleted file mode 100644 index 5437e1b24..000000000 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/Parser/JsonWithStorageParser.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: c103da8b4a2b4050be09e7bfce2769fd -timeCreated: 1624803637 \ No newline at end of file diff --git a/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs b/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs index f8265df94..3acbc4a65 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/GlbParserTests.cs @@ -11,7 +11,6 @@ namespace UniGLTF var data = new ExportingGltfData(); var gltf = data.GLTF; gltf.asset.version = "2.0"; - gltf.buffers.Add(new glTFBuffer(new ArrayByteBuffer(Array.Empty()))); gltf.textures.Add(new glTFTexture { name = "FooBar", diff --git a/Assets/UniGLTF/Tests/UniGLTF/TextureEnumerateTests.cs b/Assets/UniGLTF/Tests/UniGLTF/TextureEnumerateTests.cs index 25765b3ef..b40ebce51 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/TextureEnumerateTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/TextureEnumerateTests.cs @@ -235,7 +235,7 @@ namespace UniGLTF string.Empty, gltf, new List(), - new SimpleStorage(new ArraySegment()), + default, new MigrationFlags() ); } diff --git a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs index 2ed1ed1a5..4e305cada 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs @@ -115,7 +115,7 @@ namespace UniGLTF } // parse - var parsed = new JsonWithStorageParser(json).Parse(); + var parsed = GltfData.CreateFromExport(data); // import using (var context = new ImporterContext(parsed)) @@ -129,7 +129,8 @@ namespace UniGLTF { var initBytes = init == 0 ? null : new byte[init]; var storage = new ArrayByteBuffer(initBytes); - var buffer = new glTFBuffer(storage); + var data = new ExportingGltfData(); + // var buffer = new glTFBuffer(storage); var values = new List(); int offset = 0; @@ -139,11 +140,11 @@ namespace UniGLTF values.AddRange(nums); var bytes = new ArraySegment(nums); offset += x; - buffer.Append(bytes, glBufferTarget.NONE); + data.Append(bytes, glBufferTarget.NONE); } - Assert.AreEqual(values.Count, buffer.byteLength); - Assert.True(Enumerable.SequenceEqual(values, buffer.GetBytes().ToArray())); + Assert.AreEqual(values.Count, data.GLTF.buffers[0].byteLength); + Assert.True(Enumerable.SequenceEqual(values, data.BinBytes.ToArray())); } [Test] @@ -623,9 +624,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()) { @@ -689,9 +688,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()) { diff --git a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs b/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs index 9b009aa49..d68e414fa 100644 --- a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs +++ b/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs @@ -39,7 +39,7 @@ namespace UniVRM10 count = self.Count; } var slice = self.Bytes.Slice(offset * stride, count * stride); - return storage.AppendToBuffer(bufferIndex, slice); + return storage.AppendToBuffer(slice); } static glTFAccessor CreateGltfAccessor(this VrmLib.BufferAccessor self, @@ -116,8 +116,8 @@ namespace UniVRM10 sparseValueSpan[i] = value; } - var sparseIndexView = storage.AppendToBuffer(bufferIndex, sparseIndexBin); - var sparseValueView = storage.AppendToBuffer(bufferIndex, sparseValueBin); + var sparseIndexView = storage.AppendToBuffer(sparseIndexBin); + var sparseValueView = storage.AppendToBuffer(sparseValueBin); var accessorIndex = storage.Gltf.accessors.Count; var accessor = new glTFAccessor diff --git a/Assets/VRM10/Runtime/IO/Vrm10Storage.cs b/Assets/VRM10/Runtime/IO/Vrm10Storage.cs index 3bccfbc21..c79730938 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Storage.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Storage.cs @@ -15,8 +15,6 @@ namespace UniVRM10 UniGLTF.GltfData m_data; public UniGLTF.glTF Gltf => m_data.GLTF; - public List Buffers; - public UniGLTF.Extensions.VRMC_vrm.VRMC_vrm gltfVrm; public UniGLTF.Extensions.VRMC_springBone.VRMC_springBone gltfVrmSpringBone; @@ -31,13 +29,9 @@ namespace UniVRM10 string.Empty, GLTF, new List(), - new SimpleStorage(new ArraySegment()), + default, new MigrationFlags() ); - Buffers = new List() - { - new UniGLTF.ArrayByteBuffer() - }; } /// @@ -61,20 +55,17 @@ namespace UniVRM10 gltfVrmSpringBone = springBone; } - Buffers = new List() - { - Gltf.buffers[0].Buffer, - }; + _buffer = new ArraySegmentByteBuffer(data.Bin); } public void Reserve(int bytesLength) { - Buffers[0].ExtendCapacity(bytesLength); + _buffer.ExtendCapacity(bytesLength); } - public int AppendToBuffer(int bufferIndex, ArraySegment segment) + public int AppendToBuffer(ArraySegment segment) { - var gltfBufferView = Buffers[bufferIndex].Extend(segment); + var gltfBufferView = _buffer.Extend(segment); var viewIndex = Gltf.bufferViews.Count; Gltf.bufferViews.Add(gltfBufferView); return viewIndex; @@ -138,7 +129,7 @@ namespace UniVRM10 var view = Gltf.bufferViews[bufferViewIndex]; if (view.buffer.TryGetValidIndex(Gltf.buffers.Count, out int bufferIndex)) { - var buffer = Buffers[bufferIndex]; + var buffer = _buffer; var bin = buffer.Bytes; var byteSize = accessor.CalcByteSize(); bytes = bin.Slice(view.byteOffset, view.byteLength).Slice(accessor.byteOffset, byteSize); @@ -480,18 +471,22 @@ namespace UniVRM10 public ArraySegment GetBufferBytes(UniGLTF.glTFBuffer buffer) { int index = Gltf.buffers.IndexOf(buffer); - return Buffers[index].Bytes; + if (index != 0) + { + throw new NotImplementedException(); + } + return _buffer.Bytes; } public byte[] ToBytes() { - Gltf.buffers[0].byteLength = Buffers[0].Bytes.Count; + Gltf.buffers[0].byteLength = _buffer.Bytes.Count; var f = new JsonFormatter(); UniGLTF.GltfSerializer.Serialize(f, Gltf); var json = f.GetStoreBytes(); - var glb = UniGLTF.Glb.Create(json, Buffers[0].Bytes); + var glb = UniGLTF.Glb.Create(json, _buffer.Bytes); return glb.ToBytes(); } } From 4b98e1e868e23cac6837c71db38ce88e814be5bd Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 27 Oct 2021 20:19:50 +0900 Subject: [PATCH 5/9] ExportingGltfData.GLTF --- .../Runtime/UniGLTF/IO/ExportingGltfData.cs | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs index b853835f4..8327e1e21 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs @@ -7,9 +7,7 @@ namespace UniGLTF { public class ExportingGltfData { - readonly glTF _gltf = new glTF(); - - public glTF GLTF => _gltf; + public glTF GLTF { get; } = new glTF(); protected IBytesBuffer _buffer; /// @@ -25,7 +23,7 @@ namespace UniGLTF } // buffers[0] is export target - _gltf.buffers.Add(new glTFBuffer()); + GLTF.buffers.Add(new glTFBuffer()); _buffer = new ArrayByteBuffer(new byte[reserved]); } @@ -51,8 +49,8 @@ namespace UniGLTF return -1; } var view = Append(array, target); - var viewIndex = _gltf.bufferViews.Count; - _gltf.bufferViews.Add(view); + var viewIndex = GLTF.bufferViews.Count; + GLTF.bufferViews.Add(view); return viewIndex; } @@ -74,10 +72,10 @@ namespace UniGLTF var viewIndex = ExtendBufferAndGetViewIndex(array, target); // index buffer's byteStride is unnecessary - _gltf.bufferViews[viewIndex].byteStride = 0; + GLTF.bufferViews[viewIndex].byteStride = 0; - var accessorIndex = _gltf.accessors.Count; - _gltf.accessors.Add(new glTFAccessor + var accessorIndex = GLTF.accessors.Count; + GLTF.accessors.Add(new glTFAccessor { bufferView = viewIndex, byteOffset = 0, @@ -118,8 +116,8 @@ namespace UniGLTF return -1; } var sparseValuesViewIndex = ExtendBufferAndGetViewIndex(sparseValues, target); - var accessorIndex = _gltf.accessors.Count; - _gltf.accessors.Add(new glTFAccessor + var accessorIndex = GLTF.accessors.Count; + GLTF.accessors.Add(new glTFAccessor { byteOffset = 0, componentType = glTFExtensions.GetComponentType(), @@ -212,11 +210,11 @@ namespace UniGLTF public byte[] ToGlbBytes() { var f = new JsonFormatter(); - GltfSerializer.Serialize(f, _gltf); + GltfSerializer.Serialize(f, GLTF); // remove unused extenions var json = f.ToString().ParseAsJson().ToString(" "); - RemoveUnusedExtensions(_gltf, json); + RemoveUnusedExtensions(GLTF, json); return Glb.Create(json, BinBytes).ToBytes(); } @@ -228,10 +226,10 @@ namespace UniGLTF public (string, glTFBuffer) ToGltf(string gltfPath) { // fix buffer path - if (_gltf.buffers.Count == 1) + if (GLTF.buffers.Count == 1) { var withoutExt = Path.GetFileNameWithoutExtension(gltfPath); - _gltf.buffers[0].uri = $"{withoutExt}.bin"; + GLTF.buffers[0].uri = $"{withoutExt}.bin"; } else { @@ -239,10 +237,10 @@ namespace UniGLTF } var f = new JsonFormatter(); - GltfSerializer.Serialize(f, _gltf); + GltfSerializer.Serialize(f, GLTF); var json = f.ToString().ParseAsJson().ToString(" "); - RemoveUnusedExtensions(_gltf, json); - return (json, _gltf.buffers[0]); + RemoveUnusedExtensions(GLTF, json); + return (json, GLTF.buffers[0]); } #endregion } From 14d420e6a4b07d1473bd7a7ffad474fbdb3f0f3a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 27 Oct 2021 20:22:00 +0900 Subject: [PATCH 6/9] Append to ExtendBufferAndGetView --- Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs | 9 ++------- Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs | 2 +- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs index 8327e1e21..c08ee784b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs @@ -28,18 +28,13 @@ namespace UniGLTF } #region Buffer management for export - public glTFBufferView Append(ArraySegment segment, glBufferTarget target) where T : struct + public glTFBufferView ExtendBufferAndGetView(ArraySegment segment, glBufferTarget target) where T : struct { var view = _buffer.Extend(segment, target); GLTF.buffers[0].byteLength = _buffer.Bytes.Count; return view; } - public glTFBufferView Append(T[] array, glBufferTarget target) where T : struct - { - return Append(new ArraySegment(array), target); - } - public int ExtendBufferAndGetViewIndex( ArraySegment array, glBufferTarget target = glBufferTarget.NONE) where T : struct @@ -48,7 +43,7 @@ namespace UniGLTF { return -1; } - var view = Append(array, target); + var view = ExtendBufferAndGetView(array, target); var viewIndex = GLTF.bufferViews.Count; GLTF.bufferViews.Add(view); return viewIndex; diff --git a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs index 4e305cada..fea2221af 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs @@ -140,7 +140,7 @@ namespace UniGLTF values.AddRange(nums); var bytes = new ArraySegment(nums); offset += x; - data.Append(bytes, glBufferTarget.NONE); + data.ExtendBufferAndGetView(bytes, glBufferTarget.NONE); } Assert.AreEqual(values.Count, data.GLTF.buffers[0].byteLength); From 48561c77ef851892e0c60178c808deb11ea7cc06 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 27 Oct 2021 20:23:59 +0900 Subject: [PATCH 7/9] CreateFromExportForTest --- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index 101fa5f45..d68737ea9 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -78,7 +78,7 @@ namespace UniGLTF MigrationFlags = migrationFlags; } - public static GltfData CreateFromExport(ExportingGltfData data) + public static GltfData CreateFromExportForTest(ExportingGltfData data) { return CreateFromGltfDataForTest(data.GLTF, data.BinBytes); } From 4ea2115c8dd6a6bb961f3909cf8419ed9b8c0f8f Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 27 Oct 2021 20:25:36 +0900 Subject: [PATCH 8/9] =?UTF-8?q?=E6=94=B9=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index d68737ea9..ab584aad2 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -89,7 +89,8 @@ namespace UniGLTF string.Empty, string.Empty, gltf, - new List{ + new List + { new GlbChunk(), // json GlbChunk.CreateBin(bytes), }, From 13ff1fdb454effde68c2b957d4a62816a3332fda Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 27 Oct 2021 20:35:16 +0900 Subject: [PATCH 9/9] =?UTF-8?q?=E9=A0=86=E7=95=AA=E3=80=81=E5=90=8D?= =?UTF-8?q?=E5=89=8D=E8=AA=BF=E6=95=B4=E3=80=82ForTest=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 20 +++++++++---------- Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs | 10 +++++----- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index ab584aad2..fb6107190 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -58,16 +58,18 @@ namespace UniGLTF } } - /// - /// URI access - /// - public IStorage _storage; - /// /// Migration Flags used by ImporterContext /// public MigrationFlags MigrationFlags { get; } + /// + /// URI access + /// + IStorage _storage; + + Dictionary> _dataUriCache = new Dictionary>(); + public GltfData(string targetPath, string json, glTF gltf, IReadOnlyList chunks, IStorage storage, MigrationFlags migrationFlags) { TargetPath = targetPath; @@ -89,7 +91,7 @@ namespace UniGLTF string.Empty, string.Empty, gltf, - new List + new List { new GlbChunk(), // json GlbChunk.CreateBin(bytes), @@ -99,8 +101,6 @@ namespace UniGLTF ); } - Dictionary> _dataCache = new Dictionary>(); - public ArraySegment GetBytesFromUri(string uri) { if (string.IsNullOrEmpty(uri)) @@ -109,12 +109,12 @@ namespace UniGLTF } if (uri.StartsWith("data:", StringComparison.Ordinal)) { - if (_dataCache.TryGetValue(uri, out ArraySegment data)) + if (_dataUriCache.TryGetValue(uri, out ArraySegment data)) { return data; } data = new ArraySegment(UriByteBuffer.ReadEmbedded(uri)); - _dataCache.Add(uri, data); + _dataUriCache.Add(uri, data); return data; } else diff --git a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs index fea2221af..0a90b7d38 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/UniGLTFTests.cs @@ -115,7 +115,7 @@ namespace UniGLTF } // parse - var parsed = GltfData.CreateFromExport(data); + var parsed = GltfData.CreateFromExportForTest(data); // import using (var context = new ImporterContext(parsed)) @@ -554,7 +554,7 @@ namespace UniGLTF // import { - var parsed = GltfData.CreateFromExport(data); + var parsed = GltfData.CreateFromExportForTest(data); using (var context = new ImporterContext(parsed)) using (var loaded = context.Load()) { @@ -572,7 +572,7 @@ namespace UniGLTF // import new version { - var parsed = GltfData.CreateFromExport(data); + var parsed = GltfData.CreateFromExportForTest(data); using (var context = new ImporterContext(parsed)) using (var loaded = context.Load()) { @@ -624,7 +624,7 @@ namespace UniGLTF // import { - var parsed = GltfData.CreateFromExport(data); + var parsed = GltfData.CreateFromExportForTest(data); using (var context = new ImporterContext(parsed)) using (var loaded = context.Load()) { @@ -688,7 +688,7 @@ namespace UniGLTF // import { - var parsed = GltfData.CreateFromExport(data); + var parsed = GltfData.CreateFromExportForTest(data); using (var context = new ImporterContext(parsed)) using (var loaded = context.Load()) {