in packages/gltf-gen/src/meshprimitive.ts [183:231]
private _serializeIndices(document: GLTF.GlTf, data: Buffer): GLTF.GlTfId {
if (!document.bufferViews) {
document.bufferViews = [];
}
if (!document.accessors) {
document.accessors = [];
}
let lastBV: GLTF.BufferView;
if (document.bufferViews.length > 0) {
lastBV = document.bufferViews[document.bufferViews.length - 1];
}
const bufferView: GLTF.BufferView = {
buffer: 0,
byteOffset: lastBV ? roundUpToNextMultipleOf4(lastBV.byteOffset + lastBV.byteLength) : 0,
byteLength: (this.vertices.length <= 65535 ? 2 : 4) * this.triangles.length
};
const bufferViewData = data.slice(bufferView.byteOffset, bufferView.byteOffset + bufferView.byteLength);
const accessor: GLTF.Accessor = {
bufferView: document.bufferViews.length,
componentType: this.vertices.length <= 65535 ? AccessorComponentType.UShort : AccessorComponentType.UInt,
type: AccessorType.Scalar,
count: this.triangles.length
};
if (this.vertices.length <= 65535) {
for (let ti = 0; ti < this.triangles.length; ti++) {
bufferViewData.writeUInt16LE(this.triangles[ti], 2 * ti);
}
} else {
for (let ti = 0; ti < this.triangles.length; ti++) {
bufferViewData.writeUInt32LE(this.triangles[ti], 4 * ti);
}
}
// fill padding with zeros
for (let i = roundUpToNextMultipleOf4(bufferView.byteOffset + bufferView.byteLength) - 1;
i >= bufferView.byteOffset + bufferView.byteLength;
i--) {
data.writeUInt8(0, i);
}
document.bufferViews.push(bufferView);
document.accessors.push(accessor);
return document.accessors.length - 1;
}