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

Add quaternion.fromBufferAttribute #18947

Merged
merged 1 commit into from
Mar 23, 2020
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
8 changes: 8 additions & 0 deletions docs/api/en/math/Quaternion.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ <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:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )</h3>
<p>
[page:BufferAttribute attribute] - the source attribute.<br />
[page:Integer index] - index in the attribute.<br /><br />

Sets [page:.x x], [page:.y y], [page:.z z], [page:.w w] properties of this quaternion from the [page:BufferAttribute attribute].
</p>

<h2>Static Methods</h2>

<p>
Expand Down
8 changes: 8 additions & 0 deletions docs/api/zh/math/Quaternion.html
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ <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:this fromBufferAttribute]( [param:BufferAttribute attribute], [param:Integer index] )</h3>
<p>
[page:BufferAttribute attribute] - the source attribute.<br />
[page:Integer index] - index in the attribute.<br /><br />

Sets [page:.x x], [page:.y y], [page:.z z], [page:.w w] properties of this quaternion from the [page:BufferAttribute attribute].
</p>

<h2>Static Methods</h2>

<p>
Expand Down
11 changes: 11 additions & 0 deletions src/math/Quaternion.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,17 @@ Object.assign( Quaternion.prototype, {

},

fromBufferAttribute: function ( attribute, index ) {

this._x = attribute.getX( index );
this._y = attribute.getY( index );
this._z = attribute.getZ( index );
this._w = attribute.getW( index );

return this;

},

_onChange: function ( callback ) {

this._onChangeCallback = callback;
Expand Down
35 changes: 33 additions & 2 deletions test/unit/src/math/Quaternion.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
/* global QUnit */

import { BufferAttribute } from '../../../../src/core/BufferAttribute';
import { Quaternion } from '../../../../src/math/Quaternion';
import { Vector3 } from '../../../../src/math/Vector3';
import { Vector4 } from '../../../../src/math/Vector4';
Expand Down Expand Up @@ -711,6 +712,38 @@ export default QUnit.module( 'Maths', () => {

} );

QUnit.test( "fromBufferAttribute", ( assert ) => {

var a = new Quaternion();

var attribute = new BufferAttribute( new Float32Array( [

0, 0, 0, 1,
.7, 0, 0, .7,
0, .7, 0, .7,

] ), 4 );

a.fromBufferAttribute( attribute, 0 );
assert.numEqual( a.x, 0, 'index 0, component x' );
assert.numEqual( a.y, 0, 'index 0, component y' );
assert.numEqual( a.z, 0, 'index 0, component z' );
assert.numEqual( a.w, 1, 'index 0, component w' );

a.fromBufferAttribute( attribute, 1 );
assert.numEqual( a.x, .7, 'index 1, component x' );
assert.numEqual( a.y, 0, 'index 1, component y' );
assert.numEqual( a.z, 0, 'index 1, component z' );
assert.numEqual( a.w, .7, 'index 1, component w' );

a.fromBufferAttribute( attribute, 2 );
assert.numEqual( a.x, 0, 'index 2, component x' );
assert.numEqual( a.y, .7, 'index 2, component y' );
assert.numEqual( a.z, 0, 'index 2, component z' );
assert.numEqual( a.w, .7, 'index 2, component w' );

} );

QUnit.test( "_onChange", ( assert ) => {

var b = false;
Expand Down Expand Up @@ -778,5 +811,3 @@ export default QUnit.module( 'Maths', () => {
} );

} );

QUnit.module( "Quaternion" );