From 571148df98861ddfc64d61b7d630179a9e3a8499 Mon Sep 17 00:00:00 2001
From: netpro2k <netpro2k@gmail.com>
Date: Wed, 18 Jul 2018 18:30:51 -0700
Subject: [PATCH] Attempt at fixing some of the brittleness of bounding box
 stuff. Still needs lots of cleanup

---
 src/components/media-loader.js                | 27 ++++----
 .../position-at-box-shape-border.js           | 13 +++-
 src/utils/auto-box-collider.js                | 62 +------------------
 3 files changed, 26 insertions(+), 76 deletions(-)

diff --git a/src/components/media-loader.js b/src/components/media-loader.js
index dbbd09dbb..473f4a4c8 100644
--- a/src/components/media-loader.js
+++ b/src/components/media-loader.js
@@ -1,4 +1,4 @@
-import { getBox, getCenterAndHalfExtents, getScaleCoefficient } from "../utils/auto-box-collider";
+import { getBox, getScaleCoefficient } from "../utils/auto-box-collider";
 import { resolveFarsparkUrl } from "../utils/media-utils";
 
 const fetchContentType = async url => fetch(url, { method: "HEAD" }).then(r => r.headers.get("content-type"));
@@ -16,24 +16,23 @@ AFRAME.registerComponent("media-loader", {
   setShapeAndScale(resize) {
     const mesh = this.el.getObject3D("mesh");
     const box = getBox(this.el, mesh);
-    const scaleCoefficient = getScaleCoefficient(0.5, box);
+    if (resize) {
+      this.el.object3D.scale.multiplyScalar(getScaleCoefficient(0.5 / this.el.object3D.scale.x, box));
+    } else {
+      this.el.object3D.scale.set(1, 1, 1);
+    }
     if (this.el.body && this.el.body.shapes.length > 1) {
-      if (resize) {
-        this.el.object3D.scale.multiplyScalar(scaleCoefficient);
-      } else {
-        this.el.object3D.scale.set(1, 1, 1);
-      }
       this.el.removeAttribute("shape");
     } else {
       const center = new THREE.Vector3();
-      const halfExtents = new THREE.Vector3();
-      getCenterAndHalfExtents(this.el, box, center, halfExtents);
+      const { min, max } = box;
+      const halfExtents = {
+        x: Math.abs(min.x - max.x) / 2,
+        y: Math.abs(min.y - max.y) / 2,
+        z: Math.abs(min.z - max.z) / 2
+      };
+      center.addVectors(min, max).multiplyScalar(0.5);
       mesh.position.sub(center);
-      if (resize) {
-        this.el.object3D.scale.multiplyScalar(scaleCoefficient);
-      } else {
-        this.el.object3D.scale.set(1, 1, 1);
-      }
       this.el.setAttribute("shape", {
         shape: "box",
         halfExtents: halfExtents
diff --git a/src/components/position-at-box-shape-border.js b/src/components/position-at-box-shape-border.js
index 1a63c1b46..27ce62dd0 100644
--- a/src/components/position-at-box-shape-border.js
+++ b/src/components/position-at-box-shape-border.js
@@ -30,6 +30,11 @@ const dirs = {
   }
 };
 
+const inverseHalfExtents = {
+  x: "z",
+  z: "x"
+};
+
 AFRAME.registerComponent("position-at-box-shape-border", {
   schema: {
     target: { type: "string" },
@@ -74,12 +79,14 @@ AFRAME.registerComponent("position-at-box-shape-border", {
 
       let minSquareDistance = Infinity;
       let targetDir = this.dirs[0].dir;
-      let targetHalfExtent = this.halfExtents[this.dirs[0].halfExtent];
+      let targetHalfExtentStr = this.dirs[0].halfExtent;
+      let targetHalfExtent = this.halfExtents[targetHalfExtentStr];
       let targetRotation = this.dirs[0].rotation;
 
       for (let i = 0; i < this.dirs.length; i++) {
         const dir = this.dirs[i].dir;
-        const halfExtent = this.halfExtents[this.dirs[i].halfExtent];
+        const halfExtentStr = this.dirs[i].halfExtent;
+        const halfExtent = this.halfExtents[halfExtentStr];
         pointOnBoxFace.copy(dir).multiplyScalar(halfExtent);
         this.el.object3D.localToWorld(pointOnBoxFace);
         const squareDistance = pointOnBoxFace.distanceToSquared(camWorldPos);
@@ -88,11 +95,13 @@ AFRAME.registerComponent("position-at-box-shape-border", {
           targetDir = dir;
           targetHalfExtent = halfExtent;
           targetRotation = this.dirs[i].rotation;
+          targetHalfExtentStr = halfExtentStr;
         }
       }
 
       this.target.position.copy(targetPosition.copy(targetDir).multiplyScalar(targetHalfExtent));
       this.target.rotation.set(0, targetRotation, 0);
+      this.target.scale.setScalar(this.halfExtents[inverseHalfExtents[targetHalfExtentStr]] * 4);
     };
   })()
 });
diff --git a/src/utils/auto-box-collider.js b/src/utils/auto-box-collider.js
index 12fccbb4d..255c579ae 100644
--- a/src/utils/auto-box-collider.js
+++ b/src/utils/auto-box-collider.js
@@ -3,24 +3,13 @@ export function getBox(entity, boxRoot) {
   const rotation = entity.object3D.rotation.clone();
   entity.object3D.rotation.set(0, 0, 0);
   entity.object3D.updateMatrixWorld(true);
-
-  box.expandByObject = expandByObject;
   box.setFromObject(boxRoot);
+  entity.object3D.worldToLocal(box.min);
+  entity.object3D.worldToLocal(box.max);
   entity.object3D.rotation.copy(rotation);
   return box;
 }
 
-export function getCenterAndHalfExtents(entity, box, center, halfExtents) {
-  const { min, max } = box;
-  center.addVectors(min, max).multiplyScalar(0.5);
-  entity.object3D.worldToLocal(center);
-  halfExtents
-    .copy(min)
-    .negate()
-    .add(max)
-    .multiplyScalar(0.5 / entity.object3D.scale.x);
-}
-
 export function getScaleCoefficient(length, box) {
   const { max, min } = box;
   const dX = Math.abs(max.x - min.x);
@@ -29,50 +18,3 @@ export function getScaleCoefficient(length, box) {
   const lengthOfLongestComponent = Math.max(dX, dY, dZ);
   return length / lengthOfLongestComponent;
 }
-
-const expandByObject = (function() {
-  // Computes the world-axis-aligned bounding box of an object (including its children),
-  // accounting for both the object's, and children's, world transforms
-
-  var scope, i, l;
-
-  var v1 = new THREE.Vector3();
-
-  function traverse(node) {
-    var geometry = node.geometry;
-
-    if (geometry !== undefined) {
-      if (geometry.isGeometry) {
-        var vertices = geometry.vertices;
-
-        for (i = 0, l = vertices.length; i < l; i++) {
-          v1.copy(vertices[i]);
-          v1.applyMatrix4(node.matrixWorld);
-
-          scope.expandByPoint(v1);
-        }
-      } else if (geometry.isBufferGeometry) {
-        var attribute = geometry.attributes.position;
-
-        if (attribute !== undefined) {
-          for (i = 0, l = attribute.count; i < l; i++) {
-            v1.fromBufferAttribute(attribute, i).applyMatrix4(node.matrixWorld);
-            if (isNaN(v1.x) || isNaN(v1.y) || isNaN(v1.z)) {
-              continue;
-            }
-
-            scope.expandByPoint(v1);
-          }
-        }
-      }
-    }
-  }
-
-  return function expandByObject(object) {
-    scope = this;
-
-    object.traverse(traverse);
-
-    return this;
-  };
-})();
-- 
GitLab