-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgenerateThree.js
133 lines (100 loc) · 3.56 KB
/
generateThree.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
/*
Generate 3D render using serial data from IMU
*/
'use strict';
// Declare required variables
var dataRollx = 0;
var dataRolly = 0;
var dataRollz = 0;
var dataRollxArray = [];
var dataRollyArray = [];
var dataRollzArray = [];
var accuracy = 2;
var orderOfMag = (Math.PI/180);
var container;
var camera, scene, renderer;
var cube, plane;
var targetRotation = 0;
var targetRotationOnMouseDown = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
//Connect to socket.io
var serverIP = "localhost";
var socket = io.connect(serverIP + ':5000');
console.log('socket connected to: ' + serverIP);
// Start reading IMU data
runSocket();
init();
animate();
function runSocket() {
socket.on('serial_update', function(data) {
if (data.charAt(0) === 'O') {
console.log(data);
var dataArray = data.split(/ /);
// set x
dataRollx = (dataArray[1] *= orderOfMag).toFixed(accuracy);
// set y
dataRolly = (dataArray[2] *= orderOfMag).toFixed(accuracy);
// set z
dataRollz = (dataArray[3] *= orderOfMag).toFixed(accuracy);
console.log(dataRollx + "," + dataRolly + "," + dataRollz);
}
});
}
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
var info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '10px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.innerHTML = 'Visualize IMU';
info.setAttribute('id', 'pourHeading');
container.appendChild( info );
$("#pourHeading").append("<div id='subHeading'></div>");
// Set up camera
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.y = 150;
camera.position.z = 500;
scene = new THREE.Scene();
// Create cube
var geometry = new THREE.BoxGeometry( 200, 200, 200 );
for ( var i = 0; i < geometry.faces.length; i += 2 ) {
var hex = Math.random() * 0xffffff;
geometry.faces[ i ].color.setHex( hex );
geometry.faces[ i + 1 ].color.setHex( hex );
}
var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors, overdraw: 0.5 } );
cube = new THREE.Mesh( geometry, material );
cube.position.y = 150;
scene.add( cube );
// Create background plane
var geometry = new THREE.PlaneBufferGeometry( 400, 200 );
geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
var material = new THREE.MeshBasicMaterial( { color: 0xe0e0e0, overdraw: 0.5 } );
plane = new THREE.Mesh( geometry, material );
scene.add( plane );
renderer = new THREE.CanvasRenderer();
renderer.setClearColor( 0xf0f0f0 );
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize, false );
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
cube.rotation.x = -dataRollx;
cube.rotation.y = -dataRollz;
cube.rotation.z = -dataRolly;
renderer.render( scene, camera );
}