forked from ZiCog/madgwick.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.js
391 lines (313 loc) · 10.4 KB
/
scene.js
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// scene.js
"use strict";
/*global THREE: true,
window: true,
document: true,
requestAnimationFrame: true,
scene: true,
chrome: true,
ArrayBuffer: true,
Uint8Array: true
*/
var keyQ1 = new THREE.Quaternion();
var axisX = new THREE.Vector3(1, 0, 0);
var axisY = new THREE.Vector3(0, 1, 0);
var axisZ = new THREE.Vector3(0, 0, 1);
var objects = [];
//
function rotateOnAxis(object, axis, angle) {
keyQ1.setFromAxisAngle(axis, angle);
//object.quaternion.multiply(keyQ1);
object.quaternion.copy(keyQ1);
}
function dumbbell(shereRadius, cylinderRadius, colour) {
var geometry,
material,
mesh,
group;
group = new THREE.Object3D(); // Create container
geometry = new THREE.SphereGeometry(shereRadius, 32, 32);
material = new THREE.MeshPhongMaterial({color: colour});
mesh = new THREE.Mesh(geometry, material);
mesh.position.y = 1.0;
group.add(mesh);
objects.push(mesh);
mesh = new THREE.Mesh(geometry, material);
mesh.position.y = -1.0;
group.add(mesh);
objects.push(mesh);
geometry = new THREE.CylinderGeometry(cylinderRadius, cylinderRadius, 2, 32);
mesh = new THREE.Mesh(geometry, material);
group.add(mesh);
objects.push(group);
return group;
}
function showAxes() {
var xPointer,
yPointer,
zPointer;
// The color coding is X-Y-Z => red-green-blue.
// X axis pointer
xPointer = dumbbell(0.1, 0.04, 0xff0000);
rotateOnAxis(xPointer, axisZ, Math.PI / 2);
scene.add(xPointer);
// Y axis pointer
yPointer = dumbbell(0.1, 0.04, 0x00ff00, axisX, 0);
rotateOnAxis(yPointer, axisY, Math.PI / 2);
scene.add(yPointer);
// Z axis pointer
zPointer = dumbbell(0.1, 0.04, 0x0000ff, axisX, 0);
rotateOnAxis(zPointer, axisX, Math.PI / 2);
scene.add(zPointer);
}
// Create a renderer using an existing canvas
var canvas = document.getElementById("canvas");
var renderer = new THREE.WebGLRenderer({ canvas: canvas });
var CANVAS_WIDTH = canvas.scrollWidth;
var CANVAS_HEIGHT = canvas.scrollHeight;
var CANVAS_ASPECT = CANVAS_WIDTH / CANVAS_HEIGHT;
console.log("WIDTH", CANVAS_WIDTH);
console.log("HEIGHT", CANVAS_HEIGHT);
renderer.setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
// Create a scene.
var scene = new THREE.Scene();
// Create a camera
var camera = new THREE.PerspectiveCamera(30, CANVAS_ASPECT, 0.1, 1000);
camera.position.x = 2.5;
camera.position.y = -2.5;
camera.position.z = 2.5;
camera.up = new THREE.Vector3(0, 0, 1);
camera.lookAt(new THREE.Vector3(0.2, 0.2, 0.2));
// Create lights
var dirLight;
var hemiLight;
hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 0.6);
hemiLight.color.setHSL(0.6, 1, 0.6);
hemiLight.groundColor.setHSL(0.095, 1, 0.75);
hemiLight.position.set(0, 100, 0);
scene.add(hemiLight);
//
dirLight = new THREE.DirectionalLight(0xffffff, 1);
dirLight.color.setHSL(0.1, 1, 0.95);
scene.add(dirLight);
showAxes();
// Add a cube to the scene
var material = new THREE.MeshPhongMaterial({color: 0xffff00,
specular: 0xa0a0a0,
shininess: 15,
vertexColors: THREE.FaceColors,
shading: THREE.FlatShading
});
var materials = [
new THREE.MeshLambertMaterial({
map: THREE.ImageUtils.loadTexture('textures/Right.png')
}),
new THREE.MeshLambertMaterial({
map: THREE.ImageUtils.loadTexture('textures/Left.png')
}),
new THREE.MeshLambertMaterial({
map: THREE.ImageUtils.loadTexture('textures/Back.png')
}),
new THREE.MeshLambertMaterial({
map: THREE.ImageUtils.loadTexture('textures/Front.png')
}),
new THREE.MeshLambertMaterial({
map: THREE.ImageUtils.loadTexture('textures/Top.png')
}),
new THREE.MeshLambertMaterial({
map: THREE.ImageUtils.loadTexture('textures/Bottom.png')
}),
];
material = new THREE.MeshFaceMaterial(materials);
var cube = new THREE.Mesh(new THREE.CubeGeometry(1.0, 0.6, 0.3), material);
cube.position.x = 0.5;
cube.position.y = 0.5;
cube.position.z = 0.5;
scene.add(cube);
objects.push(cube);
var geometry = new THREE.TorusGeometry(1, 0.02, 16, 100);
var material = new THREE.MeshPhongMaterial({color: 0x0000ff,
specular: 0xa0a0a0,
shininess: 4,
vertexColors: THREE.FaceColors,
shading: THREE.FlatShading
});
var torus = new THREE.Mesh(geometry, material);
scene.add(torus);
geometry = new THREE.TorusGeometry(1, 0.02, 16, 100);
var material = new THREE.MeshPhongMaterial({color: 0x00ff00,
specular: 0xa0a0a0,
shininess: 4,
vertexColors: THREE.FaceColors,
shading: THREE.FlatShading
});
torus = new THREE.Mesh(geometry, material);
rotateOnAxis(torus, axisX, Math.PI / 2);
scene.add(torus);
geometry = new THREE.TorusGeometry(1, 0.02, 16, 100);
var material = new THREE.MeshPhongMaterial({color: 0xff0000,
specular: 0xa0a0a0,
shininess: 4,
vertexColors: THREE.FaceColors,
shading: THREE.FlatShading
});
torus = new THREE.Mesh(geometry, material);
rotateOnAxis(torus, axisY, Math.PI / 2);
scene.add(torus);
// Render function.
var render = function () {
requestAnimationFrame(render);
//rotateOnAxis(cube, axisX, 0.01);
//rotateOnAxis(cube, axisY, 0.01);
renderer.render(scene, camera);
};
render();
function onWindowResize() {
var layer1 = document.getElementById('layer1');
CANVAS_WIDTH = layer1.offsetWidth;
CANVAS_HEIGHT = layer1.offsetHeight;
console.log("WIDTH", CANVAS_WIDTH);
console.log("HEIGHT", CANVAS_HEIGHT);
camera.aspect = CANVAS_WIDTH / CANVAS_HEIGHT;
camera.updateProjectionMatrix();
renderer.setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
}
function onKeyDown(event) {
switch (event.keyCode) {
case 72: // h
break;
case 68: // d
break;
case 38: // Up
rotateOnAxis(cube, axisY, +0.9);
break;
case 40: // Down
rotateOnAxis(cube, axisY, -0.9);
break;
case 37: // Left
rotateOnAxis(cube, axisZ, +0.9);
break;
case 39: // Right
rotateOnAxis(cube, axisZ, -0.9);
break;
}
}
window.addEventListener('resize', onWindowResize, false);
document.addEventListener('keydown', onKeyDown, false);
function random(low, high) {
return Math.random() * (high - low) + low;
}
// Test
console.log(q0, q1, q2, q3);
var gx = 0.5;
var gy = 0.0;
var gz = 0.0;
var ax = 1.0;
var ay = 1.0;
var az = 1.0;
var mx = 0.0;
var my = 0.0;
var mz = 0.0;
var samplePeriodMillis = 1000 / sampleFreq;
var step = 0;
var frequencyRoll = 3;
var frequencyPitch = 3;
var frequencyYaw = 0.1;
var doYaw = false;
var doRoll = false;
var doPitch = false;
var noiseLevel = 0.01;
function doTest() {
// Gyro input
gx = 0.0; // Not in use
gy = 0.0;
gz = 0.0;
// Accelerometer input
if (doRoll) {
ax = 0.1 * Math.cos(frequencyRoll * (2 * Math.PI) * step / sampleFreq); // Roll oscillations
} else {
ax = 0.0;
}
if (doPitch) {
ay = 0.1 * Math.sin(frequencyPitch * (2 * Math.PI) * step / sampleFreq); // Pitch oscillations
} else {
ay = 0.0;
}
az = -1.0; // Gravity constant
if (doYaw) {
// Magnetometer input
mx = Math.cos(frequencyYaw * (2 * Math.PI) * step / sampleFreq); // Yaw, rotate around the verticle axis (z)
my = Math.sin(frequencyYaw * (2 * Math.PI) * step / sampleFreq);
mz = 0.0;
} else {
mx = 0.0;
my = 0.0;
mz = 0.0;
}
// Move the light around with the magnetometer vector.
dirLight.position.set(Math.sin(frequencyYaw * (2 * Math.PI) * step / sampleFreq),
Math.cos(frequencyYaw * (2 * Math.PI) * step / sampleFreq),
0.5);
/*
// Put some noise on the gyro
var noise = function () { return random(-noiseLevel, +noiseLevel); };
gx += noise();
gy += noise();
gz += noise();
ax += noise();
ay += noise();
az += noise();
mx += noise();
my += noise();
mz += noise();
*/
ax = imuData.a[0];
ay = imuData.a[1];
az = imuData.a[2];
gx = imuData.g[0];
gy = imuData.g[1];
gz = imuData.g[2];
// mx = imuData.m[0];
// my = imuData.m[1];
// mz = imuData.m[2];
madgwickAHRSupdate(gx, gy, gz, ax, ay, az, mx, my, mz);
// mahonyAHRSupdate(gx, gy, gz, ax, ay, az, mx, my, mz);
// Note: Seems to me Madgwick has put the quaternian axis vector in q1, q2, q3 and the angle in q0
//
cube.quaternion.set(q1, q2, q3, q0);
if ((q0 === NaN) || (q1 === NaN) || (q2 === NaN) || (q3 === NaN)) {
console.log ("Exploded!");
}
step += 1;
}
// Find intersections
var projector = new THREE.Projector();
var mouse = { x: 0, y: 0 };
var count = 0;
// Mouse listener
document.addEventListener('mousedown', function (event) {
var raycaster,
vector,
intersects;
var rectObject = renderer.domElement.getBoundingClientRect();
mouse.x = ((event.clientX - rectObject.left) / rectObject.width) * 2 - 1;
mouse.y = -((event.clientY - rectObject.top) / rectObject.height) * 2 + 1;
vector = new THREE.Vector3(mouse.x, mouse.y, 1);
projector.unprojectVector(vector, camera);
raycaster = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());
intersects = raycaster.intersectObjects(objects);
if (intersects.length > 0) {
//info.innerHTML = 'INTERSECT Count: ' + ++count;
console.log('INTERSECT Count: ' + count++);
doYaw = !doYaw;
console.log(doYaw);
var el = document.getElementById('divstack');
el.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT);
el.mozRequestFullScreen();
el.msRequestFullscreen();
el.requestFullscreen(); // standard
}
}, false);
setInterval(function () {
doTest();
}, samplePeriodMillis);