Bladeren bron

Add quaternion.fromBufferAttribute

Don McCurdy 5 jaren geleden
bovenliggende
commit
073439a5f5

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

@@ -195,6 +195,14 @@
 		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>

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

@@ -195,6 +195,14 @@
 		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>

+ 11 - 0
src/math/Quaternion.js

@@ -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;

+ 33 - 2
test/unit/src/math/Quaternion.tests.js

@@ -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';
@@ -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;
@@ -778,5 +811,3 @@ export default QUnit.module( 'Maths', () => {
 	} );
 
 } );
-
-QUnit.module( "Quaternion" );