Preventing WebGL2 TypedArray RangeErrors From Misaligned glTF Accessors
How Component Alignment and Byte Stride Govern GPU TypedArray Views
When Three.js or Babylon.js parses a binary GLB, it constructs zero-copy JavaScript TypedArrays—such as Float32Array or Uint16Array—directly over the ArrayBuffer at byteOffset = bufferView.byteOffset + accessor.byteOffset. JavaScript engines enforce a strict hardware rule: a Float32Array (componentType 5126) or Uint32Array (5125) throws an immediate RangeError if its starting byte offset is not an exact multiple of 4, and a Uint16Array (5123) fails if odd-aligned. Custom C++, Rust, or Python GLB packers frequently forget 2-byte padding after an odd count of UNSIGNED_SHORT triangle indices (for instance, 3 indices = 6 bytes, leaving the next bufferView at offset 78 instead of 80!).
Our accessor validator checks every accessor’s component byte width, element count, effective byteStride, total byteSpan ((count - 1) × stride + elementBytes), modulo alignment, and upper bound against both its parent bufferView and the physical BIN chunk.
- Flags MISALIGNED offsets before Float32Array or Uint16Array constructors crash browser loaders.
- Catches OUT_OF_BOUNDS spans caused by truncated BIN chunks or off-by-one stride calculations.
- Pairs custom byte-span math with official Khronos gltf-validator buffer diagnostics.
Verifying 2-Byte Index Padding in animated-child.glb
Look closely at animated-child.glb: bufferViews 0 and 1 occupy 36 bytes each (offsets 0 and 36), bufferView 2 holds 3 UNSIGNED_SHORT indices = 6 bytes at offset 72..78, and the exporter properly inserts 2 pad bytes so Float32 animation timestamps in bufferView 3 start cleanly at 4-byte-aligned offset 80.