-
Notifications
You must be signed in to change notification settings - Fork 12
/
example_camera_spin.html
172 lines (121 loc) · 4.64 KB
/
example_camera_spin.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Camera Spin 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 Controls<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, trackballWidget, meshMaterial, lineMaterial;
var orthographicHeight = 1000;
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 aspect = window.innerWidth / window.innerHeight;
camera = new THREE.PerspectiveCamera( 40, aspect, 1, 10000 );
// var width = orthographicHeight * aspect;
// camera = new THREE.OrthographicCamera( width / -2, width / 2,
// orthographicHeight / 2, orthographicHeight / -2, .1, 2000 );
// Set offset from pivot point before creating CameraSpinControls
camera.position.set(0, 0, 500);
cameraSpinControl = new CameraSpinControls( camera, renderer.domElement );
if ( camera.isOrthographicCamera ) {
// With orthographic projection calculations are more direct
// using POINTER_SPHERE_MAPPING.HOLROYD or SHOEMAKE
cameraSpinControl.spinControl.setPointerToSphereMapping(
cameraSpinControl.spinControl.POINTER_SPHERE_MAPPING.SHOEMAKE );
}
var light = new THREE.DirectionalLight( 0xffffff, 2 );
light.position.set( 1, 1, 1 );
scene.add( light );
// 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 );
// 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;
function updateCameraSpinUI() {
trackballWidget.position.copy( cameraSpinControl.target );
var r = cameraSpinControl.spinControl.trackballRadius;
trackballWidget.scale.set( r, r, r );
}
cameraSpinControl.addEventListener( 'start', function ( event ) {
trackballWidget.visible = true;
updateCameraSpinUI();
} );
cameraSpinControl.addEventListener( 'change', function ( event ) {
updateCameraSpinUI();
} );
cameraSpinControl.addEventListener( 'end', function ( event ) {
trackballWidget.visible = false;
} );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
var aspect = window.innerWidth / window.innerHeight;
if ( camera.isPerspectiveCamera ) {
camera.aspect = aspect;
} else if( camera.isOrthographicCamera ) {
var width = orthographicHeight * aspect;
camera.left = width / -2;
camera.right = width / 2;
}
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
cameraSpinControl.onWindowResize();
render();
}
function animate(timeStamp) {
requestAnimationFrame( animate );
cameraSpinControl.update();
render();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>