Przeglądaj źródła

Math: Make Euler, Quaternion and Color iterable. (#23796)

* Math: Make Euler, Quaternion and Color iterable.

* Euler: Honor order in iterator.

* Update Euler.html
Michael Herzog 3 lat temu
rodzic
commit
b857c9d6bb

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

@@ -13,6 +13,10 @@
 		Class representing a color.
 		</p>
 
+		<p>
+		Iterating through a [name] instance will yield its components (r, g, b) in the corresponding order.
+		</p>
+
 		<h2>Code Examples</h2>
 
 		<p>

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

@@ -16,6 +16,10 @@
 			axes in specified amounts per axis, and a specified axis order.
 		</p>
 
+		<p>
+		Iterating through a [name] instance will yield its components (x, y, z, order) in the corresponding order.
+		</p>
+
 		<h2>Code Example</h2>
 
 		<code>const a = new THREE.Euler( 0, 1, 1.57, 'XYZ' );

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

@@ -14,6 +14,10 @@
 			Quaternions are used in three.js to represent [link:https://en.wikipedia.org/wiki/Quaternions_and_spatial_rotation rotations].
 		</p>
 
+		<p>
+		Iterating through a [name] instance will yield its components (x, y, z, w) in the corresponding order.
+		</p>
+
 		<h2>Code Example</h2>
 
 		<code>

+ 1 - 1
docs/api/en/math/Vector2.html

@@ -37,7 +37,7 @@
 		</p>
 
 		<p>
-			Iterating through a Vector2 instance will yield its components (x, y) in the corresponding order.
+			Iterating through a [name] instance will yield its components (x, y) in the corresponding order.
 		</p>
 
 		<h2>Code Example</h2>

+ 1 - 1
docs/api/en/math/Vector3.html

@@ -36,7 +36,7 @@
 		</p>
 
 		<p>
-		Iterating through a Vector3 instance will yield its components (x, y, z) in the corresponding order.
+		Iterating through a [name] instance will yield its components (x, y, z) in the corresponding order.
 		</p>
 
 

+ 1 - 1
docs/api/en/math/Vector4.html

@@ -35,7 +35,7 @@
 		</p>
 
 		<p>
-		Iterating through a Vector4 instance will yield its components (x, y, z, w) in the corresponding order.
+		Iterating through a [name] instance will yield its components (x, y, z, w) in the corresponding order.
 		</p>
 
 		<h2>Code Example</h2>

+ 8 - 0
src/math/Color.js

@@ -594,6 +594,14 @@ class Color {
 
 	}
 
+	*[ Symbol.iterator ]() {
+
+		yield this.r;
+		yield this.g;
+		yield this.b;
+
+	}
+
 }
 
 Color.NAMES = _colorKeywords;

+ 9 - 0
src/math/Euler.js

@@ -297,6 +297,15 @@ class Euler {
 
 	_onChangeCallback() {}
 
+	*[ Symbol.iterator ]() {
+
+		yield this._x;
+		yield this._y;
+		yield this._z;
+		yield this._order;
+
+	}
+
 }
 
 Euler.prototype.isEuler = true;

+ 9 - 0
src/math/Quaternion.js

@@ -682,6 +682,15 @@ class Quaternion {
 
 	_onChangeCallback() {}
 
+	*[ Symbol.iterator ]() {
+
+		yield this._x;
+		yield this._y;
+		yield this._z;
+		yield this._w;
+
+	}
+
 }
 
 Quaternion.prototype.isQuaternion = true;

+ 10 - 0
test/unit/src/math/Color.tests.js

@@ -651,6 +651,16 @@ export default QUnit.module( 'Maths', () => {
 
 		} );
 
+		QUnit.test( 'iterable', ( assert ) => {
+
+			var c = new Color( 0.5, 0.75, 1 );
+			var array = [ ...c ];
+			assert.strictEqual( array[ 0 ], 0.5, 'Color is iterable.' );
+			assert.strictEqual( array[ 1 ], 0.75, 'Color is iterable.' );
+			assert.strictEqual( array[ 2 ], 1, 'Color is iterable.' );
+
+		} );
+
 
 	} );
 

+ 11 - 0
test/unit/src/math/Euler.tests.js

@@ -413,6 +413,17 @@ export default QUnit.module( 'Maths', () => {
 
 		} );
 
+		QUnit.test( 'iterable', ( assert ) => {
+
+			var e = new Euler( 0.5, 0.75, 1, 'YZX' );
+			var array = [ ...e ];
+			assert.strictEqual( array[ 0 ], 0.5, 'Euler is iterable.' );
+			assert.strictEqual( array[ 1 ], 0.75, 'Euler is iterable.' );
+			assert.strictEqual( array[ 2 ], 1, 'Euler is iterable.' );
+			assert.strictEqual( array[ 3 ], 'YZX', 'Euler is iterable.' );
+
+		} );
+
 	} );
 
 } );

+ 11 - 0
test/unit/src/math/Quaternion.tests.js

@@ -850,6 +850,17 @@ export default QUnit.module( 'Maths', () => {
 
 		} );
 
+		QUnit.test( 'iterable', ( assert ) => {
+
+			var q = new Quaternion( 0, 0.5, 0.7, 1 );
+			var array = [ ...q ];
+			assert.strictEqual( array[ 0 ], 0, 'Quaternion is iterable.' );
+			assert.strictEqual( array[ 1 ], 0.5, 'Quaternion is iterable.' );
+			assert.strictEqual( array[ 2 ], 0.7, 'Quaternion is iterable.' );
+			assert.strictEqual( array[ 3 ], 1, 'Quaternion is iterable.' );
+
+		} );
+
 	} );
 
 } );

+ 9 - 0
test/unit/src/math/Vector2.tests.js

@@ -686,6 +686,15 @@ export default QUnit.module( 'Maths', () => {
 
 		} );
 
+		QUnit.test( 'iterable', ( assert ) => {
+
+			var v = new Vector2( 0, 1 );
+			var array = [ ...v ];
+			assert.strictEqual( array[ 0 ], 0, 'Vector2 is iterable.' );
+			assert.strictEqual( array[ 1 ], 1, 'Vector2 is iterable.' );
+
+		} );
+
 	} );
 
 } );

+ 10 - 0
test/unit/src/math/Vector3.tests.js

@@ -1006,6 +1006,16 @@ export default QUnit.module( 'Maths', () => {
 
 		} );
 
+		QUnit.test( 'iterable', ( assert ) => {
+
+			var v = new Vector3( 0, 0.5, 1 );
+			var array = [ ...v ];
+			assert.strictEqual( array[ 0 ], 0, 'Vector3 is iterable.' );
+			assert.strictEqual( array[ 1 ], 0.5, 'Vector3 is iterable.' );
+			assert.strictEqual( array[ 2 ], 1, 'Vector3 is iterable.' );
+
+		} );
+
 	} );
 
 } );

+ 11 - 0
test/unit/src/math/Vector4.tests.js

@@ -717,6 +717,17 @@ export default QUnit.module( 'Maths', () => {
 
 		} );
 
+		QUnit.test( 'iterable', ( assert ) => {
+
+			var v = new Vector4( 0, 0.3, 0.7, 1 );
+			var array = [ ...v ];
+			assert.strictEqual( array[ 0 ], 0, 'Vector4 is iterable.' );
+			assert.strictEqual( array[ 1 ], 0.3, 'Vector4 is iterable.' );
+			assert.strictEqual( array[ 2 ], 0.7, 'Vector4 is iterable.' );
+			assert.strictEqual( array[ 3 ], 1, 'Vector4 is iterable.' );
+
+		} );
+
 	} );
 
 } );