-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathmain.js
286 lines (251 loc) · 7.63 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
let _canvasVideo = null, _canvasAR = null;
let _selectedDOMColorButton = null;
// tweak contours coefficients - 0 -> no tweak:
const mouthWiden = 0.01;
const upperLipOut = 0;//0.01;
const lowerLipOut = 0.005;//0.01;
const SHAPELIPS = {
name: 'LIPS',
// list of the points involved in this shape.
// each point is given as its label
// the label depends on the used neural network
// run WEBARROCKSFACE.get_LMLabels() to get all labels
points: [
"lipsExt0", // 0
"lipsExtTop1", // 1
"lipsExtTop2", // 2
"lipsExtTop3", // 3
"lipsExtTop4", // 4
"lipsExtTop5", // 5
"lipsExt6", // 6
"lipsExtBot7", // 7
"lipsExtBot8", // 8
"lipsExtBot9", // 9
"lipsExtBot10", // 10
"lipsExtBot11", // 11
"lipsInt12", // 12
"lipsIntTop13", // 13
"lipsIntTop14", // 14
"lipsIntTop15", // 15
"lipsInt16", // 16
"lipsIntBot17", // 17
"lipsIntBot18", // 18
"lipsIntBot19" // 19
],
// iVals are interpolated values
// a value is given for each shape point
// in the same order as points array
// a value can have between 0 and 4 elements
// the value will be retrieved in the fragment shader used to color the shape
// as a float, vec2, vec3 or vec4 depending on its components count
// it is useful to not color evenly the shape
// we can apply gradients, smooth borders, ...
iVals: [
[1], // lipsExt0
[1], // lipsExtTop1
[1], // lipsExtTop2
[1], // lipsExtTop3
[1], // lipsExtTop4
[1], // lipsExtTop5
[1], // lipsExt6
[1], // lipsExtBot7
[1], // lipsExtBot8
[1], // lipsExtBot9
[1], // lipsExtBot10
[1], // lipsExtBot11
[-1], // lipsInt12
[-1], // lipsIntTop13
[-1], // lipsIntTop14
[-1], // lipsIntTop15
[-1], // lipsInt16
[-1], // lipsIntBot17
[-1], // lipsIntBot18
[-1] // lipsIntBot1
],
// how to group shape points to draw triangles
// each value is an index in shape points array
tesselation: [
// upper lip:
0,1,13, // each group of 3 indices is a triangular face
0,12,13,
1,13,2,
2,13,14,
2,3,14,
3,4,14,
14,15,4,
4,5,15,
15,5,6,
15,6,16,
// lower lip:
0,12,19,
0,19,11,
11,10,19,
10,18,19,
10,9,18,
8,9,18,
8,17,18,
7,8,17,
6,7,17,
6,17,16 //*/
],
// interpolated points:
// to make shape border smoother, we can add computed points
// each value of this array will insert 2 new points
//
// the first point will be between the first 2 points indices
// the second point will be between the last 2 points indices
//
// the first value of ks controls the position of the first interpolated point
// if -1, it will match the first point, if 0 it will match the middle point
// the second value of ks controls the position of the second interpolated point
// if 1, it will match the last point, if 0 it will match the middle point
//
// computed using Cubic Hermite interpolation
// the point is automatically inserted into the tesselation
// points are given by their indices in shape points array
interpolations: [
{ // upper lip sides:
tangentInfluences: [2, 2, 2],
points: [1, 2, 3],
ks: [-0.25, 0.25] // between -1 and 1
},
{
tangentInfluences: [2, 2, 2],
points: [3, 4, 5],
ks: [-0.25, 0.25] // between -1 and 1
},
{ // upper lip middle
tangentInfluences: [2, 2, 2],
points: [2, 3, 4],
ks: [-0.25, 0.25] // between -1 and 1
},
{ // lower lip middle:
tangentInfluences: [2, 2, 2],
points: [10, 9, 8],
ks: [-0.25, 0.25] // between -1 and 1
}
],
// we can move points along their normals using the outline feature.
// an outline is specified by the list of point indices in shape points array
// it will be used to compute the normals, the inside and the outside
//
// displacement array are the displacement along normals to apply
// for each point of the outline.
outlines: [
{ // upper lip. Indices of points in points array:
points: [
0,
1,2,3,4,5, // exterior
6, 16,
15, 14, 13, // interior
12
],
displacements: [ // displacements, relative to perimeter:
mouthWiden,
upperLipOut, upperLipOut, upperLipOut - 0.015, upperLipOut, upperLipOut, // exterior
0.00, 0,
0.01, 0.015, 0.01, // interior
mouthWiden
]
},
{ // lower lip:
points: [
12,
19, 18, 17, // interior
16, 6,
7, 8, 9, 10, 11, // exterior
0
],
displacements: [
0,
0.015, 0.02, 0.015,
0, 0.0,
lowerLipOut, lowerLipOut, lowerLipOut, lowerLipOut, lowerLipOut,
0.0
]
}
],
// RENDERING:
// GLSLFragmentSource is the GLSL source code of the shader used
// to fill the shape:
// Debug interpolated vals:
/*GLSLFragmentSource: "void main(void){\n\
gl_FragColor = vec4(0.5 + 0.5*iVal, 0., 1.);\n\
}" //*/
// uniform color:
/*GLSLFragmentSource: "void main(void){\n\
gl_FragColor = vec4(0.1, 0.0, 0.2, 0.5);\n\
}" //*/
// debug samplerVideo and vUV:
/*GLSLFragmentSource: "void main(void){\n\
gl_FragColor = vec4(0., 1., 0., 1.) * texture2D(samplerVideo, vUV);\n\
}" //*/
// color with smooth border:
GLSLFragmentSource: "\n\
const vec2 ALPHARANGE = vec2(0.1, 0.6);\n\
const vec3 LUMA = 1.3 * vec3(0.299, 0.587, 0.114);\n\
\n\
float linStep(float edge0, float edge1, float x){\n\
float val = (x - edge0) / (edge1 - edge0);\n\
return clamp(val, 0.0, 1.0);\n\
}\n\
\n\
\n\
void main(void){\n\
// get grayscale video color:\n\
vec3 videoColor = texture2D(samplerVideo, vUV).rgb;\n\
vec3 videoColorGs = vec3(1., 1., 1.) * dot(videoColor, LUMA);\n\
\n\
// computer alpha:\n\
float alpha = 1.0; // no border smoothing\n\
alpha *= linStep(-1.0, -0.95, abs(iVal)); // interior\n\
alpha *= 0.5 + 0.5 * linStep(1.0, 0.6, abs(iVal)); // exterior smoothing\n\
float alphaClamped = ALPHARANGE.x + (ALPHARANGE.y - ALPHARANGE.x) * alpha;\n\
\n\
// mix colors:\n\
vec3 color = videoColorGs * lipstickColor;\n\
gl_FragColor = vec4(color*alphaClamped, alphaClamped);\n\
\n\
// DEBUG ZONE:\n\
//gl_FragColor = vec4(0., alpha, 0., 1.0);\n\
//gl_FragColor = vec4(alpha, alpha, alphaClamped, 1.0);\n\
//gl_FragColor = vec4(0., 1., 0., 1.);\n\
}",
uniforms: [{
name: 'lipstickColor',
value: [1, 0, 0.3]
}]
}; //end SHAPELIPS
function start(){
WebARRocksFaceShape2DHelper.init({
NNCPath: '../../neuralNets/NN_LIPS_8.json',
canvasVideo: _canvasVideo,
canvasAR:_canvasAR,
shapes: [ SHAPELIPS ]
// ,videoURL: '../../../../testVideos/1032526922-hd.mov'
//,videoURL: '../../../../testVideos/1057538806-hd.mp4'
}).then(function(){
}).catch(function(err){
throw new Error(err);
});
}
// entry point:
function main(){
_canvasAR = document.getElementById('WebARRocksFaceCanvasAR');
_canvasVideo = document.getElementById('WebARRocksFaceCanvasVideo');
_selectedDOMColorButton = document.getElementById('colorRed');
WebARRocksResizer.size_canvas({
canvas: _canvasVideo,
overlayCanvas: [_canvasAR],
callback: start,
isFullScreen: true
});
}
function change_lipstickColor(color, event){
_selectedDOMColorButton.classList.remove('controlButtonSelected');
const domLink = event.target;
domLink.classList.add('controlButtonSelected');
_selectedDOMColorButton = domLink;
WebARRocksFaceShape2DHelper.set_uniformValue('LIPS', 'lipstickColor', color);
}
window.addEventListener('load', main);