-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
119 lines (103 loc) · 3.07 KB
/
index.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
const openvr = require("openvr");
const express = require('express')
const app = express()
app.get('/', (req, res) => res.redirect('/index.html'));
app.use(express.static('static'));
app.use('/modules', express.static('node_modules'));
const server = app.listen(8080)
try {
openvr.k_unMaxTrackedDeviceCount = 64;
var vr = openvr.VR_Init(openvr.EVRApplicationType.Background)
} catch(e) {
console.log(e)
}
function convert_to_quaternion(_pose_mat) {
let pose_mat = Array.from(_pose_mat)
let r_w = Math.sqrt(Math.abs(1 + pose_mat[0][0] + pose_mat[1][1] + pose_mat[2][2]))/2
let r_x = (pose_mat[2][1]-pose_mat[1][2])/(4*r_w)
let r_y = (pose_mat[0][2]-pose_mat[2][0])/(4*r_w)
let r_z = (pose_mat[1][0]-pose_mat[0][1])/(4*r_w)
let x = pose_mat[0][3]
let y = pose_mat[1][3]
let z = pose_mat[2][3]
return {
'x':x,
'y':y,
'z':z,
'qw':r_w,
'qx':r_x,
'qy':r_y,
'qz':r_z
};
}
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 9090 });
wss.on('connection', function connection(ws) {
console.log("SRV connection");
ws.on('open', function(event){
console.log("CLT Open", event);
});
ws.on('message', function(event){
console.log("CLT Message", event);
});
ws.on('error', function(event){
console.error("CLT Error", event);
});
ws.on('close', function(event){
console.log("CLT Close", event);
});
});
wss.on('close', function(event){
console.log("SRV Close", event);
})
wss.on('error', function(event){
console.error("SRV Error", event);
})
if (vr) {
const interval = setInterval(function updateClientsPosition() {
let poses = vr.GetDeviceToAbsoluteTrackingPose(openvr.ETrackingUniverseOrigin.Standing, 0)
for (let i = 0; i < openvr.k_unMaxTrackedDeviceCount; ++i) {
if (poses[i] !== undefined && poses[i].poseIsValid === true) {
switch (vr.GetTrackedDeviceClass(i)) {
case openvr.ETrackedDeviceClass.Invalid:
console.error('INVALID DEVICE');
break;
case openvr.ETrackedDeviceClass.HMD:
console.log('HMD');
break;
case openvr.ETrackedDeviceClass.Controller:
console.log('CONTROLLER');
let quat = convert_to_quaternion(poses[i].deviceToAbsoluteTracking)
let msg = JSON.stringify(quat)
console.log(msg)
wss.clients.forEach(function each(ws) {
if (ws.readyState == ws.OPEN)
ws.send(msg);
})
return;
break;
case openvr.ETrackedDeviceClass.GenericTracker:
console.log('GENERIC TRACKER');
break;
case openvr.ETrackedDeviceClass.TrackingReference:
console.log('TRACKING REFERENCE');
break;
case openvr.ETrackedDeviceClass.DisplayRedirect:
console.log('DISPLAY REDIRECT');
break;
default:
console.error("ERROR UNKNOWN DEVICE")
}
}
}
}, 50);
}
function shutdown(code) {
console.log('SIGNAL received...');
clearInterval(interval);
wss.close();
server.close();
openvr.VR_Shutdown();
};
process.once('SIGINT', shutdown);
process.once('SIGTERM', shutdown);