Explorar o código

Fixed #3259: Add normalizeToNew and NormalizeToRef

Sebastien Vandenberghe %!s(int64=7) %!d(string=hai) anos
pai
achega
a5553a7845
Modificáronse 1 ficheiros con 28 adicións e 1 borrados
  1. 28 1
      src/Math/babylon.math.ts

+ 28 - 1
src/Math/babylon.math.ts

@@ -1348,10 +1348,10 @@
             return (this.x * this.x + this.y * this.y + this.z * this.z);
         }
 
-        // Methods
         /**
          * Normalize the current Vector3.  
          * Returns the updated Vector3.  
+         * /!\ In place operation.
          */
         public normalize(): Vector3 {
             var len = this.length();
@@ -1366,6 +1366,33 @@
         }
 
         /**
+         * Normalize the current Vector3 to a new vector.
+         * @returns the new Vector3.
+         */
+        public normalizeToNew(): Vector3 {
+            const normalized = new Vector3(0, 0, 0);
+            this.normalizeToRef(normalized);
+            return normalized;
+        }
+
+        /**
+         * Normalize the current Vector3 to the reference.
+         * @param the reference to update.
+         * @returns the updated Vector3.
+         */
+        public normalizeToRef(reference: Vector3): Vector3 {
+            var len = this.length();
+            if (len === 0 || len === 1.0) {
+                reference.set(this.x, this.y, this.z);
+                return reference;
+            }
+
+            const scale = 1.0 / len;
+            this.scaleToRef(scale, reference);
+            return reference;
+        }
+
+        /**
          * Returns a new Vector3 copied from the current Vector3.  
          */
         public clone(): Vector3 {