-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacemotion.js
367 lines (308 loc) · 9.99 KB
/
facemotion.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
Face = (function() {
var FaceConstructor;
stylePath = function(path, style) {
path.style = style;
return path;
};
smoothPath = function(path) {
path.smooth();
return path;
};
chordLength = function(hyp, y) {
return Math.sqrt(hyp*hyp-y*y);
};
move = function(point, x, y) {
point.x = x;
point.y = y;
return point;
};
reflect = function(path) {
for (var i = 0; i < path.segments.length; i++) {
path.segments[i].point.x = -path.segments[i].point.x;
}
return path;
};
plusEquals = function(point1, point2) {
point1.x += point2.x;
point1.y += point2.y;
};
buildEye = function(pts) {
var eye = new Path();
eye.add(pts[0]);
eye.arcTo(pts[1],pts[2]);
eye.lineTo(pts[3]);
eye.arcTo(pts[4],pts[5]);
eye.closePath();
return eye;
};
FaceConstructor = function(morphology) {
if (false === (this instanceof Face)) {
return new Face(morphology);
}
this.styles = {
'basic': {
strokeColor: 'black',
strokeWidth: 5,
strokeCap: 'round',
strokeJoin: 'round'
},
'filled': {
strokeWidth: 0,
fillColor: 'black'
},
'blank': {
strokeWidth: 0
},
'muscle': {
strokeWidth: 2,
strokeColor: 'blue'
}
};
project.currentStyle = this.styles.basic;
this.version = morphology.version;
this.stiffness = morphology.stiffness;
this.damping = morphology.damping;
this.recipes = morphology.recipes;
this.muscles = {};
this.showMuscles = true;
this.assemble(morphology);
this.attachMuscles(morphology);
};
FaceConstructor.prototype.setEmotions = function(emotions) {
var pulls = {};
// Reset muscle vals
for (var name in this.muscles) {
pulls[name] = 0;
}
// Calculate new muscle values from emotion sliders
for (var emotion in emotions) {
var muscles = this.recipes[emotion];
var pull = emotions[emotion];
for (var muscle in muscles) {
pulls[muscle] = 1 - ((1 - muscles[muscle]*pull) * (1 - pulls[muscle]));
}
}
this.setMuscles(pulls);
return pulls;
};
FaceConstructor.prototype.setMuscles = function(musclePulls) {
for (var muscle in musclePulls) {
this.muscles[muscle] = musclePulls[muscle];
}
};
FaceConstructor.prototype.forEachPoint = function(func) {
this.face.children.forEach(function(group, i) {
if (group.resolve) {
group.children.forEach(function(path, i) {
path.segments.forEach(function(segment, i) {
func(segment.point);
});
});
}
});
};
FaceConstructor.prototype.setupAnimation = function() {
this.forEachPoint(function(point) {
point.origin = point.clone();
point.muscles = {};
point.vel = new Point(0, 0);
point.acc = new Point(0, 0);
});
};
FaceConstructor.prototype.assemble = function(morphology) {
this.face = new Group();
for (var name in morphology.features) {
var feature = morphology.features[name];
this[name] = new Group();
if (feature.type !== 'circle') {
var side = new Path();
for (var i = 0; i < feature.points.length; i++) {
side.add(new Point(feature.points[i]));
}
side.style.strokeWidth = side.style.strokeWidth * feature.weight;
// Mirror the feature and add as children to the face
this[name].addChildren([
side,
reflect(side.clone())
]);
} else {
var topChord = chordLength(feature.radius,feature.upperChord*feature.radius),
botChord = chordLength(feature.radius,feature.lowerChord*feature.radius),
leftEyePoints = [
new Point(
-feature.center.x-topChord,
feature.center.y-feature.upperChord*feature.radius
),
new Point(
-feature.center.x-feature.radius, feature.center.y
),
new Point(
-feature.center.x-botChord,
feature.center.y+feature.lowerChord*feature.radius
),
new Point(
-feature.center.x+botChord,
feature.center.y+feature.lowerChord*feature.radius
),
new Point(
-feature.center.x+feature.radius, feature.center.y
),
new Point(
-feature.center.x+topChord,
feature.center.y-feature.upperChord*feature.radius
)
],
rightEyePoints = [
new Point(
feature.center.x-topChord,
feature.center.y-feature.upperChord*feature.radius
),
new Point(
feature.center.x-feature.radius, feature.center.y
),
new Point(
feature.center.x-botChord,
feature.center.y+feature.lowerChord*feature.radius
),
new Point(
feature.center.x+botChord,
feature.center.y+feature.lowerChord*feature.radius
),
new Point(
feature.center.x+feature.radius, feature.center.y
),
new Point(
feature.center.x+topChord,
feature.center.y-feature.upperChord*feature.radius
)
];
var leftEye = buildEye(leftEyePoints), rightEye = buildEye(rightEyePoints);
leftEye.style.strokeWidth *= feature.weight;
rightEye.style.strokeWidth *= feature.weight;
this[name].addChildren([rightEye, leftEye]);
}
this[name].resolve = true;
this.face.addChild(this[name]);
}
this.setupAnimation();
this.basicOffset = this.face.position.clone();
this.face.position = new Point(0, 0);
this.face.origin = this.face.position;
};
// Attach the muscles in the morphology to the appropriate points on the face
FaceConstructor.prototype.attachMuscles = function(morphology) {
this.visualMuscles = new Group();
var temp = [];
for (var name in morphology.muscleGroups) {
var group = morphology.muscleGroups[name];
this.muscles[name] = 0;
for (var attachment in group.muscles) {
var weight = group.muscles[attachment],
feature = attachment.split(':')[0],
n = attachment.split(':')[1];
this[feature].children.forEach(function(path, i) {
if (!isNaN(n)) {
point = path.segments[n].point;
var origin = new Point(group.origin);
if (point.x < 0 || i == 1) {
origin.x = -origin.x;
}
var muscle = new Path();
muscle.add(point);
temp.push([point.x, point.y]);
console.log([point.x, point.y].join(", "), [origin.x, origin.y].join(", "));
muscle.add(origin);
muscle.opacity = 0.5;
this.visualMuscles.addChild(muscle);
if (point.x === 0) {
point.constrainX = true;
}
point.muscles[name] = {
'strength': group.strength,
'stroke': group.stroke,
'origin': origin,
'weight': weight,
'path': stylePath(muscle, this.styles.muscle)
};
}
}.bind(this));
}
}
var boxsort = {};
for (var i = 0; i < temp.length; i++) {
if (boxsort[temp[i][1]] !== undefined) {
boxsort[temp[i][1]].push(temp[i]);
} else {
boxsort[temp[i][1]] = temp[i];
}
}
this.face.addChild(this.visualMuscles);
};
FaceConstructor.prototype.computePull = function(point, muscle, engagement) {
// calculate free length of spring from current muscle engagement
var freeLength = point.origin
.subtract(muscle.origin)
.length - (muscle.stroke*engagement);
// calculate spring displacement given current attachment point position
var springDisplacement = muscle.origin
.subtract(point)
.normalize(
point.subtract(muscle.origin)
.length - freeLength
);
// Store muscle engagement so we can update the muscle color
muscle.engagement = engagement;
// Add spring force to attachment point acceleration (-k*x)
return springDisplacement.multiply(muscle.weight * muscle.strength * engagement);
};
FaceConstructor.prototype.resolvePoint = function(point) {
// Add some restoring force to the attachment point's origin
point.acc = point.origin
.multiply(this.stiffness).subtract(point.multiply(this.stiffness));
// Add some damping to avoid oscillations
plusEquals(
point.acc,
point.vel.multiply(-this.damping)
);
// Calculate force from each muscle
for (var name in point.muscles) {
var muscle = point.muscles[name];
var pull = this.computePull(point, muscle, this.muscles[name]);
plusEquals(
point.acc,
pull
);
}
};
FaceConstructor.prototype.update = function() {
// Translate face to origin
this.face.position = this.face.origin;
this.forEachPoint(function(point) {
this.resolvePoint(point);
}.bind(this));
this.face.position = view.center.subtract(this.basicOffset);
};
// Run a basic diff-eq solver to animate the points
FaceConstructor.prototype.loop = function(event) {
this.update();
this.forEachPoint(function(point) {
plusEquals(point.vel, point.acc.multiply(event.delta));
plusEquals(point, point.vel.multiply(event.delta));
for (var name in point.muscles) {
var muscle = point.muscles[name];
if (point.constrainX) {
point.x = muscle.path.segments[0].point.x;
}
move(muscle.path.segments[0].point, point.x, point.y);
if (this.showMuscles) {
stylePath(muscle.path, this.styles.muscle);
muscle.path.strokeColor.hue = remap(muscle.engagement, 0, 1, 240, 0);
muscle.path.opacity = 0.5;
} else {
stylePath(muscle.path, this.styles.blank);
}
}
}.bind(this));
};
return FaceConstructor;
}());