UNPKG

gltf-pipeline

Version:

Content pipeline tools for optimizing glTF assets.

76 lines (65 loc) 2.45 kB
"use strict"; const Cesium = require("cesium"); const ComponentDatatype = Cesium.ComponentDatatype; const defined = Cesium.defined; const getAccessorByteStride = require("./getAccessorByteStride"); const getComponentReader = require("./getComponentReader"); const numberOfComponentsForType = require("./numberOfComponentsForType"); module.exports = findAccessorMinMax; /** * Finds the min and max values of the accessor. * * @param {object} gltf A javascript object containing a glTF asset. * @param {object} accessor The accessor object from the glTF asset to read. * @returns {{min: Array, max: Array}} min holding the array of minimum values and max holding the array of maximum values. * * @private */ function findAccessorMinMax(gltf, accessor) { const bufferViews = gltf.bufferViews; const buffers = gltf.buffers; const bufferViewId = accessor.bufferView; const numberOfComponents = numberOfComponentsForType(accessor.type); // According to the spec, when bufferView is not defined, accessor must be initialized with zeros if (!defined(accessor.bufferView)) { return { min: new Array(numberOfComponents).fill(0.0), max: new Array(numberOfComponents).fill(0.0), }; } const min = new Array(numberOfComponents).fill(Number.POSITIVE_INFINITY); const max = new Array(numberOfComponents).fill(Number.NEGATIVE_INFINITY); const bufferView = bufferViews[bufferViewId]; const bufferId = bufferView.buffer; const buffer = buffers[bufferId]; const source = buffer.extras._pipeline.source; const count = accessor.count; const byteStride = getAccessorByteStride(gltf, accessor); let byteOffset = accessor.byteOffset + bufferView.byteOffset + source.byteOffset; const componentType = accessor.componentType; const componentTypeByteLength = ComponentDatatype.getSizeInBytes(componentType); const dataView = new DataView(source.buffer); const components = new Array(numberOfComponents); const componentReader = getComponentReader(componentType); for (let i = 0; i < count; i++) { componentReader( dataView, byteOffset, numberOfComponents, componentTypeByteLength, components, ); for (let j = 0; j < numberOfComponents; j++) { const value = components[j]; min[j] = Math.min(min[j], value); max[j] = Math.max(max[j], value); } byteOffset += byteStride; } return { min: min, max: max, }; }