Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quaternion: Add toJSON(). #25631

Merged
merged 2 commits into from
Mar 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/api/en/math/Color.html
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] ) </h3>
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>
Expand Down
5 changes: 5 additions & 0 deletions docs/api/en/math/Quaternion.html
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] )</h3>
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 />
Expand Down
5 changes: 5 additions & 0 deletions docs/api/it/math/Color.html
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] ) </h3>
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>
Expand Down
5 changes: 5 additions & 0 deletions docs/api/it/math/Quaternion.html
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] )</h3>
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 />
Expand Down
5 changes: 5 additions & 0 deletions docs/api/zh/math/Color.html
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] ) </h3>
返回一个格式为[ 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>
Expand Down
5 changes: 5 additions & 0 deletions docs/api/zh/math/Quaternion.html
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ <h3>[method:Array toArray]( [param:Array array], [param:Integer offset] )</h3>
在形如[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 />
Expand Down
2 changes: 2 additions & 0 deletions examples/webgl_geometry_cube.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

function init() {

console.log( JSON.stringify( new THREE.Quaternion() ) );
Mugen87 marked this conversation as resolved.
Show resolved Hide resolved

camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 400;

Expand Down
6 changes: 6 additions & 0 deletions src/math/Quaternion.js
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,12 @@ class Quaternion {

}

toJSON() {

return this.toArray();

}

_onChange( callback ) {

this._onChangeCallback = callback;
Expand Down
11 changes: 11 additions & 0 deletions test/unit/src/math/Quaternion.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down