-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
282 lines (243 loc) · 9.21 KB
/
index.html
File metadata and controls
282 lines (243 loc) · 9.21 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>3js-ctr-sample</title>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
<script type="importmap">
{
"imports": {
"three": "https://unpkg.com/three@0.160.0/build/three.module.js",
"three/addons/": "https://unpkg.com/three@0.160.0/examples/jsm/",
"webxr-polyfill": "https://cdn.jsdelivr.net/npm/webxr-polyfill@latest/build/webxr-polyfill.module.js"
}
}
</script>
<script type="module">
import * as THREE from "three";
import { VRButton } from "three/addons/webxr/VRButton.js";
import { XRControllerModelFactory } from "three/addons/webxr/XRControllerModelFactory.js";
import WebXRPolyfill from "webxr-polyfill";
// webXRのポリフィルを有効にする
const polyfill = new WebXRPolyfill();
// シーンの作成
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xb0c4de);
// カメラの作成
const camera = new THREE.PerspectiveCamera(
80,
window.innerWidth / window.innerHeight,
0.1,
1000
);
// カメラ用コンテナの作成
const cameraContainer = new THREE.Group();
cameraContainer.add(camera);
scene.add(cameraContainer);
cameraContainer.position.y = -1;
cameraContainer.position.z = 5;
// レンダラーの準備
const renderer = new THREE.WebGLRenderer({
canvas: document.querySelector("#canvas"),
antialias: true,
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.xr.enabled = true;
document.body.appendChild(renderer.domElement);
// VRモード開始用のボタンを追加
document.body.appendChild(VRButton.createButton(renderer));
// コントローラ光線の準備
const ray_geometry = new THREE.BufferGeometry().setFromPoints([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 0, -1),
]);
const ray = new THREE.Line(ray_geometry);
ray.name = "ray";
ray.scale.z = 3;
// コントローラモデルファクトリーの準備
const ctrModelFactory = new XRControllerModelFactory();
// 左手コントローラ
const ctr0 = renderer.xr.getController(0);
cameraContainer.add(ctr0);
const ctr0Grip = renderer.xr.getControllerGrip(0);
const ctr0Model = ctrModelFactory.createControllerModel(ctr0Grip);
ctr0Grip.add(ctr0Model);
cameraContainer.add(ctr0Grip);
ctr0.add(ray.clone());
// 右手コントローラ
const ctr1 = renderer.xr.getController(1);
cameraContainer.add(ctr1);
const ctr1Grip = renderer.xr.getControllerGrip(1);
const ctr1Model = ctrModelFactory.createControllerModel(ctr1Grip);
ctr1Grip.add(ctr1Model);
cameraContainer.add(ctr1Grip);
ctr1.add(ray.clone());
// 立方体の作成
const cube = new THREE.BoxGeometry(1, 1, 1);
const cube_material = new THREE.MeshStandardMaterial({
color: 0xffffff,
roughness: 0.0,
});
const cube_mesh = new THREE.Mesh(cube, cube_material);
scene.add(cube_mesh);
// 環境光源
const light0 = new THREE.AmbientLight(0xffffff, 1.0);
scene.add(light0);
// ディレクションライト
const light1 = new THREE.DirectionalLight(0xffffff, 10.0);
scene.add(light1);
// 照明を可視化するヘルパー
const lightHelper = new THREE.DirectionalLightHelper(light1);
scene.add(lightHelper);
function genColor() {
const color = Math.floor(Math.random() * 16777215).toString(16);
return color;
}
function resetAll() {
scene.background.setHex(0xb0c4de);
cube_material.color.setHex(0xffffff);
cube_mesh.position.x = 0;
cube_mesh.position.y = 0;
cube_mesh.position.z = 0;
cube_mesh.rotation.x = 0;
cube_mesh.rotation.y = 0;
cube_mesh.rotation.z = 0;
cube_mesh.scale.x = 1;
cube_mesh.scale.y = 1;
cube_mesh.scale.z = 1;
light0.color.setHex(0xffffff);
light1.color.setHex(0xffffff);
light1.position.x = 0;
light1.position.y = 0;
}
var push_left_thumbstk = false;
var push_right_thumbstk = false;
var touch_left_thumbrest = false;
var touch_right_thumbrest = false;
// 毎フレーム実行
function tick() {
lightHelper.update();
if (ctr0Model.motionController) {
var json = JSON.stringify(ctr0Model.motionController.data, null, " ");
var input = JSON.parse(json);
// Xボタン押下 -> x軸回転
if (input[3].button == 1) {
cube_mesh.rotation.x += 0.02;
}
// Yボタン押下 -> y軸回転
if (input[4].button == 1) {
cube_mesh.rotation.y += 0.02;
}
// 左トリガー -> オブジェクトの縮小
if (input[0].button == 1) {
cube_mesh.scale.x -= 0.02;
cube_mesh.scale.y -= 0.02;
cube_mesh.scale.z -= 0.02;
}
// ジョイスティック左右 -> オブジェクトをx軸方向に移動
if (input[2].xAxis >= 0.9) {
cube_mesh.position.x += 0.02;
} else if (input[2].xAxis <= -0.9) {
cube_mesh.position.x -= 0.02;
}
// ジョイスティック上下 -> オブジェクトをy軸方向に移動
if (input[2].yAxis >= 0.9) {
cube_mesh.position.y -= 0.02;
} else if (input[2].yAxis <= -0.9) {
cube_mesh.position.y += 0.02;
}
// ジョイスティック押下 -> オブジェクトの色をランダムに変更
if (push_left_thumbstk == false && input[2].button == 1) {
cube_material.color.setHex("0x" + genColor());
push_left_thumbstk = true;
}
if (push_left_thumbstk == true && input[2].button == 0) {
push_left_thumbstk = false;
}
// squeeze -> z軸方向マイナス
if (input[1].state == "pressed") {
cube_mesh.position.z -= 0.02;
}
// thumbrest -> light1の色変更
if (touch_left_thumbrest == false && input[5].state == "touched") {
light1.color.setHex("0x" + genColor());
touch_left_thumbrest = true;
}
if (touch_left_thumbrest == true && input[5].state == "default") {
touch_left_thumbrest = false;
}
}
if (ctr1Model.motionController) {
var json = JSON.stringify(ctr1Model.motionController.data, null, " ");
var input = JSON.parse(json);
// Aボタン押下 -> z軸回転
if (input[3].button == 1) {
cube_mesh.rotation.z += 0.02;
}
// Bボタン押下 -> 回転リセット
if (input[4].button == 1) {
resetAll();
}
// 右トリガー -> オブジェクトの拡大
if (input[0].button == 1) {
cube_mesh.scale.x += 0.02;
cube_mesh.scale.y += 0.02;
cube_mesh.scale.z += 0.02;
}
// ジョイスティック左右 -> light1をx軸方向に移動
if (input[2].xAxis >= 0.9) {
light1.position.x += 0.02;
} else if (input[2].xAxis <= -0.9) {
light1.position.x -= 0.02;
}
// ジョイスティック上下 -> light1をy軸方向に移動
if (input[2].yAxis >= 0.9) {
light1.position.y -= 0.02;
} else if (input[2].yAxis <= -0.9) {
light1.position.y += 0.02;
}
// ジョイスティック押下 -> 背景色をランダムに変更
if (push_right_thumbstk == false && input[2].button == 1) {
scene.background.setHex("0x" + genColor());
push_right_thumbstk = true;
}
if (push_right_thumbstk == true && input[2].button == 0) {
push_right_thumbstk = false;
}
// squeeze -> z軸方向プラス
if (input[1].state == "pressed") {
cube_mesh.position.z += 0.02;
}
// thumbrest -> light0の色変更
if (touch_right_thumbrest == false && input[5].state == "touched") {
light0.color.setHex("0x" + genColor());
touch_right_thumbrest = true;
}
if (touch_right_thumbrest == true && input[5].state == "default") {
touch_right_thumbrest = false;
}
}
renderer.render(scene, camera);
}
renderer.setAnimationLoop(tick);
// ウインドウのリサイズ検知
window.addEventListener("resize", onResize);
// リサイズ処理
function onResize() {
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
// カメラのアスペクト比を正す
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
}
</script>
</body>
</html>