-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
211 lines (182 loc) · 6.42 KB
/
demo.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
const path = require('path');
const fs = require('fs');
const THREE = require('three-zeo');
const oneVector = new THREE.Vector3(1, 1, 1);
const localVector = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const nativeHtml = document.createElement('native-html');
nativeHtml.show();
let canvasWidth = 1280;
let canvasHeight = 1024;
const canvas = document.createElement('native-canvas', canvasWidth, canvasHeight);
const gl = canvas.getContext('webgl');
const _requestJsonFile = p => new Promise((accept, reject) => {
fs.readFile(p, (err, s) => {
if (!err) {
accept(JSON.parse(s));
} else {
reject(err);
}
});
});
const _requestJsonMesh = (modelJson, modelTexturePath) => new Promise((accept, reject) => {
const loader = new THREE.ObjectLoader();
loader.setTexturePath(modelTexturePath);
loader.parse(modelJson, accept);
});
const controllerjsPath = path.join(require.resolve('controllerjs'), '..');
Promise.all([
_requestJsonFile(path.join(controllerjsPath, 'model', 'controller.json'))
.then(controllerJson => _requestJsonMesh(controllerJson, path.join(controllerjsPath, 'model', '/'))),
navigator.getVRDisplays()
.then(displays => displays[0]),
])
.then(([
controllerModel,
display,
]) => {
const renderer = new THREE.WebGLRenderer({
canvas: canvas,
context: gl,
antialias: true,
});
// renderer.setSize(canvas.width, canvas.height);
renderer.setClearColor(0xffffff, 1);
const scene = new THREE.Scene();
const _makeCamera = () => {
const camera = new THREE.PerspectiveCamera(90, canvasWidth / canvasHeight, 0.1, 1000);
camera.position.set(0, 0, 2);
camera.lookAt(new THREE.Vector3(0, 0, 0));
return camera;
};
let camera = _makeCamera();
scene.add(camera);
const directionalLight = new THREE.DirectionalLight(0xffffff, 1);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
const boxMesh = (() => {
const geometry = new THREE.BoxBufferGeometry(1, 1, 1);
const material = new THREE.MeshPhongMaterial({
color: 0xFF0000,
});
return new THREE.Mesh(geometry, material);
})();
scene.add(boxMesh);
const leftControllerMesh = controllerModel.children[0].clone(true);
scene.add(leftControllerMesh);
const rightControllerMesh = controllerModel.children[0].clone(true);
scene.add(rightControllerMesh);
const _render = () => {
let gamepads = navigator.getGamepads() || [];
const leftGamepad = gamepads.find(gamepad => gamepad && gamepad.hand === 'left');
if (leftGamepad) {
leftControllerMesh.matrix.compose(localVector.fromArray(leftGamepad.position), localQuaternion.fromArray(leftGamepad.quaternion), oneVector);
leftControllerMesh.matrix.decompose(leftControllerMesh.position, leftControllerMesh.quaternion, leftControllerMesh.scale);
leftControllerMesh.updateMatrixWorld();
}
const rightGamepad = gamepads.find(gamepad => gamepad && gamepad.hand === 'right');
if (rightGamepad) {
rightControllerMesh.matrix.compose(localVector.fromArray(rightGamepad.position), localQuaternion.fromArray(rightGamepad.quaternion), oneVector);
rightControllerMesh.matrix.decompose(rightControllerMesh.position, rightControllerMesh.quaternion, rightControllerMesh.scale);
rightControllerMesh.updateMatrixWorld();
}
renderer.render(scene, camera);
renderer.context.flush();
requestAnimationFrame(_render);
};
requestAnimationFrame(_render);
canvas.on('resize', e => {
console.log('resize', e);
canvasWidth = e.width;
canvasHeight = e.height;
canvas.style.width = canvasWidth;
canvas.style.height = canvasHeight;
if (display && !display.isPresenting) {
renderer.setSize(canvasWidth, canvasHeight);
camera.aspect = canvasWidth / canvasHeight;
camera.updateProjectionMatrix();
}
});
canvas.on('mousemove', e => {
if (canvas.pointerLockElement) {
e.deltaX = e.pageX - (canvasWidth / 2);
e.deltaY = e.pageY - (canvasHeight / 2);
canvas.setCursorPos(canvasWidth / 2, canvasHeight / 2);
} else {
e.deltaX = 0;
e.deltaY = 0;
}
console.log('mousemove', e);
});
canvas.on('mousedown', e => {
console.log('mousedown', e);
if ((e.pageX / canvasWidth) < 0.5) {
if (!canvas.pointerLockElement) {
canvas.requestPointerLock();
}
} else {
if (display && !display.isPresenting) {
return display.requestPresent([
{
leftBounds: [0, 0, 0.5, 1],
rightBounds: [0.5, 0, 0.5, 1],
source: canvas,
},
])
.then(() => {
renderer.vr.enabled = true;
// renderer.vr.standing = true;
renderer.vr.setDevice(display);
const leftEye = display.getEyeParameters('left');
const rightEye = display.getEyeParameters('right');
const width = Math.max(leftEye.renderWidth, rightEye.renderWidth) * 2;
const height = Math.max(leftEye.renderHeight, rightEye.renderHeight);
renderer.setSize(width, height);
canvas.style.width = canvasWidth;
canvas.style.height = canvasHeight;
})
.catch(err => {
console.warn(err);
});
}
}
});
canvas.on('mouseup', e => {
console.log('mouseup', e);
});
canvas.on('keydown', e => {
console.log('keyup', e);
if (e.keyCode === 27) { // esc
if (canvas.pointerLockElement) {
canvas.exitPointerLock();
} else if (display && display.isPresenting) {
renderer.vr.enabled = false;
renderer.vr.setDevice(null);
renderer.setSize(canvasWidth, canvasHeight);
scene.remove(camera);
camera = _makeCamera();
scene.add(camera);
display.exitPresent()
.then(() => {
// nothing
})
.catch(err => {
console.warn(err);
});
}
}
});
canvas.on('keyup', e => {
console.log('keyup', e);
});
canvas.on('keypress', e => {
console.log('keypress', e);
});
canvas.on('quit', () => {
process.exit(0);
});
})
.catch(err => {
console.warn(err.stack);
process.exit(1);
});