瀏覽代碼

Merge pull request #6559 from horusscope/patch-1

Added a function for calculating the intercept
David Catuhe 6 年之前
父節點
當前提交
5c45e66442
共有 2 個文件被更改,包括 27 次插入1 次删除
  1. 3 0
      dist/preview release/what's new.md
  2. 24 1
      src/Culling/ray.ts

+ 3 - 0
dist/preview release/what's new.md

@@ -71,6 +71,9 @@
 ### Sounds
 - Added `ISoundOptions.skipCodecCheck` to make `Sound` more flexible with URLs ([nbduke](https://github.com/nbduke))
 
+### Ray
+- Added `Ray.intersectsAxis` to translate screen to axis coordinates without checking collisions ([horusscope](https://github.com/horusscope))
+
 ### Documentation
 - Added a note on shallow bounding of getBoundingInfo ([tibotiber](https://github.com/tibotiber))
 

+ 24 - 1
src/Culling/ray.ts

@@ -245,6 +245,29 @@ export class Ray {
             return distance;
         }
     }
+    /**
+     * Calculate the intercept of a ray on a given axis
+     * @param axis to check 'x' | 'y' | 'z'
+     * @param offset from axis interception (i.e. an offset of 1y is intercepted above ground)
+     * @returns a vector containing the coordinates where 'axis' is equal to zero (else offset), or null if there is no intercept.
+     */
+    public intersectsAxis(axis: string, offset: number = 0): Nullable<Vector3> {
+          switch (axis) {
+              case 'y':
+        var t = (this.origin.y - offset) / this.direction.y;
+        if (t > 0) { return null; } // for example if the sky was clicked
+        return new Vector3(this.origin.x + (this.direction.x * -t), offset, this.origin.z + (this.direction.z * -t));
+              case 'x':
+        var t = (this.origin.x - offset) / this.direction.x;
+        if (t > 0) { return null; }
+        return new Vector3(offset, this.origin.y + (this.direction.y * -t), this.origin.z + (this.direction.z * -t));
+              case 'z':
+        var t = (this.origin.z - offset) / this.direction.z;
+        if (t > 0) { return null; }
+        return new Vector3(this.origin.x + (this.direction.x * -t), this.origin.y + (this.direction.y * -t), offset);
+              default: return null;
+          }
+    }
 
     /**
      * Checks if ray intersects a mesh
@@ -775,4 +798,4 @@ Camera.prototype.getForwardRay = function(length = 100, transform?: Matrix, orig
     var direction = Vector3.Normalize(forwardWorld);
 
     return new Ray(origin, direction, length);
-};
+};