From 758a9ffff92103eb77c9f17fca129b95171becdf Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 26 Oct 2021 00:39:06 +0900 Subject: [PATCH] IStorage --- .../ScriptedImporter/TextureExtractor.cs | 1 - .../Runtime/Extensions/glTFExtensions.cs | 18 ------- Assets/UniGLTF/Runtime/UniGLTF/Format/glTF.cs | 7 --- .../Runtime/UniGLTF/IO/FileSystemStorage.cs | 21 ++++++++ Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 54 ++++++++++++++++--- .../Runtime/UniGLTF/IO/ImporterContext.cs | 1 - .../IO/TextureIO/GltfTextureImporter.cs | 10 ++-- .../UniGLTF/EditorTextureSerializerTests.cs | 4 +- Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs | 5 +- .../Vrm10TextureDescriptorGenerator.cs | 2 +- Assets/VRM10/Runtime/Migration/MeshUpdater.cs | 2 +- Assets/VRM10/Runtime/Migration/RotateY180.cs | 26 ++++----- 12 files changed, 94 insertions(+), 57 deletions(-) diff --git a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs index d59c81b3c..3824ccd7d 100644 --- a/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs +++ b/Assets/UniGLTF/Editor/UniGLTF/ScriptedImporter/TextureExtractor.cs @@ -17,7 +17,6 @@ namespace UniGLTF public GltfData Data => m_data; public glTF GLTF => m_data.GLTF; - public IStorage Storage => m_data.Storage; public readonly Dictionary Textures = new Dictionary(); private readonly IReadOnlyDictionary m_subAssets; diff --git a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs index 8bf042ba8..674801c6d 100644 --- a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs +++ b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs @@ -246,24 +246,6 @@ namespace UniGLTF - public static ArraySegment GetImageBytesFromTextureIndex(this glTF self, IStorage storage, int textureIndex) - { - var imageIndex = self.textures[textureIndex].source; - return self.GetImageBytes(storage, imageIndex); - } - - public static ArraySegment GetImageBytes(this glTF self, IStorage storage, int imageIndex) - { - var image = self.images[imageIndex]; - if (string.IsNullOrEmpty(image.uri)) - { - return self.GetViewBytes(image.bufferView); - } - else - { - return storage.Get(image.uri); - } - } static Utf8String s_extensions = Utf8String.From("extensions"); diff --git a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTF.cs b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTF.cs index 73aef408a..6cc4a0912 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/Format/glTF.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/Format/glTF.cs @@ -40,13 +40,6 @@ namespace UniGLTF [JsonSchema(MinItems = 1, ExplicitIgnorableItemLength = 0)] public List accessors = new List(); - - public ArraySegment GetViewBytes(int bufferView) - { - var view = bufferViews[bufferView]; - var segment = buffers[view.buffer].GetBytes(); - return new ArraySegment(segment.Array, segment.Offset + view.byteOffset, view.byteLength); - } #endregion [JsonSchema(MinItems = 1, ExplicitIgnorableItemLength = 0)] diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/FileSystemStorage.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/FileSystemStorage.cs index 99961fea8..d325bdcda 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/FileSystemStorage.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/FileSystemStorage.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; namespace UniGLTF @@ -58,4 +59,24 @@ namespace UniGLTF } } } + + public class GltfStorage : IStorage + { + glTF _gltf; + + public GltfStorage(glTF gltf) + { + _gltf = gltf; + } + + public ArraySegment Get(string url) + { + return _gltf.buffers[0].GetBytes(); + } + + public string GetPath(string url) + { + return null; + } + } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index b6a622717..886dcbbf9 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -50,7 +50,7 @@ namespace UniGLTF /// /// URI access /// - public IStorage Storage { get; } + IStorage _storage; /// /// Migration Flags used by ImporterContext @@ -63,27 +63,50 @@ namespace UniGLTF Json = json; GLTF = gltf; Chunks = chunks; - Storage = storage; + _storage = storage; MigrationFlags = migrationFlags; } - public static GltfData CreateFromGltfDataForTest(glTF gltf) + public static GltfData CreateFromGltfDataForTest(glTF gltf, ArraySegment bytes = default) { + IStorage storage = null; + if (bytes.Array != null) + { + storage = new SimpleStorage(bytes); + } + else + { + storage = new GltfStorage(gltf); + } return new GltfData( string.Empty, string.Empty, gltf, new List(), - new SimpleStorage(new ArraySegment()), + storage, new MigrationFlags() ); } #region bytes access helper methods. buffer, bufferView, accessor(may sparse), image + public ArraySegment GetBytes(int bufferIndex) + { + // TODO: + var buffer = GLTF.buffers[bufferIndex]; + return _storage.Get(buffer.uri); + } + + public ArraySegment GetViewBytes(int bufferView) + { + var view = GLTF.bufferViews[bufferView]; + var segment = GetBytes(view.buffer); + return new ArraySegment(segment.Array, segment.Offset + view.byteOffset, view.byteLength); + } + T[] GetAttrib(int count, int byteOffset, glTFBufferView view) where T : struct { + var segment = GetBytes(view.buffer); var attrib = new T[count]; - var segment = GLTF.buffers[view.buffer].GetBytes(); var bytes = new ArraySegment(segment.Array, segment.Offset + view.byteOffset + byteOffset, count * view.byteStride); bytes.MarshalCopyTo(attrib); return attrib; @@ -207,7 +230,7 @@ namespace UniGLTF { var attrib = new float[vertexAccessor.count * vertexAccessor.TypeCount]; var view = GLTF.bufferViews[vertexAccessor.bufferView]; - var segment = GLTF.buffers[view.buffer].GetBytes(); + var segment = GetBytes(view.buffer); var bytes = new ArraySegment(segment.Array, segment.Offset + view.byteOffset + vertexAccessor.byteOffset, vertexAccessor.count * view.byteStride); bytes.MarshalCopyTo(attrib); result = attrib; @@ -233,6 +256,25 @@ namespace UniGLTF } return result; } + + public ArraySegment GetImageBytes(int imageIndex) + { + var image = GLTF.images[imageIndex]; + if (string.IsNullOrEmpty(image.uri)) + { + return GetViewBytes(image.bufferView); + } + else + { + return _storage.Get(image.uri); + } + } + + public ArraySegment GetImageBytesFromTextureIndex(int textureIndex) + { + var imageIndex = GLTF.textures[textureIndex].source; + return GetImageBytes(imageIndex); + } #endregion } } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs index 0484501af..e27c409a0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ImporterContext.cs @@ -57,7 +57,6 @@ namespace UniGLTF public GltfData Data { get; } public String Json => Data.Json; public glTF GLTF => Data.GLTF; - public IStorage Storage => Data.Storage; #endregion // configuration diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs index 68a89cb87..39e5cea19 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/TextureIO/GltfTextureImporter.cs @@ -35,7 +35,7 @@ namespace UniGLTF var gltfImage = data.GLTF.images[gltfTexture.source]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.sRGB, gltfTexture.name, gltfImage.uri); var sampler = TextureSamplerUtil.CreateSampler(data.GLTF, textureIndex); - GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(data.GLTF.GetImageBytesFromTextureIndex(data.Storage, textureIndex))); + GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(data.GetImageBytesFromTextureIndex(textureIndex))); var param = new TextureDescriptor(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.sRGB, default, default, getTextureBytesAsync, default, default, default, default, default); return (param.SubAssetKey, param); } @@ -46,7 +46,7 @@ namespace UniGLTF var gltfImage = data.GLTF.images[gltfTexture.source]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.Linear, gltfTexture.name, gltfImage.uri); var sampler = TextureSamplerUtil.CreateSampler(data.GLTF, textureIndex); - GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(data.GLTF.GetImageBytesFromTextureIndex(data.Storage, textureIndex))); + GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(data.GetImageBytesFromTextureIndex(textureIndex))); var param = new TextureDescriptor(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.Linear, default, default, getTextureBytesAsync, default, default, default, default, default); return (param.SubAssetKey, param); } @@ -57,7 +57,7 @@ namespace UniGLTF var gltfImage = data.GLTF.images[gltfTexture.source]; var name = TextureImportName.GetUnityObjectName(TextureImportTypes.NormalMap, gltfTexture.name, gltfImage.uri); var sampler = TextureSamplerUtil.CreateSampler(data.GLTF, textureIndex); - GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(data.GLTF.GetImageBytesFromTextureIndex(data.Storage, textureIndex))); + GetTextureBytesAsync getTextureBytesAsync = () => Task.FromResult(ToArray(data.GetImageBytesFromTextureIndex(textureIndex))); var param = new TextureDescriptor(name, gltfImage.GetExt(), gltfImage.uri, offset, scale, sampler, TextureImportTypes.NormalMap, default, default, getTextureBytesAsync, default, default, default, default, default); return (param.SubAssetKey, param); } @@ -73,7 +73,7 @@ namespace UniGLTF var gltfTexture = data.GLTF.textures[metallicRoughnessTextureIndex.Value]; name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, data.GLTF.images[gltfTexture.source].uri); sampler = TextureSamplerUtil.CreateSampler(data.GLTF, metallicRoughnessTextureIndex.Value); - getMetallicRoughnessAsync = () => Task.FromResult(ToArray(data.GLTF.GetImageBytesFromTextureIndex(data.Storage, metallicRoughnessTextureIndex.Value))); + getMetallicRoughnessAsync = () => Task.FromResult(ToArray(data.GetImageBytesFromTextureIndex(metallicRoughnessTextureIndex.Value))); } GetTextureBytesAsync getOcclusionAsync = default; @@ -85,7 +85,7 @@ namespace UniGLTF name = TextureImportName.GetUnityObjectName(TextureImportTypes.StandardMap, gltfTexture.name, data.GLTF.images[gltfTexture.source].uri); } sampler = TextureSamplerUtil.CreateSampler(data.GLTF, occlusionTextureIndex.Value); - getOcclusionAsync = () => Task.FromResult(ToArray(data.GLTF.GetImageBytesFromTextureIndex(data.Storage, occlusionTextureIndex.Value))); + getOcclusionAsync = () => Task.FromResult(ToArray(data.GetImageBytesFromTextureIndex(occlusionTextureIndex.Value))); } var texDesc = new TextureDescriptor(name, ".png", null, offset, scale, sampler, TextureImportTypes.StandardMap, metallicFactor, roughnessFactor, getMetallicRoughnessAsync, getOcclusionAsync, default, default, default, default); diff --git a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs index d625b3c28..71cd70566 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/EditorTextureSerializerTests.cs @@ -142,8 +142,10 @@ namespace UniGLTF UnityEngine.Object.DestroyImmediate(mat); UnityEngine.Object.DestroyImmediate(root); + var data = GltfData.CreateFromGltfDataForTest(gltf); + // Extract Image to Texture2D - var exportedBytes = gltf.GetViewBytes(exportedImage.bufferView).ToArray(); + var exportedBytes = data.GetViewBytes(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/UniGLTF/Tests/UniGLTF/MeshTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs index ae1435e9e..ef310e0d5 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs @@ -25,7 +25,6 @@ namespace UniGLTF w.Write(8.0f); bytes = ms.ToArray(); } - var storage = new SimpleStorage(new ArraySegment(bytes)); var gltf = new glTF { @@ -54,9 +53,9 @@ namespace UniGLTF } } }; - gltf.buffers[0].OpenStorage(storage); - var (getter, len) = WeightsAccessor.GetAccessor(GltfData.CreateFromGltfDataForTest(gltf), 0); + var data = GltfData.CreateFromGltfDataForTest(gltf, new ArraySegment(bytes)); + var (getter, len) = WeightsAccessor.GetAccessor(data, 0); Assert.AreEqual((1.0f, 2.0f, 3.0f, 4.0f), getter(0)); Assert.AreEqual((5.0f, 6.0f, 7.0f, 8.0f), getter(1)); } diff --git a/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs b/Assets/VRM10/Runtime/IO/Texture/Vrm10TextureDescriptorGenerator.cs index 9541a5d0b..ea1aaf321 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.GLTF.GetImageBytes(data.Storage, imageIndex); + var bytes = data.GetImageBytes(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 34c5300da..86b6c5f66 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 = gltf.GetViewBytes(image.bufferView); + var bytes = _data.GetViewBytes(image.bufferView); image.bufferView = AddBuffer(bytes); } diff --git a/Assets/VRM10/Runtime/Migration/RotateY180.cs b/Assets/VRM10/Runtime/Migration/RotateY180.cs index 5b7550481..fb7ca77c2 100644 --- a/Assets/VRM10/Runtime/Migration/RotateY180.cs +++ b/Assets/VRM10/Runtime/Migration/RotateY180.cs @@ -47,7 +47,7 @@ namespace UniVRM10 } } - static void ReverseVector3Array(glTF gltf, int accessorIndex, HashSet used) + static void ReverseVector3Array(GltfData data, int accessorIndex, HashSet used) { if (accessorIndex == -1) { @@ -59,7 +59,7 @@ namespace UniVRM10 return; } - var accessor = gltf.accessors[accessorIndex]; + var accessor = data.GLTF.accessors[accessorIndex]; var bufferViewIndex = -1; if (accessor.bufferView != -1) { @@ -72,7 +72,7 @@ namespace UniVRM10 if (bufferViewIndex != -1) { - var buffer = gltf.GetViewBytes(bufferViewIndex); + var buffer = data.GetViewBytes(bufferViewIndex); var span = SpanLike.Wrap(buffer); for (int i = 0; i < span.Length; ++i) { @@ -85,35 +85,35 @@ namespace UniVRM10 /// シーンをY軸で180度回転する /// /// - public static void Rotate(glTF gltf) + public static void Rotate(GltfData data) { - foreach (var node in gltf.nodes) + foreach (var node in data.GLTF.nodes) { Rotate(node); } // mesh の回転のみでよい var used = new HashSet(); - foreach (var mesh in gltf.meshes) + foreach (var mesh in data.GLTF.meshes) { foreach (var prim in mesh.primitives) { - ReverseVector3Array(gltf, prim.attributes.POSITION, used); - ReverseVector3Array(gltf, prim.attributes.NORMAL, used); + ReverseVector3Array(data, prim.attributes.POSITION, used); + ReverseVector3Array(data, prim.attributes.NORMAL, used); foreach (var target in prim.targets) { - ReverseVector3Array(gltf, target.POSITION, used); - ReverseVector3Array(gltf, target.NORMAL, used); + ReverseVector3Array(data, target.POSITION, used); + ReverseVector3Array(data, target.NORMAL, used); } } } - foreach (var skin in gltf.skins) + foreach (var skin in data.GLTF.skins) { if (used.Add(skin.inverseBindMatrices)) { - var accessor = gltf.accessors[skin.inverseBindMatrices]; - var buffer = gltf.GetViewBytes(accessor.bufferView); + var accessor = data.GLTF.accessors[skin.inverseBindMatrices]; + var buffer = data.GetViewBytes(accessor.bufferView); var span = SpanLike.Wrap(buffer); for (int i = 0; i < span.Length; ++i) {