/
webxr_ar_megane.html
208 lines (179 loc) · 5.72 KB
/
webxr_ar_megane.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
<!DOCTYPE html><html lang="ja"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width"><link rel="icon" href="data:">
<title>AR plane-detection</title>
</head><body>
<div id="info">
AR megane - plane-detection - forked <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> ar - plane detection<br>
</div>
<style>
body {
background-color: #333;
color: #fff;
margin: 0;
}
a {
color: #eee !important;
}
</style>
<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 { ARButton } from "three/addons/webxr/ARButton.js";
import { log } from "./log.js";
import { createMegane } from "https://code4fukui.github.io/ar-meganefes/createMegane.js";
const container = document.createElement("div");
document.body.appendChild(container);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(70, innerWidth / innerHeight, 0.01, 20);
const light = new THREE.HemisphereLight(0xffffff, 0xbbbbff, 1);
light.position.set(0.5, 1, 0.25);
scene.add(light);
//
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(devicePixelRatio);
renderer.setSize(innerWidth, innerHeight);
renderer.xr.enabled = true;
container.appendChild(renderer.domElement);
//
const planesAdded = new Set();
document.body.appendChild(ARButton.createButton(renderer, {
requiredFeatures: ["plane-detection"]
}));
renderer.xr.addEventListener("sessionstart", function () {
camera.position.set(0, 0, 0);
log("start");
});
renderer.xr.addEventListener("planeadded", function (e) {
console.log("plane added", e.data);
log("add", e.data);
});
renderer.xr.addEventListener("planeremoved", function (e) {
console.log("plane removed", e.data);
});
renderer.xr.addEventListener("planechanged", function (e) {
console.log("plane changed", e.data);
});
// from https://qiita.com/wakufactory/items/602bf4bc1ce075e7a279
// but not worked yet
let col = 0xf00000;
const setPlanesAsPolygon = (to, p, planePose) => {
//pl.forEach(p => {
const poly = p[0];
const geometry = new THREE.BufferGeometry();
const vertices = new Float32Array([
poly[0].x, poly[0].y, poly[0].z,
poly[1].x, poly[1].y, poly[1].z,
poly[2].x, poly[2].y, poly[2].z,
poly[2].x, poly[2].y, poly[2].z,
poly[3].x, poly[3].y, poly[3].z,
poly[4].x, poly[4].y, poly[4].z,
]);
geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
const material = new THREE.MeshBasicMaterial({ color: col, opacity: 0.5, side: THREE.FrontSide });
const mesh = new THREE.Mesh(geometry, material);
col += 32;
mesh.matrix.elements = p[1];
mesh.matrix.decompose(mesh.position, mesh.rotation, mesh.scale);
//mesh.matrixAutoUpdate = false;
//mesh.matrix.fromArray(planePose.transform.matrix);
to.add(mesh);
console.log(mesh.position);
console.log(mesh.rotation);
// change color by orientation
if (Math.abs(mesh.rotation.x) < 0.01) {
mesh.material.color = new THREE.Color(0x00f000);
}
if (Math.abs(Math.abs(mesh.rotation.x) - Math.PI) < 0.01) {
mesh.material.color = new THREE.Color(0x0000f0);
}
//})
};
const setPlanesAsBox = (to, polygon, planePose) => {
let minX = Number.MAX_SAFE_INTEGER;
let maxX = Number.MIN_SAFE_INTEGER;
let minZ = Number.MAX_SAFE_INTEGER;
let maxZ = Number.MIN_SAFE_INTEGER;
polygon.forEach(point => {
minX = Math.min(minX, point.x);
maxX = Math.max(maxX, point.x);
minZ = Math.min(minZ, point.z);
maxZ = Math.max(maxZ, point.z);
});
const width = maxX - minX;
const height = maxZ - minZ;
const color = 0xff0000;
const boxMesh = new THREE.Mesh(
new THREE.BoxGeometry(width, 0.01, height),
//new THREE.MeshBasicMaterial({ color, transparent: true, opacity: 0.5 }),
//new THREE.MeshBasicMaterial({ color, wireframe: true }),
new THREE.MeshBasicMaterial({ colorWrite: false }),
//new THREE.MeshBasicMaterial({ colorWrite: false, color, wireframe: true }),
);
boxMesh.matrixAutoUpdate = false;
boxMesh.matrix.fromArray(planePose.transform.matrix);
boxMesh.renderOrder = -1;
to.add(boxMesh);
};
renderer.xr.addEventListener("planesdetected", function (e) {
const detectedPlanes = e.data;
detectedPlanes.forEach(plane => {
if (planesAdded.has(plane)) return;
log("detect", e.data);
console.log(`Detected ${detectedPlanes.size} planes`);
planesAdded.add(plane);
const frame = renderer.xr.getFrame();
const referenceSpace = renderer.xr.getReferenceSpace();
const planePose = frame.getPose(plane.planeSpace, referenceSpace);
const polygon = plane.polygon;
//setPlanesAsPolygon(scene, polygon, planePose);
setPlanesAsBox(scene, polygon, planePose);
});
});
// add objects
const group = new THREE.Group();
scene.add(group);
const getObjects = (nset = 50) => {
const res = [];
for (let i = 0; i < nset; i++) {
res[i] = createMegane();
}
return res;
};
const objs = getObjects();
const w = 10;
const h = 5;
const d = 5;
for (const object of objs) {
object.position.x = (Math.random() - .5) * w;
object.position.y = (Math.random() - .5) * h;
object.position.z = (Math.random() - .5) * d;
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(5);
group.add(object);
}
const animation = () => {
for (const object of objs) {
object.rotation.x += .01;
}
};
//
renderer.setAnimationLoop(() => {
animation();
renderer.render(scene, camera);
});
addEventListener("resize", () => {
camera.aspect = innerWidth / innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(innerWidth, innerHeight);
});
</script>
</body>
</html>