Skip to content

Commit 1279d5b

Browse files
committed
attempting to fix accessors with missing BufferView [WIP]
1 parent 5787b3e commit 1279d5b

File tree

16 files changed

+382
-167
lines changed

16 files changed

+382
-167
lines changed

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<!-- Configuration =================================================================================== -->
1313

1414
<PropertyGroup>
15-
<LangVersion>8.0</LangVersion>
15+
<LangVersion>10.0</LangVersion>
1616
<IsPackable>true</IsPackable>
1717
</PropertyGroup>
1818

src/SharpGLTF.Core/Diagnostics/DebugViews.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ internal sealed class _AccessorDebugProxy
5353

5454
public String Identity => $"Accessor[{_Value.LogicalIndex}] {_Value.Name}";
5555

56-
public Schema2.BufferView Source => _Value.SourceBufferView;
56+
public Schema2.BufferView Source => _Value.TryGetBufferView(out var bv) ? bv : null;
5757

5858
public (Schema2.DimensionType Dimensions, Schema2.EncodingType Encoding, bool Normalized) Format => (_Value.Dimensions, _Value.Encoding, _Value.Normalized);
5959

@@ -62,14 +62,15 @@ public Object[] Items
6262
get
6363
{
6464
if (_Value == null) return null;
65-
if (Source == null) return null;
6665

67-
if (Source.IsIndexBuffer)
66+
if (!_Value.TryGetBufferView(out var bv)) return null;
67+
68+
if (bv.IsIndexBuffer)
6869
{
6970
return _Value.AsIndicesArray().Cast<Object>().ToArray();
7071
}
7172

72-
if (Source.IsVertexBuffer)
73+
if (bv.IsVertexBuffer)
7374
{
7475
if (_Value.Dimensions == Schema2.DimensionType.SCALAR) return _Value.AsScalarArray().Cast<Object>().ToArray();
7576
if (_Value.Dimensions == Schema2.DimensionType.VEC2) return _Value.AsVector2Array().Cast<Object>().ToArray();
@@ -79,15 +80,21 @@ public Object[] Items
7980

8081
if (_Value.Dimensions == Schema2.DimensionType.MAT4) return _Value.AsMatrix4x4Array().Cast<Object>().ToArray();
8182

82-
var itemByteSz = _Value.Format.ByteSize;
83-
var byteStride = Math.Max(_Value.SourceBufferView.ByteStride, itemByteSz);
84-
var items = new ArraySegment<Byte>[_Value.Count];
83+
// fallback to plain bytes
8584

86-
var buffer = _Value.SourceBufferView.Content.Slice(_Value.ByteOffset, _Value.Count * byteStride);
85+
var items = new ArraySegment<Byte>[_Value.Count];
8786

88-
for (int i = 0; i < items.Length; ++i )
87+
if (_Value.TryGetBufferView(out var bufferView))
8988
{
90-
items[i] = buffer.Slice(i * byteStride, itemByteSz);
89+
var itemByteSz = _Value.Format.ByteSize;
90+
var byteStride = Math.Max(bufferView.ByteStride, itemByteSz);
91+
92+
var buffer = bufferView.Content.Slice(_Value.ByteOffset, _Value.Count * byteStride);
93+
94+
for (int i = 0; i < items.Length; ++i)
95+
{
96+
items[i] = buffer.Slice(i * byteStride, itemByteSz);
97+
}
9198
}
9299

93100
return items.Cast<Object>().ToArray();

src/SharpGLTF.Core/Diagnostics/DebuggerDisplay.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,15 @@ public static string ToReportShort(this Accessor accessor)
7272

7373
public static string ToReportLong(this Accessor accessor)
7474
{
75-
var path = string.Empty;
76-
77-
var bv = accessor.SourceBufferView;
75+
var path = string.Empty;
7876

79-
if (bv.IsVertexBuffer) path += "VertexBuffer";
80-
else if (bv.IsIndexBuffer) path += "IndexBuffer";
81-
else path += "BufferView";
82-
path += $"[{bv.LogicalIndex}ᴵᵈˣ] ⇨";
77+
if (accessor.TryGetBufferView(out var bv))
78+
{
79+
if (bv.IsVertexBuffer) path += "VertexBuffer";
80+
else if (bv.IsIndexBuffer) path += "IndexBuffer";
81+
else path += "BufferView";
82+
path += $"[{bv.LogicalIndex}ᴵᵈˣ] ⇨";
83+
}
8384

8485
path += $" Accessor[{accessor.LogicalIndex}ᴵᵈˣ] Offset:{accessor.ByteOffset}ᴮʸᵗᵉˢ ⇨";
8586

@@ -140,12 +141,16 @@ string toShort(string name, Accessor accessor)
140141
case PrimitiveType.LINES:
141142
case PrimitiveType.LINE_LOOP:
142143
case PrimitiveType.LINE_STRIP:
143-
pcount = indices.HasValue ? prim.DrawPrimitiveType.GetLinesIndices(indices.Value).Count() : prim.DrawPrimitiveType.GetLinesIndices(vcount).Count();
144+
pcount = indices != null
145+
? prim.DrawPrimitiveType.GetLinesIndices(indices).Count()
146+
: prim.DrawPrimitiveType.GetLinesIndices(vcount).Count();
144147
break;
145148
case PrimitiveType.TRIANGLES:
146149
case PrimitiveType.TRIANGLE_FAN:
147150
case PrimitiveType.TRIANGLE_STRIP:
148-
pcount = indices.HasValue ? prim.DrawPrimitiveType.GetTrianglesIndices(indices.Value).Count() : prim.DrawPrimitiveType.GetTrianglesIndices(vcount).Count();
151+
pcount = indices != null
152+
? prim.DrawPrimitiveType.GetTrianglesIndices(indices).Count()
153+
: prim.DrawPrimitiveType.GetTrianglesIndices(vcount).Count();
149154
break;
150155
}
151156

src/SharpGLTF.Core/Memory/ColorArray.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace SharpGLTF.Memory
1414
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an array of <see cref="Vector4"/> values.
1515
/// </summary>
1616
[System.Diagnostics.DebuggerDisplay("Color4[{Count}]")]
17-
public readonly struct ColorArray : IList<Vector4>, IReadOnlyList<Vector4>
17+
public readonly struct ColorArray : IAccessorArray<Vector4>
1818
{
1919
#region constructors
2020

src/SharpGLTF.Core/Memory/FloatingArrays.cs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ public Single this[int index]
225225
}
226226

227227
/// <summary>
228-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{single}"/>.
228+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{single}"/>.
229229
/// </summary>
230230
[System.Diagnostics.DebuggerDisplay("Float[{Count}]")]
231-
public readonly struct ScalarArray : IList<Single>, IReadOnlyList<Single>
231+
public readonly struct ScalarArray : IAccessorArray<Single>
232232
{
233233
#region constructors
234234

@@ -321,10 +321,10 @@ public void Fill(IEnumerable<Single> values, int dstStart = 0)
321321
}
322322

323323
/// <summary>
324-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{Vector2}"/>.
324+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{Vector2}"/>.
325325
/// </summary>
326326
[System.Diagnostics.DebuggerDisplay("Vector2[{Count}]")]
327-
public readonly struct Vector2Array : IList<Vector2>, IReadOnlyList<Vector2>
327+
public readonly struct Vector2Array : IAccessorArray<Vector2>
328328
{
329329
#region constructors
330330

@@ -425,10 +425,10 @@ public void Fill(IEnumerable<Vector2> values, int dstStart = 0)
425425
}
426426

427427
/// <summary>
428-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{Vector3}"/>.
428+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{Vector3}"/>.
429429
/// </summary>
430430
[System.Diagnostics.DebuggerDisplay("Vector3[{Count}]")]
431-
public readonly struct Vector3Array : IList<Vector3>, IReadOnlyList<Vector3>
431+
public readonly struct Vector3Array : IAccessorArray<Vector3>
432432
{
433433
#region constructors
434434

@@ -530,10 +530,10 @@ public void Fill(IEnumerable<Vector3> values, int dstStart = 0)
530530
}
531531

532532
/// <summary>
533-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{Vector4}"/>.
533+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{Vector4}"/>.
534534
/// </summary>
535535
[System.Diagnostics.DebuggerDisplay("Vector4[{Count}]")]
536-
public readonly struct Vector4Array : IList<Vector4>, IReadOnlyList<Vector4>
536+
public readonly struct Vector4Array : IAccessorArray<Vector4>
537537
{
538538
#region constructors
539539

@@ -636,10 +636,10 @@ public void Fill(IEnumerable<Vector4> values, int dstStart = 0)
636636
}
637637

638638
/// <summary>
639-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{Quaternion}"/>.
639+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{Quaternion}"/>.
640640
/// </summary>
641641
[System.Diagnostics.DebuggerDisplay("Quaternion[{Count}]")]
642-
public readonly struct QuaternionArray : IList<Quaternion>, IReadOnlyList<Quaternion>
642+
public readonly struct QuaternionArray : IAccessorArray<Quaternion>
643643
{
644644
#region constructors
645645

@@ -720,14 +720,14 @@ public void Fill(IEnumerable<Quaternion> values, int dstStart = 0)
720720
}
721721

722722
/// <summary>
723-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{Matrix3x2}"/>.
723+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{Matrix3x2}"/>.
724724
/// </summary>
725725
/// <remarks>
726726
/// <see cref="Vector"/> namespace doesn't support a 2x2 matrix, so the array is<br/>
727727
/// decoded as a Matrix2x2 matrix internally, but exposed as a <see cref="Matrix3x2"/>.
728728
/// </remarks>
729729
[System.Diagnostics.DebuggerDisplay("Matrix2x2[{Count}]")]
730-
public readonly struct Matrix2x2Array : IList<Matrix3x2>, IReadOnlyList<Matrix3x2>
730+
public readonly struct Matrix2x2Array : IAccessorArray<Matrix3x2>
731731
{
732732
#region constructors
733733

@@ -813,10 +813,10 @@ public void Fill(IEnumerable<Matrix3x2> values, int dstStart = 0)
813813
}
814814

815815
/// <summary>
816-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{Matrix3x2}"/>.
816+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{Matrix3x2}"/>.
817817
/// </summary>
818818
[System.Diagnostics.DebuggerDisplay("Matrix3x2[{Count}]")]
819-
public readonly struct Matrix3x2Array : IList<Matrix3x2>, IReadOnlyList<Matrix3x2>
819+
public readonly struct Matrix3x2Array : IAccessorArray<Matrix3x2>
820820
{
821821
#region constructors
822822

@@ -904,14 +904,14 @@ public void Fill(IEnumerable<Matrix3x2> values, int dstStart = 0)
904904
}
905905

906906
/// <summary>
907-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{Matrix4x4}"/>.
907+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{Matrix4x4}"/>.
908908
/// </summary>
909909
/// <remarks>
910910
/// <see cref="Vector"/> namespace doesn't support a 3x3 matrix, so the array is<br/>
911911
/// decoded as a Matrix3x3 matrix internally, but exposed as a <see cref="Matrix4x4"/>.
912912
/// </remarks>
913913
[System.Diagnostics.DebuggerDisplay("Matrix3x3[{Count}]")]
914-
public readonly struct Matrix3x3Array : IList<Matrix4x4>, IReadOnlyList<Matrix4x4>
914+
public readonly struct Matrix3x3Array : IAccessorArray<Matrix4x4>
915915
{
916916
#region constructors
917917

@@ -1003,14 +1003,14 @@ public void Fill(IEnumerable<Matrix4x4> values, int dstStart = 0)
10031003
}
10041004

10051005
/// <summary>
1006-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{Matrix4x4}"/>.
1006+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{Matrix4x4}"/>.
10071007
/// </summary>
10081008
/// <remarks>
10091009
/// <see cref="Vector"/> namespace doesn't support a 4x3 matrix, so the array is<br/>
10101010
/// decoded as a Matrix4x3 matrix internally, but exposed as a <see cref="Matrix4x4"/>.
10111011
/// </remarks>
10121012
[System.Diagnostics.DebuggerDisplay("Matrix4x3[{Count}]")]
1013-
public readonly struct Matrix4x3Array : IList<Matrix4x4>, IReadOnlyList<Matrix4x4>
1013+
public readonly struct Matrix4x3Array : IAccessorArray<Matrix4x4>
10141014
{
10151015
#region constructors
10161016

@@ -1105,10 +1105,10 @@ public void Fill(IEnumerable<Matrix4x4> values, int dstStart = 0)
11051105
}
11061106

11071107
/// <summary>
1108-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{Matrix4x4}"/>.
1108+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{Matrix4x4}"/>.
11091109
/// </summary>
11101110
[System.Diagnostics.DebuggerDisplay("Matrix4x4[{Count}]")]
1111-
public readonly struct Matrix4x4Array : IList<Matrix4x4>, IReadOnlyList<Matrix4x4>
1111+
public readonly struct Matrix4x4Array : IAccessorArray<Matrix4x4>
11121112
{
11131113
#region constructors
11141114

@@ -1207,10 +1207,10 @@ public void Fill(IEnumerable<Matrix4x4> values, int dstStart = 0)
12071207
}
12081208

12091209
/// <summary>
1210-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an IList{Single[]}/>.
1210+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{Single[]}"/>.
12111211
/// </summary>
12121212
[System.Diagnostics.DebuggerDisplay("Float[][{Count}]")]
1213-
public readonly struct MultiArray : IList<Single[]>, IReadOnlyList<Single[]>
1213+
public readonly struct MultiArray : IAccessorArray<Single[]>
12141214
{
12151215
#region constructors
12161216
public MultiArray(BYTES source, int byteOffset, int itemsCount, int byteStride, int dimensions, ENCODING encoding, Boolean normalized)

src/SharpGLTF.Core/Memory/IntegerArrays.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
namespace SharpGLTF.Memory
1111
{
1212
/// <summary>
13-
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IList{UInt32}"/>.
13+
/// Wraps an encoded <see cref="BYTES"/> and exposes it as an <see cref="IAccessorArray{UInt32}"/>.
1414
/// </summary>
1515
[System.Diagnostics.DebuggerDisplay("Integer[{Count}]")]
16-
public readonly struct IntegerArray : IList<UInt32>, IReadOnlyList<UInt32>
16+
public readonly struct IntegerArray : IAccessorArray<UInt32>
1717
{
1818
#region constructors
1919

0 commit comments

Comments
 (0)