-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebcam-texture.js
71 lines (59 loc) · 1.96 KB
/
webcam-texture.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
define([], function() {
'use strict';
var WIDTH = 1280,
HEIGHT = 720,
video, videoImage, videoImageContext, videoTexture;
function sizeAndOffscreen(element) {
element.width = WIDTH;
element.height = HEIGHT;
element.style.visibility = 'hidden';
element.style.position = 'absolute';
element.style.top = 0;
element.style.left = '-' + WIDTH.toString() + 'px';
}
var getUserMedia = navigator.getUserMedia ||
navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia;
var module = {
start: function(callback) {
if (video) {
return;
}
getUserMedia.call(navigator, {audio: false, video: true},
function(stream) {
video = document.createElement('video');
sizeAndOffscreen(video);
video.autoplay = 'autoplay';
video.src = window.URL.createObjectURL(stream);
document.body.appendChild(video);
videoImage = document.createElement('canvas');
sizeAndOffscreen(videoImage);
document.body.appendChild(videoImage);
videoImageContext = videoImage.getContext('2d');
videoImageContext.fillStyle = '#008800';
videoImageContext.fillRect(0, 0, videoImage.width, videoImage.height);
videoTexture = new THREE.Texture(videoImage);
videoTexture.minFilter = THREE.LinearFilter;
videoTexture.magFilter = THREE.LinearFilter;
callback(new THREE.MeshBasicMaterial({map: videoTexture, color: 0xFFFFFF}));
},
function(error) {
console.error(error);
}
);
},
update: function(delta) {
if (!video) {
return;
}
if (video.readyState !== video.HAVE_ENOUGH_DATA) {
return;
}
videoImageContext.drawImage(video, 0, 0, videoImage.width, videoImage.height);
if (videoTexture) {
videoTexture.needsUpdate = true;
}
}
};
return module;
});