-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
398 lines (354 loc) · 10.6 KB
/
index.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><link rel="icon" href="data:">
<title>AR Tsumiki</title>
</head>
<body>
<div id="info">
AR Tsumiki forked <a href=https://code4fukui.github.io/three.js_examples/?q=webxr#webxr_ar_dragging>ar - dragging</a> / <a href="https://threejs.org">three.js</a><br>
<a href="./">colorfull</a><br>
<a href="?grayscale=true">grayscale</a><br>
<a href="?grayscale=true&opacity=1.0">gray scale and no transparency</a><br>
<a href="?opacity=1.0">colorfull and no transparency</a><br>
<a href="?geometry=jig&nset=1&monocolor=ffffff&opacity=1.0&snap=0.15">jig.jp puzzle</a><br>
<a href="?geometry=megane&nset=30">MeganeFes version</a><br>
</div>
<style>
body {
margin: 0;
background-color: #eee;
font-family: sans-serif;
}
#info {
position: absolute;
background-color: rgba(155, 155, 155, 0.5);
padding: 0.5em;
margin: 0.5em;
color: #111;
}
a {
color: #228 !important;
}
#ARButton {
color: black !important;
border: 1px solid black !important;
}
</style>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "https://code4fukui.github.io/three.js/build/three.module.js",
"three/addons/": "https://code4fukui.github.io/three.js/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from "three";
import { OrbitControls } from "three/addons/controls/OrbitControls.js"; // for PC
import { ARButton } from "three/addons/webxr/ARButton.js";
import { createMegane } from "https://code4fukui.github.io/ar-meganefes/createMegane.js";
const param = new URL(location.href).searchParams;
const grayscale = param.get("grayscale") || false;
const monocolor = param.get("monocolor") || undefined;
const opacity = parseFloat(param.get("opacity")) || 0.8;
const geometry = param.get("geometry") || "blocks"; // or jig, megane
const nset = param.get("nset") || 5;
const snap = param.get("snap") || 0;
console.log(param, opacity, geometry, monocolor);
let container;
let camera, scene, renderer;
let controller1, controller2;
let raycaster;
const intersected = [];
const tempMatrix = new THREE.Matrix4();
let group;
const onWindowResize = () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
};
const isTouch = (intersection) => {
const d = intersection.distance;
return d > 0 && d < .10;
};
const onSelectStart = (event) => {
const controller = event.target;
const intersections = getIntersections(controller);
const objects = [];
for (const intersection of intersections) {
const object = intersection.object;
object.material.emissive.b = 1;
controller.attach(object);
objects.push(object);
if (isTouch(intersection)) {
break;
}
}
controller.userData.selected = objects;
};
const onSelectEnd = (event) => {
const controller = event.target;
if (controller.userData.selected !== undefined) {
for (const object of controller.userData.selected) {
object.material.emissive.b = 0;
group.attach(object);
if (snap) {
object.position.x = Math.floor((object.position.x + snap / 2) / snap) * snap;
object.position.y = Math.floor((object.position.y + snap / 2) / snap) * snap;
object.position.z = Math.floor((object.position.z + snap / 2) / snap) * snap;
const dsnap = Math.PI / 2;
object.rotation.x = Math.floor((object.rotation.x + dsnap / 2) / dsnap) * dsnap;
object.rotation.y = Math.floor((object.rotation.y + dsnap / 2) / dsnap) * dsnap;
object.rotation.z = Math.floor((object.rotation.z + dsnap / 2) / dsnap) * dsnap;
}
}
controller.userData.selected = undefined;
}
};
const getIntersections = (controller) => {
tempMatrix.identity().extractRotation(controller.matrixWorld);
raycaster.ray.origin.setFromMatrixPosition(controller.matrixWorld);
raycaster.ray.direction.set(0, 0, -1).applyMatrix4(tempMatrix);
return raycaster.intersectObjects(group.children, false);
};
const intersectObjects = (controller) => {
// Do not highlight when already selected
if (controller.userData.selected !== undefined) return;
const intersections = getIntersections(controller);
for (const intersection of intersections) {
const object = intersection.object;
object.material.emissive.r = 1;
intersected.push(object);
if (isTouch(intersection)) {
object.material.emissive.g = 1;
break;
}
}
};
const cleanIntersected = () => {
while (intersected.length) {
const object = intersected.pop();
object.material.emissive.r = 0;
object.material.emissive.g = 0;
}
};
const render = () => {
cleanIntersected();
intersectObjects(controller1);
intersectObjects(controller2);
renderer.render(scene, camera);
};
const animate = () => {
renderer.setAnimationLoop(render);
};
const init = () => {
container = document.createElement("div");
document.body.appendChild(container);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(50, window.innerWidth / window.innerHeight, 0.1, 10);
camera.position.set(0, 0, 3);
const controls = new OrbitControls(camera, container);
controls.minDistance = 0;
controls.maxDistance = 8;
scene.add(new THREE.HemisphereLight(0x808080, 0x606060));
const light = new THREE.DirectionalLight(0xffffff);
light.position.set(0, 6, 0);
scene.add(light);
group = new THREE.Group();
scene.add(group);
const getGeometries = () => {
if (geometry == "jig") {
const w = 0.15;
const extrudeSettings = {
steps: 1,
depth: w,
bevelEnabled: false,
/*
bevelThickness: 1,
bevelSize: 1,
bevelOffset: 0,
bevelSegments: 1
*/
};
const dot = new THREE.ExtrudeGeometry((() => {
const s = new THREE.Shape();
s.moveTo(0, 0);
s.lineTo(w, 0);
s.lineTo(w, w);
s.lineTo(0, w);
s.lineTo(0, 0);
return s;
})(), extrudeSettings);
const i = new THREE.ExtrudeGeometry((() => {
const s = new THREE.Shape();
s.moveTo(0, 0);
s.lineTo(w, 0);
s.lineTo(w, w * 3);
s.lineTo(0, w * 3);
s.lineTo(0, 0);
return s;
})(), extrudeSettings);
const j = new THREE.ExtrudeGeometry((() => {
const s = new THREE.Shape();
s.moveTo(0, 0);
s.lineTo(w, 0);
s.lineTo(w, w * 5);
s.lineTo(-w, w * 5);
s.lineTo(-w, w * 4);
s.lineTo(0, w * 4);
s.lineTo(0, 0);
return s;
})(), extrudeSettings);
const g = new THREE.ExtrudeGeometry((() => {
const s = new THREE.Shape();
s.moveTo(0, 0);
s.lineTo(w * 4, 0);
s.lineTo(w * 4, w);
s.lineTo(w * 3, w);
s.lineTo(w * 3, w * 5);
s.lineTo(0, w * 5);
s.lineTo(0, w * 4);
s.lineTo(w * 2, w * 4);
s.lineTo(w * 2, w * 3);
s.lineTo(0, w * 3);
s.lineTo(0, 0);
s.moveTo(w, w);
s.lineTo(w, w * 2);
s.lineTo(w * 2, w * 2);
s.lineTo(w * 2, w);
s.lineTo(w, w);
return s;
})(), extrudeSettings);
const p = new THREE.ExtrudeGeometry((() => {
const s = new THREE.Shape();
s.moveTo(0, 0);
s.lineTo(w * 3, 0);
s.lineTo(w * 3, w * 3);
s.lineTo(w, w * 3);
s.lineTo(w, w * 5);
s.lineTo(0, w * 5);
s.lineTo(0, 0);
s.moveTo(w, w);
s.lineTo(w, w * 2);
s.lineTo(w * 2, w * 2);
s.lineTo(w * 2, w);
s.lineTo(w, w);
return s;
})(), extrudeSettings);
return [
j,
dot,
i,
dot,
g,
dot,
j,
dot,
p,
];
} else {
return [
new THREE.BoxGeometry(1.6, 0.8, 0.1),
new THREE.BoxGeometry(0.2, 0.2, 0.2),
new THREE.BoxGeometry(0.2, 0.4, 0.2),
new THREE.BoxGeometry(0.4, 0.4, 0.1),
new THREE.ConeGeometry(0.1, 0.2, 64),
new THREE.CylinderGeometry(0.1, 0.1, 0.2, 64),
new THREE.CylinderGeometry(0.1, 0.1, 0.8, 64),
new THREE.IcosahedronGeometry(0.1, 8),
new THREE.TorusGeometry(0.2 - 0.04, 0.04, 64, 32)
];
}
};
const getObjects = () => {
if (geometry == "megane") {
const res = [];
console.log(nset);
for (let i = 0; i < nset; i++) {
res[i] = createMegane();
}
return res;
}
const geometries = getGeometries();
const ngeo = geometries.length * nset;
const res = [];
for (let i = 0; i < ngeo; i++) {
//const geometry = geometries[Math.floor(Math.random() * geometries.length)];
const geometry = geometries[i % geometries.length];
const getColor = () => {
if (monocolor !== undefined) {
return parseInt(monocolor, 16);
}
if (grayscale) {
const nc = (Math.random() * 0x100) >> 0;
const color = (nc << 16) | (nc << 8) | nc;
return color;
}
return Math.random() * 0x1000000;
};
const material = new THREE.MeshStandardMaterial({
color: getColor(),
roughness: 1.0,
metalness: 0.0,
transparent: opacity < 1.0,
opacity,
});
const object = new THREE.Mesh(geometry, material);
res.push(object);
}
return res;
};
const objs = getObjects();
for (const object of objs) {
object.position.x = Math.random() * 4 - 2;
object.position.y = Math.random() * 4 - 2;
object.position.z = Math.random() * 4 - 2;
object.rotation.x = Math.random() * 2 * Math.PI;
object.rotation.y = Math.random() * 2 * Math.PI;
object.rotation.z = Math.random() * 2 * Math.PI;
//object.scale.setScalar(Math.random() / 2 + 0.5);
group.add(object);
}
//
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.outputEncoding = THREE.sRGBEncoding;
renderer.xr.enabled = true;
container.appendChild(renderer.domElement);
document.body.appendChild(ARButton.createButton(renderer));
// controllers
controller1 = renderer.xr.getController(0);
controller1.addEventListener("selectstart", onSelectStart);
controller1.addEventListener("selectend", onSelectEnd);
scene.add(controller1);
controller2 = renderer.xr.getController(1);
controller2.addEventListener("selectstart", onSelectStart);
controller2.addEventListener("selectend", onSelectEnd);
scene.add(controller2);
raycaster = new THREE.Raycaster();
// line
const makeLine = (len) => {
const material = new THREE.LineBasicMaterial({
//color: 0xff0000, // red
//color: 0x0000ff, // blue
color: 0xffffff, // white
});
const points = [];
points.push(new THREE.Vector3(0, 0, 0));
points.push(new THREE.Vector3(0, 0, -len));
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const line = new THREE.Line(geometry, material);
return line;
};
controller1.add(makeLine(5));
controller2.add(makeLine(5));
//
window.addEventListener("resize", onWindowResize);
};
init();
animate();
</script>
</body>
</html>