-
Notifications
You must be signed in to change notification settings - Fork 2
/
face_deformer.js
executable file
·292 lines (244 loc) · 9.6 KB
/
face_deformer.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
287
288
289
290
291
292
"use strict";
var faceDeformer = function() {
var gl, verticeMap;
var numTriangles;
var maxx, minx, maxy, miny;
var width, height;
var first = true;
var textureVertices, gridVertices;
var texCoordBuffer, gridCoordbuffer;
var texCoordLocation;
var pdmModel;
var usegrid = false;
var drawProgram, gridProgram;
this.init = function(canvas) {
// ready a webgl element
gl = getWebGLContext(canvas);
}
this.load = function(element, points, pModel, vertices) {
pdmModel = pModel;
if (vertices) {
verticeMap = vertices;
} else {
verticeMap = pdmModel.path.vertices;
}
numTriangles = verticeMap.length;
// get cropping
maxx = 0;
minx = element.width;
maxy = 0;
miny = element.height;
for (var i = 0;i < points.length;i++) {
if (points[i][0] > maxx) maxx = points[i][0];
if (points[i][0] < minx) minx = points[i][0];
if (points[i][1] > maxy) maxy = points[i][1];
if (points[i][1] < miny) miny = points[i][1];
}
minx = Math.floor(minx);
maxx = Math.ceil(maxx);
miny = Math.floor(miny);
maxy = Math.ceil(maxy);
width = maxx-minx;
height = maxy-miny;
if (element.tagName == 'VIDEO' || element.tagName == 'IMG') {
var ca = document.createElement('canvas');
ca.width = element.width;
ca.height = element.height;
var cc = ca.getContext('2d');
cc.drawImage(element, 0, 0, element.width, element.height);
} else if (element.tagName == 'CANVAS') {
var cc = element.getContext('2d');
}
var image = cc.getImageData(minx, miny, width, height);
// correct points
var nupoints = [];
for (var i = 0;i < points.length;i++) {
nupoints[i] = [];
nupoints[i][0] = points[i][0] - minx;
nupoints[i][1] = points[i][1] - miny;
}
// create vertices based on points
var textureVertices = [];
for (var i = 0;i < verticeMap.length;i++) {
textureVertices.push(nupoints[verticeMap[i][0]][0]/width);
textureVertices.push(nupoints[verticeMap[i][0]][1]/height);
textureVertices.push(nupoints[verticeMap[i][1]][0]/width);
textureVertices.push(nupoints[verticeMap[i][1]][1]/height);
textureVertices.push(nupoints[verticeMap[i][2]][0]/width);
textureVertices.push(nupoints[verticeMap[i][2]][1]/height);
}
if (first) {
// create program for drawing grid
var gridVertexShaderProg = [
"attribute vec2 a_position;",
"",
"uniform vec2 u_resolution;",
"",
"void main() {",
" vec2 zeroToOne = a_position / u_resolution;",
" vec2 zeroToTwo = zeroToOne * 2.0;",
" vec2 clipSpace = zeroToTwo - 1.0;",
" gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);",
"}"
].join('\n');
var gridFragmentShaderProg = [
"void main() {",
" gl_FragColor = vec4(0.2, 0.2, 0.2, 1.0);",
"}"
].join('\n');
var gridVertexShader = loadShader(gl, gridVertexShaderProg, gl.VERTEX_SHADER);
var gridFragmentShader = loadShader(gl, gridFragmentShaderProg, gl.FRAGMENT_SHADER);
try {
gridProgram = createProgram(gl, [gridVertexShader, gridFragmentShader]);
} catch(err) {
alert("There was a problem setting up the webGL programs. Maybe you should try it in another browser. :(");
}
gridCoordbuffer = gl.createBuffer();
// create program for drawing deformed face
var vertexShaderProg = [
"attribute vec2 a_texCoord;",
"attribute vec2 a_position;",
"",
"varying vec2 v_texCoord;",
"",
"uniform vec2 u_resolution;",
"",
"void main() {",
" vec2 zeroToOne = a_position / u_resolution;",
" vec2 zeroToTwo = zeroToOne * 2.0;",
" vec2 clipSpace = zeroToTwo - 1.0;",
" gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);",
" ",
" v_texCoord = a_texCoord;",
"}"
].join('\n');
var fragmentShaderProg = [
"precision mediump float;",
"",
"uniform sampler2D u_image;",
"",
"varying vec2 v_texCoord;",
"",
"void main() {",
" gl_FragColor = texture2D(u_image, v_texCoord);",
"}"
].join('\n');
var vertexShader = loadShader(gl, vertexShaderProg, gl.VERTEX_SHADER);
var fragmentShader = loadShader(gl, fragmentShaderProg, gl.FRAGMENT_SHADER);
drawProgram = createProgram(gl, [vertexShader, fragmentShader]);
texCoordBuffer = gl.createBuffer();
first = false;
}
// load program for drawing grid
gl.useProgram(gridProgram);
// set the resolution for grid program
var resolutionLocation = gl.getUniformLocation(gridProgram, "u_resolution");
gl.uniform2f(resolutionLocation, gl.drawingBufferWidth, gl.drawingBufferHeight);
// load program for drawing deformed face
gl.useProgram(drawProgram);
// look up where the vertex data needs to go.
texCoordLocation = gl.getAttribLocation(drawProgram, "a_texCoord");
// provide texture coordinates for face vertices (i.e. where we're going to copy face vertices from).
gl.enableVertexAttribArray(texCoordLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(textureVertices), gl.STATIC_DRAW);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
// Create the texture.
var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
// Upload the image into the texture.
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image);
// set the resolution for draw program
resolutionLocation = gl.getUniformLocation(drawProgram, "u_resolution");
gl.uniform2f(resolutionLocation, gl.drawingBufferWidth, gl.drawingBufferHeight);
}
this.draw = function(points) {
if (usegrid) {
// switch program if needed
gl.useProgram(drawProgram);
//texCoordLocation = gl.getAttribLocation(drawProgram, "a_texCoord");
gl.enableVertexAttribArray(texCoordLocation);
gl.bindBuffer(gl.ARRAY_BUFFER, texCoordBuffer);
gl.vertexAttribPointer(texCoordLocation, 2, gl.FLOAT, false, 0, 0);
usegrid = false;
}
// create drawvertices based on points
var vertices = [];
for (var i = 0;i < verticeMap.length;i++) {
vertices.push(points[verticeMap[i][0]][0]);
vertices.push(points[verticeMap[i][0]][1]);
vertices.push(points[verticeMap[i][1]][0]);
vertices.push(points[verticeMap[i][1]][1]);
vertices.push(points[verticeMap[i][2]][0]);
vertices.push(points[verticeMap[i][2]][1]);
}
var positionLocation = gl.getAttribLocation(drawProgram, "a_position");
// Create a buffer for the position of the vertices.
var drawPosBuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, drawPosBuffer);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
// Draw the face vertices
gl.drawArrays(gl.TRIANGLES, 0, numTriangles*3);
}
this.drawGrid = function(points) {
if (!usegrid) {
gl.useProgram(gridProgram);
usegrid = true;
}
// create drawvertices based on points
var vertices = [];
// create new texturegrid
for (var i = 0;i < verticeMap.length;i++) {
vertices.push(points[verticeMap[i][0]][0]);
vertices.push(points[verticeMap[i][0]][1]);
vertices.push(points[verticeMap[i][1]][0]);
vertices.push(points[verticeMap[i][1]][1]);
vertices.push(points[verticeMap[i][1]][0]);
vertices.push(points[verticeMap[i][1]][1]);
vertices.push(points[verticeMap[i][2]][0]);
vertices.push(points[verticeMap[i][2]][1]);
vertices.push(points[verticeMap[i][2]][0]);
vertices.push(points[verticeMap[i][2]][1]);
vertices.push(points[verticeMap[i][0]][0]);
vertices.push(points[verticeMap[i][0]][1]);
}
var positionLocation = gl.getAttribLocation(gridProgram, "a_position");
// Create a buffer for position of the vertices (lines)
gl.bindBuffer(gl.ARRAY_BUFFER, gridCoordbuffer);
gl.enableVertexAttribArray(positionLocation);
gl.vertexAttribPointer(positionLocation, 2, gl.FLOAT, false, 0, 0);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
// Draw the lines
gl.drawArrays(gl.LINES, 0, numTriangles*6);
}
this.clear = function() {
gl.clear(gl.COLOR_BUFFER_BIT);
}
this.calculatePositions = function(parameters, useTransforms) {
var x, y, a, b;
var numParameters = parameters.length;
var positions = [];
for (var i = 0;i < pdmModel.patchModel.numPatches;i++) {
x = pdmModel.shapeModel.meanShape[i][0];
y = pdmModel.shapeModel.meanShape[i][1];
for (var j = 0;j < numParameters-4;j++) {
x += pdmModel.shapeModel.eigenVectors[(i*2)][j]*parameters[j+4];
y += pdmModel.shapeModel.eigenVectors[(i*2)+1][j]*parameters[j+4];
}
if (useTransforms) {
a = parameters[0]*x - parameters[1]*y + parameters[2];
b = parameters[0]*y + parameters[1]*x + parameters[3];
x += a;
y += b;
}
positions[i] = [x,y];
}
return positions;
}
}