-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathexample1.html
More file actions
289 lines (252 loc) · 7.42 KB
/
example1.html
File metadata and controls
289 lines (252 loc) · 7.42 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<!doctype html>
<html lang="en">
<head>
<title>Gimbal Device Test</title>
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:site" content="@Marquizzo">
<meta name="twitter:title" content="Three.js gimbal tool">
<meta name="twitter:description" content="A Three.js-based device to isolate accelerometer movements by its three-axes components: Yaw (y-axis), Pitch (x-axis), Roll(z-axis) without gimbal-lock.">
<meta name="twitter:image" content="http://dyadstudios.com/code/gimbal/sshot.png">
<meta name="twitter:image:width" content="1200">
<meta name="twitter:image:height" content="630">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no,shrink-to-fit=no">
<meta name="description" content="A Three.js-based device to isolate accelerometer movements by its three-axes components: Yaw (y-axis), Pitch (x-axis), Roll(z-axis) without gimbal-lock.">
<meta name="author" content="Marquizzo">
<style type="text/css">
body, html{
margin: 0px;
padding: 0px;
overflow: hidden;
background: #000;
color: #fff;
font-family: "Helvetica", sans-serif;
}
.quadrant{
position: absolute;
width: 50%;
height: 50%;
border: 1px dashed #666;
box-sizing: border-box;
}
#topLeft{
top: 0;
left: 0;
}
#topRight{
top: 0;
right: 0;
}
#botLeft{
bottom: 0;
left: 0;
}
#botRight{
bottom: 0;
right: 0;
}
.label, .angle{
position: absolute;
width: 50%;
left: 10px;
}
.label{
top: 10px;
font-weight: 700;
}
.angle{
top: 30px;
}
#recalibrateBtn {
position: fixed;
bottom: 50%;
right: 0;
width: 50%;
background: #333;
height: 30px;
padding-top: 5px;
vertical-align: middle;
text-align: center;
font-weight: bold;
}
#overlay {
position: fixed;
top: 0;
bottom: 0;
left: 0;
right: 0;
background: rgba(0, 0, 0, 0.7);
display: flex;
justify-content: center;
align-items: center;
}
#permissionBtn {
background: #ccc;
color: #111;
border-radius: 10px;
width: 200px;
text-align: center;
padding: 20px 10px;
border: 1px solid #fff;
cursor: pointer;
text-transform: uppercase;
}
#permissionBtn:hover {
background: #ddd;
}
</style>
</head>
<body>
<main id="pageMain">
<div id="topLeft" class="quadrant">
<div class="label">Yaw</div>
<div class="angle" id="divYaw">0°</div>
</div>
<div id="topRight" class="quadrant">
<div class="label">Pitch</div>
<div class="angle" id="divPitch">0°</div>
</div>
<div id="botLeft" class="quadrant">
<div class="label">Roll</div>
<div class="angle" id="divRoll">0°</div>
</div>
<div id="botRight" class="quadrant">
<div class="label">All</div>
<div class="angle"></div>
</div>
<div id="recalibrateBtn">Recalibrate</div>
<div id="overlay">
<div id="permissionBtn">Request gyroscope access</div>
</div>
</main>
<!-- Load Three.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/99/three.min.js"></script>
<!-- Load Gimbal.js -->
<script src="./src/Gimbal.js"></script>
<script>
// Three.js boilerplate
var scene, renderer, cam;
// Arrows
var arrowYaw, arrowPitch, arrowRoll, arrowAll;
// DOM Elements
var divYaw, divPitch, divRoll;
// Viewport
var vpW, vpH, ratio;
var gimbal;
var DEG = 180 / Math.PI;
function init() {
scene = new THREE.Scene();
cam = new THREE.OrthographicCamera(-10, 10, 10, -10, -10, 10);
renderer = new THREE.WebGLRenderer({antialias: true});
document.getElementById("pageMain").appendChild(renderer.domElement);
document.getElementById("recalibrateBtn").addEventListener("click", recalibrateBtnClick);
document.getElementById("permissionBtn").addEventListener("click", accessBtnClick);
// Get divs write angles
divYaw = document.getElementById("divYaw");
divPitch = document.getElementById("divPitch");
divRoll = document.getElementById("divRoll");
buildArrows();
// Create Gimbal object (disabled by default)
gimbal = new Gimbal();
window.addEventListener("resize", onWindowResize, false);
onWindowResize();
// Fire up render loop
render();
}
// Gimbal access must be requested upon user interaction
function accessBtnClick() {
// Check if request method exists in this browser
if (!DeviceMotionEvent || !DeviceMotionEvent.requestPermission) {
console.warn("This browser does not support requesting DeviceMotionEvent permission.");
document.getElementById("permissionBtn").innerHTML = "Not supported";
return;
}
DeviceMotionEvent.requestPermission().then(response => {
// Access granted by user
if (response == 'granted') {
gimbal.enable();
document.getElementById("overlay").style.display = "none";
}
// Access denied by user
else {
document.getElementById("permissionBtn").innerHTML = "Permission Denied";
}
});
}
// Creates a single arrow-shaped mesh
function makeArrowMesh(color) {
var shape = new THREE.Shape();
shape.moveTo(1, -2);
shape.lineTo(1, 0);
shape.lineTo(2, 0);
shape.lineTo(0, 2);
shape.lineTo(-2, 0);
shape.lineTo(-1, 0);
shape.lineTo(-1, -2);
shape.lineTo(1, -2);
var extrudeSettings = {
steps: 1,
depth: 1,
bevelEnabled: false
}
var arrowGeom = new THREE.ExtrudeBufferGeometry(shape, extrudeSettings);
return new THREE.Mesh(arrowGeom, new THREE.MeshLambertMaterial({color}));
}
// Builds four arrows in the scene
function buildArrows() {
// Scene lights
var light = new THREE.DirectionalLight(0xffffff, 1.0);
light.position.set(-0.5, 1, 1);
scene.add(light);
var ambLight = new THREE.DirectionalLight(0xffffff, 0.3);
ambLight.position.set(0.5, -1, 1);
scene.add(ambLight);
// Yaw arrow
arrowYaw = makeArrowMesh(0xff0099);
arrowYaw.position.set(-5, 5, 0);
scene.add(arrowYaw);
// Pitch arrow
arrowPitch = makeArrowMesh(0x99ff00);
arrowPitch.position.set(5, 5, 0);
scene.add(arrowPitch);
// Roll arrow
arrowRoll = makeArrowMesh(0x0099ff);
arrowRoll.position.set(-5, -5, 0);
scene.add(arrowRoll);
// All arrow
arrowAll = makeArrowMesh(0xff9900);
arrowAll.position.set(5, -5, 0);
scene.add(arrowAll);
}
function onWindowResize() {
vpW = window.innerWidth;
vpH = window.innerHeight;
ratio = vpW / vpH;
cam.left = -ratio * 10;
cam.right = ratio * 10;
cam.updateProjectionMatrix();
renderer.setSize(vpW, vpH);
}
function recalibrateBtnClick() {
gimbal.recalibrate();
}
function render() {
gimbal.update();
// Get Yaw from gimbal
arrowYaw.rotation.y = gimbal.yaw;
divYaw.innerHTML = (gimbal.yaw * DEG).toFixed(1) + "°";
// Get Pitch from gimbal
arrowPitch.rotation.x = gimbal.pitch;
divPitch.innerHTML = (gimbal.pitch * DEG).toFixed(1) + "°";
// Get Roll from gimbal
arrowRoll.rotation.z = gimbal.roll;
divRoll.innerHTML = (gimbal.roll * DEG).toFixed(1) + "°";
// Gimbal.quaternion contains combined rotations
arrowAll.quaternion.copy(gimbal.quaternion);
renderer.render(scene, cam);
requestAnimationFrame(render);
}
init();
</script>
</body>
</html>