forked from PaulHax/spin-controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_camera_spin.html
151 lines (112 loc) · 4.33 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
<!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;
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 ) );
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.set(0, 0, 500);
cameraSpinControl = new CameraSpinControls( camera, renderer.domElement );
// Uncomment for third person view of CameraSpinControls
// And comment out CameraSpinControls above
// camera.position.set( 500, 200, 500 );
// camera.lookAt( 0, 0, 0 );
// var camera2 = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
// scene.add( camera2 );
// const axesHelper = new THREE.AxesHelper( 50 );
// camera2.add( axesHelper );
// cameraSpinControl = new CameraSpinControls( camera2, renderer.domElement );
// cameraSpinControl.distanceFromPivot = 200;
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() {
camera.aspect = window.innerWidth / window.innerHeight;
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>