2
0
Эх сурвалжийг харах

Merge pull request #19034 from EthanHermsey/dev

Vector*: Added random()
Mr.doob 5 жил өмнө
parent
commit
55fcebe956

+ 5 - 0
docs/api/en/math/Vector2.html

@@ -332,6 +332,11 @@
 		Returns an array [x, y], or copies x and y into the provided [page:Array array].
 		</p>
 
+		<h3>[method:this random]()</h3>
+		<p>
+			Sets the x and y components of the vector to a random value [0-1].
+		</p>
+
 		<h2>Source</h2>
 
 		<p>

+ 4 - 0
docs/api/en/math/Vector3.html

@@ -437,6 +437,10 @@
 		Projects this vector from the camera's normalized device coordinate (NDC) space into world space.
 		</p>
 
+		<h3>[method:this random]()</h3>
+		<p>
+			Sets the x, y and z components of the vector to a random value [0-1].
+		</p>
 
 		<h2>Source</h2>
 

+ 5 - 0
docs/api/en/math/Vector4.html

@@ -323,6 +323,11 @@
 		Returns an array [x, y, z, w], or copies x, y, z and w into the provided [page:Array array].
 		</p>
 
+		<h3>[method:this random]()</h3>
+		<p>
+			Sets the x, y, z and w components of the vector to a random value [0-1].
+		</p>
+
 		<h2>Source</h2>
 
 		<p>

+ 5 - 0
docs/api/zh/math/Vector2.html

@@ -333,6 +333,11 @@
 		返回一个数组[x, y],或者将x和y复制到所传入的[page:Array array]中。
 		</p>
 
+		<h3>[method:this random]()</h3>
+		<p>
+			Sets the x and y components of the vector to a random value [0-1].
+		</p>
+
 		<h2>源代码</h2>
 
 		<p>

+ 4 - 0
docs/api/zh/math/Vector3.html

@@ -426,6 +426,10 @@
 		Projects this vector from the camera's normalized device coordinate (NDC) space into world space.
 		</p>
 
+		<h3>[method:this random]()</h3>
+		<p>
+			Sets the x, y and z components of the vector to a random value [0-1].
+		</p>
 
 		<h2>源代码</h2>
 

+ 5 - 0
docs/api/zh/math/Vector4.html

@@ -322,6 +322,11 @@
 		返回一个数组[x, y, z, w],或者将x、y、z和w复制到所传入的[page:Array array]中。
 		</p>
 
+		<h3>[method:this random]()</h3>
+		<p>
+			Sets the x, y, z and w components of the vector to a random value [0-1].
+		</p>
+
 		<h2>源代码</h2>
 
 		<p>

+ 5 - 0
src/math/Vector2.d.ts

@@ -459,4 +459,9 @@ export class Vector2 implements Vector {
 	 */
 	manhattanDistanceTo( v: Vector2 ): number;
 
+	/**
+	 * Sets this vector's x and y from Math.random
+	 */
+	random(): this;
+
 }

+ 9 - 0
src/math/Vector2.js

@@ -482,6 +482,15 @@ Object.assign( Vector2.prototype, {
 
 		return this;
 
+	},
+
+	random: function () {
+
+		this.x = Math.random();
+		this.y = Math.random();
+
+		return this;
+
 	}
 
 } );

+ 5 - 0
src/math/Vector3.d.ts

@@ -287,4 +287,9 @@ export class Vector3 implements Vector {
 		offset?: number
 	): this;
 
+	/**
+	 * Sets this vector's x, y and z from Math.random
+	 */
+	random(): this;
+
 }

+ 10 - 0
src/math/Vector3.js

@@ -712,6 +712,16 @@ Object.assign( Vector3.prototype, {
 
 		return this;
 
+	},
+
+	random: function () {
+
+		this.x = Math.random();
+		this.y = Math.random();
+		this.z = Math.random();
+
+		return this;
+
 	}
 
 } );

+ 5 - 0
src/math/Vector4.d.ts

@@ -210,4 +210,9 @@ export class Vector4 implements Vector {
 		offset?: number
 	): this;
 
+	/**
+	 * Sets this vector's x, y, z and w from Math.random
+	 */
+	random(): this;
+
 }

+ 18 - 7
src/math/Vector4.js

@@ -323,17 +323,17 @@ Object.assign( Vector4.prototype, {
 			m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
 
 		if ( ( Math.abs( m12 - m21 ) < epsilon ) &&
-		     ( Math.abs( m13 - m31 ) < epsilon ) &&
-		     ( Math.abs( m23 - m32 ) < epsilon ) ) {
+			 ( Math.abs( m13 - m31 ) < epsilon ) &&
+			 ( Math.abs( m23 - m32 ) < epsilon ) ) {
 
 			// singularity found
 			// first check for identity matrix which must have +1 for all terms
 			// in leading diagonal and zero in other terms
 
 			if ( ( Math.abs( m12 + m21 ) < epsilon2 ) &&
-			     ( Math.abs( m13 + m31 ) < epsilon2 ) &&
-			     ( Math.abs( m23 + m32 ) < epsilon2 ) &&
-			     ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
+				 ( Math.abs( m13 + m31 ) < epsilon2 ) &&
+				 ( Math.abs( m23 + m32 ) < epsilon2 ) &&
+				 ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
 
 				// this singularity is identity matrix so angle = 0
 
@@ -419,8 +419,8 @@ Object.assign( Vector4.prototype, {
 		// as we have reached here there are no singularities so we can handle normally
 
 		var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) +
-		                   ( m13 - m31 ) * ( m13 - m31 ) +
-		                   ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
+						   ( m13 - m31 ) * ( m13 - m31 ) +
+						   ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
 
 		if ( Math.abs( s ) < 0.001 ) s = 1;
 
@@ -646,6 +646,17 @@ Object.assign( Vector4.prototype, {
 
 		return this;
 
+	},
+
+	random: function () {
+
+		this.x = Math.random();
+		this.y = Math.random();
+		this.z = Math.random();
+		this.w = Math.random();
+
+		return this;
+
 	}
 
 } );