Skip to content
Snippets Groups Projects
auto-box-collider.js 777 B
Newer Older
netpro2k's avatar
netpro2k committed
const rotation = new THREE.Euler();
export function getBox(entity, boxRoot) {
joni's avatar
joni committed
  const box = new THREE.Box3();
netpro2k's avatar
netpro2k committed
  rotation.copy(entity.object3D.rotation);
joni's avatar
joni committed
  entity.object3D.rotation.set(0, 0, 0);
  entity.object3D.updateMatrixWorld(true);
  box.setFromObject(boxRoot);

  if (!box.isEmpty()) {
    entity.object3D.worldToLocal(box.min);
    entity.object3D.worldToLocal(box.max);
    entity.object3D.rotation.copy(rotation);
  }

joni's avatar
joni committed
  return box;
}

export function getScaleCoefficient(length, box) {
  if (box.isEmpty()) return 1.0;

joni's avatar
joni committed
  const { max, min } = box;
  const dX = Math.abs(max.x - min.x);
  const dY = Math.abs(max.y - min.y);
  const dZ = Math.abs(max.z - min.z);
  const lengthOfLongestComponent = Math.max(dX, dY, dZ);
  return length / lengthOfLongestComponent;
}