Browse Source

First try with vector3

David Catuhe 5 năm trước cách đây
mục cha
commit
3a9b56c6db
4 tập tin đã thay đổi với 177 bổ sung77 xóa
  1. 1 1
      src/Culling/ray.ts
  2. 1 1
      src/Maths/math.plane.ts
  3. 135 57
      src/Maths/math.vector.ts
  4. 40 18
      src/Meshes/transformNode.ts

+ 1 - 1
src/Culling/ray.ts

@@ -491,7 +491,7 @@ export class Ray {
     * @param world a matrix to transform the ray to. Default is the identity matrix.
     * @returns the new ray
     */
-    public static CreateNewFromTo(origin: DeepImmutable<Vector3>, end: DeepImmutable<Vector3>, world: DeepImmutable<Matrix> = Matrix.IdentityReadOnly): Ray {
+    public static CreateNewFromTo(origin: Vector3, end: Vector3, world: DeepImmutable<Matrix> = Matrix.IdentityReadOnly): Ray {
         var direction = end.subtract(origin);
         var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
         direction.normalize();

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

@@ -186,7 +186,7 @@ export class Plane {
      * @returns a new Plane the normal vector to this plane at the given origin point.
      * Note : the vector "normal" is updated because normalized.
      */
-    static FromPositionAndNormal(origin: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>): Plane {
+    static FromPositionAndNormal(origin: DeepImmutable<Vector3>, normal: Vector3): Plane {
         var result = new Plane(0.0, 0.0, 0.0, 0.0);
         normal.normalize();
         result.normal = normal;

+ 135 - 57
src/Maths/math.vector.ts

@@ -673,6 +673,90 @@ export class Vector3 {
     private static _UpReadOnly = Vector3.Up() as DeepImmutable<Vector3>;
     private static _ZeroReadOnly = Vector3.Zero() as DeepImmutable<Vector3>;
 
+    private _x: number;
+    private _y: number;
+    private _z: number;
+
+    public get x() {
+        return this._x;
+    }
+
+    public set x(value: number) {
+        this._x = value;
+    }
+
+    public get y() {
+        return this._y;
+    }
+
+    public set y(value: number) {
+        this._y = value;
+    }
+
+    public get z() {
+        return this._z;
+    }
+
+    public set z(value: number) {
+        this._z = value;
+    }
+
+    public set onUpdateCallback(callback: Nullable<() =>void>) {
+        if (!callback) {
+            Object.defineProperty(Vector3.prototype, "x", {
+                set: function(this: Vector3, value: number) {
+                    this._x = value;                    
+                },
+                enumerable: true,
+                configurable: true
+            });
+
+            Object.defineProperty(Vector3.prototype, "y", {
+                set: function(this: Vector3, value: number) {
+                    this._y = value;                    
+                },
+                enumerable: true,
+                configurable: true
+            });
+
+            Object.defineProperty(Vector3.prototype, "z", {
+                set: function(this: Vector3, value: number) {
+                    this._z = value;                    
+                },
+                enumerable: true,
+                configurable: true
+            });
+            return;
+        }
+
+        Object.defineProperty(Vector3.prototype, "x", {
+            set: function(this: Vector3, value: number) {
+                this._x = value;  
+                callback();                  
+            },
+            enumerable: true,
+            configurable: true
+        });
+
+        Object.defineProperty(Vector3.prototype, "y", {
+            set: function(this: Vector3, value: number) {
+                this._y = value;                    
+                callback();                     
+            },
+            enumerable: true,
+            configurable: true
+        });
+
+        Object.defineProperty(Vector3.prototype, "z", {
+            set: function(this: Vector3, value: number) {
+                this._z = value;                   
+                callback();                      
+            },
+            enumerable: true,
+            configurable: true
+        });        
+    }
+
     /**
      * Creates a new Vector3 object from the given x, y, z (floats) coordinates.
      * @param x defines the first coordinates (on X axis)
@@ -680,19 +764,13 @@ export class Vector3 {
      * @param z defines the third coordinates (on Z axis)
      */
     constructor(
-        /**
-         * Defines the first coordinates (on X axis)
-         */
-        public x: number = 0,
-        /**
-         * Defines the second coordinates (on Y axis)
-         */
-        public y: number = 0,
-        /**
-         * Defines the third coordinates (on Z axis)
-         */
-        public z: number = 0
+        x: number = 0,
+        y: number = 0,
+        z: number = 0
     ) {
+        this._x = x;
+        this._y = y;
+        this._z = z;
     }
 
     /**
@@ -700,7 +778,7 @@ export class Vector3 {
      * @returns a string with the Vector3 coordinates.
      */
     public toString(): string {
-        return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
+        return "{X: " + this._x + " Y:" + this._y + " Z:" + this._z + "}";
     }
 
     /**
@@ -716,9 +794,9 @@ export class Vector3 {
      * @returns a number which tends to be unique between Vector3 instances
      */
     public getHashCode(): number {
-        let hash = this.x | 0;
-        hash = (hash * 397) ^ (this.y | 0);
-        hash = (hash * 397) ^ (this.z | 0);
+        let hash = this._x | 0;
+        hash = (hash * 397) ^ (this._y | 0);
+        hash = (hash * 397) ^ (this._z | 0);
         return hash;
     }
 
@@ -741,9 +819,9 @@ export class Vector3 {
      * @returns the current Vector3
      */
     public toArray(array: FloatArray, index: number = 0): Vector3 {
-        array[index] = this.x;
-        array[index + 1] = this.y;
-        array[index + 2] = this.z;
+        array[index] = this._x;
+        array[index + 1] = this._y;
+        array[index + 2] = this._z;
         return this;
     }
 
@@ -752,7 +830,7 @@ export class Vector3 {
      * @returns a new Quaternion object, computed from the Vector3 coordinates
      */
     public toQuaternion(): Quaternion {
-        return Quaternion.RotationYawPitchRoll(this.y, this.x, this.z);
+        return Quaternion.RotationYawPitchRoll(this._y, this._x, this._z);
     }
 
     /**
@@ -784,7 +862,7 @@ export class Vector3 {
      * @returns the resulting Vector3
      */
     public add(otherVector: DeepImmutable<Vector3>): Vector3 {
-        return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
+        return new Vector3(this._x + otherVector.x, this._y + otherVector.y, this._z + otherVector.z);
     }
 
     /**
@@ -794,7 +872,7 @@ export class Vector3 {
      * @returns the current Vector3
      */
     public addToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
-        return result.copyFromFloats(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
+        return result.copyFromFloats(this._x + otherVector.x, this._y + otherVector.y, this._z + otherVector.z);
     }
 
     /**
@@ -815,7 +893,7 @@ export class Vector3 {
      * @returns the resulting Vector3
      */
     public subtract(otherVector: DeepImmutable<Vector3>): Vector3 {
-        return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
+        return new Vector3(this._x - otherVector.x, this._y - otherVector.y, this._z - otherVector.z);
     }
 
     /**
@@ -836,7 +914,7 @@ export class Vector3 {
      * @returns the resulting Vector3
      */
     public subtractFromFloats(x: number, y: number, z: number): Vector3 {
-        return new Vector3(this.x - x, this.y - y, this.z - z);
+        return new Vector3(this._x - x, this._y - y, this._z - z);
     }
 
     /**
@@ -848,7 +926,7 @@ export class Vector3 {
      * @returns the current Vector3
      */
     public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
-        return result.copyFromFloats(this.x - x, this.y - y, this.z - z);
+        return result.copyFromFloats(this._x - x, this._y - y, this._z - z);
     }
 
     /**
@@ -856,7 +934,7 @@ export class Vector3 {
      * @returns a new Vector3
      */
     public negate(): Vector3 {
-        return new Vector3(-this.x, -this.y, -this.z);
+        return new Vector3(-this._x, -this._y, -this._z);
     }
 
     /**
@@ -876,7 +954,7 @@ export class Vector3 {
      * @returns the current Vector3
      */
     public negateToRef(result: Vector3): Vector3 {
-        return result.copyFromFloats(this.x * -1, this.y * -1, this.z * -1);
+        return result.copyFromFloats(this._x * -1, this._y * -1, this._z * -1);
     }
 
     /**
@@ -897,7 +975,7 @@ export class Vector3 {
      * @returns a new Vector3
      */
     public scale(scale: number): Vector3 {
-        return new Vector3(this.x * scale, this.y * scale, this.z * scale);
+        return new Vector3(this._x * scale, this._y * scale, this._z * scale);
     }
 
     /**
@@ -907,7 +985,7 @@ export class Vector3 {
      * @returns the current Vector3
      */
     public scaleToRef(scale: number, result: Vector3): Vector3 {
-        return result.copyFromFloats(this.x * scale, this.y * scale, this.z * scale);
+        return result.copyFromFloats(this._x * scale, this._y * scale, this._z * scale);
     }
 
     /**
@@ -917,7 +995,7 @@ export class Vector3 {
      * @returns the unmodified current Vector3
      */
     public scaleAndAddToRef(scale: number, result: Vector3): Vector3 {
-        return result.addInPlaceFromFloats(this.x * scale, this.y * scale, this.z * scale);
+        return result.addInPlaceFromFloats(this._x * scale, this._y * scale, this._z * scale);
     }
 
     /**
@@ -965,7 +1043,7 @@ export class Vector3 {
      * @returns true if both vectors are equals
      */
     public equals(otherVector: DeepImmutable<Vector3>): boolean {
-        return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
+        return otherVector && this._x === otherVector.x && this._y === otherVector.y && this._z === otherVector.z;
     }
 
     /**
@@ -975,7 +1053,7 @@ export class Vector3 {
      * @returns true if both vectors are distant less than epsilon
      */
     public equalsWithEpsilon(otherVector: DeepImmutable<Vector3>, epsilon: number = Epsilon): boolean {
-        return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon) && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon);
+        return otherVector && Scalar.WithinEpsilon(this._x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this._y, otherVector.y, epsilon) && Scalar.WithinEpsilon(this._z, otherVector.z, epsilon);
     }
 
     /**
@@ -986,7 +1064,7 @@ export class Vector3 {
      * @returns true if both vectors are equals
      */
     public equalsToFloats(x: number, y: number, z: number): boolean {
-        return this.x === x && this.y === y && this.z === z;
+        return this._x === x && this._y === y && this._z === z;
     }
 
     /**
@@ -1017,7 +1095,7 @@ export class Vector3 {
      * @returns the current Vector3
      */
     public multiplyToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
-        return result.copyFromFloats(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
+        return result.copyFromFloats(this._x * otherVector.x, this._y * otherVector.y, this._z * otherVector.z);
     }
 
     /**
@@ -1028,7 +1106,7 @@ export class Vector3 {
      * @returns the new Vector3
      */
     public multiplyByFloats(x: number, y: number, z: number): Vector3 {
-        return new Vector3(this.x * x, this.y * y, this.z * z);
+        return new Vector3(this._x * x, this._y * y, this._z * z);
     }
 
     /**
@@ -1037,7 +1115,7 @@ export class Vector3 {
      * @returns the new Vector3
      */
     public divide(otherVector: DeepImmutable<Vector3>): Vector3 {
-        return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
+        return new Vector3(this._x / otherVector.x, this._y / otherVector.y, this._z / otherVector.z);
     }
 
     /**
@@ -1047,7 +1125,7 @@ export class Vector3 {
      * @returns the current Vector3
      */
     public divideToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
-        return result.copyFromFloats(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
+        return result.copyFromFloats(this._x / otherVector.x, this._y / otherVector.y, this._z / otherVector.z);
     }
 
     /**
@@ -1085,9 +1163,9 @@ export class Vector3 {
      * @returns the current updated Vector3
      */
     public minimizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
-        if (x < this.x) { this.x = x; }
-        if (y < this.y) { this.y = y; }
-        if (z < this.z) { this.z = z; }
+        if (x < this._x) { this.x = x; }
+        if (y < this._y) { this.y = y; }
+        if (z < this._z) { this.z = z; }
         return this;
     }
 
@@ -1099,9 +1177,9 @@ export class Vector3 {
      * @returns the current updated Vector3
      */
     public maximizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
-        if (x > this.x) { this.x = x; }
-        if (y > this.y) { this.y = y; }
-        if (z > this.z) { this.z = z; }
+        if (x > this._x) { this.x = x; }
+        if (y > this._y) { this.y = y; }
+        if (z > this._z) { this.z = z; }
         return this;
     }
 
@@ -1112,13 +1190,13 @@ export class Vector3 {
      * @returns if the the vector is non uniform to a certain number of decimal places
      */
     public isNonUniformWithinEpsilon(epsilon: number) {
-        let absX = Math.abs(this.x);
-        let absY = Math.abs(this.y);
+        let absX = Math.abs(this._x);
+        let absY = Math.abs(this._y);
         if (!Scalar.WithinEpsilon(absX, absY, epsilon)) {
             return true;
         }
 
-        let absZ = Math.abs(this.z);
+        let absZ = Math.abs(this._z);
         if (!Scalar.WithinEpsilon(absX, absZ, epsilon)) {
             return true;
         }
@@ -1134,13 +1212,13 @@ export class Vector3 {
      * Gets a boolean indicating that the vector is non uniform meaning x, y or z are not all the same
      */
     public get isNonUniform(): boolean {
-        let absX = Math.abs(this.x);
-        let absY = Math.abs(this.y);
+        let absX = Math.abs(this._x);
+        let absY = Math.abs(this._y);
         if (absX !== absY) {
             return true;
         }
 
-        let absZ = Math.abs(this.z);
+        let absZ = Math.abs(this._z);
         if (absX !== absZ) {
             return true;
         }
@@ -1153,7 +1231,7 @@ export class Vector3 {
      * @returns a new Vector3
      */
     public floor(): Vector3 {
-        return new Vector3(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z));
+        return new Vector3(Math.floor(this._x), Math.floor(this._y), Math.floor(this._z));
     }
 
     /**
@@ -1161,7 +1239,7 @@ export class Vector3 {
      * @returns a new Vector3
      */
     public fract(): Vector3 {
-        return new Vector3(this.x - Math.floor(this.x), this.y - Math.floor(this.y), this.z - Math.floor(this.z));
+        return new Vector3(this._x - Math.floor(this._x), this._y - Math.floor(this._y), this._z - Math.floor(this._z));
     }
 
     // Properties
@@ -1170,7 +1248,7 @@ export class Vector3 {
      * @returns the length of the Vector3
      */
     public length(): number {
-        return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
+        return Math.sqrt(this._x * this._x + this._y * this._y + this._z * this._z);
     }
 
     /**
@@ -1178,7 +1256,7 @@ export class Vector3 {
      * @returns squared length of the Vector3
      */
     public lengthSquared(): number {
-        return (this.x * this.x + this.y * this.y + this.z * this.z);
+        return (this._x * this._x + this._y * this._y + this._z * this._z);
     }
 
     /**
@@ -1272,10 +1350,10 @@ export class Vector3 {
      * @param reference define the Vector3 to update
      * @returns the updated Vector3
      */
-    public normalizeToRef(reference: DeepImmutable<Vector3>): Vector3 {
+    public normalizeToRef(reference: Vector3): Vector3 {
         var len = this.length();
         if (len === 0 || len === 1.0) {
-            return reference.copyFromFloats(this.x, this.y, this.z);
+            return reference.copyFromFloats(this._x, this._y, this._z);
         }
 
         return this.scaleToRef(1.0 / len, reference);
@@ -1286,7 +1364,7 @@ export class Vector3 {
      * @returns the new Vector3
      */
     public clone(): Vector3 {
-        return new Vector3(this.x, this.y, this.z);
+        return new Vector3(this._x, this._y, this._z);
     }
 
     /**
@@ -1742,7 +1820,7 @@ export class Vector3 {
      * @param right defines the right operand
      * @param result defines the Vector3 where to store the result
      */
-    public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
+    public static CrossToRef(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>, result: Vector3): void {
         const x = left.y * right.z - left.z * right.y;
         const y = left.z * right.x - left.x * right.z;
         const z = left.x * right.y - left.y * right.x;

+ 40 - 18
src/Meshes/transformNode.ts

@@ -46,6 +46,7 @@ export class TransformNode extends Node {
     private _up = new Vector3(0, 1, 0);
     private _right = new Vector3(1, 0, 0);
     private _rightInverted = new Vector3(-1, 0, 0);
+    private _tmpRotation = Quaternion.Zero();
 
     // Properties
     @serializeAsVector3("position")
@@ -172,6 +173,11 @@ export class TransformNode extends Node {
         if (isPure) {
             this.getScene().addTransformNode(this);
         }
+
+        let bind = this._markAsDirty.bind(this);
+        this._position.onUpdateCallback = bind;        
+        this._rotation.onUpdateCallback = bind;
+        this._scaling.onUpdateCallback = bind;
     }
 
     /**
@@ -182,6 +188,10 @@ export class TransformNode extends Node {
         return "TransformNode";
     }
 
+    public _markAsDirty() {
+        this._isDirty = true;
+    }
+
     /**
       * Gets or set the node position (default is (0.0, 0.0, 0.0))
       */
@@ -190,8 +200,12 @@ export class TransformNode extends Node {
     }
 
     public set position(newPosition: Vector3) {
+        this._position.onUpdateCallback = null;
+
         this._position = newPosition;
         this._isDirty = true;
+
+        this._position.onUpdateCallback = this._markAsDirty.bind(this);
     }
 
     /**
@@ -202,10 +216,14 @@ export class TransformNode extends Node {
         return this._rotation;
     }
 
-    public set rotation(newRotation: Vector3) {
+    public set rotation(newRotation: Vector3) {        
+        this._rotation.onUpdateCallback = null;
+
         this._rotation = newRotation;
         this._rotationQuaternion = null;
         this._isDirty = true;
+
+        this._rotation.onUpdateCallback = this._markAsDirty.bind(this);
     }
 
     /**
@@ -216,8 +234,12 @@ export class TransformNode extends Node {
     }
 
     public set scaling(newScaling: Vector3) {
+        this._scaling.onUpdateCallback = null;
+
         this._scaling = newScaling;
         this._isDirty = true;
+        
+        this._scaling.onUpdateCallback = this._markAsDirty.bind(this);
     }
 
     /**
@@ -308,21 +330,22 @@ export class TransformNode extends Node {
             return false;
         }
 
-        if (!cache.position.equals(this._position)) {
-            return false;
-        }
+        // if (!cache.position.equals(this._position)) {
+        //     return false;
+        // }
 
         if (this._rotationQuaternion) {
             if (!cache.rotationQuaternion.equals(this._rotationQuaternion)) {
                 return false;
             }
-        } else if (!cache.rotation.equals(this._rotation)) {
-            return false;
-        }
+        } 
+        // else if (!cache.rotation.equals(this._rotation)) {
+        //     return false;
+        // }
 
-        if (!cache.scaling.equals(this._scaling)) {
-            return false;
-        }
+        // if (!cache.scaling.equals(this._scaling)) {
+        //     return false;
+        // }
 
         return true;
     }
@@ -1001,10 +1024,10 @@ export class TransformNode extends Node {
         }
 
         this._updateCache();
-        let cache = this._cache;
-        cache.pivotMatrixUpdated = false;
-        cache.billboardMode = this.billboardMode;
-        cache.infiniteDistance = this.infiniteDistance;
+        // let cache = this._cache;
+        // cache.pivotMatrixUpdated = false;
+        // cache.billboardMode = this.billboardMode;
+        // cache.infiniteDistance = this.infiniteDistance;
 
         this._currentRenderId = currentRenderId;
         this._childUpdateId++;
@@ -1012,8 +1035,8 @@ export class TransformNode extends Node {
         let parent = this._getEffectiveParent();
 
         // Scaling
-        let scaling: Vector3 = cache.scaling;
-        let translation: Vector3 = cache.position;
+        let scaling: Vector3 = this.scaling;
+        let translation: Vector3 = this.position;
 
         // Translation
         if (this._infiniteDistance) {
@@ -1033,7 +1056,7 @@ export class TransformNode extends Node {
         scaling.copyFromFloats(this._scaling.x * this.scalingDeterminant, this._scaling.y * this.scalingDeterminant, this._scaling.z * this.scalingDeterminant);
 
         // Rotation
-        let rotation: Quaternion = cache.rotationQuaternion;
+        let rotation: Quaternion = this._tmpRotation;
         if (this._rotationQuaternion) {
             if (this.reIntegrateRotationIntoRotationQuaternion) {
                 var len = this.rotation.lengthSquared();
@@ -1045,7 +1068,6 @@ export class TransformNode extends Node {
             rotation.copyFrom(this._rotationQuaternion);
         } else {
             Quaternion.RotationYawPitchRollToRef(this._rotation.y, this._rotation.x, this._rotation.z, rotation);
-            cache.rotation.copyFrom(this._rotation);
         }
 
         // Compose