diff --git a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs index 2ea4400cd..bbffdb6aa 100644 --- a/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs +++ b/Assets/UniGLTF/Runtime/Extensions/glTFExtensions.cs @@ -72,6 +72,18 @@ 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; + } else if (typeof(T) == typeof(uint)) { return glComponentType.UNSIGNED_INT; 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)); } diff --git a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs index 5fdf3fe61..34c5300da 100644 --- a/Assets/VRM10/Runtime/Migration/MeshUpdater.cs +++ b/Assets/VRM10/Runtime/Migration/MeshUpdater.cs @@ -76,7 +76,43 @@ 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 1: + { + // byte + var byte_indices = mesh.IndexBuffer.GetSpan(); + indices = SpanLike.Create(byte_indices.Length); + for (int i = 0; i < byte_indices.Length; ++i) + { + indices[i] = byte_indices[i]; + } + break; + } + + 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);