From 8d80c32a1156794c1fd1a2824e6b2af51047aed7 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 10 Feb 2022 18:49:48 +0900 Subject: [PATCH] Merge Vrm10ExportData to ExportingGltfData --- .../Runtime/UniGLTF/IO/ExportingGltfData.cs | 26 ++++++++++++++ Assets/VRM10/Editor/Vrm10ExportDialog.cs | 2 +- .../VRM10/Runtime/IO/BufferAccessorAdapter.cs | 22 ++++++------ Assets/VRM10/Runtime/IO/Model/MeshWriter.cs | 12 +++---- Assets/VRM10/Runtime/IO/Vrm10ExportData.cs | 34 ------------------- .../VRM10/Runtime/IO/Vrm10ExportData.cs.meta | 11 ------ Assets/VRM10/Runtime/IO/Vrm10Exporter.cs | 8 ++--- 7 files changed, 48 insertions(+), 67 deletions(-) delete mode 100644 Assets/VRM10/Runtime/IO/Vrm10ExportData.cs delete mode 100644 Assets/VRM10/Runtime/IO/Vrm10ExportData.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs index a5b29d16c..f73f43e2d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/ExportingGltfData.cs @@ -135,6 +135,20 @@ namespace UniGLTF }); return accessorIndex; } + + public void Reserve(int bytesLength) + { + _buffer.ExtendCapacity(bytesLength); + } + + public int AppendToBuffer(ArraySegment segment) + { + var gltfBufferView = _buffer.Extend(segment); + var viewIndex = GLTF.bufferViews.Count; + GLTF.bufferViews.Add(gltfBufferView); + return viewIndex; + } + #endregion #region ToGltf & ToGlb @@ -154,6 +168,18 @@ namespace UniGLTF return Glb.Create(json, BinBytes).ToBytes(); } + // public byte[] ToBytes() + // { + // 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, _buffer.Bytes); + // return glb.ToBytes(); + // } + /// /// glTF 形式で出力する? /// diff --git a/Assets/VRM10/Editor/Vrm10ExportDialog.cs b/Assets/VRM10/Editor/Vrm10ExportDialog.cs index 696966175..cab5d65c9 100644 --- a/Assets/VRM10/Editor/Vrm10ExportDialog.cs +++ b/Assets/VRM10/Editor/Vrm10ExportDialog.cs @@ -278,7 +278,7 @@ namespace UniVRM10 var option = new VrmLib.ExportArgs(); exporter.Export(root, model, converter, option, Vrm ? Vrm.Meta : m_tmpObject.Meta); - var exportedBytes = exporter.Storage.ToBytes(); + var exportedBytes = exporter.Storage.ToGlbBytes(); m_logLabel += $"write to {path}...\n"; File.WriteAllBytes(path, exportedBytes); diff --git a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs b/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs index 940f06551..17c3d91ab 100644 --- a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs +++ b/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs @@ -30,7 +30,7 @@ namespace UniVRM10 } public static int AddViewTo(this VrmLib.BufferAccessor self, - Vrm10ExportData storage, int bufferIndex, + ExportingGltfData data, int bufferIndex, int offset = 0, int count = 0) { var stride = self.Stride; @@ -39,7 +39,7 @@ namespace UniVRM10 count = self.Count; } var slice = self.Bytes.Slice(offset * stride, count * stride); - return storage.AppendToBuffer(slice); + return data.AppendToBuffer(slice); } static glTFAccessor CreateGltfAccessor(this VrmLib.BufferAccessor self, @@ -60,11 +60,11 @@ namespace UniVRM10 } public static int AddAccessorTo(this VrmLib.BufferAccessor self, - Vrm10ExportData storage, int viewIndex, + ExportingGltfData data, int viewIndex, Action, glTFAccessor> minMax = null, int offset = 0, int count = 0) { - var gltf = storage.GLTF; + var gltf = data.GLTF; var accessorIndex = gltf.accessors.Count; var accessor = self.CreateGltfAccessor(viewIndex, count, offset * self.Stride); if (minMax != null) @@ -76,7 +76,7 @@ namespace UniVRM10 } public static int AddAccessorTo(this VrmLib.BufferAccessor self, - Vrm10ExportData storage, int bufferIndex, + ExportingGltfData data, int bufferIndex, // GltfBufferTargetType targetType, bool useSparse, Action, glTFAccessor> minMax = null, @@ -116,10 +116,10 @@ namespace UniVRM10 sparseValueSpan[i] = value; } - var sparseIndexView = storage.AppendToBuffer(sparseIndexBin); - var sparseValueView = storage.AppendToBuffer(sparseValueBin); + var sparseIndexView = data.AppendToBuffer(sparseIndexBin); + var sparseValueView = data.AppendToBuffer(sparseValueBin); - var accessorIndex = storage.GLTF.accessors.Count; + var accessorIndex = data.GLTF.accessors.Count; var accessor = new glTFAccessor { componentType = (glComponentType)self.ComponentType, @@ -144,13 +144,13 @@ namespace UniVRM10 { minMax(sparseValueBin, accessor); } - storage.GLTF.accessors.Add(accessor); + data.GLTF.accessors.Add(accessor); return accessorIndex; } } - var viewIndex = self.AddViewTo(storage, bufferIndex, offset, count); - return self.AddAccessorTo(storage, viewIndex, minMax, 0, count); + var viewIndex = self.AddViewTo(data, bufferIndex, offset, count); + return self.AddAccessorTo(data, viewIndex, minMax, 0, count); } } } diff --git a/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs b/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs index 1ce60e8e9..236a2492d 100644 --- a/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs +++ b/Assets/VRM10/Runtime/IO/Model/MeshWriter.cs @@ -26,7 +26,7 @@ namespace UniVRM10 accessor.max = max.ToFloat3(); } - static int ExportIndices(Vrm10ExportData storage, BufferAccessor x, int offset, int count, ExportArgs option) + static int ExportIndices(ExportingGltfData data, BufferAccessor x, int offset, int count, ExportArgs option) { if (x.Count <= ushort.MaxValue) { @@ -41,16 +41,16 @@ namespace UniVRM10 dst[i] = (ushort)src[i]; } var accessor = new BufferAccessor(new ArraySegment(bytes), AccessorValueType.UNSIGNED_SHORT, AccessorVectorType.SCALAR, count); - return accessor.AddAccessorTo(storage, 0, option.sparse, null, 0, count); + return accessor.AddAccessorTo(data, 0, option.sparse, null, 0, count); } else { - return x.AddAccessorTo(storage, 0, option.sparse, null, offset, count); + return x.AddAccessorTo(data, 0, option.sparse, null, offset, count); } } else { - return x.AddAccessorTo(storage, 0, option.sparse, null, offset, count); + return x.AddAccessorTo(data, 0, option.sparse, null, offset, count); } } @@ -62,7 +62,7 @@ namespace UniVRM10 /// /// /// - /// + /// /// /// static IEnumerable ExportMeshDivided(this VrmLib.Mesh mesh, List materials, @@ -157,7 +157,7 @@ namespace UniVRM10 /// /// /// - /// + /// /// /// public static glTFMesh ExportMeshGroup(this MeshGroup src, List materials, ExportingGltfData writer, ExportArgs option) diff --git a/Assets/VRM10/Runtime/IO/Vrm10ExportData.cs b/Assets/VRM10/Runtime/IO/Vrm10ExportData.cs deleted file mode 100644 index afcd0b037..000000000 --- a/Assets/VRM10/Runtime/IO/Vrm10ExportData.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using UniGLTF; -using UniJSON; - -namespace UniVRM10 -{ - public class Vrm10ExportData : ExportingGltfData - { - public void Reserve(int bytesLength) - { - _buffer.ExtendCapacity(bytesLength); - } - - public int AppendToBuffer(ArraySegment segment) - { - var gltfBufferView = _buffer.Extend(segment); - var viewIndex = GLTF.bufferViews.Count; - GLTF.bufferViews.Add(gltfBufferView); - return viewIndex; - } - - public byte[] ToBytes() - { - 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, _buffer.Bytes); - return glb.ToBytes(); - } - } -} diff --git a/Assets/VRM10/Runtime/IO/Vrm10ExportData.cs.meta b/Assets/VRM10/Runtime/IO/Vrm10ExportData.cs.meta deleted file mode 100644 index c6293b0c4..000000000 --- a/Assets/VRM10/Runtime/IO/Vrm10ExportData.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 53bfc0b12550a494e8da90e632e1c456 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs index dad002ec2..d529d5624 100644 --- a/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs +++ b/Assets/VRM10/Runtime/IO/Vrm10Exporter.cs @@ -18,7 +18,7 @@ namespace UniVRM10 public const string LICENSE_URL_JA = "https://vrm.dev/licenses/1.0/"; public const string LICENSE_URL_EN = "https://vrm.dev/licenses/1.0/en/"; - public readonly Vrm10ExportData Storage = new Vrm10ExportData(); + public readonly ExportingGltfData Storage = new ExportingGltfData(); public readonly string VrmExtensionName = "VRMC_vrm"; @@ -70,7 +70,7 @@ namespace UniVRM10 } } - public static IEnumerable<(glTFNode, glTFSkin)> ExportNodes(List nodes, List groups, Vrm10ExportData storage, ExportArgs option) + public static IEnumerable<(glTFNode, glTFSkin)> ExportNodes(List nodes, List groups, ExportingGltfData data, ExportArgs option) { foreach (var node in nodes) { @@ -100,7 +100,7 @@ namespace UniVRM10 } if (skin.InverseMatrices != null) { - gltfSkin.inverseBindMatrices = skin.InverseMatrices.AddAccessorTo(storage, 0, option.sparse); + gltfSkin.inverseBindMatrices = skin.InverseMatrices.AddAccessorTo(data, 0, option.sparse); } if (skin.Root != null) { @@ -816,7 +816,7 @@ namespace UniVRM10 { }; exporter10.Export(go, model, converter, option); - return exporter10.Storage.ToBytes(); + return exporter10.Storage.ToGlbBytes(); } } }