ソースを参照

BufferGeometry: add .applyQuaternion() method (#21835)

WestLangley 4 年 前
コミット
3f7f2ccce6

+ 5 - 2
docs/api/en/core/BufferGeometry.html

@@ -176,8 +176,11 @@
 		</p>
 
 
-		<h3>[method:null applyMatrix4]( [param:Matrix4 matrix] )</h3>
-		<p>Bakes matrix transform directly into vertex coordinates.</p>
+		<h3>[method:this applyMatrix4]( [param:Matrix4 matrix] )</h3>
+		<p>Applies the matrix transform to the geometry.</p>
+
+		<h3>[method:this applyQuaternion]( [param:Quaternion quaternion] )</h3>
+		<p>Applies the rotation represented by the quaternion to the geometry.</p>
 
 		<h3>[method:BufferGeometry center] ()</h3>
 		<p>Center the geometry based on the bounding box.</p>

+ 10 - 0
src/core/BufferGeometry.js

@@ -174,6 +174,16 @@ class BufferGeometry extends EventDispatcher {
 
 	}
 
+	applyQuaternion( q ) {
+
+		_m1.makeRotationFromQuaternion( q );
+
+		this.applyMatrix4( _m1 );
+
+		return this;
+
+	}
+
 	rotateX( angle ) {
 
 		// rotate geometry around world x-axis

+ 16 - 0
test/unit/src/core/BufferGeometry.tests.js

@@ -8,6 +8,7 @@ import {
 } from '../../../../src/core/BufferAttribute';
 import { Vector3 } from '../../../../src/math/Vector3';
 import { Matrix4 } from '../../../../src/math/Matrix4';
+import { Quaternion } from '../../../../src/math/Quaternion';
 import { Sphere } from '../../../../src/math/Sphere';
 import {
 	x,
@@ -212,6 +213,21 @@ export default QUnit.module( 'Core', () => {
 
 		} );
 
+		QUnit.test( "applyQuaternion", ( assert ) => {
+
+			var geometry = new BufferGeometry();
+			geometry.setAttribute( "position", new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
+
+			var q = new Quaternion( 0.5, 0.5, 0.5, 0.5 );
+			geometry.applyQuaternion( q );
+
+			var pos = geometry.attributes.position.array;
+
+			// geometry was rotated around the (1, 1, 1) axis.
+			assert.ok( pos[ 0 ] === 3 && pos[ 1 ] === 1 && pos[ 2 ] === 2 &&
+				pos[ 3 ] === 6 && pos[ 4 ] === 4 && pos[ 5 ] === 5, "vertices were rotated properly" );
+		} );
+
 		QUnit.test( "rotateX/Y/Z", ( assert ) => {
 
 			var geometry = new BufferGeometry();