Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
147 lines (126 loc) 路 5.05 KB
/
Copy pathmain.js
File metadata and controls
147 lines (126 loc) 路 5.05 KB
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
// Globals:
let THREECAMERA = null, MASKMATERIAL = null, CANVAS = null, THREERENDERER = null;
// Entry point:
function main(){
// set canvas fullscreen with JeelizResizer.js helper:
JeelizResizer.size_canvas({
canvasId: 'matrixCanvas',
CSSFlipX: true, // This option was previously called isFlipY
isFullScreen: true,
callback: start,
onResize: function(){
if (THREECAMERA){
THREECAMERA.aspect = CANVAS.width / CANVAS.height;
THREECAMERA.updateProjectionMatrix();
}
if (MASKMATERIAL){
MASKMATERIAL.uniforms.resolution.value.set(CANVAS.width, CANVAS.height);
}
}
}); //end size_canvas call
}
// called when the canvas is resized:
function start(){
// initialise Jeeliz Facefilter:
JEELIZFACEFILTER.init({
canvasId: 'matrixCanvas',
// path of NN_DEFAULT.json:
NNCPath: '../../../neuralNets/',
callbackReady: function(errCode, spec){
if (errCode){
console.log('HEY, THERE IS AN ERROR =', errCode);
return;
}
console.log('JEELIZFACEFILTER WORKS YEAH!');
init_scene(spec);
}, //end callbackReady()
callbackTrack: callbackTrack
});
}
function init_scene(spec){
CANVAS = spec.canvasElement;
const threeInstances = JeelizThreeHelper.init(spec);
THREERENDERER = threeInstances.renderer;
// create a camera with a 20掳 FoV - obsolete because FoV depend on device:
//var aspecRatio = spec.canvasElement.width / spec.canvasElement.height;
//THREECAMERA = new THREE.PerspectiveCamera(20, aspecRatio, 0.1, 100);
// New way to create the camera, try to guess a good FoV:
THREECAMERA = JeelizThreeHelper.create_camera();
// create the background video texture:
const video = document.createElement('video');
video.src = 'matrixRain.mp4';
video.setAttribute('loop', 'true');
video.setAttribute('preload', 'true');
video.setAttribute('autoplay', 'true');
const videoTexture = new THREE.VideoTexture( video );
videoTexture.magFilter = THREE.LinearFilter;
videoTexture.minFilter = THREE.LinearFilter;
threeInstances.videoMesh.material.uniforms.samplerVideo.value = videoTexture;
try{ // workaround otherwise chrome do not want to play the video sometimes...
video.play();
} catch(e){
}
const playVideo = function(){
video.play();
window.removeEventListener('mousemove', playVideo);
window.removeEventListener('touchmove', playVideo);
}
window.addEventListener('mousedown', playVideo, false);
window.addEventListener('touchdown', playVideo, false);
// import the mesh:
new THREE.BufferGeometryLoader().load('maskMesh.json', function(maskGeometry){
maskGeometry.computeVertexNormals();
// create the customized material:
MASKMATERIAL = new THREE.ShaderMaterial({
vertexShader: "\n\
varying vec3 vNormalView, vPosition;\n\
void main(void){\n\
#include <beginnormal_vertex>\n\
#include <defaultnormal_vertex>\n\
#include <begin_vertex>\n\
#include <project_vertex>\n\
vNormalView = vec3(viewMatrix*vec4(normalize( transformedNormal ),0.));\n\
vPosition = position;\n\
}",
fragmentShader: "precision lowp float;\n\
uniform vec2 resolution;\n\
uniform sampler2D samplerCamera, samplerVideo;\n\
uniform mat2 videoTransformMat2;\n\
varying vec3 vNormalView, vPosition;\n\
\n\
void main(void){\n\
float isNeck = 1. - smoothstep(-1.2, -0.85, vPosition.y);\n\
float isTangeant = pow(length(vNormalView.xy),3.);\n\
float isInsideFace = (1.-isTangeant)*(1.-isNeck);\n\
vec2 uv = gl_FragCoord.xy / resolution;\n\
vec2 uvCameraCentered = 2.0 * videoTransformMat2 * (uv - 0.5);\n\
vec3 colorCamera = texture2D(samplerCamera, uvCameraCentered + 0.5).rgb;\n\
float colorCameraVal = dot(colorCamera, vec3(0.299, 0.587, 0.114));\n\
colorCamera = colorCameraVal*vec3(0.0, 1.5, 0.0);\n\
vec3 refracted = refract(vec3(0.,0.,-1.), vNormalView, 0.3);\n\
vec2 uvRefracted = uv + 0.1*refracted.xy;\n\
uvRefracted = mix(uv, uvRefracted, smoothstep(0.,1.,isInsideFace));\n\
vec3 colorLineCode = texture2D(samplerVideo, uvRefracted).rgb;\n\
colorCamera += vec3(1.) * smoothstep(0.3,0.6,colorCameraVal);\n\
vec3 finalColor = colorCamera * isInsideFace + colorLineCode;\n\
gl_FragColor = vec4(finalColor, 1.);\n\
}",
uniforms:{
samplerCamera: {value: JeelizThreeHelper.get_threeVideoTexture()},
samplerVideo: {value: videoTexture},
videoTransformMat2: {value: spec.videoTransformMat2},
resolution: {
value: new THREE.Vector2(spec.canvasElement.width,
spec.canvasElement.height)}
}
});
const maskMesh = new THREE.Mesh(maskGeometry, MASKMATERIAL);
maskMesh.position.set(0, 0.3,-0.35);
threeInstances.faceObject.add(maskMesh);
JeelizThreeHelper.apply_videoTexture(maskMesh);
});
}
function callbackTrack(detectState){
JeelizThreeHelper.render(detectState, THREECAMERA);
}
window.addEventListener('load', main);