forked from PaulHax/spin-controls
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_camera_spin_pointer_pivot.html
More file actions
217 lines (151 loc) · 5.97 KB
/
Copy pathexample_camera_spin_pointer_pivot.html
File metadata and controls
217 lines (151 loc) · 5.97 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<!DOCTYPE html>
<html lang="en">
<head>
<title>Camera Spin Pointer Pivot Example - SpinControls</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
margin: 0px;
background-color: #000000;
color: #fff;
font-family:Monospace;
text-align: center;
font-size: 15px;
line-height: 30px;
overflow: hidden;
}
#info {
position: absolute;
top: 0px; width: 100%;
padding: 15px;
z-index:100;
box-sizing: border-box;
pointer-events: none;
}
</style>
</head>
<body>
<div id="info">
Camera Spin Pointer Pivot<br />Orbit camera - Left click / 1 finger | Pan - Right click / 2 fingers | Dolly - Mouse wheel / pinch
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r124/three.min.js"></script>
<script src="SpinControls.js"></script>
<script src="CameraSpinControls.js"></script>
<script>
var camera, scene, renderer, controls, trackballWidget, meshMaterial, lineMaterial;
init();
render();
animate( 0 );
function init() {
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
scene.add( new THREE.GridHelper( 1000, 6 ) );
var light = new THREE.DirectionalLight( 0xffffff, 2 );
light.position.set( 1, 1, 1 );
scene.add( light );
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set(100, 0, 500);
controls = new CameraSpinControls( camera, renderer.domElement );
controls.isTargetOffCenter = true;
// Something to look at
var geometry = new THREE.TorusKnotBufferGeometry( 100, 20, 100, 16 );
var material = new THREE.MeshNormalMaterial();
var torusKnot = new THREE.Mesh( geometry, material );
scene.add( torusKnot );
var raycastable = [ torusKnot ];
// Transparent sphere to represent the CameraSpinControls trackball
trackballWidget = new THREE.Group();
trackballWidget.position.set( 0, 0, 0 );
var geometry = new THREE.SphereBufferGeometry( 1, 8, 8 );
meshMaterial = new THREE.MeshBasicMaterial( { color: 0xaaaaff, flatShading: true, transparent: true, opacity: 0.2 } );
lineMaterial = new THREE.LineBasicMaterial( { color: 0xffffff, transparent: true, opacity: 0.5 } );
trackballWidget.add( new THREE.LineSegments( geometry, lineMaterial ) );
trackballWidget.add( new THREE.Mesh( geometry, meshMaterial ) );
scene.add( trackballWidget );
trackballWidget.visible = false;
var raycaster = new THREE.Raycaster();
var pointerPos = new THREE.Vector2();
function onMouseMove( event ) {
pointerPos.x = ( event.clientX / window.innerWidth ) * 2 - 1;
pointerPos.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
window.addEventListener( 'mousemove', onMouseMove, false );
function onPointerDown() {
raycaster.setFromCamera( pointerPos, camera );
var intersects = raycaster.intersectObjects( raycastable );
if(intersects.length) {
controls.setTargetPosition(intersects[0].point);
} else {
// Move trackball to center of screen if not clicking on anything.
// Comment out to rotate with trackball centered on mouse.
controls.target.set(0, 0, -controls.distanceFromPivot);
controls.target.applyQuaternion(camera.quaternion);
controls.target.add(camera.position);
controls.setTargetPosition(controls.target);
}
}
window.addEventListener( 'mousedown', onPointerDown, true );
function touchStart( event ) {
pointerPos.x = ( event.touches[ 0 ].clientX / window.innerWidth ) * 2 - 1;
pointerPos.y = - ( event.touches[ 0 ].clientY / window.innerHeight ) * 2 + 1;
onPointerDown();
}
window.addEventListener( 'touchstart', touchStart, true );
function updateCameraSpinUI() {
trackballWidget.position.copy( controls.target );
var r = controls.spinControl.trackballRadius;
trackballWidget.scale.set( r, r, r );
}
controls.addEventListener( 'start', function ( event ) {
if (event.state === controls.STATE.DOLLY) {
// Center trackball on cursor so dolly/zoom moves in mouse direction.
// Clamp min/max zoom distance by placing trackball at intersection point.
// Only useful if CameraSpinControls.minDistance/maxDistance set.
raycaster.setFromCamera( pointerPos, camera );
var intersects = raycaster.intersectObjects( raycastable );
if(intersects.length) {
controls.setTargetPosition(intersects[0].point);
} else {
controls.target.copy(raycaster.ray.direction);
controls.target.setLength(controls.distanceFromPivot);
controls.target.add(raycaster.ray.origin);
controls.setTargetPosition(controls.target);
}
} else {
// Don't turn on trackball graphics for dolly
trackballWidget.visible = true;
updateCameraSpinUI();
}
} );
controls.addEventListener( 'change', function ( event ) {
updateCameraSpinUI();
} );
controls.addEventListener( 'end', function ( event ) {
if (event.state !== controls.STATE.DOLLY) {
trackballWidget.visible = false;
}
} );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
controls.onWindowResize();
render();
}
function animate(timeStamp) {
requestAnimationFrame( animate );
controls.update();
render();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>