-
-
Notifications
You must be signed in to change notification settings - Fork 266
/
Copy pathauto-rotate.html
136 lines (99 loc) · 3.45 KB
/
auto-rotate.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>=^.^=</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="info">
<p><a href="https://github.com/yomotsu/camera-controls">GitHub repo</a></p>
<button onclick="cameraControls.rotate( 45 * THREE.MathUtils.DEG2RAD, 0, true )">rotate theta 45deg</button>
<button onclick="cameraControls.rotate( -90 * THREE.MathUtils.DEG2RAD, 0, true )">rotate theta -90deg</button>
<button onclick="cameraControls.rotate( 360 * THREE.MathUtils.DEG2RAD, 0, true )">rotate theta 360deg</button>
<button onclick="cameraControls.rotate( 0, 20 * THREE.MathUtils.DEG2RAD, true )">rotate phi 20deg</button>
<br>
<button onclick="cameraControls.truck( 1, 0, true)">truck( 1, 0 )</button>
<button onclick="cameraControls.truck( 0, 1, true)">truck( 0, 1 )</button>
<button onclick="cameraControls.truck( -1, -1, true )">truck( -1, -1 )</button>
<br>
<button onclick="cameraControls.reset( true )">reset</button>
</div>
<script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/[email protected]/build/three.module.js",
"three/addons/": "https://unpkg.com/[email protected]/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import CameraControls from './dist/camera-controls.module.js';
CameraControls.install( { THREE: THREE } );
const width = window.innerWidth;
const height = window.innerHeight;
const clock = new THREE.Clock();
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera( 60, width / height, 0.01, 100 );
camera.position.set( 0, 0, 5 );
const renderer = new THREE.WebGLRenderer();
renderer.setSize( width, height );
document.body.appendChild( renderer.domElement );
const cameraControls = new CameraControls( camera, renderer.domElement );
const mesh = new THREE.Mesh(
new THREE.BoxGeometry( 1, 1, 1 ),
new THREE.MeshBasicMaterial( { color: 0xff0000, wireframe: true } )
);
scene.add( mesh );
const gridHelper = new THREE.GridHelper( 50, 50 );
gridHelper.position.y = - 1;
scene.add( gridHelper );
renderer.render( scene, camera );
// Exclusive control for user dragging
let userDragging = false;
let disableAutoRotate = false;
const onRest = () => {
cameraControls.removeEventListener( 'rest', onRest );
userDragging = false;
disableAutoRotate = false;
}
cameraControls.addEventListener( 'controlstart', () => {
cameraControls.removeEventListener( 'rest', onRest );
userDragging = true;
disableAutoRotate = true;
} );
cameraControls.addEventListener( 'controlend', () => {
if ( cameraControls.active ) {
cameraControls.addEventListener( 'rest', onRest );
} else {
onRest();
}
} );
//
cameraControls.addEventListener( 'transitionstart', () => {
if ( userDragging ) return;
disableAutoRotate = true;
cameraControls.addEventListener( 'rest', onRest );
} );
( function anim () {
const delta = clock.getDelta();
const elapsed = clock.getElapsedTime();
const updated = cameraControls.update( delta );
if ( ! disableAutoRotate ) {
cameraControls.azimuthAngle += 20 * delta * THREE.MathUtils.DEG2RAD;
}
// if ( elapsed > 30 ) { return; }
requestAnimationFrame( anim );
if ( updated ) {
renderer.render( scene, camera );
}
} )();
// make variable available to browser console
globalThis.THREE = THREE;
globalThis.cameraControls = cameraControls;
</script>
</body>
</html>