Browse Source

Quaternion: Add toJSON(). (#25631)

* Quaternion: Add toJSON().

* Docs: Improve Color.
Michael Herzog 2 years ago
parent
commit
365e9de079

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

@@ -322,6 +322,11 @@ const color7 = new THREE.Color( 1, 0, 0 );
 		Returns an array of the form [ r, g, b ].
 		</p>
 
+		<h3>[method:Number toJSON]()</h3>
+		<p>
+		This methods defines the serialization result of [name]. Returns the color as a hexadecimal value.
+		</p>
+
 		<h2>Source</h2>
 
 		<p>

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

@@ -218,6 +218,11 @@
 		Returns the numerical elements of this quaternion in an array of format [x, y, z, w].
 		</p>
 
+		<h3>[method:Array toJSON]()</h3>
+		<p>
+		This methods defines the serialization result of [name]. Returns the numerical elements of this quaternion in an array of format [x, y, z, w].
+		</p>
+
 		<h3>[method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )</h3>
 		<p>
 		[page:BufferAttribute attribute] - the source attribute.<br />

+ 5 - 0
docs/api/it/math/Color.html

@@ -319,6 +319,11 @@ const color7 = new THREE.Color( 1, 0, 0 );
 		Restituisce un array della forma [ r, g, b ].
 		</p>
 
+		<h3>[method:Number toJSON]()</h3>
+		<p>
+		This methods defines the serialization result of [name]. Returns the color as a hexadecimal value.
+		</p>
+
 		<h2>Source</h2>
 
 		<p>

+ 5 - 0
docs/api/it/math/Quaternion.html

@@ -218,6 +218,11 @@
 		Restituisce gli elementi numerici di questo quaternione in un array del formato [x, y, z, w].
 		</p>
 
+		<h3>[method:Array toJSON]()</h3>
+		<p>
+		This methods defines the serialization result of [name]. Restituisce gli elementi numerici di questo quaternione in un array del formato [x, y, z, w].
+		</p>
+
 		<h3>[method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )</h3>
 		<p>
 		[page:BufferAttribute attribute] - l'attributo sorgente.<br />

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

@@ -315,6 +315,11 @@
 		返回一个格式为[ r, g, b ] 数组。
 		</p>
 
+		<h3>[method:Number toJSON]()</h3>
+		<p>
+		This methods defines the serialization result of [name]. Returns the color as a hexadecimal value.
+		</p>
+
 		<h2>源码(Source)</h2>
 
 		<p>

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

@@ -203,6 +203,11 @@
 		在形如[x, y, z, w]的数组中,返回四元数中的数字元素。
 		</p>
 
+		<h3>[method:Array toJSON]()</h3>
+		<p>
+		This methods defines the serialization result of [name]. 在形如[x, y, z, w]的数组中,返回四元数中的数字元素。
+		</p>
+
 		<h3>[method:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )</h3>
 		<p>
 		[page:BufferAttribute attribute] - 源 attribute。<br />

+ 2 - 0
examples/webgl_geometry_cube.html

@@ -33,6 +33,8 @@
 
 			function init() {
 
+				console.log( JSON.stringify( new THREE.Quaternion() ) );
+
 				camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
 				camera.position.z = 400;
 

+ 6 - 0
src/math/Quaternion.js

@@ -654,6 +654,12 @@ class Quaternion {
 
 	}
 
+	toJSON() {
+
+		return this.toArray();
+
+	}
+
 	_onChange( callback ) {
 
 		this._onChangeCallback = callback;

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

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