loaders.gl
Version:
Framework-independent loaders for 3D graphics formats
49 lines (37 loc) • 1.57 kB
JavaScript
import { flattenToTypedArray } from '../common/loader-utils'; // Recursively packs (replaces) binary objects
// Replaces "typed arrays" with "JSON pointers" to binary chunks tracked by glbBuilder
//
export default function packBinaryJson(json, glbBuilder, options = {}) {
const _options$flattenArray = options.flattenArrays,
flattenArrays = _options$flattenArray === void 0 ? false : _options$flattenArray;
let object = json; // Check if string has same syntax as our "JSON pointers", if so "escape it".
if (typeof object === 'string' && object.indexOf('#/') === 0) {
return `#${object}`;
}
if (Array.isArray(object)) {
// TODO - handle numeric arrays, flatten them etc.
const typedArray = flattenArrays && flattenToTypedArray(object);
if (typedArray) {
object = typedArray;
} else {
return object.map(element => packBinaryJson(element, glbBuilder, options));
}
} // Typed arrays, pack them as binary
if (ArrayBuffer.isView(object) && glbBuilder) {
if (glbBuilder.isImage(object)) {
const imageIndex = glbBuilder.addImage(object);
return `#/images/${imageIndex}`;
} // if not an image, pack as accessor
const bufferIndex = glbBuilder.addBuffer(object);
return `#/accessors/${bufferIndex}`;
}
if (object !== null && typeof object === 'object') {
const newObject = {};
for (const key in object) {
newObject[key] = packBinaryJson(object[key], glbBuilder, options);
}
return newObject;
}
return object;
}
//# sourceMappingURL=pack-binary-json.js.map