Skip to content

Commit 3df6633

Browse files
Fix OBJ mesh index buffer overflow by using UNSIGNED_INT for large models (#148)
1 parent 87c289f commit 3df6633

6 files changed

Lines changed: 14 additions & 1 deletion

File tree

lib/gltf/buffer-builder.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ export class BufferBuilder {
6767
return byteOffset
6868
}
6969

70+
addUint32Array(values: number[]): number {
71+
const byteOffset = this.offset
72+
this.ensureCapacity(values.length * 4)
73+
for (const value of values) {
74+
this.view.setUint32(this.offset, value, true)
75+
this.offset += 4
76+
}
77+
return byteOffset
78+
}
79+
7080
addBytes(bytes: Uint8Array): number {
7181
const byteOffset = this.offset
7282
this.ensureCapacity(bytes.length)

lib/gltf/gltf-builder.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ export class GLTFBuilder {
239239
const indicesAccessorIndex = this.addAccessor(
240240
transformedMeshData.indices,
241241
"SCALAR",
242-
COMPONENT_TYPE.UNSIGNED_SHORT,
242+
COMPONENT_TYPE.UNSIGNED_INT,
243243
TARGET.ELEMENT_ARRAY_BUFFER,
244244
)
245245

@@ -722,6 +722,9 @@ export class GLTFBuilder {
722722
} else if (componentType === COMPONENT_TYPE.UNSIGNED_SHORT) {
723723
byteOffset = this.bufferBuilder.addUint16Array(data)
724724
byteLength = data.length * 2
725+
} else if (componentType === COMPONENT_TYPE.UNSIGNED_INT) {
726+
byteOffset = this.bufferBuilder.addUint32Array(data)
727+
byteLength = data.length * 4
725728
} else {
726729
throw new Error(`Unsupported component type: ${componentType}`)
727730
}
2.16 KB
Loading
2.67 KB
Loading
2.27 KB
Loading
1.03 KB
Loading

0 commit comments

Comments
 (0)