Merge pull request #1308 from ousttrue/fix/ushort_indices

[1.0] ushort, byte 型の indices を持つモデルのマイグレーション
This commit is contained in:
ousttrue
2021-10-14 21:33:17 +09:00
committed by GitHub
3 changed files with 51 additions and 3 deletions

View File

@@ -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;

View File

@@ -475,10 +475,10 @@ namespace UniGLTF
return new SpanLike<T>(bytes, Marshal.SizeOf<T>(), getset.Getter, getset.Setter);
}
public static SpanLike<T> Create<T>(int count) where T : struct
public static SpanLike<T> Create<T>(int itemCount) where T : struct
{
var itemSize = Marshal.SizeOf<T>();
var array = new byte[count / itemSize];
var array = new byte[itemCount * itemSize];
return Wrap<T>(new ArraySegment<byte>(array));
}

View File

@@ -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<uint>();
SpanLike<uint> indices;
switch (mesh.IndexBuffer.Stride)
{
case 1:
{
// byte
var byte_indices = mesh.IndexBuffer.GetSpan<byte>();
indices = SpanLike.Create<uint>(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<ushort>();
indices = SpanLike.Create<uint>(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<uint>();
break;
}
default:
throw new NotImplementedException();
}
var position = AddAccessor<Vector3>(mesh.VertexBuffer.Positions);
var normal = AddAccessor<Vector3>(mesh.VertexBuffer.Normals);
var uv = AddAccessor<Vector2>(mesh.VertexBuffer.TexCoords);