Skip to content

Commit 3f7f2cc

Browse files
authored
BufferGeometry: add .applyQuaternion() method (#21835)
1 parent a943d00 commit 3f7f2cc

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

docs/api/en/core/BufferGeometry.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,11 @@ <h3>[method:null addGroup]( [param:Integer start], [param:Integer count], [param
176176
</p>
177177

178178

179-
<h3>[method:null applyMatrix4]( [param:Matrix4 matrix] )</h3>
180-
<p>Bakes matrix transform directly into vertex coordinates.</p>
179+
<h3>[method:this applyMatrix4]( [param:Matrix4 matrix] )</h3>
180+
<p>Applies the matrix transform to the geometry.</p>
181+
182+
<h3>[method:this applyQuaternion]( [param:Quaternion quaternion] )</h3>
183+
<p>Applies the rotation represented by the quaternion to the geometry.</p>
181184

182185
<h3>[method:BufferGeometry center] ()</h3>
183186
<p>Center the geometry based on the bounding box.</p>

src/core/BufferGeometry.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,16 @@ class BufferGeometry extends EventDispatcher {
174174

175175
}
176176

177+
applyQuaternion( q ) {
178+
179+
_m1.makeRotationFromQuaternion( q );
180+
181+
this.applyMatrix4( _m1 );
182+
183+
return this;
184+
185+
}
186+
177187
rotateX( angle ) {
178188

179189
// rotate geometry around world x-axis

test/unit/src/core/BufferGeometry.tests.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
} from '../../../../src/core/BufferAttribute';
99
import { Vector3 } from '../../../../src/math/Vector3';
1010
import { Matrix4 } from '../../../../src/math/Matrix4';
11+
import { Quaternion } from '../../../../src/math/Quaternion';
1112
import { Sphere } from '../../../../src/math/Sphere';
1213
import {
1314
x,
@@ -212,6 +213,21 @@ export default QUnit.module( 'Core', () => {
212213

213214
} );
214215

216+
QUnit.test( "applyQuaternion", ( assert ) => {
217+
218+
var geometry = new BufferGeometry();
219+
geometry.setAttribute( "position", new BufferAttribute( new Float32Array( [ 1, 2, 3, 4, 5, 6 ] ), 3 ) );
220+
221+
var q = new Quaternion( 0.5, 0.5, 0.5, 0.5 );
222+
geometry.applyQuaternion( q );
223+
224+
var pos = geometry.attributes.position.array;
225+
226+
// geometry was rotated around the (1, 1, 1) axis.
227+
assert.ok( pos[ 0 ] === 3 && pos[ 1 ] === 1 && pos[ 2 ] === 2 &&
228+
pos[ 3 ] === 6 && pos[ 4 ] === 4 && pos[ 5 ] === 5, "vertices were rotated properly" );
229+
} );
230+
215231
QUnit.test( "rotateX/Y/Z", ( assert ) => {
216232

217233
var geometry = new BufferGeometry();

0 commit comments

Comments
 (0)