-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
426 lines (352 loc) · 12.1 KB
/
main.js
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
var scene, camera, renderer, clock, deltaTime, totalTime;
var arToolkitSource, arToolkitContext;
var markerRoot1, markerRoot2;
var mesh1, mesh2;
var videoChunks = [];
var mediaRecorder;
var capabilitiesByDeviceId = {};
var noFlip = false;
const RECORD_START_TIME = 500;
class AlphaVideoMaterial extends THREE.ShaderMaterial {
constructor(videoElement) {
super();
this.video = videoElement;
this.videoTexture = new THREE.VideoTexture(this.video);
this.videoTexture.minFilter = THREE.LinearFilter;
this.videoTexture.magFilter = THREE.LinearFilter;
this.setValues({
uniforms: {
texture: {
type: "t",
value: this.videoTexture
}
},
vertexShader: `
varying vec2 vUv;
void main(void) {
vUv = uv;
vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
gl_Position = projectionMatrix * mvPosition;
}
`,
fragmentShader: `
uniform sampler2D texture;
varying vec2 vUv;
void main(void) {
vec3 tColor = texture2D( texture, vUv).rgb;
vec3 aColor = texture2D( texture, (vUv + vec2(0, -0.5))).rgb;
gl_FragColor = vec4(tColor, aColor[1]);
}
`,
transparent: true
});
}
update() {
if (this.video.readyState === this.video.HAVE_ENOUGH_DATA && this.videoTexture) {
this.videoTexture.needsUpdate = true;
}
}
}
function initialize() {
scene = new THREE.Scene();
let ambientLight = new THREE.AmbientLight(0xcccccc, 0.5);
scene.add(ambientLight);
camera = new THREE.Camera();
scene.add(camera);
renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setClearColor(new THREE.Color('lightgrey'), 0)
renderer.setSize(640, 480);
renderer.domElement.style.display = 'none'
renderer.domElement.style.position = 'absolute'
renderer.domElement.style.top = '0px'
renderer.domElement.style.left = '0px'
document.body.appendChild(renderer.domElement);
clock = new THREE.Clock();
deltaTime = 0;
totalTime = 0;
////////////////////////////////////////////////////////////
// setup arToolkitContext
////////////////////////////////////////////////////////////
// create atToolkitContext
arToolkitContext = new THREEx.ArToolkitContext({
cameraParametersUrl: 'data/camera_para.dat',
detectionMode: 'mono'
});
// copy projection matrix to camera when initialization complete
arToolkitContext.init(function onCompleted() {
camera.projectionMatrix.copy(arToolkitContext.getProjectionMatrix());
});
////////////////////////////////////////////////////////////
// setup markerRoots
////////////////////////////////////////////////////////////
// build markerControls
markerRoot1 = new THREE.Group();
scene.add(markerRoot1);
let markerControls1 = new THREEx.ArMarkerControls(arToolkitContext, markerRoot1, {
type: 'pattern', patternUrl: "data/hiro.patt",
})
markerControls1.addEventListener("markerFound", () => {
hideHelp();
});
// let geometry1 = new THREE.PlaneBufferGeometry(2, 2, 4, 4);
let geometry1 = new THREE.PlaneGeometry(5, 3);
geometry1.scale(1.5, 1.5, 1.5);
let uvs = geometry1.faceVertexUvs[0];
uvs[0][1].y = 0.5;
uvs[1][0].y = 0.5;
uvs[1][1].y = 0.5;
const animationVideo = document.getElementById('nitelito')
const alphaVideoMaterial = new AlphaVideoMaterial(animationVideo);
mesh1 = new THREE.Mesh(geometry1, alphaVideoMaterial);
mesh1.rotation.x = -Math.PI / 2;
markerRoot1.add(mesh1);
/// nitelito2
// build markerControls
markerRoot2 = new THREE.Group();
scene.add(markerRoot2);
let markerControls2 = new THREEx.ArMarkerControls(arToolkitContext, markerRoot2, {
type: 'pattern', patternUrl: "data/kanji.patt",
})
markerControls2.addEventListener("markerFound", () => {
hideHelp();
});
// let geometry1 = new THREE.PlaneBufferGeometry(2, 2, 4, 4);
let geometry2 = new THREE.PlaneGeometry(5, 3);
geometry2.scale(1.5, 1.5, 1.5);
let uvs2 = geometry2.faceVertexUvs[0];
uvs2[0][1].y = 0.5;
uvs2[1][0].y = 0.5;
uvs2[1][1].y = 0.5;
const animationVideo2 = document.getElementById('nitelito2')
const alphaVideoMaterial2 = new AlphaVideoMaterial(animationVideo2);
mesh2 = new THREE.Mesh(geometry2, alphaVideoMaterial2);
mesh2.rotation.x = -Math.PI / 2;
markerRoot2.add(mesh2);
}
function update() {
// update artoolkit on every frame
if (arToolkitSource && arToolkitSource.ready !== false)
arToolkitContext.update(arToolkitSource.domElement);
}
function render() {
const mainCanvas = document.getElementById("main-canvas");
const width = arToolkitContext.arController.videoWidth;
const height = arToolkitContext.arController.videoHeight;
// TODO: If on portrait, recalculate widhtRecalculate wiodth height to remove
// black borders if on portrait.
if (width && height) {
// Setup a canvas with the same dimensions as the video.
mainCanvas.width = width;
mainCanvas.height = height;
}
renderer.render(scene, camera);
// Copy AR and Threejs canvas onto main canvas
const ctx = mainCanvas.getContext('2d');
ctx.clearRect(0, 0, width, height);
if (noFlip) {
ctx.drawImage(arToolkitContext.arController.canvas, 0, 0, width, height);
ctx.drawImage(renderer.domElement, 0, 0, width, height);
} else {
// horizontal-flip
ctx.scale(-1, 1);
ctx.translate(-width, 0);
ctx.drawImage(arToolkitContext.arController.canvas, 0, 0, width, height);
ctx.drawImage(renderer.domElement, 0, 0, width, height);
// reset the transform-matrix
ctx.setTransform(1, 0, 0, 1, 0, 0);
}
}
function animate() {
requestAnimationFrame(animate);
deltaTime = clock.getDelta();
totalTime += deltaTime;
update();
render();
}
// Cameras
function setCameraSource(deviceId) {
// const capabilities = capabilitiesByDeviceId[deviceId];
arToolkitSource = new THREEx.ArToolkitSource({
sourceType: 'webcam',
deviceId: deviceId,
});
arToolkitSource.init(function onReady() {
arToolkitSource.domElement.style.display = 'none';
const capabilities = capabilitiesByDeviceId[deviceId];
// console.clear();
// console.log(JSON.stringify(capabilities, null, 2));
noFlip = capabilities.facingMode.includes("environment");
onResize()
});
}
function listCameras() {
navigator.mediaDevices.getUserMedia({ video: true, audio: false })
.then(() => navigator.mediaDevices.enumerateDevices())
.then(devices => {
const cameraSelect = document.getElementById("camera")
devices.filter(device => device.kind === "videoinput").forEach((device, n) => {
// console.log(JSON.stringify(device, null, 2))
cameraSelect.options.add(new Option(device.label, device.deviceId));
try {
capabilitiesByDeviceId[device.deviceId] = device.getCapabilities()
} catch (err) {
// console.error(err)
capabilitiesByDeviceId[device.deviceId] = { facingMode: ["environment"] }
}
})
if (cameraSelect.options.length <= 1) {
document.getElementById("change-button").classList.add("hidden");
}
setCameraSource(cameraSelect.options[cameraSelect.selectedIndex].value)
})
.catch(e => console.error(e));
}
function onResize() {
const mainCanvas = document.getElementById("main-canvas");
arToolkitSource.onResizeElement()
arToolkitSource.copyElementSizeTo(renderer.domElement)
if (arToolkitContext.arController !== null) {
arToolkitSource.copyElementSizeTo(arToolkitContext.arController.canvas)
arToolkitSource.copyElementSizeTo(mainCanvas)
}
}
function hideHelp() {
const helpDiv = document.getElementById("help");
helpDiv.classList.add("hidden");
}
function showHelp() {
const helpDiv = document.getElementById("help");
helpDiv.classList.remove("hidden");
}
function recordImage() {
const mainCanvas = document.getElementById('main-canvas');
const mimetype = 'image/jpeg'
const dataUrl = mainCanvas.toDataURL(mimetype);
// Share image (if possible), otherwise download as file
if (typeof navigator.share === "function") {
shareCanvas(dataUrl, "nitelito.jpg", mimetype);
} else {
downloadCanvas(dataUrl, "nitelite.jpg")
}
}
function startRecordingVideo() {
const mainCanvas = document.getElementById('main-canvas');
const canvasStream = mainCanvas.captureStream(60); // fps
// Create media recorder from canvas stream
mediaRecorder = new MediaRecorder(canvasStream, {
// mimeType: "video/webm; codecs=vp9"
videoBitsPerSecond: 2500000,
mimeType: "video/webm",
});
// Record data in chunks array when data is available
mediaRecorder.ondataavailable = (evt) => { videoChunks.push(evt.data); };
// Provide recorded data when recording stops
mediaRecorder.onstop = recordVideo;
// Start recording using a 1s timeslice [ie data is made available every 1s)
mediaRecorder.start(1000);
}
function endRecordingVideo() {
setTimeout(() => {
mediaRecorder.stop();
}, 500);
}
function cancelRecordingVideo() {
}
function recordVideo() {
const mimetype = "video/webm";
const blob = new Blob(videoChunks, { type: mimetype });
const dataUrl = URL.createObjectURL(blob);
// Share image (if possible), otherwise download as file
if (typeof navigator.share === "function") {
shareCanvas(dataUrl, "nitelito.webm", mimetype);
} else {
downloadCanvas(dataUrl, "nitelite.webm")
}
}
async function shareCanvas(dataUrl, filename, mimetype) {
const blob = await (await fetch(dataUrl)).blob();
const filesArray = [
new File([blob], filename, { type: mimetype, lastModified: new Date().getTime() })
];
const shareData = {
files: filesArray,
};
try {
await navigator.share(shareData);
} catch (err) {
// console.error(err)
}
}
function downloadCanvas(dataUrl, filename) {
let a = document.createElement("a");
a.href = dataUrl
a.download = filename;
a.dispatchEvent(new MouseEvent("click"));
a.remove();
}
// UI
initialize();
listCameras();
const button = document.getElementById("start")
button.addEventListener("click", () => {
document.getElementById("start-overlay").style = 'display: none';
document.getElementById("help").classList.remove("hidden");
document.getElementById('nitelito').play();
document.getElementById('nitelito2').play();
animate();
})
const cameraSelect = document.getElementById("camera")
cameraSelect.addEventListener("change", (e) => {
setCameraSource(e.target.value)
})
const changeButton = document.getElementById("change-button")
changeButton.addEventListener("click", () => {
cameraSelect.selectedIndex = (cameraSelect.selectedIndex + 1) % cameraSelect.options.length
setCameraSource(cameraSelect.options[cameraSelect.selectedIndex].value)
})
const recordButton = document.getElementById("record-button");
let startsAt, endsAt, recordingTimeout;
const buttonDownHandler = () => {
startsAt = new Date()
clearTimeout(recordingTimeout);
recordingTimeout = setTimeout(() => {
recordButton.classList.add("recording")
startRecordingVideo()
}, RECORD_START_TIME)
};
const buttonCancelHandler = () => {
clearTimeout(recordingTimeout);
recordButton.classList.remove("recording");
};
const buttonUpHandler = async () => {
endsAt = new Date()
// console.log(`diff ${endsAt - startsAt}`);
clearTimeout(recordingTimeout);
recordButton.classList.remove("recording");
if (endsAt - startsAt >= RECORD_START_TIME) {
endRecordingVideo();
} else {
cancelRecordingVideo();
recordImage();
}
};
recordButton.addEventListener("mousedown", buttonDownHandler);
recordButton.addEventListener("mouseout", buttonCancelHandler);
recordButton.addEventListener("mouseup", buttonUpHandler);
recordButton.addEventListener("touchstart", buttonDownHandler);
recordButton.addEventListener("touchcancel", buttonCancelHandler);
recordButton.addEventListener("touchend", buttonUpHandler);
// handle resize event
window.addEventListener('resize', function () {
onResize()
});
const debugDiv = document.getElementById("debug");
const console = {
log: msg => debugDiv.innerHTML += `<pre>${msg}${'\n'}</pre>`,
error: msg => debugDiv.innerHTML += `<pre class="error">${msg}${'\n'}</pre>`,
clear: () => debugDiv.innerHTML = ""
};