-
Notifications
You must be signed in to change notification settings - Fork 196
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): getting typed arrays for accessor #2691
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,5 +1,8 @@ | ||||||
// TODO - GLTFScenegraph should use these | ||||||
import {assert} from '../utils/assert'; | ||||||
import type {TypedArray} from '@loaders.gl/schema'; | ||||||
import type {GLTF, GLTFExternalBuffer, GLTFAccessor} from '../types/gltf-types'; | ||||||
import {getAccessorArrayTypeAndLength} from './gltf-utils'; | ||||||
|
||||||
// accepts buffer view index or buffer view object | ||||||
// returns a `Uint8Array` | ||||||
|
@@ -24,18 +27,54 @@ export function getTypedArrayForImageData(json, buffers, imageIndex) { | |||||
return getTypedArrayForBufferView(json, buffers, bufferViewIndex); | ||||||
} | ||||||
|
||||||
/* | ||||||
// accepts accessor index or accessor object | ||||||
// returns a typed array with type that matches the types | ||||||
export function getTypedArrayForAccessor(accessor) { | ||||||
accessor = this.getAccessor(accessor); | ||||||
const bufferView = this.getBufferView(accessor.bufferView); | ||||||
const buffer = this.getBuffer(bufferView.buffer); | ||||||
const arrayBuffer = buffer.data; | ||||||
|
||||||
// Create a new typed array as a view into the combined buffer | ||||||
const {ArrayType, length} = getAccessorArrayTypeAndLength(accessor, bufferView); | ||||||
const byteOffset = bufferView.byteOffset + accessor.byteOffset; | ||||||
return new ArrayType(arrayBuffer, byteOffset, length); | ||||||
/** | ||||||
* Gets data pointed by the accessor. | ||||||
* @param json - json part of gltf content of a GLTF tile. | ||||||
* @param buffers - Array containing buffers of data. | ||||||
* @param accessor - accepts accessor index or accessor object. | ||||||
* @returns {TypedArray} Typed array with type matching the type of data poited by the accessor. | ||||||
*/ | ||||||
// eslint-disable-next-line complexity | ||||||
export function getTypedArrayForAccessor( | ||||||
json: GLTF, | ||||||
buffers: GLTFExternalBuffer[], | ||||||
accessor: GLTFAccessor | number | ||||||
): TypedArray { | ||||||
const gltfAccessor = typeof accessor === 'number' ? json.accessors?.[accessor] : accessor; | ||||||
if (!gltfAccessor) { | ||||||
throw new Error(`No Accessor ${accessor}`); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be very hard for end users to understand such an error message. Maybe mentioning glTF could give them a hint?
Suggested change
|
||||||
} | ||||||
const bufferView = json.bufferViews?.[gltfAccessor.bufferView || 0]; | ||||||
if (!bufferView) { | ||||||
throw new Error(`No Buffer View for Accessor ${bufferView}`); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
// Get `arrayBuffer` the `bufferView` looks at | ||||||
const {arrayBuffer, byteOffset: bufferByteOffset} = buffers[bufferView.buffer]; | ||||||
// Resulting byteOffset is sum of the buffer, accessor and bufferView byte offsets | ||||||
const byteOffset = | ||||||
(bufferByteOffset || 0) + (gltfAccessor.byteOffset || 0) + (bufferView.byteOffset || 0); | ||||||
// Deduce TypedArray type and its length from `accessor` and `bufferView` data | ||||||
const {ArrayType, length, componentByteSize, numberOfComponentsInElement} = | ||||||
getAccessorArrayTypeAndLength(gltfAccessor, bufferView); | ||||||
// 'length' is a whole number of components of all elements in the buffer pointed by the accessor | ||||||
// Multiplier to calculate the address of the element in the arrayBuffer | ||||||
const elementByteSize = componentByteSize * numberOfComponentsInElement; | ||||||
const elementAddressScale = bufferView.byteStride || elementByteSize; | ||||||
// Creare an array of component's type where all components (not just elements) will reside | ||||||
if (typeof bufferView.byteStride === 'undefined' || bufferView.byteStride === elementByteSize) { | ||||||
// No iterleaving | ||||||
const result: TypedArray = new ArrayType(arrayBuffer, byteOffset, length); | ||||||
return result; | ||||||
} | ||||||
// Iterleaving | ||||||
const result: TypedArray = new ArrayType(length); | ||||||
for (let i = 0; i < gltfAccessor.count; i++) { | ||||||
const values = new ArrayType( | ||||||
arrayBuffer, | ||||||
byteOffset + i * elementAddressScale, | ||||||
numberOfComponentsInElement | ||||||
); | ||||||
result.set(values, i * numberOfComponentsInElement); | ||||||
} | ||||||
return result; | ||||||
} | ||||||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A function like this should really have a couple of test cases. It can be very painful to debug code when the error happens in a low-level utility like this.