All files utils.js

100% Statements 28/28
90.91% Branches 10/11
100% Functions 5/5
100% Lines 28/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          1x 471x                       1x 106x 5x   101x 99x 99x   101x                     1x 5x 5x 5x     5x 5x 5x 5x   5x   5x 5x     5x 5x     5x 5x 5x   5x     1x 1x    
/* global THREE, AFRAME */
/**
 * Check if x, y and z properties are set.
 * @param {boolean}
 */
 module.exports.isVec3Set = (v) => {
  return typeof v === 'object' &&
    !isNaN(parseFloat(v.x)) && isFinite(v.x) &&
    !isNaN(parseFloat(v.y)) && isFinite(v.y) &&
    !isNaN(parseFloat(v.z)) && isFinite(v.z)
}
 
/**
 * Execute when entity has loaded. If it hasn't, it waits for the loaded event, executes, and
 * removes its event listener.
 * @param {AFRAME.ANode} entity
 * @param {function} cb
 */
 module.exports.onceWhenLoaded = (entity, cb) => {
  if (entity.hasLoaded) {
    cb()
  } else {
    const fn = function (evt) {
      cb()
      evt.target.removeEventListener('loaded', fn)
    }
    entity.addEventListener('loaded', fn)
  }
}
 
/**
 * Get the bounds of an Object3D in local coordinates. The regular method does this in world
 * coordinates, so it ignores any applied transformations.
 * @see https://github.com/mrdoob/three.js/issues/11967
 * @param {THREE.Object3D} object3D
 * @returns {THREE.Box3}
 */
 module.exports.getBoundingBox = (object3D) => {
  object3D.updateMatrix()
  const oldMatrix = object3D.matrix
  const oldMatrixAutoUpdate = object3D.matrixAutoUpdate
 
  // Store original local matrix.
  object3D.updateMatrixWorld()
  const m = new THREE.Matrix4()
  Eif (object3D.parent !== null) {
    m.getInverse(object3D.parent.matrixWorld, false)
  }
  object3D.matrix = m
  // To prevent matrix being reassigned.
  object3D.matrixAutoUpdate = false
  object3D.updateMatrixWorld()
 
  // Get bounds.
  const bounds = new THREE.Box3()
  bounds.setFromObject(object3D)
 
  // Reset to old matrix.
  object3D.matrix = oldMatrix
  object3D.matrixAutoUpdate = oldMatrixAutoUpdate
  object3D.updateMatrixWorld()
 
  return bounds
}
 
module.exports.aframeVersion = () => {
  return AFRAME.version
}