-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhands-press-button.html
257 lines (202 loc) · 9.16 KB
/
hands-press-button.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webxr hands - press button</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link type="text/css" rel="stylesheet" href="main.css">
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webxr hands - press button<br />
(Oculus Browser with #webxr-hands flag enabled)
</div>
<script type="module">
import * as THREE from './libs/three.module.js';
import { VRButton } from './jsm/webxr/VRButton.js';
import { XRControllerModelFactory } from './jsm/webxr/XRControllerModelFactory.js';
import { OculusHandModel } from './js/OculusHandModel.js';
import { createText } from './js/Text2D.js';
import { GameObject, Component, GameObjectManager } from './js/EntityComponentSystem.js';
import { PhysicalButtonComponent } from './js/PhysicalButtonComponent.js';
let container;
let camera, scene, renderer;
let hand1, hand2;
let handModel1, handModel2;
let consoleMesh, torusKnot, instructionText, exitText;
let controller1, controller2;
let controllerGrip1, controllerGrip2;
let xrSession;
let gameObjectManager;
let positionCalibrated = false;
let buttonPressSound, buttonReleaseSound;
init();
animate();
function makeButtonMesh(x, y, z, color) {
const geometry = new THREE.BoxGeometry(x, y, z);
const material = new THREE.MeshPhongMaterial({ color: color });
const buttonMesh = new THREE.Mesh(geometry, material);
return buttonMesh;
}
function init() {
container = document.createElement('div');
document.body.appendChild(container);
scene = new THREE.Scene();
scene.background = new THREE.Color(0x444444);
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10);
camera.position.set(0, 1.2, 0.3);
const floorGeometry = new THREE.PlaneGeometry(4, 4);
const floorMaterial = new THREE.MeshPhongMaterial({ color: 0x222222 });
const floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.rotation.x = - Math.PI / 2;
// floor.receiveShadow = true;
scene.add(floor);
scene.add(new THREE.HemisphereLight(0x808080, 0x606060));
const light = new THREE.DirectionalLight(0xffffff);
light.position.set(0, 6, 0);
light.castShadow = true;
light.shadow.camera.top = 2;
light.shadow.camera.bottom = - 2;
light.shadow.camera.right = 2;
light.shadow.camera.left = - 2;
light.shadow.mapSize.set(4096, 4096);
scene.add(light);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.shadowMap.enabled = true;
renderer.xr.enabled = true;
container.appendChild(renderer.domElement);
document.body.appendChild(VRButton.createButton(renderer));
// controllers
controller1 = renderer.xr.getController(0);
scene.add(controller1);
controller2 = renderer.xr.getController(1);
scene.add(controller2);
const controllerModelFactory = new XRControllerModelFactory();
// Hand 1
controllerGrip1 = renderer.xr.getControllerGrip(0);
controllerGrip1.add(controllerModelFactory.createControllerModel(controllerGrip1));
scene.add(controllerGrip1);
hand1 = renderer.xr.getHand(0);
handModel1 = new OculusHandModel(hand1);
hand1.add(handModel1);
scene.add(hand1);
// Hand 2
controllerGrip2 = renderer.xr.getControllerGrip(1);
controllerGrip2.add(controllerModelFactory.createControllerModel(controllerGrip2));
scene.add(controllerGrip2);
hand2 = renderer.xr.getHand(1);
handModel2 = new OculusHandModel(hand2);
hand2.add(handModel2);
scene.add(hand2);
gameObjectManager = new GameObjectManager();
// buttons
let buttonClearAction = function () {
buttonReleaseSound.play();
}
const consoleGeometry = new THREE.BoxGeometry(0.5, 0.12, 0.15);
const consoleMaterial = new THREE.MeshPhongMaterial({ color: 0x595959 });
consoleMesh = new THREE.Mesh(consoleGeometry, consoleMaterial);
consoleMesh.position.set(0, 1, -0.3);
scene.add(consoleMesh);
let orangeButtonObject = gameObjectManager.createGameObject(consoleMesh, "orange-button");
let orangeButton = makeButtonMesh(0.08, 0.1, 0.08, 0xffd3b5);
orangeButtonObject.transform.add(orangeButton);
orangeButtonObject.transform.position.set(-0.15, 0.04, 0);
let orangeButtonPressAction = function () {
buttonPressSound.play();
torusKnot.material.color.setHex(0xffd3b5);
}
orangeButtonObject.addComponent(PhysicalButtonComponent, [handModel1, handModel2], orangeButtonPressAction, buttonClearAction);
let pinkButtonObject = gameObjectManager.createGameObject(consoleMesh, "pink-button");
let pinkButton = makeButtonMesh(0.08, 0.1, 0.08, 0xe84a5f);
pinkButtonObject.transform.add(pinkButton);
pinkButtonObject.transform.position.set(-0.05, 0.04, 0);
let pinkButtonPressAction = function () {
buttonPressSound.play();
torusKnot.material.color.setHex(0xe84a5f);
}
pinkButtonObject.addComponent(PhysicalButtonComponent, [handModel1, handModel2], pinkButtonPressAction, buttonClearAction);
let resetButtonObject = gameObjectManager.createGameObject(consoleMesh, "exit-button");
let resetButton = makeButtonMesh(0.08, 0.1, 0.08, 0x355c7d);
let resetText = createText("reset", 0.03);
resetButton.add(resetText);
resetText.rotation.x = - Math.PI / 2;
resetText.position.set(0, 0.051, 0);
resetButtonObject.transform.add(resetButton);
resetButtonObject.transform.position.set(0.05, 0.04, 0);
let resetButtonPressAction = function () {
buttonPressSound.play();
torusKnot.material.color.setHex(0xffffff);
}
resetButtonObject.addComponent(PhysicalButtonComponent, [handModel1, handModel2], resetButtonPressAction, buttonClearAction);
let exitButtonObject = gameObjectManager.createGameObject(consoleMesh, "exit-button");
let exitButton = makeButtonMesh(0.08, 0.1, 0.08, 0xff0000);
let exitText = createText("exit", 0.03);
exitButton.add(exitText);
exitText.rotation.x = - Math.PI / 2;
exitText.position.set(0, 0.051, 0);
exitButtonObject.transform.add(exitButton);
exitButtonObject.transform.position.set(0.15, 0.04, 0);
let exitButtonPressAction = function () {
buttonPressSound.play();
exitText.visible = true;
setTimeout(function () { exitText.visible = false; renderer.xr.getSession().end(); }, 2000);
}
exitButtonObject.addComponent(PhysicalButtonComponent, [handModel1, handModel2], exitButtonPressAction, buttonClearAction);
const tkGeometry = new THREE.TorusKnotGeometry(0.5, 0.2, 200, 32);
const tkMaterial = new THREE.MeshPhongMaterial({ color: 0xffffff });
tkMaterial.metalness = 0.8;
torusKnot = new THREE.Mesh(tkGeometry, tkMaterial);
torusKnot.position.set(0, 1, -5);
scene.add(torusKnot);
instructionText = createText("This is a WebXR Hands demo, please explore with hands.", 0.04);
instructionText.position.set(0, 1.6, -0.6);
scene.add(instructionText);
exitText = createText("Exiting session...", 0.04);
exitText.position.set(0, 1.5, -0.6);
exitText.visible = false;
scene.add(exitText);
window.addEventListener('resize', onWindowResize);
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
//
function animate() {
renderer.setAnimationLoop(render);
}
function render() {
gameObjectManager.update();
torusKnot.rotation.x += 0.005;
torusKnot.rotation.y += 0.005;
instructionText.visible = controllerGrip1.visible || controllerGrip2.visible;
renderer.render(scene, camera);
// adjusting console position when user first go immersive
if (renderer.xr.getSession() && !positionCalibrated) {
let xrCamera = renderer.xr.getCamera(camera);
consoleMesh.position.y = xrCamera.position.y - 0.4;
consoleMesh.position.z = xrCamera.position.z - 0.3;
positionCalibrated = true;
const listener = new THREE.AudioListener();
xrCamera.add(listener);
// create a global audio source
buttonPressSound = new THREE.Audio(listener);
buttonReleaseSound = new THREE.Audio(listener);
// load a sound and set it as the Audio object's buffer
const audioLoader = new THREE.AudioLoader();
audioLoader.load('sounds/button-press.ogg', function (buffer) {
buttonPressSound.setBuffer(buffer);
});
audioLoader.load('sounds/button-release.ogg', function (buffer) {
buttonReleaseSound.setBuffer(buffer);
});
}
}
</script>
</body>
</html>