Browse Source

added feature to Path3D : the ability to choose the first normal orientation giving an optional Vector3 as parameter

jbousquie 10 năm trước cách đây
mục cha
commit
c68262975a
1 tập tin đã thay đổi với 27 bổ sung19 xóa
  1. 27 19
      Babylon/Math/babylon.math.ts

+ 27 - 19
Babylon/Math/babylon.math.ts

@@ -3281,9 +3281,9 @@
         private _normals = new Array<Vector3>();
         private _binormals = new Array<Vector3>();
 
-        constructor(public path: Vector3[]) {
+        constructor(public path: Vector3[], firstNormal?: Vector3) {
             this._copyPath(path);
-            this._compute();
+            this._compute(firstNormal);
         }
 
         public getCurve(): Vector3[] {
@@ -3306,9 +3306,9 @@
             return this._distances;
         }
 
-        public update(path: Vector3[]): Path3D {
+        public update(path: Vector3[], firstNormal?: Vector3): Path3D {
             this._copyPath(path);
-            this._compute();
+            this._compute(firstNormal);
             return this;
         }
 
@@ -3319,7 +3319,7 @@
         }
 
         // private function compute() : computes tangents, normals and binormals
-        private _compute() {
+        private _compute(firstNormal) {
             var l = this._curve.length;
 
             // first and last tangents
@@ -3330,7 +3330,7 @@
             
             // normals and binormals at first point : arbitrary vector with _normalVector()
             var tg0 = this._tangents[0];
-            var pp0 = this._normalVector(this._curve[0], tg0);
+            var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
             this._normals[0] = pp0;
             this._normals[0].normalize();
             this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
@@ -3390,22 +3390,30 @@
             return nLVector;
         }
 
-        // private function normalVector(v0, vt) :
+        // private function normalVector(v0, vt, va) :
         // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
-        private _normalVector(v0: Vector3, vt: Vector3): Vector3 {
-            var point: Vector3;
-
-            if (vt.x !== 1) {     // search for a point in the plane
-                point = new Vector3(1, 0, 0);
-            }
-            else if (vt.y !== 1) {
-                point = new Vector3(0, 1, 0);
+        // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
+        private _normalVector(v0: Vector3, vt: Vector3, va: Vector3): Vector3 {
+            var normal0: Vector3;
+            if (va === undefined || va === null) {
+                var point: Vector3;
+                if (vt.x !== 1) {     // search for a point in the plane
+                    point = new Vector3(1, 0, 0);
+                }
+                else if (vt.y !== 1) {
+                    point = new Vector3(0, 1, 0);
+                }
+                else if (vt.z !== 1) {
+                    point = new Vector3(0, 0, 1);
+                }
+                normal0 = Vector3.Cross(vt, point);
             }
-            else if (vt.z !== 1) {
-                point = new Vector3(0, 0, 1);
+            else {
+                normal0 = Vector3.Cross(vt, va);
+                Vector3.CrossToRef(normal0, vt, normal0);
+                //normal0 = Vector3.Cross(normal0, vt);
             }
-            var normal0: Vector3 = Vector3.Cross(vt, point);
-            normal0.normalize();
+            normal0.normalize();       
             return normal0;
         }
     }