-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.html
103 lines (86 loc) · 3.53 KB
/
graphics.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
<!DOCTYPE html>
<html>
<head> <meta charset="utf-8">
<title>IoT IMU</title>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<script type="module">
import * as THREE from './js/three.module.js';
let camera, scene, renderer;
let mesh;
let x = 0,y = 0,z = 0,w = 1;
const targetQuaternion = new THREE.Quaternion();
const euler = new THREE.Euler()
const clock = new THREE.Clock();
const speed = 2;
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 400;
scene = new THREE.Scene();
const texture = new THREE.TextureLoader().load( 'textures/crate.gif' );
const geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
const material = new THREE.MeshBasicMaterial( { map: texture } );
mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer( { antialias: true } );
renderer.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
window.addEventListener( 'resize', onWindowResize, false );
let ip = localStorage.getItem("ip");
let url = localStorage.getItem("url");
let ws;
console.log(ip);
console.log(url);
if(ip){
ws = new WebSocket(ip);
websocketWaiter();
} else if(url){
ws = new WebSocket(url);
websocketWaiter();
}
if(ws){
ws.onmessage = function (event) {
let data = event.data.split(',');
w = parseFloat(data[4], 10);
x = parseFloat(data[5], 10);
y = parseFloat(data[6], 10);
z = parseFloat(data[7], 10);
};
}
function websocketWaiter(){
setTimeout(function(){
if (ws.readyState === 1) {
console.log("Connection is made")
ws.send("imu");
} else {
console.log("wait for connection...")
websocketWaiter();
}
}, 5); // wait 5 milisecond for the connection...
};
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function animate() {
requestAnimationFrame( animate );
const delta = clock.getDelta();
targetQuaternion.set(x,y,z,w);
if ( ! mesh.quaternion.equals( targetQuaternion ) ) {
const step = speed * delta;
euler.setFromQuaternion(targetQuaternion)
mesh.setRotationFromEuler(euler)
mesh.quaternion.rotateTowards(targetQuaternion, step );
}
renderer.render( scene, camera );
}
</script>
</body>
</html>