-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy paththreex.chromakey.js
68 lines (58 loc) · 1.63 KB
/
threex.chromakey.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
var THREEx = THREEx || {};
THREEx.ChromaKeyMaterial = function (url, keyColor) {
THREE.ShaderMaterial.call(this);
video = document.createElement('video');
video.src = url;
video.load();
var keyColorObject = new THREE.Color(keyColor);
var videoTexture = new THREE.Texture(video);
videoTexture.minFilter = THREE.LinearFilter;
videoTexture.magFilter = THREE.LinearFilter;
this.startVideo = function() {
video.play();
};
this.stopVideo = function() {
video.pause();
video.src = "";
};
this.update = function () {
if (video.readyState === video.HAVE_ENOUGH_DATA) {
// videoImageContext.drawImage(video, 0, 0);
if (videoTexture) {
videoTexture.needsUpdate = true;
}
}
};
this.setValues({
uniforms: {
texture: {
type: "t",
value: videoTexture
},
color: {
type: "c",
value: keyColorObject
}
},
vertexShader:
"varying mediump vec2 vUv;\n" +
"void main(void)\n" +
"{\n" +
"vUv = uv;\n" +
"mediump vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );\n" +
"gl_Position = projectionMatrix * mvPosition;\n" +
"}",
fragmentShader:
"uniform mediump sampler2D texture;\n" +
"uniform mediump vec3 color;\n" +
"varying mediump vec2 vUv;\n" +
"void main(void)\n" +
"{\n" +
" mediump vec3 tColor = texture2D( texture, vUv ).rgb;\n" +
" mediump float a = (length(tColor - color) - 0.5) * 7.0;\n" +
" gl_FragColor = vec4(tColor, a);\n" +
"}",
transparent: true
});
};
THREEx.ChromaKeyMaterial.prototype = Object.create(THREE.ShaderMaterial.prototype);