From 9a950f8fab7365f295d9a8d83faa0bc98a4322e6 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 14 Oct 2021 18:26:37 +0900 Subject: [PATCH 1/7] =?UTF-8?q?primitive.indices=20=E3=81=AE=E5=9E=8B?= =?UTF-8?q?=E3=82=92=20ushort=20=E3=81=AB=E5=AF=BE=E5=BF=9C=E3=81=95?= =?UTF-8?q?=E3=81=9B=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs | 8 ++++++-- .../UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs index 2ea4400cd..5ef920f63 100644 --- a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs +++ b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs @@ -72,6 +72,10 @@ namespace UniGLTF { return cv.ComponentType; } + else if (typeof(T) == typeof(ushort)) + { + return glComponentType.UNSIGNED_SHORT; + } else if (typeof(T) == typeof(uint)) { return glComponentType.UNSIGNED_INT; @@ -119,10 +123,10 @@ namespace UniGLTF } } - public static int ExtendBufferAndGetAccessorIndex(this glTF gltf, int bufferIndex, T[] array, + public static int ExtendBufferAndGetAccessorIndex(this glTF gltf, int bufferIndex, IReadOnlyList list, glBufferTarget target = glBufferTarget.NONE) where T : struct { - return gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, new ArraySegment(array), target); + return gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, new ArraySegment(list.ToArray()), target); } public static int ExtendBufferAndGetAccessorIndex(this glTF gltf, int bufferIndex, diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs index 192077151..1c4be08d9 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs @@ -127,7 +127,7 @@ namespace UniGLTF indices.Add((uint)i0); } - var indicesAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, indices.ToArray(), glBufferTarget.ELEMENT_ARRAY_BUFFER); + var indicesAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, indices, glBufferTarget.ELEMENT_ARRAY_BUFFER); if (indicesAccessorIndex < 0) { // https://github.com/vrm-c/UniVRM/issues/664 From 0a9b989d2509de2ac2e60326b4923db67871f062 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 14 Oct 2021 18:43:46 +0900 Subject: [PATCH 2/7] =?UTF-8?q?bug=20=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs index 9c7cdd3e0..71ce55e5f 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/SpanLike.cs @@ -475,10 +475,10 @@ namespace UniGLTF return new SpanLike(bytes, Marshal.SizeOf(), getset.Getter, getset.Setter); } - public static SpanLike Create(int count) where T : struct + public static SpanLike Create(int itemCount) where T : struct { var itemSize = Marshal.SizeOf(); - var array = new byte[count / itemSize]; + var array = new byte[itemCount * itemSize]; return Wrap(new ArraySegment(array)); } From f52e6b481f96f9793ced0bcf572ce1053a5e48a7 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 14 Oct 2021 18:44:15 +0900 Subject: [PATCH 3/7] =?UTF-8?q?ushort=20=E3=81=AE=20index=20=E3=82=92=20ui?= =?UTF-8?q?nt=20=E3=81=AB=E5=A4=89=E6=8F=9B=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/VRM10/Runtime/Migration/MeshUpdater.cs | 26 ++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs index 5fdf3fe61..d6b62d08b 100644 --- a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs +++ b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs @@ -76,7 +76,31 @@ namespace UniVRM10 // update Mesh foreach (var (gltfMesh, mesh) in Enumerable.Zip(gltf.meshes, model.MeshGroups, (l, r) => (l, r.Meshes[0]))) { - var indices = mesh.IndexBuffer.GetSpan(); + SpanLike indices; + switch (mesh.IndexBuffer.Stride) + { + case 2: + { + // ushort + var ushort_indices = mesh.IndexBuffer.GetSpan(); + indices = SpanLike.Create(ushort_indices.Length); + for (int i = 0; i < ushort_indices.Length; ++i) + { + indices[i] = ushort_indices[i]; + } + break; + } + + case 4: + { + // uint + indices = mesh.IndexBuffer.GetSpan(); + break; + } + + default: + throw new NotImplementedException(); + } var position = AddAccessor(mesh.VertexBuffer.Positions); var normal = AddAccessor(mesh.VertexBuffer.Normals); var uv = AddAccessor(mesh.VertexBuffer.TexCoords); From 9f1207cfa987d54c50c5fcc450a9b12e1e5ae98d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 14 Oct 2021 18:45:14 +0900 Subject: [PATCH 4/7] =?UTF-8?q?=E3=81=84=E3=81=A1=E3=81=8A=E3=81=86=20byte?= =?UTF-8?q?=20=E5=9E=8B=E3=82=82=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/VRM10/Runtime/Migration/MeshUpdater.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs index d6b62d08b..3756b3257 100644 --- a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs +++ b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs @@ -79,6 +79,18 @@ namespace UniVRM10 SpanLike indices; switch (mesh.IndexBuffer.Stride) { + case 1: + { + // byte + var ushort_indices = mesh.IndexBuffer.GetSpan(); + indices = SpanLike.Create(ushort_indices.Length); + for (int i = 0; i < ushort_indices.Length; ++i) + { + indices[i] = ushort_indices[i]; + } + break; + } + case 2: { // ushort From c10d19c5804cfbf51f01780628626f3e9e9f1206 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 14 Oct 2021 18:58:39 +0900 Subject: [PATCH 5/7] byte_indices --- Assets/VRM10/Runtime/Migration/MeshUpdater.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs index 3756b3257..34c5300da 100644 --- a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs +++ b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs @@ -82,11 +82,11 @@ namespace UniVRM10 case 1: { // byte - var ushort_indices = mesh.IndexBuffer.GetSpan(); - indices = SpanLike.Create(ushort_indices.Length); - for (int i = 0; i < ushort_indices.Length; ++i) + var byte_indices = mesh.IndexBuffer.GetSpan(); + indices = SpanLike.Create(byte_indices.Length); + for (int i = 0; i < byte_indices.Length; ++i) { - indices[i] = ushort_indices[i]; + indices[i] = byte_indices[i]; } break; } From 018f74402b6d6df24584f7f3cf13d4c41f50552a Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 14 Oct 2021 19:00:40 +0900 Subject: [PATCH 6/7] =?UTF-8?q?ToArray=20=E3=82=92=E6=9C=80=E5=B0=8F?= =?UTF-8?q?=E5=8C=96=E3=81=99=E3=82=8B=E3=81=B9=E3=81=8F=E3=82=82=E3=81=A3?= =?UTF-8?q?=E3=81=A8=E3=81=BE=E3=81=98=E3=82=81=E3=81=AB=E3=82=84=E3=82=8B?= =?UTF-8?q?=E3=81=B9=E3=81=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs | 4 ++-- .../UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs index 5ef920f63..494610167 100644 --- a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs +++ b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs @@ -123,10 +123,10 @@ namespace UniGLTF } } - public static int ExtendBufferAndGetAccessorIndex(this glTF gltf, int bufferIndex, IReadOnlyList list, + public static int ExtendBufferAndGetAccessorIndex(this glTF gltf, int bufferIndex, T[] array, glBufferTarget target = glBufferTarget.NONE) where T : struct { - return gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, new ArraySegment(list.ToArray()), target); + return gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, new ArraySegment(array), target); } public static int ExtendBufferAndGetAccessorIndex(this glTF gltf, int bufferIndex, diff --git a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs index 1c4be08d9..192077151 100644 --- a/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs +++ b/Assets/UniGLTF/Runtime/UniGLTF/IO/MeshIO/MeshExporter_SharedVertexBuffer.cs @@ -127,7 +127,7 @@ namespace UniGLTF indices.Add((uint)i0); } - var indicesAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, indices, glBufferTarget.ELEMENT_ARRAY_BUFFER); + var indicesAccessorIndex = gltf.ExtendBufferAndGetAccessorIndex(bufferIndex, indices.ToArray(), glBufferTarget.ELEMENT_ARRAY_BUFFER); if (indicesAccessorIndex < 0) { // https://github.com/vrm-c/UniVRM/issues/664 From 907044ed783bb932e4ca8c5ea29449c00a8f18ab Mon Sep 17 00:00:00 2001 From: ousttrue Date: Thu, 14 Oct 2021 19:05:15 +0900 Subject: [PATCH 7/7] byte, sbyte --- Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs index 494610167..bbffdb6aa 100644 --- a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs +++ b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs @@ -72,6 +72,14 @@ namespace UniGLTF { return cv.ComponentType; } + else if (typeof(T) == typeof(sbyte)) + { + return glComponentType.BYTE; + } + else if (typeof(T) == typeof(byte)) + { + return glComponentType.UNSIGNED_BYTE; + } else if (typeof(T) == typeof(ushort)) { return glComponentType.UNSIGNED_SHORT;