Skip to content

Commit 2ec52a4

Browse files
authored
Examples: Integrate lookAt() into the rotateTowards() demo. (#31063)
1 parent 0e317c0 commit 2ec52a4

File tree

4 files changed

+30
-150
lines changed

4 files changed

+30
-150
lines changed

examples/files.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,6 @@
534534
"misc_exporter_usdz",
535535
"misc_exporter_exr",
536536
"misc_exporter_ktx2",
537-
"misc_lookat",
538537
"misc_raycaster_helper"
539538
],
540539
"css2d": [

examples/misc_lookat.html

Lines changed: 0 additions & 145 deletions
This file was deleted.
-113 KB
Binary file not shown.

examples/webgl_math_orientation_transform.html

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@
2626

2727
import * as THREE from 'three';
2828

29+
import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
30+
2931
let camera, scene, renderer, mesh, target;
3032

3133
const spherical = new THREE.Spherical();
3234
const rotationMatrix = new THREE.Matrix4();
3335
const targetQuaternion = new THREE.Quaternion();
3436
const clock = new THREE.Clock();
35-
const speed = 2;
37+
const speed = Math.PI / 2;
38+
39+
const params = {
40+
useLookAt: false,
41+
};
3642

3743
init();
3844

@@ -78,6 +84,13 @@
7884

7985
//
8086

87+
const gui = new GUI();
88+
89+
gui.add( params, 'useLookAt' );
90+
gui.open();
91+
92+
//
93+
8194
generateTarget();
8295

8396
}
@@ -95,10 +108,23 @@
95108

96109
const delta = clock.getDelta();
97110

98-
if ( ! mesh.quaternion.equals( targetQuaternion ) ) {
111+
if ( mesh.quaternion.equals( targetQuaternion ) === false ) {
112+
113+
if ( params.useLookAt === true ) {
114+
115+
// using lookAt() will make the mesh instantly look at the target
116+
117+
mesh.lookAt( target.position );
118+
119+
} else {
120+
121+
// using rotateTowards() will gradually rotate the mesh towards the target
122+
// the "speed" variable represents the rotation speed in radians per seconds
123+
124+
const step = speed * delta;
125+
mesh.quaternion.rotateTowards( targetQuaternion, step );
99126

100-
const step = speed * delta;
101-
mesh.quaternion.rotateTowards( targetQuaternion, step );
127+
}
102128

103129
}
104130

0 commit comments

Comments
 (0)