浏览代码

projectOnPlaneToRef

David Catuhe 5 年之前
父节点
当前提交
6d8441fbec
共有 3 个文件被更改,包括 48 次插入5 次删除
  1. 7 4
      dist/preview release/what's new.md
  2. 1 1
      src/Maths/math.plane.ts
  3. 40 0
      src/Maths/math.vector.ts

+ 7 - 4
dist/preview release/what's new.md

@@ -14,7 +14,7 @@
 - Simplified code contributions by fully automating the dev setup with gitpod ([nisarhassan12](https://github.com/nisarhassan12))
 - Add a `CascadedShadowMap.IsSupported` method and log an error instead of throwing an exception when CSM is not supported ([Popov72](https://github.com/Popov72))
 - Added initial code for DeviceInputSystem ([PolygonalSun](https://github.com/PolygonalSun))
-- Added support for `material.disableColorWrite` ([Deltakosh](https://github.com/deltakosh)
+- Added support for `material.disableColorWrite` ([Deltakosh](https://github.com/deltakosh))
 
 ### Engine
 
@@ -32,8 +32,8 @@
 - Handle PBR colors as colors in linear space ([Popov72](https://github.com/Popov72))
 - Allow removing textures ([Popov72](https://github.com/Popov72))
 - Edit all textures (anisotropic, clear coat, sheen, ...) for the PBR materials ([Popov72](https://github.com/Popov72))
-- Added right click options to create PBR and Standard Materials ([Deltakosh](https://github.com/deltakosh)
-- Added support for recording GIF ([Deltakosh](https://github.com/deltakosh)
+- Added right click options to create PBR and Standard Materials ([Deltakosh](https://github.com/deltakosh))
+- Added support for recording GIF ([Deltakosh](https://github.com/deltakosh))
 
 ### Cameras
 
@@ -43,7 +43,7 @@
 
 ### Sprites
 
-- Added support for 'sprite.useAlphaForPicking` to enable precise picking using sprite alpha ([Deltakosh](https://github.com/deltakosh) 
+- Added support for 'sprite.useAlphaForPicking` to enable precise picking using sprite alpha ([Deltakosh](https://github.com/deltakosh))
 
 ### Physics
 
@@ -92,6 +92,9 @@
 
 - Added support for Additive Animation Blending. Existing animations can be converted to additive using the new MakeAnimationAdditive method for Skeletons, AnimationGroups and Animations. Animations can be played additively using the new isAdditive input parameter to the begin animation methods. ([c-morten](https://github.com/c-morten))
 
+### Maths
+- Added `Vector3.projectOnPlaneToRef` ([Deltakosh](https://github.com/deltakosh))
+
 ### Build
 
 - Fixed an issue with gulp webpack, webpack stream and the viewer ([RaananW](https://github.com/RaananW))

+ 1 - 1
src/Maths/math.plane.ts

@@ -95,7 +95,7 @@ export class Plane {
     }
 
     /**
-     * Calcualtte the dot product between the point and the plane normal
+     * Compute the dot product between the point and the plane normal
      * @param point point to calculate the dot product with
      * @returns the dot product (float) of the point coordinates and the plane normal.
      */

+ 40 - 0
src/Maths/math.vector.ts

@@ -5,6 +5,7 @@ import { DeepImmutable, Nullable, FloatArray, float } from "../types";
 import { ArrayTools } from '../Misc/arrayTools';
 import { IPlaneLike } from './math.like';
 import { _TypeStore } from '../Misc/typeStore';
+import { Plane } from './math.plane';
 
 /**
  * Class representing a vector containing 2 coordinates
@@ -919,6 +920,45 @@ export class Vector3 {
         return result.addInPlaceFromFloats(this.x * scale, this.y * scale, this.z * scale);
     }
 
+    /**    
+     * Projects the current vector3 to a plane along a ray starting from a specified origin and directed towards the point. 
+     * @param origin defines the origin of the projection ray
+     * @param plane defines the plane to project to
+     * @returns the projected vector3
+     */
+    public projectOnPlane(plane: Plane, origin: Vector3): Vector3 {   
+        let result = Vector3.Zero();
+        
+        this.projectOnPlaneToRef(plane, origin, result);
+
+        return result;
+    }
+
+    /**    
+     * Projects the current vector3 to a plane along a ray starting from a specified origin and directed towards the point. 
+     * @param origin defines the origin of the projection ray
+     * @param plane defines the plane to project to
+     * @param result defines the Vector3 where to store the result
+     */
+    public projectOnPlaneToRef(plane: Plane, origin: Vector3, result: Vector3): void {        
+        let n = plane.normal;        
+        let d = plane.d;
+
+        let V  = MathTmp.Vector3[0];
+
+        // ray direction
+        this.subtractToRef(origin, V);
+
+        V.normalize();
+
+        let denom = Vector3.Dot(V, n);
+        let t = -(Vector3.Dot(origin, n) + d) / denom;
+
+        // P = P0 + t*V
+        let scaledV = V.scaleInPlace(t);
+        origin.addToRef(scaledV, result);
+    }    
+
     /**
      * Returns true if the current Vector3 and the given vector coordinates are strictly equal
      * @param otherVector defines the second operand