Skip to content

Commit

Permalink
Fix buffer view target overrides in glTF exporter (#7152)
Browse files Browse the repository at this point in the history
  • Loading branch information
willeastcott authored Nov 28, 2024
1 parent c869024 commit 8376de2
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions src/extras/exporters/gltf-exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,15 +242,21 @@ class GltfExporter extends CoreExporter {

// FIXME: don't create the function every time
const addBufferView = (target, byteLength, byteOffset, byteStride) => {

const bufferView = {
target: target,
buffer: 0,
byteLength: byteLength,
byteOffset: byteOffset,
byteStride: byteStride
byteOffset: byteOffset
};

// Only add target if it's a vertex or index buffer
if (target === ARRAY_BUFFER || target === ELEMENT_ARRAY_BUFFER) {
bufferView.target = target;
}

if (byteStride !== undefined) {
bufferView.byteStride = byteStride;
}

return json.bufferViews.push(bufferView) - 1;
};

Expand All @@ -260,42 +266,31 @@ class GltfExporter extends CoreExporter {

const format = buffer.getFormat();
if (format.interleaved) {

const bufferViewIndex = addBufferView(ARRAY_BUFFER, arrayBuffer.byteLength, offset, format.size);
resources.bufferViewMap.set(buffer, [bufferViewIndex]);

} else {

// generate buffer view per element
const bufferViewIndices = [];
for (const element of format.elements) {

const bufferViewIndex = addBufferView(
ARRAY_BUFFER,
element.size * format.vertexCount,
offset + element.offset,
element.size
);
bufferViewIndices.push(bufferViewIndex);

}

resources.bufferViewMap.set(buffer, bufferViewIndices);
}

} else if (buffer instanceof IndexBuffer) { // index buffer
} else if (buffer instanceof IndexBuffer) {
arrayBuffer = buffer.lock();

const bufferViewIndex = addBufferView(ARRAY_BUFFER, arrayBuffer.byteLength, offset);
const bufferViewIndex = addBufferView(ELEMENT_ARRAY_BUFFER, arrayBuffer.byteLength, offset);
resources.bufferViewMap.set(buffer, [bufferViewIndex]);

} else {
// buffer is an array buffer
// buffer is an array buffer (for images)
arrayBuffer = buffer;

const bufferViewIndex = addBufferView(ELEMENT_ARRAY_BUFFER, arrayBuffer.byteLength, offset);
const bufferViewIndex = addBufferView(undefined, arrayBuffer.byteLength, offset);
resources.bufferViewMap.set(buffer, [bufferViewIndex]);

}

// increment buffer by the size of the array buffer to allocate buffer with enough space
Expand Down

0 comments on commit 8376de2

Please sign in to comment.