All files / engine/Source/Scene/GltfPipeline findAccessorMinMax.js

96.66% Statements 29/30
50% Branches 1/2
100% Functions 1/1
96.42% Lines 27/28

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72                                10x 10x 10x 10x     10x             10x 10x   10x 10x 10x 10x   10x 10x   10x 10x   10x 10x 10x 10x   10x 10x             10x 30x 30x 30x   10x     10x              
import getAccessorByteStride from "./getAccessorByteStride.js";
import getComponentReader from "./getComponentReader.js";
import numberOfComponentsForType from "./numberOfComponentsForType.js";
import ComponentDatatype from "../../Core/ComponentDatatype.js";
import defined from "../../Core/defined.js";
 
/**
 * 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
  Iif (!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,
  };
}
 
export default findAccessorMinMax;