Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix gltf accessor's bufferView could be undefined #2013

Merged
merged 2 commits into from
Feb 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/loader/src/gltf/GLTFUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,8 @@ export class GLTFUtils {
accessor: IAccessor
): Promise<BufferInfo> {
const componentType = accessor.componentType;
const bufferView = bufferViews[accessor.bufferView];
const bufferViewIndex = accessor.bufferView ?? 0;
const bufferView = bufferViews[bufferViewIndex];

return context.get<ArrayBuffer>(GLTFParserType.Buffer).then((buffers) => {
const bufferIndex = bufferView.buffer;
Expand All @@ -153,7 +154,7 @@ export class GLTFUtils {
// According to the glTF official documentation only byteStride not undefined is allowed
if (bufferStride !== undefined && bufferStride !== elementStride) {
const bufferSlice = Math.floor(byteOffset / bufferStride);
const bufferCacheKey = accessor.bufferView + ":" + componentType + ":" + bufferSlice + ":" + accessorCount;
const bufferCacheKey = bufferViewIndex + ":" + componentType + ":" + bufferSlice + ":" + accessorCount;
const accessorBufferCache = context.accessorBufferCache;
bufferInfo = accessorBufferCache[bufferCacheKey];
if (!bufferInfo) {
Expand Down Expand Up @@ -206,7 +207,7 @@ export class GLTFUtils {
*/
static getAccessorData(glTF: IGLTF, accessor: IAccessor, buffers: ArrayBuffer[]): TypedArray {
const bufferViews = glTF.bufferViews;
const bufferView = bufferViews[accessor.bufferView];
const bufferView = bufferViews[accessor.bufferView ?? 0];
const arrayBuffer = buffers[bufferView.buffer];
const accessorByteOffset = accessor.hasOwnProperty("byteOffset") ? accessor.byteOffset : 0;
const bufferViewByteOffset = bufferView.hasOwnProperty("byteOffset") ? bufferView.byteOffset : 0;
Expand Down
Loading