From 115ca18b5185f45481917a30085397437294624f Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 15 Feb 2022 17:13:26 +0900 Subject: [PATCH 1/8] =?UTF-8?q?BufferAccessor=20=E3=82=92=20UniGLTF=20?= =?UTF-8?q?=E3=81=AB=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/UniGLTF/IO}/BufferAccessor.cs | 11 +----- .../UniGLTF/IO}/BufferAccessor.cs.meta | 2 +- .../Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 2 +- .../VRM10/Runtime/IO/BufferAccessorAdapter.cs | 14 ++++---- .../Runtime/IO/Model/MeshImporterDivided.cs | 35 +++++++++---------- .../VRM10/Runtime/IO/Model/ModelExporter.cs | 32 ++++++++--------- Assets/VRM10/Runtime/Migration/MeshUpdater.cs | 2 +- Assets/VRM10/vrmlib/Runtime/VertexBuffer.cs | 1 + 8 files changed, 44 insertions(+), 55 deletions(-) rename Assets/{VRM10/vrmlib/Runtime => UniGLTF/Runtime/UniGLTF/IO}/BufferAccessor.cs (96%) rename Assets/{VRM10/vrmlib/Runtime => UniGLTF/Runtime/UniGLTF/IO}/BufferAccessor.cs.meta (83%) diff --git a/Assets/VRM10/vrmlib/Runtime/BufferAccessor.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs similarity index 96% rename from Assets/VRM10/vrmlib/Runtime/BufferAccessor.cs rename to Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs index 602ded4c3..643164887 100644 --- a/Assets/VRM10/vrmlib/Runtime/BufferAccessor.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs @@ -6,7 +6,7 @@ using UniGLTF; using Unity.Collections; using Unity.Collections.LowLevel.Unsafe; -namespace VrmLib +namespace UniGLTF { public enum AccessorVectorType { @@ -163,15 +163,6 @@ namespace VrmLib return Bytes.Reinterpret(1); } - /// - /// バッファをNativeSliceへと書き込む - /// - public unsafe void CopyToNativeSlice(NativeSlice destArray) where T : unmanaged - { - var byteArray = NativeArrayUnsafeUtility.GetUnsafePtr(Bytes); - UnsafeUtility.MemCpy((T*)destArray.GetUnsafePtr(), byteArray, Bytes.Length); - } - public void Assign(T[] values) where T : struct { if (Marshal.SizeOf(typeof(T)) != Stride) diff --git a/Assets/VRM10/vrmlib/Runtime/BufferAccessor.cs.meta b/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs.meta similarity index 83% rename from Assets/VRM10/vrmlib/Runtime/BufferAccessor.cs.meta rename to Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs.meta index 2162bb5a6..6f1ec6565 100644 --- a/Assets/VRM10/vrmlib/Runtime/BufferAccessor.cs.meta +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 27cdb60e5e9634743875e0655372e682 +guid: aac48ad8d56549248a1ef57ff815d151 MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index b1507791a..9da8d70d0 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -6,7 +6,7 @@ using UnityEngine.Profiling; using UnityEngine.Rendering; namespace UniGLTF -{ +{ internal class MeshContext { private readonly List _vertices = new List(); diff --git a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs b/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs index c3ae9509c..4ef05afb8 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, + public static int AddViewTo(this BufferAccessor self, ExportingGltfData data, int bufferIndex, int offset = 0, int count = 0) { @@ -43,7 +43,7 @@ namespace UniVRM10 return data.AppendToBuffer(slice); } - static glTFAccessor CreateGltfAccessor(this VrmLib.BufferAccessor self, + static glTFAccessor CreateGltfAccessor(this BufferAccessor self, int viewIndex, int count = 0, int byteOffset = 0) { if (count == 0) @@ -60,7 +60,7 @@ namespace UniVRM10 }; } - public static int AddAccessorTo(this VrmLib.BufferAccessor self, + public static int AddAccessorTo(this BufferAccessor self, ExportingGltfData data, int viewIndex, Action, glTFAccessor> minMax = null, int offset = 0, int count = 0) @@ -76,15 +76,15 @@ namespace UniVRM10 return accessorIndex; } - public static int AddAccessorTo(this VrmLib.BufferAccessor self, + public static int AddAccessorTo(this BufferAccessor self, ExportingGltfData data, int bufferIndex, // GltfBufferTargetType targetType, bool useSparse, Action, glTFAccessor> minMax = null, int offset = 0, int count = 0) { - if (self.ComponentType == VrmLib.AccessorValueType.FLOAT - && self.AccessorType == VrmLib.AccessorVectorType.VEC3 + if (self.ComponentType == AccessorValueType.FLOAT + && self.AccessorType == AccessorVectorType.VEC3 ) { var values = self.GetSpan(); @@ -133,7 +133,7 @@ namespace UniVRM10 count = sparseValuesWithIndex.Count, indices = new glTFSparseIndices { - componentType = (glComponentType)VrmLib.AccessorValueType.UNSIGNED_INT, + componentType = (glComponentType)AccessorValueType.UNSIGNED_INT, bufferView = sparseIndexView, }, values = new glTFSparseValues diff --git a/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs b/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs index bda8578c2..62943ec7b 100644 --- a/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs +++ b/Assets/VRM10/Runtime/IO/Model/MeshImporterDivided.cs @@ -66,32 +66,29 @@ namespace UniVRM10 foreach (var mesh in meshGroup.Meshes) { var morphTarget = mesh.MorphTargets[i]; - morphTarget.VertexBuffer.Positions.CopyToNativeSlice( - new NativeSlice( - blendShapePositions, - blendShapePositionOffset, - morphTarget.VertexBuffer.Positions.Count - ) - ); - // nullならdefault(0)のまま - morphTarget.VertexBuffer.Normals?.CopyToNativeSlice( - new NativeSlice( - blendShapeNormals, - blendShapeNormalOffset, - morphTarget.VertexBuffer.Normals.Count - ) - ); + + NativeArray.Copy( + morphTarget.VertexBuffer.Positions.Bytes.Reinterpret(1), + blendShapePositions.GetSubArray(blendShapePositionOffset, morphTarget.VertexBuffer.Positions.Count)); + + if (morphTarget.VertexBuffer.Normals != null) + { + // nullならdefault(0)のまま + NativeArray.Copy( + morphTarget.VertexBuffer.Normals.Bytes.Reinterpret(1), + blendShapeNormals.GetSubArray(blendShapeNormalOffset, morphTarget.VertexBuffer.Normals.Count)); + } blendShapePositionOffset += morphTarget.VertexBuffer.Positions.Count; blendShapeNormalOffset += morphTarget.VertexBuffer.Normals?.Count ?? morphTarget.VertexBuffer.Count; } resultMesh.AddBlendShapeFrame(meshGroup.Meshes[0].MorphTargets[i].Name, - 100.0f, - blendShapePositions.ToArray(), - blendShapeNormals.ToArray(), - null); + 100.0f, + blendShapePositions.ToArray(), + blendShapeNormals.ToArray(), + null); } } diff --git a/Assets/VRM10/Runtime/IO/Model/ModelExporter.cs b/Assets/VRM10/Runtime/IO/Model/ModelExporter.cs index 3db11857c..06eea892a 100644 --- a/Assets/VRM10/Runtime/IO/Model/ModelExporter.cs +++ b/Assets/VRM10/Runtime/IO/Model/ModelExporter.cs @@ -272,44 +272,44 @@ namespace UniVRM10 return skin; } - private static VrmLib.BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, SkinJoints[] values) + private static BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, SkinJoints[] values) { - return ToBufferAccessor(arrayManager, values, VrmLib.AccessorValueType.UNSIGNED_SHORT, VrmLib.AccessorVectorType.VEC4); + return ToBufferAccessor(arrayManager, values, AccessorValueType.UNSIGNED_SHORT, AccessorVectorType.VEC4); } - private static VrmLib.BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, Color[] colors) + private static BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, Color[] colors) { - return ToBufferAccessor(arrayManager, colors, VrmLib.AccessorValueType.FLOAT, VrmLib.AccessorVectorType.VEC4); + return ToBufferAccessor(arrayManager, colors, AccessorValueType.FLOAT, AccessorVectorType.VEC4); } - private static VrmLib.BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, Vector4[] vectors) + private static BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, Vector4[] vectors) { - return ToBufferAccessor(arrayManager, vectors, VrmLib.AccessorValueType.FLOAT, VrmLib.AccessorVectorType.VEC4); + return ToBufferAccessor(arrayManager, vectors, AccessorValueType.FLOAT, AccessorVectorType.VEC4); } - private static VrmLib.BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, Vector3[] vectors) + private static BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, Vector3[] vectors) { - return ToBufferAccessor(arrayManager, vectors, VrmLib.AccessorValueType.FLOAT, VrmLib.AccessorVectorType.VEC3); + return ToBufferAccessor(arrayManager, vectors, AccessorValueType.FLOAT, AccessorVectorType.VEC3); } - private static VrmLib.BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, Vector2[] vectors) + private static BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, Vector2[] vectors) { - return ToBufferAccessor(arrayManager, vectors, VrmLib.AccessorValueType.FLOAT, VrmLib.AccessorVectorType.VEC2); + return ToBufferAccessor(arrayManager, vectors, AccessorValueType.FLOAT, AccessorVectorType.VEC2); } - private static VrmLib.BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, int[] scalars) + private static BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, int[] scalars) { - return ToBufferAccessor(arrayManager, scalars, VrmLib.AccessorValueType.UNSIGNED_INT, VrmLib.AccessorVectorType.SCALAR); + return ToBufferAccessor(arrayManager, scalars, AccessorValueType.UNSIGNED_INT, AccessorVectorType.SCALAR); } - private static VrmLib.BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, Matrix4x4[] matrixes) + private static BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, Matrix4x4[] matrixes) { - return ToBufferAccessor(arrayManager, matrixes, VrmLib.AccessorValueType.FLOAT, VrmLib.AccessorVectorType.MAT4); + return ToBufferAccessor(arrayManager, matrixes, AccessorValueType.FLOAT, AccessorVectorType.MAT4); } - private static VrmLib.BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, T[] value, VrmLib.AccessorValueType valueType, VrmLib.AccessorVectorType vectorType) where T : struct + private static BufferAccessor ToBufferAccessor(INativeArrayManager arrayManager, T[] value, AccessorValueType valueType, AccessorVectorType vectorType) where T : struct { - return new VrmLib.BufferAccessor(arrayManager, + return new BufferAccessor(arrayManager, arrayManager.CreateNativeArray(value).Reinterpret(Marshal.SizeOf()), valueType, vectorType, diff --git a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs index ce62da032..aeb8611f5 100644 --- a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs +++ b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs @@ -49,7 +49,7 @@ namespace UniVRM10 return index; } - int? AddAccessor(VrmLib.BufferAccessor buffer) where T : struct + int? AddAccessor(BufferAccessor buffer) where T : struct { if (buffer == null) { diff --git a/Assets/VRM10/vrmlib/Runtime/VertexBuffer.cs b/Assets/VRM10/vrmlib/Runtime/VertexBuffer.cs index 44dbd87df..c6b74351d 100644 --- a/Assets/VRM10/vrmlib/Runtime/VertexBuffer.cs +++ b/Assets/VRM10/vrmlib/Runtime/VertexBuffer.cs @@ -2,6 +2,7 @@ using System; using System.Collections; using System.Collections.Generic; using System.Linq; +using UniGLTF; namespace VrmLib { From dac2a5e19e4c6987b8cd6893bd211be4752e88bf Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 15 Feb 2022 17:31:47 +0900 Subject: [PATCH 2/8] GetIntIndicesFromView return BufferAccessor --- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 93 +++++++++++++++---- 1 file changed, 74 insertions(+), 19 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index ff35786c3..1d4d24ac4 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -157,15 +157,16 @@ namespace UniGLTF return segment.GetSubArray(view.byteOffset, view.byteLength); } - NativeArray GetTypedFromBufferView(int count, int byteOffset, glTFBufferView view) where T : struct + NativeArray GetBytesFromBufferView(glTFBufferView view) { var segment = GetBytesFromBuffer(view.buffer); - return segment.GetSubArray(view.byteOffset + byteOffset, count * Marshal.SizeOf()).Reinterpret(1); + return segment.GetSubArray(view.byteOffset, view.byteLength); } NativeArray GetTypedFromAccessor(glTFAccessor accessor, glTFBufferView view) where T : struct { - return GetTypedFromBufferView(accessor.count, accessor.byteOffset, view); + var bytes = GetBytesFromBufferView(view); + return bytes.GetSubArray(accessor.byteOffset, bytes.Length - accessor.byteOffset).Reinterpret(1).GetSubArray(0, accessor.count); } /// @@ -176,23 +177,27 @@ namespace UniGLTF /// /// /// - IEnumerable GetIntIndicesFromView(glTFBufferView view, int count, int byteOffset, glComponentType componentType) + BufferAccessor GetIntIndicesFromView(glTFBufferView view, int count, int byteOffset, glComponentType componentType) { + var bytes = GetBytesFromBufferView(view); switch (componentType) { case glComponentType.UNSIGNED_BYTE: { - return GetTypedFromBufferView(count, byteOffset, view).Select(x => (int)(x)); + return new BufferAccessor(NativeArrayManager, bytes.GetSubArray(byteOffset, bytes.Length - byteOffset), + AccessorValueType.UNSIGNED_BYTE, AccessorVectorType.SCALAR, count); } case glComponentType.UNSIGNED_SHORT: { - return GetTypedFromBufferView(count, byteOffset, view).Select(x => (int)(x)); + return new BufferAccessor(NativeArrayManager, bytes.GetSubArray(byteOffset, bytes.Length - byteOffset), + AccessorValueType.UNSIGNED_SHORT, AccessorVectorType.SCALAR, count); } case glComponentType.UNSIGNED_INT: { - return GetTypedFromBufferView(count, byteOffset, view).Select(x => (int)(x)); + return new BufferAccessor(NativeArrayManager, bytes.GetSubArray(byteOffset, bytes.Length - byteOffset), + AccessorValueType.UNSIGNED_INT, AccessorVectorType.SCALAR, count); } } throw new NotImplementedException("GetIndices: unknown componenttype: " + componentType); @@ -263,14 +268,39 @@ namespace UniGLTF if (sparse != null && sparse.count > 0) { // override sparse values - 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 _indices = GetIntIndicesFromView(GLTF.bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType); + var bytes = GetBytesFromBufferView(GLTF.bufferViews[sparse.values.bufferView]); + var values = bytes.GetSubArray(sparse.values.byteOffset, bytes.Length - sparse.values.byteOffset).Reinterpret(1).GetSubArray(0, sparse.count); - var it = indices.GetEnumerator(); - for (int i = 0; i < sparse.count; ++i) + switch (_indices.ComponentType) { - it.MoveNext(); - result[it.Current] = values[i]; + case AccessorValueType.UNSIGNED_BYTE: + { + var indices = _indices.Bytes; + for (int i = 0; i < sparse.count; ++i) + { + result[indices[i]] = values[i]; + } + break; + } + case AccessorValueType.UNSIGNED_SHORT: + { + var indices = _indices.Bytes.Reinterpret(1); + for (int i = 0; i < sparse.count; ++i) + { + result[indices[i]] = values[i]; + } + break; + } + case AccessorValueType.UNSIGNED_INT: + { + var indices = _indices.Bytes.Reinterpret(1); + for (int i = 0; i < sparse.count; ++i) + { + result[indices[i]] = values[i]; + } + break; + } } } return result; @@ -300,14 +330,39 @@ namespace UniGLTF if (sparse != null && sparse.count > 0) { // override sparse values - 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 _indices = GetIntIndicesFromView(GLTF.bufferViews[sparse.indices.bufferView], sparse.count, sparse.indices.byteOffset, sparse.indices.componentType); + var bytes = GetBytesFromBufferView(GLTF.bufferViews[sparse.values.bufferView]); + var values = bytes.GetSubArray(sparse.values.byteOffset, bytes.Length - sparse.values.byteOffset).Reinterpret(1).GetSubArray(0, sparse.count * vertexAccessor.TypeCount); - var it = indices.GetEnumerator(); - for (int i = 0; i < sparse.count; ++i) + switch (_indices.ComponentType) { - it.MoveNext(); - result[it.Current] = values[i]; + case AccessorValueType.UNSIGNED_BYTE: + { + var indices = _indices.Bytes; + for (int i = 0; i < sparse.count; ++i) + { + result[indices[i]] = values[i]; + } + break; + } + case AccessorValueType.UNSIGNED_SHORT: + { + var indices = _indices.Bytes.Reinterpret(1); + for (int i = 0; i < sparse.count; ++i) + { + result[indices[i]] = values[i]; + } + break; + } + case AccessorValueType.UNSIGNED_INT: + { + var indices = _indices.Bytes.Reinterpret(1); + for (int i = 0; i < sparse.count; ++i) + { + result[indices[i]] = values[i]; + } + break; + } } } return result; From fb09c3fa6deaa8c65abb2712c65df79b539fd752 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 15 Feb 2022 18:27:36 +0900 Subject: [PATCH 3/8] =?UTF-8?q?GltfData.GetIndices=20=E3=81=AE=E8=B2=AC?= =?UTF-8?q?=E5=8B=99=E3=82=92=20MeshContext=20=E3=81=AB=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * triangle flip * concat submesh indices --- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 52 ++----------- .../Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 77 +++++++++++++++++-- Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs | 8 +- 3 files changed, 79 insertions(+), 58 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index 1d4d24ac4..8494e1bb8 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -199,58 +199,16 @@ namespace UniGLTF return new BufferAccessor(NativeArrayManager, bytes.GetSubArray(byteOffset, bytes.Length - byteOffset), AccessorValueType.UNSIGNED_INT, AccessorVectorType.SCALAR, count); } + + default: + throw new NotImplementedException("GetIndices: unknown componenttype: " + componentType); } - throw new NotImplementedException("GetIndices: unknown componenttype: " + componentType); } - /// - /// Get indices and cast to int - /// - /// - /// - /// - IEnumerable GetIntIndicesFromAccessor(glTFAccessor accessor, out int count) + public BufferAccessor GetIndicesFromAccessor(glTFAccessor accessor) { - count = accessor.count; var view = GLTF.bufferViews[accessor.bufferView]; - switch ((glComponentType)accessor.componentType) - { - case glComponentType.UNSIGNED_BYTE: - { - return GetTypedFromAccessor(accessor, view).Select(x => (int)(x)); - } - - case glComponentType.UNSIGNED_SHORT: - { - return GetTypedFromAccessor(accessor, view).Select(x => (int)(x)); - } - - case glComponentType.UNSIGNED_INT: - { - return GetTypedFromAccessor(accessor, view).Select(x => (int)(x)); - } - } - throw new NotImplementedException("GetIndices: unknown componenttype: " + accessor.componentType); - } - - public int[] GetIndices(int accessorIndex) - { - int count; - var result = GetIntIndicesFromAccessor(GLTF.accessors[accessorIndex], out count); - var indices = new int[count]; - - // flip triangles - var it = result.GetEnumerator(); - { - for (int i = 0; i < count; i += 3) - { - it.MoveNext(); indices[i + 2] = it.Current; - it.MoveNext(); indices[i + 1] = it.Current; - it.MoveNext(); indices[i] = it.Current; - } - } - - return indices; + return GetIntIndicesFromView(view, accessor.count, accessor.byteOffset, accessor.componentType); } public NativeArray GetArrayFromAccessor(int accessorIndex) where T : struct diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index 9da8d70d0..163b75e6b 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -6,7 +6,7 @@ using UnityEngine.Profiling; using UnityEngine.Rendering; namespace UniGLTF -{ +{ internal class MeshContext { private readonly List _vertices = new List(); @@ -23,6 +23,46 @@ namespace UniGLTF public string Name { get; } + /// + /// * flip triangle + /// * add submesh offset + /// + /// + /// + void PushIndices(BufferAccessor src, int offset) + { + switch (src.ComponentType) + { + case AccessorValueType.UNSIGNED_SHORT: + { + var indices = src.Bytes.Reinterpret(1); + for (int i = 0; i < src.Count; i += 3) + { + _indices.Add(offset + indices[i + 2]); + _indices.Add(offset + indices[i + 1]); + _indices.Add(offset + indices[i]); + } + } + break; + + case AccessorValueType.UNSIGNED_INT: + { + // たぶん int で OK + var indices = src.Bytes.Reinterpret(1); + for (int i = 0; i < src.Count; i += 3) + { + _indices.Add(offset + indices[i + 2]); + _indices.Add(offset + indices[i + 1]); + _indices.Add(offset + indices[i]); + } + } + break; + + default: + throw new NotImplementedException(); + } + } + /// /// 頂点情報をMeshに対して送る /// @@ -112,6 +152,25 @@ namespace UniGLTF return src; } + int GetIndicesCapacity(GltfData data, glTFMesh gltfMesh) + { + var count = 0; + foreach (var primitive in gltfMesh.primitives) + { + if (primitive.indices == -1) + { + var positions = data.GLTF.accessors[primitive.attributes.POSITION]; + count += positions.count; + } + else + { + var accessor = data.GLTF.accessors[primitive.indices]; + count += accessor.count; + } + } + return count; + } + /// /// 各 primitive の attribute の要素が同じでない。=> uv が有るものと無いものが混在するなど /// glTF 的にはありうる。 @@ -123,6 +182,8 @@ namespace UniGLTF /// public void ImportMeshIndependentVertexBuffer(GltfData data, glTFMesh gltfMesh, IAxisInverter inverter) { + _indices.Capacity = GetIndicesCapacity(data, gltfMesh); + foreach (var primitives in gltfMesh.primitives) { var vertexOffset = _vertices.Count; @@ -232,9 +293,9 @@ namespace UniGLTF if (indexBufferCount >= 0) { var indexOffset = _indices.Count; - var dataIndices = data.GetIndices(indexBufferCount); - _indices.AddRange(dataIndices.Select(index => index + vertexOffset)); - _subMeshes.Add(new SubMeshDescriptor(indexOffset, dataIndices.Length)); + var dataIndices = data.GetIndicesFromAccessor(data.GLTF.accessors[indexBufferCount]); + PushIndices(dataIndices, vertexOffset); + _subMeshes.Add(new SubMeshDescriptor(indexOffset, dataIndices.Count)); } else { @@ -268,6 +329,8 @@ namespace UniGLTF /// public void ImportMeshSharingVertexBuffer(GltfData data, glTFMesh gltfMesh, IAxisInverter inverter) { + _indices.Capacity = GetIndicesCapacity(data, gltfMesh); + { // 同じVertexBufferを共有しているので先頭のモノを使う var primitives = gltfMesh.primitives.First(); @@ -369,9 +432,9 @@ namespace UniGLTF else { var indexOffset = _indices.Count; - var indices = data.GetIndices(primitive.indices); - _indices.AddRange(indices); - _subMeshes.Add(new SubMeshDescriptor(indexOffset, indices.Length)); + var indices = data.GetIndicesFromAccessor(data.GLTF.accessors[primitive.indices]); + PushIndices(indices, 0); + _subMeshes.Add(new SubMeshDescriptor(indexOffset, indices.Count)); } // material diff --git a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs index 5a8c0d48a..6fb1ecdd4 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs @@ -143,7 +143,7 @@ namespace UniGLTF { { - var indices = parsed.GetIndices(gltfMesh.primitives[0].indices); + var indices = parsed.GetIndicesFromAccessor(parsed.GLTF.accessors[gltfMesh.primitives[0].indices]).Bytes.Reinterpret(1); Assert.AreEqual(0, indices[0]); Assert.AreEqual(1, indices[1]); Assert.AreEqual(5, indices[2]); @@ -153,7 +153,7 @@ namespace UniGLTF } { - var indices = parsed.GetIndices(gltfMesh.primitives[1].indices); + var indices = parsed.GetIndicesFromAccessor(parsed.GLTF.accessors[gltfMesh.primitives[1].indices]).Bytes.Reinterpret(1); Assert.AreEqual(1, indices[0]); Assert.AreEqual(2, indices[1]); Assert.AreEqual(4, indices[2]); @@ -193,7 +193,7 @@ namespace UniGLTF using (var parsed = GltfData.CreateFromGltfDataForTest(data.Gltf, data.BinBytes)) { { - var indices = parsed.GetIndices(gltfMesh.primitives[0].indices); + var indices = parsed.GetIndicesFromAccessor(parsed.GLTF.accessors[gltfMesh.primitives[0].indices]).Bytes.Reinterpret(1); Assert.AreEqual(0, indices[0]); Assert.AreEqual(1, indices[1]); Assert.AreEqual(3, indices[2]); @@ -207,7 +207,7 @@ namespace UniGLTF } { - var indices = parsed.GetIndices(gltfMesh.primitives[1].indices); + var indices = parsed.GetIndicesFromAccessor(parsed.GLTF.accessors[gltfMesh.primitives[1].indices]).Bytes.Reinterpret(1); Assert.AreEqual(0, indices[0]); Assert.AreEqual(1, indices[1]); Assert.AreEqual(3, indices[2]); From b044dc99a0cd3073026cb622759ec037945da407 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 15 Feb 2022 20:34:23 +0900 Subject: [PATCH 4/8] merge BufferAccessorAdapter to BufferAccessor. remove unused. --- .../Runtime/UniGLTF/IO/BufferAccessor.cs | 279 ++++++++---------- .../VRM10/Runtime/IO/BufferAccessorAdapter.cs | 159 ---------- .../Runtime/IO/BufferAccessorAdapter.cs.meta | 11 - 3 files changed, 116 insertions(+), 333 deletions(-) delete mode 100644 Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs delete mode 100644 Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs.meta diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs index 643164887..57e0ab900 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs @@ -2,9 +2,7 @@ using System; using System.Collections.Generic; using System.Numerics; using System.Runtime.InteropServices; -using UniGLTF; using Unity.Collections; -using Unity.Collections.LowLevel.Unsafe; namespace UniGLTF { @@ -101,54 +99,6 @@ namespace UniGLTF Count = count; } - public static BufferAccessor Create(INativeArrayManager arrayManager, T[] list) where T : struct - { - var t = typeof(T); - var bytes = arrayManager.CreateNativeArray(list.Length * Marshal.SizeOf(t)); - var span = bytes.Reinterpret(1); - for (int i = 0; i < list.Length; ++i) - { - span[i] = list[i]; - } - AccessorValueType componentType = default(AccessorValueType); - AccessorVectorType accessorType = default(AccessorVectorType); - if (t == typeof(Vector2)) - { - componentType = AccessorValueType.FLOAT; - accessorType = AccessorVectorType.VEC2; - } - else if (t == typeof(Vector3)) - { - componentType = AccessorValueType.FLOAT; - accessorType = AccessorVectorType.VEC3; - } - else if (t == typeof(Vector4)) - { - componentType = AccessorValueType.FLOAT; - accessorType = AccessorVectorType.VEC4; - } - else if (t == typeof(Quaternion)) - { - componentType = AccessorValueType.FLOAT; - accessorType = AccessorVectorType.VEC4; - } - else if (t == typeof(SkinJoints)) - { - componentType = AccessorValueType.UNSIGNED_SHORT; - accessorType = AccessorVectorType.VEC4; - } - else if (t == typeof(int)) - { - componentType = AccessorValueType.UNSIGNED_INT; - accessorType = AccessorVectorType.SCALAR; - } - else - { - throw new NotImplementedException(); - } - return new BufferAccessor(arrayManager, bytes, componentType, accessorType, list.Length); - } - public override string ToString() { return $"{Stride}stride x{Count}"; @@ -218,112 +168,6 @@ namespace UniGLTF } } - public void Resize(int count) - { - if (count < Count) - { - throw new Exception(); - } - ToByteLength(Stride * count); - - Count = count; - } - - void ToByteLength(int byteLength) - { - var newBytes = ArrayManager.CreateNativeArray(byteLength); - NativeArray.Copy(Bytes, newBytes); - Bytes = newBytes; - } - - public void Extend(int count) - { - var oldLength = Bytes.Length; - ToByteLength(oldLength + Stride * count); - Count += count; - } - - // - // ArraySegment を新規に確保して置き換える - // - public void Append(BufferAccessor a, int offset = -1) - { - if (AccessorType != a.AccessorType) - { - System.Console.WriteLine(AccessorType.ToString() + "!=" + a.AccessorType.ToString()); - throw new Exception("different AccessorType"); - } - - // UNSIGNED_SHORT <-> UNSIGNED_INT の変換を許容して処理を続行 - // 統合メッシュのprimitiveのIndexBufferが65,535(ushort.MaxValue)を超える場合や、変換前にindexBuffer.ComponetTypeがushortとuint混在する場合など - if (ComponentType != a.ComponentType) - { - switch (a.ComponentType) - { - //ushort to uint - case AccessorValueType.UNSIGNED_SHORT: - { - var bytes = ArrayManager.Convert(a.Bytes.Reinterpret(1), (UInt16 x) => (UInt32)x).Reinterpret(Marshal.SizeOf()); - var accessor = new BufferAccessor(ArrayManager, bytes, AccessorValueType.UNSIGNED_INT, AccessorVectorType.SCALAR, a.Count); - a = accessor; - break; - } - - //uint to ushort (おそらく通ることはない) - case AccessorValueType.UNSIGNED_INT: - { - var bytes = ArrayManager.Convert(a.Bytes.Reinterpret(1), (UInt32 x) => (UInt16)x).Reinterpret(Marshal.SizeOf()); - var accessor = new BufferAccessor(ArrayManager, bytes, ComponentType, AccessorVectorType.SCALAR, a.Count); - a = accessor; - break; - } - - default: - throw new Exception("Cannot Convert ComponentType"); - - } - } - - // 連結した新しいバッファを確保 - var oldLength = Bytes.Length; - ToByteLength(oldLength + a.Bytes.Length); - // 後ろにコピー - NativeArray.Copy(a.Bytes, Bytes.GetSubArray(oldLength, Bytes.Length - oldLength)); - Count += a.Count; - - if (offset > 0) - { - // 後半にoffsetを足す - switch (ComponentType) - { - case AccessorValueType.UNSIGNED_SHORT: - { - var span = Bytes.GetSubArray(oldLength, Bytes.Length - oldLength).Reinterpret(1); - var ushortOffset = (ushort)offset; - for (int i = 0; i < span.Length; ++i) - { - span[i] += ushortOffset; - } - } - break; - - case AccessorValueType.UNSIGNED_INT: - { - var span = Bytes.GetSubArray(oldLength, Bytes.Length - oldLength).Reinterpret(1); - var uintOffset = (uint)offset; - for (int i = 0; i < span.Length; ++i) - { - span[i] += uintOffset; - } - } - break; - - default: - throw new NotImplementedException(); - } - } - } - public BufferAccessor Skip(int skipFrames) { skipFrames = Math.Min(Count, skipFrames); @@ -335,17 +179,126 @@ namespace UniGLTF return new BufferAccessor(ArrayManager, Bytes.GetSubArray(start, Bytes.Length - start), ComponentType, AccessorType, Count - skipFrames); } - public BufferAccessor CloneWithOffset(int offsetCount) + int AddViewTo(ExportingGltfData data, int bufferIndex, + int offset = 0, int count = 0) { - var offsetSize = Stride * offsetCount; - var buffer = ArrayManager.CreateNativeArray(offsetSize + Bytes.Length); - NativeArray.Copy(Bytes, buffer.GetSubArray(offsetSize, buffer.Length - offsetSize)); - return new BufferAccessor(ArrayManager, buffer, ComponentType, AccessorType, Count + offsetCount); + var stride = Stride; + if (count == 0) + { + count = Count; + } + var slice = Bytes.GetSubArray(offset * stride, count * stride); + return data.AppendToBuffer(slice); } - public void AddTo(Dictionary dict, string key) + glTFAccessor CreateGltfAccessor(int viewIndex, int count = 0, int byteOffset = 0) { - dict.Add(key, this); + if (count == 0) + { + count = Count; + } + return new glTFAccessor + { + bufferView = viewIndex, + byteOffset = byteOffset, + componentType = (glComponentType)ComponentType, + type = AccessorType.ToString(), + count = count, + }; + } + + int AddAccessorTo(ExportingGltfData data, int viewIndex, + Action, glTFAccessor> minMax = null, + int offset = 0, int count = 0) + { + var gltf = data.Gltf; + var accessorIndex = gltf.accessors.Count; + var accessor = CreateGltfAccessor(viewIndex, count, offset * Stride); + if (minMax != null) + { + minMax(Bytes, accessor); + } + gltf.accessors.Add(accessor); + return accessorIndex; + } + + public int AddAccessorTo(ExportingGltfData data, int bufferIndex, + // GltfBufferTargetType targetType, + bool useSparse, + Action, glTFAccessor> minMax = null, + int offset = 0, int count = 0) + { + if (ComponentType == AccessorValueType.FLOAT + && AccessorType == AccessorVectorType.VEC3 + ) + { + var values = GetSpan(); + // 巨大ポリゴンのモデル対策にValueTupleの型をushort -> uint へ + var sparseValuesWithIndex = new List>(); + for (int i = 0; i < values.Length; ++i) + { + var v = values[i]; + if (v != Vector3.Zero) + { + sparseValuesWithIndex.Add((i, v)); + } + } + + //var status = $"{sparseIndices.Count * 14}/{values.Length * 12}"; + if (useSparse + && sparseValuesWithIndex.Count > 0 // avoid empty sparse + && sparseValuesWithIndex.Count * 16 < values.Length * 12) + { + // use sparse + using (var sparseIndexBin = new NativeArray(sparseValuesWithIndex.Count * 4, Allocator.Persistent)) + using (var sparseValueBin = new NativeArray(sparseValuesWithIndex.Count * 12, Allocator.Persistent)) + { + var sparseIndexSpan = sparseIndexBin.Reinterpret(1); + var sparseValueSpan = sparseValueBin.Reinterpret(1); + + for (int i = 0; i < sparseValuesWithIndex.Count; ++i) + { + var (index, value) = sparseValuesWithIndex[i]; + sparseIndexSpan[i] = index; + sparseValueSpan[i] = value; + } + + var sparseIndexView = data.AppendToBuffer(sparseIndexBin); + var sparseValueView = data.AppendToBuffer(sparseValueBin); + + var accessorIndex = data.Gltf.accessors.Count; + var accessor = new glTFAccessor + { + componentType = (glComponentType)ComponentType, + type = AccessorType.ToString(), + count = Count, + byteOffset = -1, + sparse = new glTFSparse + { + count = sparseValuesWithIndex.Count, + indices = new glTFSparseIndices + { + componentType = (glComponentType)AccessorValueType.UNSIGNED_INT, + bufferView = sparseIndexView, + }, + values = new glTFSparseValues + { + bufferView = sparseValueView, + }, + } + }; + if (minMax != null) + { + minMax(sparseValueBin, accessor); + } + data.Gltf.accessors.Add(accessor); + return accessorIndex; + } + } + } + + var viewIndex = AddViewTo(data, bufferIndex, offset, count); + return AddAccessorTo(data, viewIndex, minMax, 0, count); } } } diff --git a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs b/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs deleted file mode 100644 index 4ef05afb8..000000000 --- a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs +++ /dev/null @@ -1,159 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Numerics; -using UniGLTF; -using Unity.Collections; - -namespace UniVRM10 -{ - public static class BufferAccessorAdapter - { - public static int TypeCount(this string type) - { - switch (type) - { - case "SCALAR": - return 1; - case "VEC2": - return 2; - case "VEC3": - return 3; - case "VEC4": - case "MAT2": - return 4; - case "MAT3": - return 9; - case "MAT4": - return 16; - default: - throw new NotImplementedException(); - } - } - - public static int AddViewTo(this BufferAccessor self, - ExportingGltfData data, int bufferIndex, - int offset = 0, int count = 0) - { - var stride = self.Stride; - if (count == 0) - { - count = self.Count; - } - var slice = self.Bytes.GetSubArray(offset * stride, count * stride); - return data.AppendToBuffer(slice); - } - - static glTFAccessor CreateGltfAccessor(this BufferAccessor self, - int viewIndex, int count = 0, int byteOffset = 0) - { - if (count == 0) - { - count = self.Count; - } - return new glTFAccessor - { - bufferView = viewIndex, - byteOffset = byteOffset, - componentType = (glComponentType)self.ComponentType, - type = self.AccessorType.ToString(), - count = count, - }; - } - - public static int AddAccessorTo(this BufferAccessor self, - ExportingGltfData data, int viewIndex, - Action, glTFAccessor> minMax = null, - int offset = 0, int count = 0) - { - var gltf = data.Gltf; - var accessorIndex = gltf.accessors.Count; - var accessor = self.CreateGltfAccessor(viewIndex, count, offset * self.Stride); - if (minMax != null) - { - minMax(self.Bytes, accessor); - } - gltf.accessors.Add(accessor); - return accessorIndex; - } - - public static int AddAccessorTo(this BufferAccessor self, - ExportingGltfData data, int bufferIndex, - // GltfBufferTargetType targetType, - bool useSparse, - Action, glTFAccessor> minMax = null, - int offset = 0, int count = 0) - { - if (self.ComponentType == AccessorValueType.FLOAT - && self.AccessorType == AccessorVectorType.VEC3 - ) - { - var values = self.GetSpan(); - // 巨大ポリゴンのモデル対策にValueTupleの型をushort -> uint へ - var sparseValuesWithIndex = new List>(); - for (int i = 0; i < values.Length; ++i) - { - var v = values[i]; - if (v != Vector3.Zero) - { - sparseValuesWithIndex.Add((i, v)); - } - } - - //var status = $"{sparseIndices.Count * 14}/{values.Length * 12}"; - if (useSparse - && sparseValuesWithIndex.Count > 0 // avoid empty sparse - && sparseValuesWithIndex.Count * 16 < values.Length * 12) - { - // use sparse - using (var sparseIndexBin = new NativeArray(sparseValuesWithIndex.Count * 4, Allocator.Persistent)) - using (var sparseValueBin = new NativeArray(sparseValuesWithIndex.Count * 12, Allocator.Persistent)) - { - var sparseIndexSpan = sparseIndexBin.Reinterpret(1); - var sparseValueSpan = sparseValueBin.Reinterpret(1); - - for (int i = 0; i < sparseValuesWithIndex.Count; ++i) - { - var (index, value) = sparseValuesWithIndex[i]; - sparseIndexSpan[i] = index; - sparseValueSpan[i] = value; - } - - var sparseIndexView = data.AppendToBuffer(sparseIndexBin); - var sparseValueView = data.AppendToBuffer(sparseValueBin); - - var accessorIndex = data.Gltf.accessors.Count; - var accessor = new glTFAccessor - { - componentType = (glComponentType)self.ComponentType, - type = self.AccessorType.ToString(), - count = self.Count, - byteOffset = -1, - sparse = new glTFSparse - { - count = sparseValuesWithIndex.Count, - indices = new glTFSparseIndices - { - componentType = (glComponentType)AccessorValueType.UNSIGNED_INT, - bufferView = sparseIndexView, - }, - values = new glTFSparseValues - { - bufferView = sparseValueView, - }, - } - }; - if (minMax != null) - { - minMax(sparseValueBin, accessor); - } - data.Gltf.accessors.Add(accessor); - return accessorIndex; - } - } - } - - var viewIndex = self.AddViewTo(data, bufferIndex, offset, count); - return self.AddAccessorTo(data, viewIndex, minMax, 0, count); - } - } -} diff --git a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs.meta b/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs.meta deleted file mode 100644 index 0f617c8c4..000000000 --- a/Assets/VRM10/Runtime/IO/BufferAccessorAdapter.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 9bdd7001c32368b4da917a0384e1ef86 -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: From 48f666939f832957192c7ec32d74ad3bd07e2acb Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 16 Feb 2022 12:46:59 +0900 Subject: [PATCH 5/8] private setter --- Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs index 57e0ab900..0c316d4da 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs @@ -78,7 +78,7 @@ namespace UniGLTF { public INativeArrayManager ArrayManager { get; } - public NativeArray Bytes; + public NativeArray Bytes { get; private set; } public AccessorValueType ComponentType; From c94691ff2595cc010e0f30d477406deea5976d6a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 16 Feb 2022 12:47:10 +0900 Subject: [PATCH 6/8] switch throw --- Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs index 8494e1bb8..fbe63b10d 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -259,6 +259,9 @@ namespace UniGLTF } break; } + + default: + throw new ArgumentOutOfRangeException(); } } return result; @@ -321,6 +324,9 @@ namespace UniGLTF } break; } + + default: + throw new ArgumentOutOfRangeException(); } } return result; From 72e0a6c95266ef9471dd7f44ddbd95da5399a483 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 16 Feb 2022 13:11:02 +0900 Subject: [PATCH 7/8] =?UTF-8?q?GetIndicesFromAccessor=20to=20GetIndicesFro?= =?UTF-8?q?mAccessorIndex.=20test=20=E4=BF=AE=E6=AD=A3=E3=80=82=20triangle?= =?UTF-8?q?=20flip=20=E3=81=97=E3=81=AA=E3=81=8F=E3=81=AA=E3=81=A3?= =?UTF-8?q?=E3=81=9F.=20=E3=81=8A=E3=82=88=E3=81=B3=20UNSIGNED=5FBYTE=20?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=81=A8=E3=81=93=E3=82=8D=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 | 3 +- .../Runtime/UniGLTF/IO/MeshIO/MeshContext.cs | 16 +++++- Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs | 51 +++++++++---------- 3 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 fbe63b10d..899c6bde7 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/GltfData.cs @@ -205,8 +205,9 @@ namespace UniGLTF } } - public BufferAccessor GetIndicesFromAccessor(glTFAccessor accessor) + public BufferAccessor GetIndicesFromAccessorIndex(int accessorIndex) { + var accessor = GLTF.accessors[accessorIndex]; var view = GLTF.bufferViews[accessor.bufferView]; return GetIntIndicesFromView(view, accessor.count, accessor.byteOffset, accessor.componentType); } diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs index 163b75e6b..db3b04e5a 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshContext.cs @@ -33,6 +33,18 @@ namespace UniGLTF { switch (src.ComponentType) { + case AccessorValueType.UNSIGNED_BYTE: + { + var indices = src.Bytes; + for (int i = 0; i < src.Count; i += 3) + { + _indices.Add(offset + indices[i + 2]); + _indices.Add(offset + indices[i + 1]); + _indices.Add(offset + indices[i]); + } + } + break; + case AccessorValueType.UNSIGNED_SHORT: { var indices = src.Bytes.Reinterpret(1); @@ -293,7 +305,7 @@ namespace UniGLTF if (indexBufferCount >= 0) { var indexOffset = _indices.Count; - var dataIndices = data.GetIndicesFromAccessor(data.GLTF.accessors[indexBufferCount]); + var dataIndices = data.GetIndicesFromAccessorIndex(indexBufferCount); PushIndices(dataIndices, vertexOffset); _subMeshes.Add(new SubMeshDescriptor(indexOffset, dataIndices.Count)); } @@ -432,7 +444,7 @@ namespace UniGLTF else { var indexOffset = _indices.Count; - var indices = data.GetIndicesFromAccessor(data.GLTF.accessors[primitive.indices]); + var indices = data.GetIndicesFromAccessorIndex(primitive.indices); PushIndices(indices, 0); _subMeshes.Add(new SubMeshDescriptor(indexOffset, indices.Count)); } diff --git a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs index 6fb1ecdd4..5f7cc4f2a 100644 --- a/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs +++ b/Assets/UniGLTF/Tests/UniGLTF/MeshTests.cs @@ -134,32 +134,29 @@ namespace UniGLTF var axisInverter = Axes.X.Create(); var unityMesh = MeshExportList.Create(go); - var (gltfMesh, blendShapeIndexMap) = meshExportSettings.DivideVertexBuffer - ? MeshExporter_DividedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings) - : MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings) - ; + var (gltfMesh, blendShapeIndexMap) = MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings); using (var parsed = GltfData.CreateFromGltfDataForTest(data.Gltf, data.BinBytes)) { { - var indices = parsed.GetIndicesFromAccessor(parsed.GLTF.accessors[gltfMesh.primitives[0].indices]).Bytes.Reinterpret(1); - Assert.AreEqual(0, indices[0]); + var indices = parsed.GetIndicesFromAccessorIndex(gltfMesh.primitives[0].indices).Bytes.Reinterpret(1); + Assert.AreEqual(5, indices[0]); Assert.AreEqual(1, indices[1]); - Assert.AreEqual(5, indices[2]); - Assert.AreEqual(5, indices[3]); + Assert.AreEqual(0, indices[2]); + Assert.AreEqual(4, indices[3]); Assert.AreEqual(1, indices[4]); - Assert.AreEqual(4, indices[5]); + Assert.AreEqual(5, indices[5]); } { - var indices = parsed.GetIndicesFromAccessor(parsed.GLTF.accessors[gltfMesh.primitives[1].indices]).Bytes.Reinterpret(1); - Assert.AreEqual(1, indices[0]); + var indices = parsed.GetIndicesFromAccessorIndex(gltfMesh.primitives[1].indices).Bytes.Reinterpret(1); + Assert.AreEqual(4, indices[0]); Assert.AreEqual(2, indices[1]); - Assert.AreEqual(4, indices[2]); - Assert.AreEqual(4, indices[3]); + Assert.AreEqual(1, indices[2]); + Assert.AreEqual(3, indices[3]); Assert.AreEqual(2, indices[4]); - Assert.AreEqual(3, indices[5]); + Assert.AreEqual(4, indices[5]); } var positions = parsed.GetArrayFromAccessor(gltfMesh.primitives[0].attributes.POSITION); @@ -185,21 +182,19 @@ namespace UniGLTF var axisInverter = Axes.X.Create(); var unityMesh = MeshExportList.Create(go); - var (gltfMesh, blendShapeIndexMap) = meshExportSettings.DivideVertexBuffer - ? MeshExporter_DividedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings) - : MeshExporter_SharedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings) - ; + + var (gltfMesh, blendShapeIndexMap) = MeshExporter_DividedVertexBuffer.Export(data, unityMesh, Materials, axisInverter, meshExportSettings); using (var parsed = GltfData.CreateFromGltfDataForTest(data.Gltf, data.BinBytes)) { { - var indices = parsed.GetIndicesFromAccessor(parsed.GLTF.accessors[gltfMesh.primitives[0].indices]).Bytes.Reinterpret(1); - Assert.AreEqual(0, indices[0]); + var indices = parsed.GetIndicesFromAccessorIndex(gltfMesh.primitives[0].indices).Bytes.Reinterpret(1); + Assert.AreEqual(3, indices[0]); Assert.AreEqual(1, indices[1]); - Assert.AreEqual(3, indices[2]); - Assert.AreEqual(3, indices[3]); + Assert.AreEqual(0, indices[2]); + Assert.AreEqual(2, indices[3]); Assert.AreEqual(1, indices[4]); - Assert.AreEqual(2, indices[5]); + Assert.AreEqual(3, indices[5]); } { var positions = parsed.GetArrayFromAccessor(gltfMesh.primitives[0].attributes.POSITION); @@ -207,13 +202,13 @@ namespace UniGLTF } { - var indices = parsed.GetIndicesFromAccessor(parsed.GLTF.accessors[gltfMesh.primitives[1].indices]).Bytes.Reinterpret(1); - Assert.AreEqual(0, indices[0]); + var indices = parsed.GetIndicesFromAccessorIndex(gltfMesh.primitives[1].indices).Bytes.Reinterpret(1); + Assert.AreEqual(3, indices[0]); Assert.AreEqual(1, indices[1]); - Assert.AreEqual(3, indices[2]); - Assert.AreEqual(3, indices[3]); + Assert.AreEqual(0, indices[2]); + Assert.AreEqual(2, indices[3]); Assert.AreEqual(1, indices[4]); - Assert.AreEqual(2, indices[5]); + Assert.AreEqual(3, indices[5]); } { var positions = parsed.GetArrayFromAccessor(gltfMesh.primitives[1].attributes.POSITION); From f47f754b01f49551e9efe10b6a87b88ece874c80 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Wed, 16 Feb 2022 13:46:26 +0900 Subject: [PATCH 8/8] =?UTF-8?q?BufferAccessor=20=E3=81=AE=E3=82=AB?= =?UTF-8?q?=E3=83=97=E3=82=BB=E3=83=AB=E5=8C=96=E3=82=92=E6=8E=A8=E9=80=B2?= =?UTF-8?q?=E3=80=82=E3=82=AB=E3=83=97=E3=82=BB=E3=83=AB=E5=8C=96=E3=82=92?= =?UTF-8?q?=E7=A0=B4=E3=82=8B=E3=81=8C=E4=BD=BF=E3=81=A3=E3=81=A6=E3=81=84?= =?UTF-8?q?=E3=81=AA=E3=81=84=E3=83=A1=E3=82=BD=E3=83=83=E3=83=89=E3=82=92?= =?UTF-8?q?=E5=89=8A=E9=99=A4=E3=80=82=E8=BF=91=E9=9A=A3=E3=81=AE=E6=9C=AA?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E3=82=82=E5=89=8A=E9=99=A4=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/UniGLTF/IO/BufferAccessor.cs | 6 +- Assets/VRM10/vrmlib/Runtime/Mesh.cs | 134 ------------------ 2 files changed, 3 insertions(+), 137 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs index 0c316d4da..3f3c40ff5 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/BufferAccessor.cs @@ -80,13 +80,13 @@ namespace UniGLTF public NativeArray Bytes { get; private set; } - public AccessorValueType ComponentType; + public AccessorValueType ComponentType { get; private set; } - public AccessorVectorType AccessorType; + public AccessorVectorType AccessorType { get; private set; } public int Stride => ComponentType.ByteSize() * AccessorType.TypeCount(); - public int Count; + public int Count { get; private set; } public int ByteLength => Stride * Count; diff --git a/Assets/VRM10/vrmlib/Runtime/Mesh.cs b/Assets/VRM10/vrmlib/Runtime/Mesh.cs index 0751bc610..8f6169aa2 100644 --- a/Assets/VRM10/vrmlib/Runtime/Mesh.cs +++ b/Assets/VRM10/vrmlib/Runtime/Mesh.cs @@ -62,139 +62,10 @@ namespace VrmLib public BufferAccessor IndexBuffer; - /// - /// indicesの最大値が65535未満(-1を避ける)ならばushort 型で、 - /// そうでなければ int型で IndexBufferを代入する - /// - public void AssignIndexBuffer(NativeArray indices) - { - bool isInt = false; - foreach (var i in indices) - { - if (i >= short.MaxValue) - { - isInt = true; - break; - } - } - - if (isInt) - { - if (IndexBuffer.Stride != 4) - { - IndexBuffer.ComponentType = AccessorValueType.UNSIGNED_INT; - if (IndexBuffer.AccessorType != AccessorVectorType.SCALAR) - { - throw new Exception(); - } - } - // 変換なし - IndexBuffer.Assign(indices); - } - else - { - // int to ushort - IndexBuffer.AssignAsShort(indices); - } - } - public TopologyType Topology = TopologyType.Triangles; public List Submeshes { private set; get; } = new List(); - public int SubmeshTotalDrawCount => Submeshes.Sum(x => x.DrawCount); - - public IEnumerable GetTriangles(int i) - { - var indices = IndexBuffer.GetAsIntArray(); - - var submesh = Submeshes[i]; - var submeshEnd = submesh.Offset + submesh.DrawCount; - for (int j = submesh.Offset; j < submeshEnd; j += 3) - { - var triangle = new Triangle( - indices[j], - indices[j + 1], - indices[j + 2] - ); - yield return triangle; - } - } - - public IEnumerable> Triangles - { - get - { - if (Topology != TopologyType.Triangles) - { - throw new InvalidOperationException(); - } - - var indices = IndexBuffer.GetAsIntArray(); - - for (int i = 0; i < Submeshes.Count; ++i) - { - var submesh = Submeshes[i]; - var submeshEnd = submesh.Offset + submesh.DrawCount; - for (int j = submesh.Offset; j < submeshEnd; j += 3) - { - var triangle = new Triangle( - indices[j], - indices[j + 1], - indices[j + 2] - ); - yield return (i, triangle); - } - } - } - } - - bool GetSubmeshOverlapped() where T : struct - { - var indices = IndexBuffer.GetSpan(); - var offset = 0; - var max = 0; - foreach (var x in Submeshes) - { - var submeshIndices = indices.Slice(offset, x.DrawCount); - var currentMax = 0; - foreach (var y in submeshIndices) - { - if (y < max) - { - return true; - } - currentMax = Math.Max(y, currentMax); - } - offset += x.DrawCount; - max = currentMax; - } - return false; - } - - public bool IsSubmeshOverlapped - { - get - { - if (Submeshes.Count <= 1) - { - return false; - } - - switch (IndexBuffer.ComponentType) - { - case AccessorValueType.UNSIGNED_SHORT: - return GetSubmeshOverlapped(); - - case AccessorValueType.UNSIGNED_INT: - return GetSubmeshOverlapped(); - - default: - throw new NotImplementedException(); - } - } - } - public List MorphTargets = new List(); public override string ToString() @@ -238,11 +109,6 @@ namespace VrmLib Topology = topology; } - public void RemoveUnusedSubmesh() - { - Submeshes = Submeshes.Where(x => x.DrawCount != 0).ToList(); - } - // Skin.Normalize public void ApplyRotationAndScaling(Matrix4x4 m) {