-
Notifications
You must be signed in to change notification settings - Fork 119
/
graffiti_ml.html
521 lines (451 loc) · 15.6 KB
/
graffiti_ml.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#008000"/>
<title>graffiti_ml</title>
<link rel="manifest" href="graffiti_ml_manifest.json">
<script>
if('serviceWorker' in navigator) {
navigator.serviceWorker.register('/graffiti_ml_sw.js')
.then(function() {
console.log('Service Worker Registered');
});
}
</script>
<style>
body {
margin: 0;
}
</style>
</head>
<body>
<h1>graffiti_ml</h1>
<script src="three.js"></script>
<script src="skin.js"></script>
<script>
(() => {
let container, scene, camera, session, controllerMeshes, hitMeshes;
let mesher = null;
const MAX_NUM_POINTS = 1024;
const POINT_FRAME_RATE = 20;
const localVector = new THREE.Vector3();
const localVector2 = new THREE.Vector3();
const localVector3 = new THREE.Vector3();
const localVector4 = new THREE.Vector3();
const localQuaternion = new THREE.Quaternion();
const localMatrix = new THREE.Matrix4();
const localVectorFloat32Array = new Float32Array(3);
const localVectorFloat32Array2 = new Float32Array(3);
const _requestImage = src => new Promise((accept, reject) => {
const img = new Image();
img.src = src;
img.onload = () => {
accept(img);
};
img.onerror = err => {
reject(err);
};
});
const controllerGeometry = new THREE.BoxBufferGeometry(0.05, 0.01, 0.1);
const controllerMaterial = new THREE.MeshPhongMaterial({
color: 0x4caf50,
});
const _makeControllerMesh = (x = 0, y = 0, z = 0, qx = 0, qy = 0, qz = 0, qw = 1) => {
const mesh = new THREE.Mesh(controllerGeometry, controllerMaterial);
mesh.position.set(x, y, z);
mesh.quaternion.set(qx, qy, qz, qw);
// mesh.matrix.compose(mesh.position, mesh.quaternion, mesh.scale);
// mesh.updateMatrix();
// mesh.updateMatrixWorld();
// mesh.matrixAutoUpdate = false;
mesh.frustumCulled = false;
return mesh;
};
const _getControllerIndex = inputSource => inputSource.handedness === 'left' ? 0 : 1;
let paintMesh = null;
let paintControllerIndex = -1;
const paintMaterial = (() => {
const texture = new THREE.Texture(
null,
THREE.UVMapping,
THREE.ClampToEdgeWrapping,
THREE.ClampToEdgeWrapping,
THREE.NearestFilter,
THREE.NearestFilter,
THREE.RGBAFormat,
THREE.UnsignedByteType,
16
);
_requestImage('brush.png')
.then(brushImg => {
texture.image = brushImg;
texture.needsUpdate = true;
});
const material = new THREE.MeshPhongMaterial({
map: texture,
shininess: 0,
vertexColors: THREE.VertexColors,
// color: 0xFF0000,
side: THREE.DoubleSide,
transparent: true,
alphaTest: 0.5,
});
return material;
})();
const paintColor = new THREE.Color(0xe91e63);
const paintMeshes = [];
const _getPaintMesh = (meshId = Math.random().toString(36).substring(7)) => {
let paintMesh = paintMeshes.find(paintMesh => paintMesh.meshId === meshId);
if (!paintMesh) {
paintMesh = _makePaintMesh(meshId);
paintMeshes.push(paintMesh);
scene.add(paintMesh);
}
return paintMesh;
};
const _makePaintMesh = meshId => {
const geometry = new THREE.BufferGeometry();
const positions = new Float32Array(MAX_NUM_POINTS * 2 * 3);
geometry.addAttribute('position', new THREE.BufferAttribute(positions, 3));
const normals = new Float32Array(MAX_NUM_POINTS * 2 * 3);
geometry.addAttribute('normal', new THREE.BufferAttribute(normals, 3));
const colors = new Float32Array(MAX_NUM_POINTS * 2 * 3);
geometry.addAttribute('color', new THREE.BufferAttribute(colors, 3));
const uvs = new Float32Array(MAX_NUM_POINTS * 2 * 2);
geometry.addAttribute('uv', new THREE.BufferAttribute(uvs, 2));
geometry.setDrawRange(0, 0);
const mesh = new THREE.Mesh(geometry, paintMaterial);
mesh.drawMode = THREE.TriangleStripDrawMode;
mesh.frustumCulled = false;
mesh.meshId = meshId;
let lastPoint = 0;
let lastPointTime = 0;
let lastPosition = null;
mesh.getBuffer = (endPoint = lastPoint) => {
const positionSize = endPoint * 2 * 3;
const uvSize = endPoint * 2 * 2;
const array = new Float32Array(
positionSize + // position
positionSize + // normal
positionSize + // color
uvSize // uv
);
array.set(positions.slice(0, positionSize), 0); // position
array.set(normals.slice(0, positionSize), positionSize); // normal
array.set(colors.slice(0, positionSize), positionSize * 2); // color
array.set(uvs.slice(0, uvSize), positionSize * 3); // uv
return array.buffer;
};
const _getFrame = t => Math.floor(t / POINT_FRAME_RATE);
mesh.update = (position, rotation) => {
if (lastPoint < MAX_NUM_POINTS) {
const lastFrame = _getFrame(lastPointTime);
const currentPointTime = Date.now();
const currentFrame = _getFrame(currentPointTime);
if (currentFrame > lastFrame) {
const positionsAttribute = geometry.getAttribute('position');
const normalsAttribute = geometry.getAttribute('normal');
const colorsAttribute = geometry.getAttribute('color');
const uvsAttribute = geometry.getAttribute('uv');
const positions = positionsAttribute.array;
const normals = normalsAttribute.array;
const colors = colorsAttribute.array;
const uvs = uvsAttribute.array;
const brushSize = 0.025;
const direction = localVector.set(1, 0, 0)
.applyQuaternion(rotation);
const posA = localVector2.copy(position)
.add(localVector3.copy(direction).multiplyScalar(brushSize / 2));
const posB = localVector3.copy(position)
.add(localVector4.copy(direction).multiplyScalar(-brushSize / 2));
// positions
const basePositionIndex = lastPoint * 2 * 3;
positions[basePositionIndex + 0] = posA.x;
positions[basePositionIndex + 1] = posA.y;
positions[basePositionIndex + 2] = posA.z;
positions[basePositionIndex + 3] = posB.x;
positions[basePositionIndex + 4] = posB.y;
positions[basePositionIndex + 5] = posB.z;
// normals
(() => {
const pA = new THREE.Vector3();
const pB = new THREE.Vector3();
const pC = new THREE.Vector3();
const cb = new THREE.Vector3();
const ab = new THREE.Vector3();
const idx = lastPoint * 2 * 3;
for (let i = 0, il = idx; i < il; i++) {
normals[i] = 0;
}
let pair = true;
for (let i = 0, il = idx; i < il; i += 3) {
if (pair) {
pA.fromArray(positions, i);
pB.fromArray(positions, i + 3);
pC.fromArray(positions, i + 6);
} else {
pA.fromArray(positions, i + 3);
pB.fromArray(positions, i);
pC.fromArray(positions, i + 6);
}
pair = !pair;
cb.subVectors(pC, pB);
ab.subVectors(pA, pB);
cb.cross(ab);
cb.normalize();
normals[i] += cb.x;
normals[i + 1] += cb.y;
normals[i + 2] += cb.z;
normals[i + 3] += cb.x;
normals[i + 4] += cb.y;
normals[i + 5] += cb.z;
normals[i + 6] += cb.x;
normals[i + 7] += cb.y;
normals[i + 8] += cb.z;
}
/*
first and last vertice (0 and 8) belongs just to one triangle
second and penultimate (1 and 7) belongs to two triangles
the rest of the vertices belongs to three triangles
1_____3_____5_____7
/\ /\ /\ /\
/ \ / \ / \ / \
/____\/____\/____\/____\
0 2 4 6 8
*/
// Vertices that are shared across three triangles
for (let i = 2 * 3, il = idx - 2 * 3; i < il; i++) {
normals[i] = normals[i] / 3;
}
// Second and penultimate triangle, that shares just two triangles
normals[3] = normals[3] / 2;
normals[3 + 1] = normals[3 + 1] / 2;
normals[3 + 2] = normals[3 * 1 + 2] / 2;
normals[idx - 2 * 3] = normals[idx - 2 * 3] / 2;
normals[idx - 2 * 3 + 1] = normals[idx - 2 * 3 + 1] / 2;
normals[idx - 2 * 3 + 2] = normals[idx - 2 * 3 + 2] / 2;
mesh.geometry.normalizeNormals();
})();
// colors
for (let i = 0; i < 2; i++) {
const baseColorIndex = basePositionIndex + (i * 3);
colors[baseColorIndex + 0] = paintColor.r;
colors[baseColorIndex + 1] = paintColor.g;
colors[baseColorIndex + 2] = paintColor.b;
}
// uvs
for (let i = 0; i <= lastPoint; i++) {
const baseUvIndex = i * 2 * 2;
uvs[baseUvIndex + 0] = i / (lastPoint - 1);
uvs[baseUvIndex + 1] = 0;
uvs[baseUvIndex + 2] = i / (lastPoint - 1);
uvs[baseUvIndex + 3] = 1;
}
positionsAttribute.needsUpdate = true;
normalsAttribute.needsUpdate = true;
colorsAttribute.needsUpdate = true;
uvsAttribute.needsUpdate = true;
lastPoint++;
lastPointTime = currentPointTime;
geometry.setDrawRange(0, lastPoint * 2);
if (!lastPosition) {
lastPosition = new THREE.Vector3();
}
lastPosition.copy(position);
}
}
};
mesh.getLastPosition = () => lastPosition;
return mesh;
};
const _clearPaintMeshes = () => {
for (let i = 0; i < paintMeshes.length; i++) {
const paintMesh = paintMeshes[i];
scene.remove(paintMesh);
paintMesh.geometry.dispose();
}
paintMeshes.length = 0;
};
function init() {
container = document.createElement('div');
document.body.appendChild(container);
scene = new THREE.Scene();
scene.matrixAutoUpdate = false;
// scene.background = new THREE.Color(0x3B3961);<F2>
camera = new THREE.PerspectiveCamera(60, window.innerWidth / window.innerHeight, 0.1, 1000);
// camera.position.set(0, 1, 0);
camera.lookAt(new THREE.Vector3());
scene.add(camera);
const ambientLight = new THREE.AmbientLight(0x808080);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xFFFFFF, 1);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
controllerMeshes = [
_makeControllerMesh(-0.1),
_makeControllerMesh(0.1),
];
controllerMeshes.forEach(controllerMesh => {
scene.add(controllerMesh);
});
const cubeGeometry = new THREE.BoxBufferGeometry(0.05, 0.05, 0.05);
const hitMeshMaterial = new THREE.MeshPhongMaterial({
color: 0x673ab7,
});
const _makeHitMesh = () => {
const geometry = cubeGeometry;
const material = hitMeshMaterial;
const mesh = new THREE.Mesh(geometry, material);
mesh.visible = false;
mesh.frustumCulled = false;
return mesh;
};
hitMeshes = [
_makeHitMesh(),
_makeHitMesh(),
];
hitMeshes.forEach(hitMesh => {
scene.add(hitMesh);
});
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth, window.innerHeight);
// window.browser.magicleap.RequestDepthPopulation(true);
// renderer.autoClear = false;
container.appendChild(renderer.domElement);
renderer.setAnimationLoop(animate);
}
const lastGrabbeds = [false, false];
const lastMenus = [false, false];
const controllerHitTesting = [false, false];
function animate(time, frame) {
if (renderer.vr.enabled) {
for (let i = 0; i < controllerMeshes.length; i++) {
controllerMeshes[i].visible = false;
}
const inputSources = session.getInputSources();
for (let i = 0; i < inputSources.length; i++) {
const inputSource = inputSources[i];
const pose = frame.getInputPose(inputSource);
const controllerIndex = _getControllerIndex(inputSource);
const controllerMesh = controllerMeshes[controllerIndex];
controllerMesh.matrix.fromArray(pose.targetRay.transformMatrix);
controllerMesh.matrix.decompose(controllerMesh.position, controllerMesh.quaternion, controllerMesh.scale);
// controllerMesh.updateMatrixWorld(true);
controllerMesh.visible = true;
if (!controllerHitTesting[controllerIndex]) {
const hitMesh = hitMeshes[controllerIndex];
session.requestHitTest(
controllerMesh.position
.toArray(localVectorFloat32Array),
localVector.set(0, 0, -1)
.applyQuaternion(controllerMesh.quaternion)
.toArray(localVectorFloat32Array2)
)
.then(result => {
if (result.length) {
const [{hitMatrix}] = result;
localMatrix
.fromArray(hitMatrix)
.decompose(hitMesh.position, hitMesh.quaternion, localVector2);
hitMesh.visible = true;
} else {
hitMesh.visible = false;
}
controllerHitTesting[controllerIndex] = false;
});
controllerHitTesting[controllerIndex] = true;
}
}
if (paintMesh) {
const hitMesh = hitMeshes[paintControllerIndex];
if (hitMesh.visible) {
if (paintMesh.getLastPosition() && paintMesh.getLastPosition().distanceTo(hitMesh.position) > 0.2) {
paintMesh = _getPaintMesh();
}
paintMesh.update(
localVector
.copy(hitMesh.position)
.add(
localVector2.set(0, 0, -0.001)
.applyQuaternion(hitMesh.quaternion)
).clone(),
hitMesh.quaternion.clone()
);
}
}
const gamepads = navigator.getGamepads();
for (let i = 0; i < gamepads.length; i++) {
const gamepad = gamepads[i];
const controllerIndex = gamepad.hand === 'left' ? 0 : 1;
const grabbed = gamepad.buttons[2].pressed;
const lastGrabbed = lastGrabbeds[controllerIndex];
if (grabbed && !lastGrabbed) {
_clearPaintMeshes();
paintMesh = null;
paintControllerIndex = -1;
}
lastGrabbeds[controllerIndex] = grabbed;
const menu = gamepad.buttons[3].pressed;
const lastMenu = lastMenus[controllerIndex];
if (menu && !lastMenu) {
terrainMeshesVisible = !terrainMeshesVisible;
for (let j = 0; j < terrainMeshes.length; j++) {
terrainMeshes[j].visible = terrainMeshesVisible;
}
}
lastMenus[controllerIndex] = menu;
}
}
renderer.render(scene, renderer.vr.enabled ? renderer.vr.getCamera(camera) : camera);
}
init();
(async () => {
console.log('request session');
session = await navigator.xr.requestSession({
exclusive: true,
extensions: {
meshing: true,
},
}).catch(err => Promise.resolve(null));
if (session) {
session.onselectstart = e => {
if (!paintMesh) {
paintMesh = _getPaintMesh();
paintControllerIndex = _getControllerIndex(e.inputSource);
}
};
session.onselectend = e => {
if (paintControllerIndex === _getControllerIndex(e.inputSource)) {
paintMesh = null;
paintControllerIndex = -1;
}
};
// console.log('request first frame');
session.requestAnimationFrame((timestamp, frame) => {
renderer.vr.setSession(session, {
frameOfReferenceType: 'stage',
});
const {views} = frame.getViewerPose();
const viewport = session.baseLayer.getViewport(views[0]);
const width = viewport.width;
const height = viewport.height;
renderer.setSize(width * 2, height);
renderer.setAnimationLoop(null);
renderer.vr.enabled = true;
renderer.vr.setAnimationLoop(animate);
console.log('running!');
});
} else {
console.log('no xr devices');
}
})();
})();
</script>
</body>
</html>