loaders.gl
Version:
Framework-independent loaders for 3D graphics formats
62 lines (47 loc) • 2.15 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = packBinaryJson;
var _loaderUtils = require("../common/loader-utils");
function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
// Recursively packs (replaces) binary objects
// Replaces "typed arrays" with "JSON pointers" to binary chunks tracked by glbBuilder
//
function packBinaryJson(json, glbBuilder) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var _options$flattenArray = options.flattenArrays,
flattenArrays = _options$flattenArray === void 0 ? false : _options$flattenArray;
var object = json; // Check if string has same syntax as our "JSON pointers", if so "escape it".
if (typeof object === 'string' && object.indexOf('#/') === 0) {
return "#".concat(object);
}
if (Array.isArray(object)) {
// TODO - handle numeric arrays, flatten them etc.
var typedArray = flattenArrays && (0, _loaderUtils.flattenToTypedArray)(object);
if (typedArray) {
object = typedArray;
} else {
return object.map(function (element) {
return packBinaryJson(element, glbBuilder, options);
});
}
} // Typed arrays, pack them as binary
if (ArrayBuffer.isView(object) && glbBuilder) {
if (glbBuilder.isImage(object)) {
var imageIndex = glbBuilder.addImage(object);
return "#/images/".concat(imageIndex);
} // if not an image, pack as accessor
var bufferIndex = glbBuilder.addBuffer(object);
return "#/accessors/".concat(bufferIndex);
}
if (object !== null && _typeof(object) === 'object') {
var newObject = {};
for (var key in object) {
newObject[key] = packBinaryJson(object[key], glbBuilder, options);
}
return newObject;
}
return object;
}
//# sourceMappingURL=pack-binary-json.js.map