-
Notifications
You must be signed in to change notification settings - Fork 0
/
chromakey-three-js.js
77 lines (64 loc) · 2.12 KB
/
chromakey-three-js.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
/*
Usage:
import ChromaKeyMaterial from './chromakey-three-js';
// 0xd432 is the green screen color, insert yours, if different, below
const keyColor = 0xd432;
const myGreenScreenMaterial = new ChromaKeyMaterial("video", keyColor);
const myGeometry = new THREE.PlaneGeometry(32, 18);
const myGreenScreenVideoObject = new THREE.Mesh(myGeometry, myGreenScreenMaterial);
scene.add( myGreenScreenVideoObject );
*/
import * as THREE from "three"; // Or window.THREE
class ChromaKeyMaterial extends THREE.ShaderMaterial {
constructor(idVideo, keyColor) {
super();
const video = document.getElementById(idVideo);
const self = this;
if (video && THREE) {
this.video = video;
this.keyColorObject = new THREE.Color(keyColor);
this.video.load();
this.videoTexture = new THREE.VideoTexture(video);
this.videoTexture.minFilter = THREE.LinearFilter;
// this.videoTexture.magFilter = THREE.LinearFilter;
// TODO: add a wait for the video to load
this.setValues({
uniforms: {
textureSampler: {
type: "t",
value: self.videoTexture,
},
color: {
type: "c",
value: self.keyColorObject,
},
},
vertexShader:
"varying vec2 vUv;\n" +
"void main(void)\n" +
"{\n" +
" vUv = uv;\n" +
" vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );\n" +
" gl_Position = projectionMatrix * mvPosition;\n" +
"}",
fragmentShader:
"uniform sampler2D textureSampler;\n" +
"uniform vec3 color;\n" +
"varying vec2 vUv;\n" +
"void main(void)\n" +
"{\n" +
" vec3 tColor = texture2D( textureSampler, vUv ).rgb;\n" +
" float a = (length(tColor - color) - 0.5) * 7.0;\n" +
" gl_FragColor = vec4(tColor, a);\n" +
"}",
transparent: true,
});
return this;
} else {
console.error(
"video is not set or the THREE.js library is not available"
);
}
}
}
export default ChromaKeyMaterial;