Преглед на файлове

Change FromUnitVectors to FromUnitVectorsToRef

Also use correct serialization function for vector3
michalzr преди 4 години
родител
ревизия
d58d591526
променени са 2 файла, в които са добавени 8 реда и са изтрити 8 реда
  1. 4 3
      materialsLibrary/src/sky/skyMaterial.ts
  2. 4 5
      src/Maths/math.vector.ts

+ 4 - 3
materialsLibrary/src/sky/skyMaterial.ts

@@ -116,11 +116,12 @@ export class SkyMaterial extends PushMaterial {
     /**
      * Defines the vector the skyMaterial should consider as up. (default is Vector3(0, 1, 0) as returned by Vector3.Up())
      */
-    @serialize()
+    @serializeAsVector3()
     public up: Vector3 = Vector3.Up();
 
     // Private members
     private _cameraPosition: Vector3 = Vector3.Zero();
+    private _skyOrientation: Quaternion = new Quaternion();
 
     /**
      * Instantiates a new sky material.
@@ -307,8 +308,8 @@ export class SkyMaterial extends PushMaterial {
             this.sunPosition.y = this.distance * Math.sin(phi) * Math.sin(theta);
             this.sunPosition.z = this.distance * Math.sin(phi) * Math.cos(theta);
 
-            const rotation = Quaternion.FromUnitVectors(Vector3.Up(), this.up);
-            this.sunPosition.rotateByQuaternionToRef(rotation, this.sunPosition);
+            Quaternion.FromUnitVectorsToRef(Vector3.Up(), this.up, this._skyOrientation);
+            this.sunPosition.rotateByQuaternionToRef(this._skyOrientation, this.sunPosition);
         }
 
         this._activeEffect.setVector3("sunPosition", this.sunPosition);

+ 4 - 5
src/Maths/math.vector.ts

@@ -3458,19 +3458,18 @@ export class Quaternion {
      * @param vecTo defines the direction vector to which to rotate
      * @returns the new quaternion
      */
-    public static FromUnitVectors(vecFrom: DeepImmutable<Vector3>, vecTo: DeepImmutable<Vector3>): Quaternion {
-        let result: Quaternion;
+    public static FromUnitVectorsToRef(vecFrom: DeepImmutable<Vector3>, vecTo: DeepImmutable<Vector3>, result: Quaternion): Quaternion {
         const r = Vector3.Dot(vecFrom, vecTo) + 1;
 
         if (r < Epsilon) {
             if (Math.abs(vecFrom.x) > Math.abs(vecFrom.z)) {
-                result = new Quaternion(-vecFrom.y, vecFrom.x, 0, 0);
+                result.set(-vecFrom.y, vecFrom.x, 0, 0);
             } else {
-                result = new Quaternion(0, - vecFrom.z, vecFrom.y, 0);
+                result.set(0, - vecFrom.z, vecFrom.y, 0);
             }
         } else {
             const crossProduct = Vector3.Cross(vecFrom, vecTo);
-            result = new Quaternion(
+            result.set(
                 crossProduct.x,
                 crossProduct.y,
                 crossProduct.z,