@anime-studio/kit 0.0.0-stage → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +58 -2
- package/dist/cartoon.d.ts +368 -0
- package/dist/cartoon.d.ts.map +1 -0
- package/dist/cartoon.js +2815 -0
- package/dist/cartoon.js.map +1 -0
- package/dist/human.d.ts +165 -0
- package/dist/human.d.ts.map +1 -0
- package/dist/human.js +433 -0
- package/dist/human.js.map +1 -0
- package/dist/humanFront.d.ts +18 -0
- package/dist/humanFront.d.ts.map +1 -0
- package/dist/humanFront.js +137 -0
- package/dist/humanFront.js.map +1 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/info.d.ts +96 -0
- package/dist/info.d.ts.map +1 -0
- package/dist/info.js +277 -0
- package/dist/info.js.map +1 -0
- package/dist/proportions.d.ts +37 -0
- package/dist/proportions.d.ts.map +1 -0
- package/dist/proportions.js +115 -0
- package/dist/proportions.js.map +1 -0
- package/dist/rig.d.ts +538 -0
- package/dist/rig.d.ts.map +1 -0
- package/dist/rig.js +1381 -0
- package/dist/rig.js.map +1 -0
- package/dist/room.d.ts +124 -0
- package/dist/room.d.ts.map +1 -0
- package/dist/room.js +217 -0
- package/dist/room.js.map +1 -0
- package/dist/util.d.ts +27 -0
- package/dist/util.d.ts.map +1 -0
- package/dist/util.js +29 -0
- package/dist/util.js.map +1 -0
- package/dist/views.d.ts +68 -0
- package/dist/views.d.ts.map +1 -0
- package/dist/views.js +92 -0
- package/dist/views.js.map +1 -0
- package/dist/volume.d.ts +105 -0
- package/dist/volume.d.ts.map +1 -0
- package/dist/volume.js +234 -0
- package/dist/volume.js.map +1 -0
- package/package.json +45 -3
package/dist/cartoon.js
ADDED
|
@@ -0,0 +1,2815 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* TV-cartoon characters from a short description (`CartoonLook`): the look of a family TV
|
|
3
|
+
* cartoon — long thin limbs, big sneakers, large expressive faces with oval eyes and small dark
|
|
4
|
+
* pupils, thick brows, a wide mouth — on the kit skeleton, so every kit clip, emotion and director
|
|
5
|
+
* feature works with them.
|
|
6
|
+
*
|
|
7
|
+
* The main view is a three-quarter view facing right (the usual TV-cartoon angle: two people talking
|
|
8
|
+
* face each other and the audience); the generated `front` and `back` views share the skeleton.
|
|
9
|
+
* Heads, hair and torsos are modelled as volumes (`volume.ts`) and drawn from each angle, so the
|
|
10
|
+
* three views always match. Measure with `cartoonInfo(doc)`.
|
|
11
|
+
*/
|
|
12
|
+
import { normalizeTrack, parsePath, pathPoints, pathToString, reach3d, rig3dPoint, rig3dPose, sampleTrack, withPoints } from "@anime-studio/core";
|
|
13
|
+
import { humanClips } from "./human.js";
|
|
14
|
+
import { cartoonHands, characterClips, emotions, fluid, gapeMouth, limbBones, limbIk, morphTowards, mouthInside, mouthShapes } from "./rig.js";
|
|
15
|
+
import { rigInfo } from "./info.js";
|
|
16
|
+
import { withViews } from "./views.js";
|
|
17
|
+
import { aimForward, longHex, mix, shade } from "./util.js";
|
|
18
|
+
import { toCam, above, blend, box, bumpy, capsule, cast, contours, curvePath, depthOf, ellipsoid, fieldPath, grow, gridFor, intersect, normal, onSurface, project, surfaceZ, smoothstep, sphere, taper, union, } from "./volume.js";
|
|
19
|
+
const BUILDS = {
|
|
20
|
+
// A baby: a big head on a small round body, short limbs.
|
|
21
|
+
baby: { L: 50, T: 54, n: 3, u: 38, S: 22, W: 24, H: 22, D: 19, arm: [9, 8], leg: [12, 10], hand: 6.5, shoe: 0.5, eye: 1.2 },
|
|
22
|
+
child: { L: 150, T: 96, n: 16, u: 58, S: 34, W: 30, H: 28, D: 22, arm: [13, 11], leg: [17, 14], hand: 10, shoe: 0.9, eye: 1.12 },
|
|
23
|
+
kid: { L: 192, T: 110, n: 24, u: 56, S: 38, W: 31, H: 30, D: 22, arm: [14, 12], leg: [18, 15], hand: 10.5, shoe: 1, eye: 1.06 },
|
|
24
|
+
teen: { L: 236, T: 134, n: 26, u: 51, S: 44, W: 34, H: 36, D: 25, arm: [15, 13], leg: [20, 17], hand: 11, shoe: 1.06, eye: 1 },
|
|
25
|
+
woman: { L: 250, T: 148, n: 26, u: 46, S: 44, W: 33, H: 44, D: 26, arm: [15, 13], leg: [20, 17], hand: 11, shoe: 1.02, eye: 0.94 },
|
|
26
|
+
man: { L: 254, T: 168, n: 22, u: 48, S: 58, W: 50, H: 46, D: 34, arm: [19, 16], leg: [24, 20], hand: 13, shoe: 1.16, eye: 0.88 },
|
|
27
|
+
big: { L: 250, T: 196, n: 18, u: 47, S: 80, W: 66, H: 54, D: 46, arm: [30, 24], leg: [30, 26], hand: 16, shoe: 1.26, eye: 0.86 },
|
|
28
|
+
elder: { L: 214, T: 150, n: 18, u: 48, S: 50, W: 48, H: 46, D: 32, arm: [17, 14], leg: [21, 18], hand: 12, shoe: 1.04, eye: 0.84 },
|
|
29
|
+
};
|
|
30
|
+
const INK = "#2a201e";
|
|
31
|
+
const SW = 2.8;
|
|
32
|
+
/**
|
|
33
|
+
* The line weights of the drawing being built: the painted style's own (`SW` for outlines, thinner
|
|
34
|
+
* for seams and lips), or one weight for every line (`CartoonStyle.line`), set for a build.
|
|
35
|
+
*/
|
|
36
|
+
const WEIGHT = { w: SW, uniform: false };
|
|
37
|
+
/** A line weight as the style draws it (one weight for everything when uniform). */
|
|
38
|
+
const lw = (w) => (WEIGHT.uniform ? WEIGHT.w : w);
|
|
39
|
+
/** The three-quarter angle of the main view. */
|
|
40
|
+
const Q = (36 * Math.PI) / 180;
|
|
41
|
+
const deg = (d) => (d * Math.PI) / 180;
|
|
42
|
+
/** How much of the camera's pitch the head is drawn with. */
|
|
43
|
+
const HEAD_PITCH = 0.3;
|
|
44
|
+
/**
|
|
45
|
+
* The view the head is drawn in at a camera pitch: nearly level (`HEAD_PITCH` of the pitch — seen
|
|
46
|
+
* from above, the nose would come down over the mouth) and put back where the camera's pitch puts
|
|
47
|
+
* the top of the neck. Everything placed on the head (its points, anchors, the ponytail's pivot,
|
|
48
|
+
* the measurements) is projected with it.
|
|
49
|
+
*/
|
|
50
|
+
const headAngle = (m, yaw, pitch) => ({ yaw, pitch: pitch * HEAD_PITCH, dy: m.y.neckTop * (Math.cos(pitch) - Math.cos(pitch * HEAD_PITCH)) });
|
|
51
|
+
/** Drawn angles, in turning order (the in-betweens make turns fluid). */
|
|
52
|
+
const VIEWS = {
|
|
53
|
+
front: 0, half: deg(12), q24: deg(24), profile: Q, q50: deg(50), q64: deg(64), q77: deg(77), side: deg(90),
|
|
54
|
+
q112: deg(112), away: deg(135), q158: deg(158), back: Math.PI,
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* The drawn angles a raised arm is kept beside the head in by its gesture (`gestures3d`): those facing
|
|
58
|
+
* the camera, three-quarters and turned away. (From nearer a side the near arm points at the camera —
|
|
59
|
+
* over a big head whatever its angle — and the engine's picture-space face clearance opens it out.)
|
|
60
|
+
*/
|
|
61
|
+
const BESIDE_VIEWS = [VIEWS.front, VIEWS.half, VIEWS.q24, VIEWS.profile, VIEWS.q50, VIEWS.away, VIEWS.q158, VIEWS.back];
|
|
62
|
+
/**
|
|
63
|
+
* The angles a hand's drawing is mirrored in (TV proportions): the drawing has its thumb towards the
|
|
64
|
+
* way the character faces (right in the picture) — from the front the far hand's would point away
|
|
65
|
+
* from the body, from behind the near hand's.
|
|
66
|
+
*/
|
|
67
|
+
const HAND_MIRROR = { B: ["front", "half", "q24"], F: ["q158", "back"] };
|
|
68
|
+
/** The yaw the near eye, the mouth and glasses of a fully side-on face with round eyes are drawn from (radians). */
|
|
69
|
+
const PROFILE_EYE = deg(68);
|
|
70
|
+
/** A wave's forearm: how far in towards the head it leans at most, and its swing out from there (degrees). */
|
|
71
|
+
const WAVE_IN = 12, WAVE_SWING = 44;
|
|
72
|
+
const SUFFIX = { front: "F", half: "H", q24: "T", profile: "", q50: "U", q64: "V", q77: "W", side: "S", q112: "X", away: "A", q158: "Y", back: "K" };
|
|
73
|
+
const r = (n) => Math.round(n * 100) / 100;
|
|
74
|
+
const st = (w = SW) => `stroke="palette(ink)" stroke-width="${lw(w)}" stroke-linejoin="round" stroke-linecap="round"`;
|
|
75
|
+
/** Outline in a dark tone of a palette colour (`<key>Line`). */
|
|
76
|
+
const stc = (key, w = SW) => `stroke="palette(${key}Line)" stroke-width="${lw(w)}" stroke-linejoin="round" stroke-linecap="round"`;
|
|
77
|
+
/** The line colour that goes with a fill (`palette(x)` → `palette(xLine)`). */
|
|
78
|
+
const LINE = (fill) => fill.replace(/palette\(([^)]+)\)/, (_, k) => `palette(${k.replace(/Shade$|Dark$/, "")}Line)`);
|
|
79
|
+
const pts = (p) => p.map((q) => `${r(q[0])} ${r(q[1])}`).join(" L");
|
|
80
|
+
const NOSES = {
|
|
81
|
+
button: { size: [0.16, 0.13, 0.2], out: 0.08 },
|
|
82
|
+
round: { size: [0.22, 0.15, 0.22], out: 0.09 },
|
|
83
|
+
wide: { size: [0.24, 0.14, 0.17], out: 0.04 },
|
|
84
|
+
long: { size: [0.13, 0.11, 0.15], out: 0.12 },
|
|
85
|
+
};
|
|
86
|
+
const CHINS = { round: 1.14, chubby: 1.14, pointy: 1.14, square: 1.16 };
|
|
87
|
+
/** The arm's length (shoulder to wrist) of an anatomy: `body.arm`, or 0.9 × torso + 0.34 × hip. */
|
|
88
|
+
export const armLength = (B) => B.arm ?? B.torso * 0.9 + B.hip * 0.34;
|
|
89
|
+
/** Thigh radius at the hip, as a fraction of `limbs.leg[0]` (the legs are drawn and checked with it). */
|
|
90
|
+
export const THIGH_R = 0.8;
|
|
91
|
+
/** The anatomy of a look (the defaults of its build, limbs, height, nose and jaw). */
|
|
92
|
+
export function cartoonAnatomy(look) {
|
|
93
|
+
const base = BUILDS[look.build];
|
|
94
|
+
const k = look.tall ?? 1;
|
|
95
|
+
// Limb thickness: thin (the default, long thin limbs), normal or thick.
|
|
96
|
+
const lk = look.limbs === "thick" ? 1.6 : look.limbs === "normal" ? 1.32 : 1;
|
|
97
|
+
const nose = NOSES[look.nose ?? "button"];
|
|
98
|
+
// A smaller nose on a female face.
|
|
99
|
+
const nk = look.female ? 0.85 : 1;
|
|
100
|
+
return {
|
|
101
|
+
body: { hip: r(base.L * k), torso: r(base.T * k), neck: base.n, head: base.u, shoulders: base.S, waist: base.W, hips: base.H, depth: base.D },
|
|
102
|
+
limbs: { arm: [r(base.arm[0] * lk), r(base.arm[1] * lk)], leg: [r(base.leg[0] * lk), r(base.leg[1] * lk)], hand: r(base.hand * (1 + (lk - 1) * 0.5)), shoe: base.shoe, calf: look.female ? 1.22 : 1.12 },
|
|
103
|
+
face: {
|
|
104
|
+
eyeY: 0.12, eyeX: 0.3, eye: [r(0.185 * base.eye), r(0.235 * base.eye)], pupil: r(0.075 * base.eye), brow: 0.13,
|
|
105
|
+
noseY: 0.38, nose: [r(nose.size[0] * nk), r(nose.size[1] * nk), r(nose.size[2] * nk)], noseOut: r(nose.out * nk),
|
|
106
|
+
mouthY: 0.74, mouthW: 0.28, mouthMargin: 0.45, earY: 0.3, chinY: CHINS[look.jaw ?? "round"],
|
|
107
|
+
},
|
|
108
|
+
physics: { body: [0.75, 0.6], forearms: [0.55, 0.45], head: [0.7, 0.55], tail: [0.32, 0.25], hair: [0.45, 0.35] },
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* The rules an anatomy must follow: features that do not touch (the mouth below the nose with room
|
|
113
|
+
* for a smile, the nose below the eyes, the eyes apart and inside the face), a chin under the mouth,
|
|
114
|
+
* limbs that fit the body, hands that reach the lap seated, physics in range.
|
|
115
|
+
*/
|
|
116
|
+
export function checkAnatomy(a) {
|
|
117
|
+
const out = [];
|
|
118
|
+
const f = a.face, B = a.body, Lm = a.limbs;
|
|
119
|
+
const err = (rule, message) => out.push({ severity: "error", rule, message });
|
|
120
|
+
const warn = (rule, message) => out.push({ severity: "warning", rule, message });
|
|
121
|
+
// A smile lifts the corners about 0.1 head radius; the lips need 0.06 more below the nose.
|
|
122
|
+
const noseBottom = f.noseY + f.nose[1];
|
|
123
|
+
if (f.mouthY - 0.1 - noseBottom < 0.06)
|
|
124
|
+
err("mouth-nose", `the mouth (at ${f.mouthY}) touches the nose (its bottom at ${r(noseBottom)}): keep mouthY ≥ ${r(noseBottom + 0.16)}`);
|
|
125
|
+
if (f.chinY - f.mouthY < 0.3)
|
|
126
|
+
err("chin", `no chin under the mouth (mouth at ${f.mouthY}, chin at ${f.chinY}): an open mouth needs 0.3 head radius`);
|
|
127
|
+
if (f.noseY - f.nose[1] < f.eyeY)
|
|
128
|
+
warn("nose-eyes", "the nose starts above the eyes' centre: it will cover them in a three-quarter view");
|
|
129
|
+
if (f.eyeX - f.eye[0] < 0.04)
|
|
130
|
+
err("eyes-apart", "the eyes touch each other");
|
|
131
|
+
if (f.eyeX + f.eye[0] > 0.8)
|
|
132
|
+
err("eyes-inside", "the eyes go past the side of the face");
|
|
133
|
+
if (f.eyeY - f.eye[1] - f.brow < -0.95)
|
|
134
|
+
err("brows", "the brows are above the top of the head");
|
|
135
|
+
if (f.mouthW < 0.15 || f.mouthW > 0.45)
|
|
136
|
+
err("mouth-width", `mouthW ${f.mouthW} out of 0.15..0.45 head radii`);
|
|
137
|
+
if (f.mouthMargin < 0 || f.mouthMargin > 0.9)
|
|
138
|
+
err("mouth-margin", "mouthMargin out of 0..0.9");
|
|
139
|
+
if (f.pupil > f.eye[0] * 0.8)
|
|
140
|
+
err("pupil", "the pupil is bigger than the eye");
|
|
141
|
+
if (Lm.leg[0] * THIGH_R > B.hips)
|
|
142
|
+
err("legs-fit", `the thighs (${r(Lm.leg[0] * THIGH_R)} radius at the hip) are wider than the hips (${B.hips})`);
|
|
143
|
+
if (Lm.calf < 1 || Lm.calf > 1.6)
|
|
144
|
+
err("calf", "limbs.calf out of 1..1.6");
|
|
145
|
+
if (Lm.arm[0] / 2 > B.shoulders * 0.6)
|
|
146
|
+
warn("arms-fit", "the arms are very thick for the shoulders");
|
|
147
|
+
// Seated, a hand reaches the middle of the thigh: from the shoulder, down the torso, forward half a thigh.
|
|
148
|
+
const arm = armLength(B), need = Math.hypot(B.torso * 0.82, B.hip * 0.28);
|
|
149
|
+
if (arm < need)
|
|
150
|
+
warn("reach", `the arms (${r(arm)}) are too short for the hands to rest in the lap seated (${r(need)})`);
|
|
151
|
+
if (B.head > (B.hip + B.torso) * 0.5)
|
|
152
|
+
warn("head", "the head is wider than half the body's height: it will hide the shoulders when it turns");
|
|
153
|
+
for (const [k, v] of Object.entries(a.physics))
|
|
154
|
+
if (v.some((x) => x <= 0 || x > 1))
|
|
155
|
+
err("physics", `physics.${k} must be in (0, 1]`);
|
|
156
|
+
return out;
|
|
157
|
+
}
|
|
158
|
+
/** Half width of the part of a face that looks at the camera at a height (a mouth must fit in it). */
|
|
159
|
+
function frontHalf(head, y, u) {
|
|
160
|
+
let x = 0;
|
|
161
|
+
for (; x < u * 1.2; x += u * 0.02) {
|
|
162
|
+
const z = surfaceZ(head, x, y);
|
|
163
|
+
if (z === undefined || normal(head, [x, y, z])[2] < 0.6)
|
|
164
|
+
break;
|
|
165
|
+
}
|
|
166
|
+
return x;
|
|
167
|
+
}
|
|
168
|
+
/** The lowest point of the chin under the middle of the mouth. */
|
|
169
|
+
function chinBottom(head, from, u) {
|
|
170
|
+
let y = from;
|
|
171
|
+
while (y < from + u * 1.5 && surfaceZ(head, 0, y + u * 0.02) !== undefined)
|
|
172
|
+
y += u * 0.02;
|
|
173
|
+
return y;
|
|
174
|
+
}
|
|
175
|
+
/** Eye half height (as drawn). */
|
|
176
|
+
const m_ry = (u) => u * 0.235;
|
|
177
|
+
/**
|
|
178
|
+
* `mirror`: the body's own left and right swapped for the asymmetric details (a breast pocket, a
|
|
179
|
+
* side part, something behind an ear) — drawn for a character facing left (the rig is mirrored, so
|
|
180
|
+
* the details land back on the same side of the body).
|
|
181
|
+
*/
|
|
182
|
+
/** The style's settings from `CartoonOptions.style`. */
|
|
183
|
+
function styleOf(style) {
|
|
184
|
+
const painted = { ink: "", shade: true, line: undefined, limbs: "tapered", hands: "fist", eyes: "oval", posture: "upright", proportions: "normal", blush: false };
|
|
185
|
+
if (style === "flat")
|
|
186
|
+
return { ink: "#1b1b1b", shade: false, line: 2.4, limbs: "tube", hands: "open", eyes: "wide", posture: "slouch", proportions: "bigHead", blush: false };
|
|
187
|
+
if (style === "painted")
|
|
188
|
+
return painted;
|
|
189
|
+
const modern = { ink: "#2b2238", shade: false, line: 2.4, limbs: "tube", hands: "open", eyes: "round", posture: "upright", proportions: "tv", blush: false };
|
|
190
|
+
if (!style || style === "modern")
|
|
191
|
+
return modern;
|
|
192
|
+
// A partial style refines the painted look (as before the modern default).
|
|
193
|
+
return { ...painted, ...style };
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* A look's anatomy as a style draws it: wide eyes closer together with dot pupils, a bigger head;
|
|
197
|
+
* `big` eyes and `cartoon` proportions: a big head on a short, chunky body; `round` eyes and `tv`
|
|
198
|
+
* proportions (the modern default): big round eyes nearly touching, a natural figure with long slim
|
|
199
|
+
* legs and thin limbs.
|
|
200
|
+
*/
|
|
201
|
+
export function styledAnatomy(a, style, look) {
|
|
202
|
+
const st = styleOf(style);
|
|
203
|
+
const out = { ...a, body: { ...a.body }, face: { ...a.face }, limbs: { ...a.limbs } };
|
|
204
|
+
if (st.eyes === "big") {
|
|
205
|
+
// Big eyes set a little lower on a big cranium, a big pupil with two glints; the nose and the
|
|
206
|
+
// mouth come down with them (the face's rules — `checkAnatomy` — still hold).
|
|
207
|
+
out.face.eye = [r(a.face.eye[0] * 1.3), r(a.face.eye[1] * 1.28)];
|
|
208
|
+
out.face.eyeX = r(Math.max(a.face.eyeX * 1.04, out.face.eye[0] + 0.06));
|
|
209
|
+
out.face.eyeY = r(a.face.eyeY + 0.04);
|
|
210
|
+
out.face.pupil = r(Math.min(a.face.pupil * 1.55, out.face.eye[0] * 0.62));
|
|
211
|
+
out.face.noseY = r(a.face.noseY + 0.1);
|
|
212
|
+
out.face.mouthY = r(Math.max(a.face.mouthY + 0.06, out.face.noseY + out.face.nose[1] + 0.17));
|
|
213
|
+
out.face.mouthW = r(Math.min(0.45, a.face.mouthW * 1.12));
|
|
214
|
+
}
|
|
215
|
+
if (st.proportions === "cartoon") {
|
|
216
|
+
// A modern TV cartoon's: about three heads tall (two and a half for children), short legs, a
|
|
217
|
+
// compact torso, noodle limbs thick enough to read at a distance, big mitten hands and shoes.
|
|
218
|
+
const young = ["baby", "child", "kid"].includes(look?.build ?? "");
|
|
219
|
+
const baby = look?.build === "baby";
|
|
220
|
+
out.body.head = r(a.body.head * (baby ? 1.08 : young ? 1.22 : 1.42));
|
|
221
|
+
out.body.hip = r(a.body.hip * (baby ? 1 : young ? 0.84 : 0.74));
|
|
222
|
+
out.body.torso = r(a.body.torso * (baby ? 1 : young ? 0.9 : 0.84));
|
|
223
|
+
out.body.neck = r(a.body.neck * (young ? 0.8 : 0.5));
|
|
224
|
+
// A softer torso: shoulders a little narrower, the waist a little fuller (a bean, not a box).
|
|
225
|
+
out.body.shoulders = r(a.body.shoulders * 0.94);
|
|
226
|
+
out.body.waist = r(a.body.waist * 1.06);
|
|
227
|
+
// Hips no wider than the waist (a little more on a woman): the body one bean down to the crotch.
|
|
228
|
+
out.body.hips = r(Math.min(a.body.hips, out.body.waist * (look?.female ? 1.12 : 1)));
|
|
229
|
+
// Short arms: hanging, the wrist comes just below the hip joint and the fingertips to the top of
|
|
230
|
+
// the thigh (not the knee).
|
|
231
|
+
out.body.arm = r((out.body.torso * 0.9 + out.body.hip * 0.08) / 0.98);
|
|
232
|
+
// Noodle limbs: arms no thicker than the painted look's, legs a little fuller at the thigh,
|
|
233
|
+
// tapering to a slim ankle.
|
|
234
|
+
out.limbs.arm = [r(a.limbs.arm[0]), r(a.limbs.arm[1] * 0.95)];
|
|
235
|
+
out.limbs.leg = [r(a.limbs.leg[0] * 1.1), r(a.limbs.leg[1] * 0.95)];
|
|
236
|
+
out.limbs.hand = r(a.limbs.hand * 1.25);
|
|
237
|
+
out.limbs.shoe = r(a.limbs.shoe * 1.35);
|
|
238
|
+
// (Thick thighs must still fit the hips.)
|
|
239
|
+
const fit = (out.body.hips * 0.98) / THIGH_R;
|
|
240
|
+
if (out.limbs.leg[0] > fit)
|
|
241
|
+
out.limbs.leg = [r(fit), r(Math.min(out.limbs.leg[1], fit))];
|
|
242
|
+
}
|
|
243
|
+
if (st.eyes === "round") {
|
|
244
|
+
// Big round eyes, nearly touching, a little above the middle of the face; a small dot pupil;
|
|
245
|
+
// the nose and the mouth below them (the face's rules — `checkAnatomy` — still hold).
|
|
246
|
+
const e = r(a.face.eye[1] * 1.22);
|
|
247
|
+
out.face.eye = [e, e];
|
|
248
|
+
out.face.eyeX = r(e + 0.045);
|
|
249
|
+
out.face.eyeY = r(a.face.eyeY + 0.02);
|
|
250
|
+
out.face.pupil = r(Math.max(0.045, e * 0.2));
|
|
251
|
+
out.face.brow = r(a.face.brow * 0.7);
|
|
252
|
+
out.face.noseY = r(Math.max(a.face.noseY + 0.06, out.face.eyeY + e * 0.95));
|
|
253
|
+
out.face.mouthY = r(Math.max(a.face.mouthY + 0.04, out.face.noseY + out.face.nose[1] + 0.2));
|
|
254
|
+
}
|
|
255
|
+
if (st.proportions === "tv") {
|
|
256
|
+
// A modern TV cartoon's: a natural figure — about five heads tall (four for a teen, three and a
|
|
257
|
+
// half for a child) — narrow hips the torso goes straight down into, long slim legs filling the
|
|
258
|
+
// hips and tapering to a slim ankle, thin arms with a gentle taper hanging to mid-thigh, simple
|
|
259
|
+
// hands about a wrist and a fifth across, shoes a little bigger than the foot.
|
|
260
|
+
const HEAD = { baby: 1, child: 0.88, kid: 0.92, teen: 1.02, woman: 1.02, man: 1.02, big: 1.08, elder: 0.98 };
|
|
261
|
+
out.body.head = r(a.body.head * HEAD[look?.build ?? "man"]);
|
|
262
|
+
out.body.shoulders = r(a.body.shoulders * 0.9);
|
|
263
|
+
out.body.waist = r(a.body.waist * 0.86);
|
|
264
|
+
out.body.hips = r(Math.min(a.body.hips, out.body.waist * (look?.female ? 0.92 : 0.96)));
|
|
265
|
+
out.limbs.arm = [r(a.limbs.arm[0] * 0.8), r(a.limbs.arm[1] * 0.72)];
|
|
266
|
+
out.limbs.hand = r(out.limbs.arm[1] * 1.5);
|
|
267
|
+
// Hanging, the fingertips at mid-thigh.
|
|
268
|
+
out.body.arm = r(out.body.torso * 0.9 + out.body.hip * 0.16);
|
|
269
|
+
// The thighs side by side as wide as the hips as drawn (`model`: the hip joints 0.44 of the hips
|
|
270
|
+
// out, the thigh `THIGH_R` of the leg at the hip): the trousers go on down from the torso's edge.
|
|
271
|
+
const f = look?.female;
|
|
272
|
+
const hipHalf = Math.max(out.body.waist * (f ? 0.86 : 1) * 0.98, out.body.hips * (f ? 1.04 : 1) * 0.95);
|
|
273
|
+
const leg = r(Math.min(out.body.hips / THIGH_R, (hipHalf + 0.5 - out.body.hips * 0.44) / THIGH_R));
|
|
274
|
+
out.limbs.leg = [leg, r(Math.min(a.limbs.leg[1], leg * 0.62))];
|
|
275
|
+
out.limbs.calf = r(Math.min(a.limbs.calf, 1.1));
|
|
276
|
+
out.limbs.shoe = r(a.limbs.shoe * 1.1);
|
|
277
|
+
}
|
|
278
|
+
if (st.eyes === "wide") {
|
|
279
|
+
out.face.eye = [r(a.face.eye[0] * 1.25), r(a.face.eye[1] * 1.2)];
|
|
280
|
+
out.face.eyeX = r(Math.max(a.face.eyeX * 0.93, out.face.eye[0] + 0.045));
|
|
281
|
+
out.face.pupil = r(a.face.pupil * 0.6);
|
|
282
|
+
}
|
|
283
|
+
if (st.proportions === "bigHead")
|
|
284
|
+
out.body.head = r(a.body.head * (["baby", "child", "kid"].includes(look?.build ?? "") ? 1.12 : 1.06));
|
|
285
|
+
return out;
|
|
286
|
+
}
|
|
287
|
+
function model(look, mirror = false, a = cartoonAnatomy(look), style = styleOf(undefined)) {
|
|
288
|
+
const shade = style.shade;
|
|
289
|
+
const M = mirror ? -1 : 1;
|
|
290
|
+
const A = a.body, F = a.face;
|
|
291
|
+
const b = { L: A.hip, T: A.torso, n: A.neck, u: A.head, S: A.shoulders, W: A.waist, H: A.hips, D: A.depth, arm: a.limbs.arm, leg: a.limbs.leg, hand: a.limbs.hand, shoe: a.limbs.shoe, eye: F.eye[1] / 0.235 };
|
|
292
|
+
const heavy = look.heavy ?? 0;
|
|
293
|
+
const u = b.u;
|
|
294
|
+
const L = b.L, T = b.T;
|
|
295
|
+
const W = b.W * (look.female ? 0.86 : 1) + heavy * b.W * 0.35;
|
|
296
|
+
const H = b.H * (look.female ? 1.04 : 1) + heavy * b.H * 0.2;
|
|
297
|
+
const hip = -L, waist = -L - T * 0.1, torsoTop = -L - T, shoulder = torsoTop + T * 0.1;
|
|
298
|
+
const neckTop = torsoTop - b.n;
|
|
299
|
+
const hy = neckTop - u * 0.92;
|
|
300
|
+
const eye = { x: u * F.eyeX, rx: u * F.eye[0], ry: u * F.eye[1] };
|
|
301
|
+
const ey = hy + u * F.eyeY;
|
|
302
|
+
const y = {
|
|
303
|
+
hip, waist, shoulder, torsoTop, neckTop, head: hy, eye: ey, brow: ey - eye.ry - u * F.brow,
|
|
304
|
+
nose: hy + u * F.noseY, mouth: hy + u * F.mouthY, ear: hy + u * F.earY, top: hy - u * 1.14,
|
|
305
|
+
};
|
|
306
|
+
// Head: a cranium and a face (jaw), blended, and a nose (its ellipsoids kept: the head's occluder).
|
|
307
|
+
const headParts = [];
|
|
308
|
+
const hell = (c, rad) => (headParts.push([c, rad]), ellipsoid(c, rad));
|
|
309
|
+
const cranium = hell([0, hy - u * 0.12, -u * 0.08], [u, u * 1.02, u]);
|
|
310
|
+
const jaw = look.jaw ?? "round";
|
|
311
|
+
const face = jaw === "square" ? blend(u * 0.18, hell([0, hy + u * 0.42, u * 0.1], [u * 0.86, u * 0.7, u * 0.8]), hell([-u * 0.46, hy + u * 0.74, u * 0.02], [u * 0.36, u * 0.32, u * 0.5]), hell([u * 0.46, hy + u * 0.74, u * 0.02], [u * 0.36, u * 0.32, u * 0.5]), hell([0, hy + u * 0.92, u * 0.32], [u * 0.36, u * 0.24, u * 0.36]))
|
|
312
|
+
: jaw === "pointy" ? blend(u * 0.2, hell([0, hy + u * 0.42, u * 0.1], [u * 0.74, u * 0.7, u * 0.76]), hell([0, hy + u * 0.9, u * 0.3], [u * 0.24, u * 0.24, u * 0.3]))
|
|
313
|
+
: jaw === "chubby" ? hell([0, hy + u * 0.42, u * 0.1], [u * 0.94, u * 0.72, u * 0.86])
|
|
314
|
+
: hell([0, hy + u * 0.42, u * 0.1], [u * 0.8, u * 0.72, u * 0.8]);
|
|
315
|
+
const skullFace = blend(u * 0.3, cranium, face);
|
|
316
|
+
// A nose with volume (it catches the light and casts a shadow), on the face.
|
|
317
|
+
const zn = surfaceZ(skullFace, 0, y.nose) ?? u * 0.8;
|
|
318
|
+
const noseKind = look.nose ?? "button";
|
|
319
|
+
// (A long nose has a bridge up from its tip.)
|
|
320
|
+
const ns = [u * F.nose[0], u * F.nose[1], u * F.nose[2]];
|
|
321
|
+
// (Its ellipsoids kept too, `noseParts`: the head's occluder and keep-outs reach the tip of the nose —
|
|
322
|
+
// an arm ahead of the face in profile clears the nose, and one behind the head stays behind it.)
|
|
323
|
+
const noseParts = [];
|
|
324
|
+
const nell = (c, rad) => (noseParts.push([c, rad]), ellipsoid(c, rad));
|
|
325
|
+
const tip = nell([0, y.nose, zn + u * F.noseOut], ns);
|
|
326
|
+
const nose = noseKind === "long" ? blend(u * 0.06, nell([0, y.nose - u * 0.16, zn], [ns[0] * 0.78, u * 0.26, ns[2]]), tip) : tip;
|
|
327
|
+
const head = blend(u * 0.05, skullFace, nose);
|
|
328
|
+
const ears = union(ellipsoid([-u * 0.98, y.ear, -u * 0.06], [u * 0.17, u * 0.3, u * 0.23]), ellipsoid([u * 0.98, y.ear, -u * 0.06], [u * 0.17, u * 0.3, u * 0.23]));
|
|
329
|
+
// Hair.
|
|
330
|
+
const skull = skullFace;
|
|
331
|
+
const shell = (g) => grow(skull, g);
|
|
332
|
+
/** Hairline: `front` at the middle of the forehead, lower by `temple` at the sides, `back` at the nape. */
|
|
333
|
+
const line = (front, temple, back) => (x, z) => {
|
|
334
|
+
const f = front + temple * Math.min(1, (x / u) ** 2);
|
|
335
|
+
const s = smoothstep(u * 0.35, -u * 0.45, z);
|
|
336
|
+
return f * (1 - s) + back * s;
|
|
337
|
+
};
|
|
338
|
+
const accent = (inside) => (p) => (inside(p[0], p[1], p[2]) < 0 ? "accent" : "hair");
|
|
339
|
+
let hair;
|
|
340
|
+
let hairColor = () => "hair";
|
|
341
|
+
let tail;
|
|
342
|
+
switch (look.hair) {
|
|
343
|
+
case "bowl": {
|
|
344
|
+
// A bowl cut: a round cap cut straight at the brows, a little lower at the back.
|
|
345
|
+
const cap = blend(u * 0.2, shell(u * 0.12), ellipsoid([0, hy - u * 0.5, -u * 0.05], [u * 1.16, u * 0.78, u * 1.14]));
|
|
346
|
+
hair = above(cap, (x, z) => y.brow - u * 0.1 + u * 0.85 * smoothstep(u * 0.45, -u * 0.5, z));
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
case "sidePart": {
|
|
350
|
+
const quiff = ellipsoid([-M * u * 0.22, hy - u * 0.92, u * 0.22], [u * 0.8, u * 0.34, u * 0.74]);
|
|
351
|
+
hair = above(blend(u * 0.15, shell(u * 0.1), quiff), line(hy - u * 0.56, u * 0.4, hy + u * 0.45));
|
|
352
|
+
break;
|
|
353
|
+
}
|
|
354
|
+
case "afro":
|
|
355
|
+
hair = above(union(shell(u * 0.06), bumpy(sphere([0, hy - u * 0.4, -u * 0.15], u * 1.42), u * 0.05, u * 0.34)), line(hy - u * 0.52, u * 0.55, hy + u * 0.55));
|
|
356
|
+
break;
|
|
357
|
+
case "puffs": {
|
|
358
|
+
const puff = (s) => bumpy(sphere([s * u * 0.82, hy - u * 0.92, -u * 0.12], u * 0.5), u * 0.04, u * 0.25);
|
|
359
|
+
const ties = union(ellipsoid([-u * 0.6, hy - u * 0.62, -u * 0.05], [u * 0.2, u * 0.12, u * 0.22]), ellipsoid([u * 0.6, hy - u * 0.62, -u * 0.05], [u * 0.2, u * 0.12, u * 0.22]));
|
|
360
|
+
hair = union(above(shell(u * 0.07), line(hy - u * 0.56, u * 0.4, hy + u * 0.42)), puff(-1), puff(1), ties);
|
|
361
|
+
hairColor = accent(ties);
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
case "ponytail": {
|
|
365
|
+
const fringe = above(intersect(ellipsoid([u * 0.1, hy - u * 0.62, u * 0.5], [u * 0.88, u * 0.4, u * 0.56]), shell(u * 0.14)), () => y.brow + u * 0.04);
|
|
366
|
+
const fall = blend(u * 0.2, capsule([0, hy - u * 0.62, -u * 1.02], [0, hy + u * 0.2, -u * 1.5], u * 0.3, u * 0.24), capsule([0, hy + u * 0.2, -u * 1.5], [0, hy + u * 1.35, -u * 1.38], u * 0.24, u * 0.08));
|
|
367
|
+
const tie = ellipsoid([0, hy - u * 0.62, -u * 1.0], [u * 0.3, u * 0.18, u * 0.3]);
|
|
368
|
+
hair = union(above(shell(u * 0.08), line(hy - u * 0.56, u * 0.42, hy + u * 0.45)), fringe);
|
|
369
|
+
tail = { sdf: union(fall, tie), color: accent(tie), pivot: [0, hy - u * 0.62, -u * 1.0], end: [0, hy + u * 1.35, -u * 1.38] };
|
|
370
|
+
break;
|
|
371
|
+
}
|
|
372
|
+
case "long": {
|
|
373
|
+
const fall = intersect(taper(hy - u * 0.4, hy + u * 2.1, [u * 1.1, u * 1.05], [u * 1.04, u * 0.6], -u * 0.32), (x, yy, z) => z - u * 0.3);
|
|
374
|
+
const fringe = above(intersect(ellipsoid([0, hy - u * 0.62, u * 0.5], [u * 0.9, u * 0.4, u * 0.56]), shell(u * 0.14)), () => y.brow + u * 0.02);
|
|
375
|
+
hair = union(above(shell(u * 0.1), line(hy - u * 0.56, u * 0.4, hy + u * 0.6)), fall, fringe);
|
|
376
|
+
break;
|
|
377
|
+
}
|
|
378
|
+
case "perm":
|
|
379
|
+
hair = above(bumpy(ellipsoid([0, hy - u * 0.22, -u * 0.12], [u * 1.48, u * 1.36, u * 1.36]), u * 0.07, u * 0.3), line(hy - u * 0.55, u * 1.15, hy + u * 1.0));
|
|
380
|
+
break;
|
|
381
|
+
case "mullet": {
|
|
382
|
+
const back = ellipsoid([0, hy + u * 0.7, -u * 0.72], [u * 0.78, u * 1.0, u * 0.42]);
|
|
383
|
+
hair = union(above(shell(u * 0.11), line(hy - u * 0.6, u * 0.4, hy + u * 0.5)), back);
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
case "buzz":
|
|
387
|
+
hair = above(shell(u * 0.045), line(hy - u * 0.62, u * 0.32, hy + u * 0.5));
|
|
388
|
+
if (look.hairLine)
|
|
389
|
+
hairColor = (p) => (M * p[0] < -u * 0.5 && Math.abs(p[1] - (hy - u * 0.42 + (p[2] / u) * u * 0.12)) < u * 0.035 ? "skin" : "hair");
|
|
390
|
+
break;
|
|
391
|
+
case "receding": {
|
|
392
|
+
// Receding at the temples (an M-shaped hairline), a little tuft left in front.
|
|
393
|
+
const tuft = ellipsoid([u * 0.05, hy - u * 0.82, u * 0.55], [u * 0.22, u * 0.14, u * 0.2]);
|
|
394
|
+
hair = union(above(shell(u * 0.07), (x, z) => {
|
|
395
|
+
const f = hy - u * 0.5 - u * 0.32 * Math.min(1, (x / (u * 0.55)) ** 2) * smoothstep(-u * 0.1, u * 0.5, z);
|
|
396
|
+
const s = smoothstep(u * 0.2, -u * 0.5, z);
|
|
397
|
+
return f * (1 - s) + (hy + u * 0.45) * s;
|
|
398
|
+
}), intersect(tuft, shell(u * 0.12)));
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
case "slick":
|
|
402
|
+
// Combed straight back with gel: smooth, a little volume on top.
|
|
403
|
+
hair = above(blend(u * 0.2, shell(u * 0.08), intersect(ellipsoid([0, hy - u * 0.72, -u * 0.12], [u * 1.04, u * 0.5, u * 1.08]), shell(u * 0.2))), line(hy - u * 0.62, u * 0.32, hy + u * 0.45));
|
|
404
|
+
break;
|
|
405
|
+
case "bob": {
|
|
406
|
+
// A bob: straight to the jaw, a straight fringe.
|
|
407
|
+
const fall = intersect(taper(hy - u * 0.4, hy + u * 0.92, [u * 1.12, u * 1.08], [u * 1.06, u * 0.86], -u * 0.2), (x, yy, z) => z - u * 0.32);
|
|
408
|
+
const fringe = above(intersect(ellipsoid([0, hy - u * 0.62, u * 0.5], [u * 0.92, u * 0.42, u * 0.58]), shell(u * 0.15)), () => y.brow + u * 0.03);
|
|
409
|
+
hair = union(above(blend(u * 0.2, shell(u * 0.12), ellipsoid([0, hy - u * 0.62, -u * 0.05], [u * 1.2, u * 0.7, u * 1.18])), line(hy - u * 0.56, u * 0.4, hy + u * 0.8)), fall, fringe);
|
|
410
|
+
break;
|
|
411
|
+
}
|
|
412
|
+
case "curtains": {
|
|
413
|
+
// Parted in the middle, two curtains over the forehead (a curtained haircut).
|
|
414
|
+
const curtain = (s) => intersect(ellipsoid([s * u * 0.46, hy - u * 0.5, u * 0.55], [u * 0.5, u * 0.44, u * 0.42]), shell(u * 0.14), (x, yy) => yy - (y.eye - m_ry(u)), (x) => -s * x + u * 0.03);
|
|
415
|
+
hair = union(above(blend(u * 0.2, shell(u * 0.1), ellipsoid([0, hy - u * 0.6, -u * 0.1], [u * 1.12, u * 0.6, u * 1.12])), line(hy - u * 0.62, u * 0.3, hy + u * 0.62)), curtain(-1), curtain(1));
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
case "spiky": {
|
|
419
|
+
// Spiked up with gel.
|
|
420
|
+
const spikes = [];
|
|
421
|
+
for (let i = 0; i < 9; i++) {
|
|
422
|
+
const a = ((-70 + (i * 140) / 8) * Math.PI) / 180, b2 = i % 3;
|
|
423
|
+
const base = [Math.sin(a) * u * 0.62, hy - u * 0.82, (b2 - 1) * u * 0.38];
|
|
424
|
+
spikes.push(capsule(base, [base[0] * 1.35, hy - u * (1.42 + (i % 2) * 0.1), base[2] * 1.3 + u * 0.05], u * 0.17, u * 0.02));
|
|
425
|
+
}
|
|
426
|
+
hair = blend(u * 0.04, above(shell(u * 0.08), line(hy - u * 0.62, u * 0.35, hy + u * 0.45)), ...spikes);
|
|
427
|
+
break;
|
|
428
|
+
}
|
|
429
|
+
case "bald":
|
|
430
|
+
hair = intersect(shell(u * 0.08), (x, yy, z) => hy - u * 0.32 - yy, (x, yy) => yy - (hy + u * 0.42), (x, yy, z) => z - u * 0.05);
|
|
431
|
+
break;
|
|
432
|
+
case "bun": {
|
|
433
|
+
const bun = sphere([0, hy - u * 0.98, -u * 0.55], u * 0.42);
|
|
434
|
+
hair = union(above(shell(u * 0.06), line(hy - u * 0.6, u * 0.4, hy + u * 0.42)), bun);
|
|
435
|
+
break;
|
|
436
|
+
}
|
|
437
|
+
case "braids": {
|
|
438
|
+
const braids = [];
|
|
439
|
+
for (let i = 0; i < 9; i++) {
|
|
440
|
+
const a = ((70 + (i * 220) / 8) * Math.PI) / 180;
|
|
441
|
+
braids.push(capsule([Math.sin(a) * u * 0.92, hy - u * 0.32, Math.cos(a) * u * 0.92], [Math.sin(a) * u * 1.08, hy + u * 1.55, Math.cos(a) * u * 0.62 - u * 0.25], u * 0.09));
|
|
442
|
+
}
|
|
443
|
+
hair = union(above(shell(u * 0.06), line(hy - u * 0.58, u * 0.42, hy + u * 0.42)), ...braids);
|
|
444
|
+
hairColor = (p) => (p[1] > hy + u * 1.3 ? "accent" : "hair");
|
|
445
|
+
break;
|
|
446
|
+
}
|
|
447
|
+
case "rollers": {
|
|
448
|
+
const rollers = [-0.5, 0, 0.5].map((x) => capsule([x * u - u * 0.18, hy - u * 1.12, -u * 0.15], [x * u + u * 0.18, hy - u * 1.12, -u * 0.15], u * 0.17));
|
|
449
|
+
const sides = [-1, 1].map((s) => capsule([s * u * 0.92, hy - u * 0.62, -u * 0.25], [s * u * 0.92, hy - u * 0.62, u * 0.15], u * 0.15));
|
|
450
|
+
const all = union(...rollers, ...sides);
|
|
451
|
+
hair = union(above(shell(u * 0.06), line(hy - u * 0.58, u * 0.45, hy + u * 0.45)), all);
|
|
452
|
+
hairColor = accent(grow(all, 0.5));
|
|
453
|
+
break;
|
|
454
|
+
}
|
|
455
|
+
case "tuft": {
|
|
456
|
+
const tufts = [-0.35, 0, 0.32].map((x, i) => capsule([x * u, hy - u * 0.95, u * 0.2], [x * u * 1.4 + u * 0.08, hy - u * (1.48 + (i % 2) * 0.1), u * 0.38], u * 0.16, u * 0.03));
|
|
457
|
+
hair = blend(u * 0.05, above(shell(u * 0.07), line(hy - u * 0.6, u * 0.4, hy + u * 0.45)), ...tufts);
|
|
458
|
+
break;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
// Below the brows, hair stays beside and behind the face (a perm frames it, never covers an eye).
|
|
462
|
+
const styled = hair;
|
|
463
|
+
hair = (x, yy, z) => Math.max(styled(x, yy, z), yy > y.brow ? z - u * 0.3 : -Infinity);
|
|
464
|
+
const bulk = hair;
|
|
465
|
+
if (look.hat) {
|
|
466
|
+
// Hats sit on the hair: the crown over the skull, a peak (cap) or a short brim (flat cap).
|
|
467
|
+
// Hats with a brim all round (top hat, bowler, cowboy): a crown on the skull, the brim a disc.
|
|
468
|
+
// Each is made for the head as drawn (all in head radii): its bottom never below the forehead
|
|
469
|
+
// line — just above the brows, wherever the face puts them (big eyes put them higher: the hat
|
|
470
|
+
// sits higher on the head, the crown cut there) — the peak on the crown's front where it is cut.
|
|
471
|
+
const brimmed = look.hat === "top" || look.hat === "bowler" || look.hat === "cowboy";
|
|
472
|
+
const forehead = y.brow - u * 0.05;
|
|
473
|
+
// Each hat's lowest edge for the default face (the crown's, the brim's) and how far the
|
|
474
|
+
// forehead line lifts the hat.
|
|
475
|
+
const bottom0 = hy - u * (look.hat === "flatCap" ? 0.26 : brimmed ? 0.55 : 0.32);
|
|
476
|
+
const dy = Math.min(0, forehead - bottom0);
|
|
477
|
+
const crownTop = hy - u * (look.hat === "top" ? 2.0 : look.hat === "cowboy" ? 1.55 : 1.35) + dy;
|
|
478
|
+
const crown = look.hat === "flatCap"
|
|
479
|
+
? ellipsoid([0, hy - u * 0.72 + dy, u * 0.05], [u * 1.16, u * 0.46, u * 1.22])
|
|
480
|
+
: look.hat === "top"
|
|
481
|
+
? box([0, (crownTop + hy - u * 0.62 + dy) / 2, -u * 0.02], [u * 0.78, (hy - u * 0.62 - crownTop + dy) / 2, u * 0.78], u * 0.12)
|
|
482
|
+
: look.hat === "bowler"
|
|
483
|
+
? intersect(ellipsoid([0, hy - u * 0.75 + dy, -u * 0.02], [u * 1.02, u * 0.62, u * 1.02]), (x, yy) => yy - (hy - u * 0.58 + dy))
|
|
484
|
+
: look.hat === "cowboy"
|
|
485
|
+
? intersect(ellipsoid([0, hy - u * 0.9 + dy, -u * 0.02], [u * 0.92, u * 0.7, u * 0.86]), (x, yy) => yy - (hy - u * 0.6 + dy))
|
|
486
|
+
: intersect(grow(skull, u * 0.16), (x, yy) => yy - (hy - u * 0.32 + dy));
|
|
487
|
+
const back = look.hat === "capBack" ? -1 : 1;
|
|
488
|
+
const brimR = look.hat === "cowboy" ? u * 1.75 : look.hat === "top" ? u * 1.25 : u * 1.2;
|
|
489
|
+
// How far the crown's front (back, for a cap turned back) stands out at a height, from the
|
|
490
|
+
// default cap's at its peak: the peak moves with it.
|
|
491
|
+
const standOut = (f, yy) => back * (surfaceZ((x, y2, z) => f(x, y2, back * z), 0, yy) ?? u);
|
|
492
|
+
const peakY = hy - u * 0.4 - (back < 0 ? u * 0.08 : 0) + dy;
|
|
493
|
+
const out = look.hat === "cap" || look.hat === "capBack" ? standOut(crown, peakY) - standOut(intersect(grow(skull, u * 0.16), (x, yy) => yy - (hy - u * 0.32)), peakY - dy) : 0;
|
|
494
|
+
const peak = brimmed
|
|
495
|
+
? (x, yy, z) => {
|
|
496
|
+
// A cowboy brim curls up at the sides.
|
|
497
|
+
const rr = Math.hypot(x, z + u * 0.02), curl = look.hat === "cowboy" ? Math.max(0, Math.abs(x) - u * 0.9) * 0.45 : 0;
|
|
498
|
+
return Math.max(rr - brimR, Math.abs(yy - (hy - u * 0.6 + dy - curl)) - u * 0.05);
|
|
499
|
+
}
|
|
500
|
+
: look.hat === "flatCap"
|
|
501
|
+
? intersect(ellipsoid([0, hy - u * 0.5 + dy, u * 0.78], [u * 0.75, u * 0.08, u * 0.42]), (x, yy, z) => u * 0.6 - z)
|
|
502
|
+
// (The peak cut where it would enter the head: in front of it, or behind it turned back.)
|
|
503
|
+
: intersect(ellipsoid([0, peakY, back * u * 0.95 + out], [u * 0.66, u * 0.065, u * 0.62]), (x, yy, z) => u * 0.55 + back * out - back * z);
|
|
504
|
+
const hat = union(crown, peak);
|
|
505
|
+
const under = hair, prevColor = hairColor;
|
|
506
|
+
// A hat sits on the hair: nothing of an ordinary haircut shows above its band, outside the crown
|
|
507
|
+
// (big hair — an afro, puffs, a perm, long hair, braids — keeps its volume round the hat).
|
|
508
|
+
const big = ["perm", "afro", "puffs", "long", "braids"].includes(look.hair);
|
|
509
|
+
const bandY = (brimmed ? hy - u * 0.6 : look.hat === "flatCap" ? hy - u * 0.5 : hy - u * 0.32) + dy;
|
|
510
|
+
hair = union(intersect(under, (x, yy, z) => -crown(x, yy, z) + 0.5, ...(big ? [] : [((x, yy) => bandY - yy)])), hat);
|
|
511
|
+
hairColor = (p) => (hat(p[0], p[1], p[2]) < 0.6 ? (peak(p[0], p[1], p[2]) < 0.6 ? "hatDark" : "hat") : prevColor(p));
|
|
512
|
+
}
|
|
513
|
+
if (look.veil) {
|
|
514
|
+
// A veil: a thin sheet from the top of the head, round the back, down past the shoulders.
|
|
515
|
+
// Over the head it is a dome round the cranium as drawn (its section at each height, over the
|
|
516
|
+
// hair), below its widest it flares out down the back.
|
|
517
|
+
const [cc, cr] = headParts[0];
|
|
518
|
+
const veilShell = (x, yy, z) => {
|
|
519
|
+
const top = hy - u * 0.95, bottom = shoulder + T * 0.35, crown = cr[1] + u * 0.12;
|
|
520
|
+
if (yy < cc[1] - crown || yy > bottom)
|
|
521
|
+
return 1;
|
|
522
|
+
const t = Math.max(0, (yy - top) / (bottom - top));
|
|
523
|
+
const dome = (yy < cc[1] ? cr[0] * Math.sqrt(Math.max(0, 1 - ((yy - cc[1]) / crown) ** 2)) : cr[0] + (yy - cc[1]) * 0.6) + u * 0.12;
|
|
524
|
+
const rad = Math.min(dome, u * (1.08 + t * 0.55)), cz = -u * (0.1 + t * 0.35);
|
|
525
|
+
const d = Math.hypot(x, z - cz) - rad;
|
|
526
|
+
return Math.max(Math.abs(d) - u * 0.03, z - (u * 0.35 - t * u * 0.6));
|
|
527
|
+
};
|
|
528
|
+
const under = hair, prev = hairColor;
|
|
529
|
+
hair = union(under, veilShell);
|
|
530
|
+
hairColor = (p) => (veilShell(p[0], p[1], p[2]) < 0.8 ? "veil" : prev(p));
|
|
531
|
+
}
|
|
532
|
+
if (look.earItem) {
|
|
533
|
+
// Resting in the fold above the near ear; its tip (a filter, a sharpened point) towards the face.
|
|
534
|
+
const pencil = look.earItem === "pencil";
|
|
535
|
+
const a = [-M * u * 1.04, y.ear - u * 0.26, -u * 0.48], c = [-M * u * 1.0, y.ear - u * 0.38, u * (pencil ? 0.42 : 0.32)];
|
|
536
|
+
const item = capsule(a, c, u * (pencil ? 0.055 : 0.05), u * (pencil ? 0.02 : 0.05));
|
|
537
|
+
const styled2 = hair, prev = hairColor;
|
|
538
|
+
hair = union(styled2, item);
|
|
539
|
+
hairColor = (p) => (item(p[0], p[1], p[2]) < 0.6 ? (p[2] > u * (pencil ? 0.28 : 0.12) ? "earTip" : "earItem") : prev(p));
|
|
540
|
+
}
|
|
541
|
+
// Neck and torso (the pelvis is part of it: the trousers' top).
|
|
542
|
+
const neckR = u * (look.build === "big" ? 0.5 : look.build === "man" ? 0.36 : 0.26) * (1 + heavy * 0.2) * (style.proportions === "bigHead" ? 0.85 : 1);
|
|
543
|
+
// (TV proportions: its base in the middle of the shoulders, inside the torso's top from every
|
|
544
|
+
// side — set back, its end showed as a square stub behind the chest in profile.)
|
|
545
|
+
const neckZ = style.proportions === "tv" ? 0 : -b.D * 0.15;
|
|
546
|
+
// (… and straight up under the middle of the skull: leaning back to it, the head read as set back.)
|
|
547
|
+
const neck = capsule([0, torsoTop + 10, neckZ], [0, hy + u * 0.55, style.proportions === "tv" ? 0 : -u * 0.12], neckR);
|
|
548
|
+
// The shoulders: round caps the arms come out of (a blouse's puffed), or, with tube arms, flush
|
|
549
|
+
// with the arm — no ball shoulders.
|
|
550
|
+
const sh = style.limbs === "tube" ? b.arm[0] * 0.5 : look.top === "blouse" ? b.arm[0] * 1.05 : b.arm[0] * 0.75;
|
|
551
|
+
// The chest's half width (of the shoulders'): TV proportions taper from the shoulders down, so an
|
|
552
|
+
// arm hangs beside the chest, not into it.
|
|
553
|
+
const chest = style.proportions === "tv" ? 0.85 : 1;
|
|
554
|
+
// The belly's height and half height (of the torso's): TV proportions keep it above the hips' cut,
|
|
555
|
+
// where the occluder, which has no cut, would hide the top of the legs under the drawing's edge.
|
|
556
|
+
const belly = style.proportions === "tv" ? [0.37, 0.35] : [0.32, 0.38];
|
|
557
|
+
// The hips' centre forward, half depth (of the body's depth) and half height (of the torso's): TV
|
|
558
|
+
// proportions make a heavy body's hips taller, so its side runs straight from the belly into the
|
|
559
|
+
// trousers (no pinch above the hips), and the hips no deeper than the thighs side by side (no seat
|
|
560
|
+
// sticking out behind the trousers in profile).
|
|
561
|
+
const hipsZ = style.proportions === "tv" ? [0, Math.min(0.92, ((Math.max(W * 0.98, H * 0.95) * 0.5 + 0.5) * 1.12) / b.D), 0.2 + heavy * 0.2] : [0, 0.92, 0.2];
|
|
562
|
+
const torso = blend(T * 0.12, ellipsoid([0, -L - T * 0.68, 0], [b.S * chest, T * 0.36, b.D]), ellipsoid([0, -L - T * belly[0], heavy * b.D * 0.55], [W, T * belly[1], b.D * (1 + heavy * 0.6)]), ellipsoid([0, -L + T * 0.02, b.D * hipsZ[0]], [Math.max(W * 0.98, H * 0.95), T * hipsZ[2], b.D * hipsZ[1]]), capsule([-b.S + sh * 0.6, shoulder, 0], [b.S - sh * 0.6, shoulder, 0], sh));
|
|
563
|
+
// Flat at the hip joints: the legs come out of it (no rounded "diaper" under the trousers).
|
|
564
|
+
const body = torso;
|
|
565
|
+
// (TV proportions: drawn a little lower than the hips' volume — the trousers, drawn over it and a
|
|
566
|
+
// line's half width narrower, leave its side lines showing down to where their own start.)
|
|
567
|
+
const cutY = -L + 5 + (style.proportions === "tv" ? 5 : 0);
|
|
568
|
+
const torsoCut = (x, yy, z) => Math.max(body(x, yy, z), yy - cutY);
|
|
569
|
+
// The inside of the collar behind the neck: a darker tone (TV proportions: flat, the neck's
|
|
570
|
+
// outline is the collar's line).
|
|
571
|
+
const collar = style.proportions === "tv" ? "top" : "topDark";
|
|
572
|
+
const topKey = (p) => {
|
|
573
|
+
const [x, yy, z] = p;
|
|
574
|
+
const nearNeck = Math.hypot(x, z - neckZ) < neckR + 6 && yy < torsoTop + T * 0.14;
|
|
575
|
+
switch (look.top) {
|
|
576
|
+
case "stripes":
|
|
577
|
+
if (nearNeck)
|
|
578
|
+
return collar;
|
|
579
|
+
return Math.floor((yy - torsoTop) / (T * (look.stripeWidth ?? 0.13))) % 2 ? "top2" : "top";
|
|
580
|
+
case "jersey":
|
|
581
|
+
if (nearNeck || (z > 0 && Math.abs(x) < (torsoTop + T * 0.24 - yy) * 0.5))
|
|
582
|
+
return "top2";
|
|
583
|
+
return Math.floor((x + b.S * 2) / (b.S * 0.32)) % 2 ? "top2" : "top";
|
|
584
|
+
case "polo":
|
|
585
|
+
if (z > 0 && Math.abs(x) < neckR * 1.9 && yy < torsoTop + T * 0.16)
|
|
586
|
+
return "top2";
|
|
587
|
+
return "top";
|
|
588
|
+
case "shirt":
|
|
589
|
+
if (z > 0 && Math.abs(x) < neckR * 1.8 && yy < torsoTop + T * 0.14)
|
|
590
|
+
return "top2";
|
|
591
|
+
// The breast pocket is on the wearer's left.
|
|
592
|
+
if (z > 0 && M * x > b.S * 0.22 && M * x < b.S * 0.62 && yy > -L - T * 0.76 && yy < -L - T * 0.56)
|
|
593
|
+
return "pocket";
|
|
594
|
+
return "top";
|
|
595
|
+
case "tank":
|
|
596
|
+
if (Math.abs(x) > b.S * 0.56 && yy < shoulder + T * 0.24)
|
|
597
|
+
return "skin";
|
|
598
|
+
if (Math.abs(x) < b.S * 0.3 && yy < torsoTop + T * 0.22 && z > 0)
|
|
599
|
+
return "skin";
|
|
600
|
+
return "top";
|
|
601
|
+
case "overalls":
|
|
602
|
+
// The trousers' waist all round under the bib (the shirt never shows below it at the sides).
|
|
603
|
+
if (yy > -L - T * (style.proportions === "tv" ? 0.3 : 0.14))
|
|
604
|
+
return "bottom";
|
|
605
|
+
if (yy > -L - T * 0.5 && (z > 0 ? Math.abs(x) < b.S * 0.56 : Math.abs(x) < b.S * 0.6))
|
|
606
|
+
return "bottom";
|
|
607
|
+
if (Math.abs(Math.abs(x) - b.S * 0.42) < b.S * 0.1)
|
|
608
|
+
return "bottom";
|
|
609
|
+
return nearNeck ? collar : "top";
|
|
610
|
+
case "blazer": {
|
|
611
|
+
// A closed suit jacket: a V opening showing the shirt, lapels along it.
|
|
612
|
+
const v = (yy - torsoTop) * 0.34 + neckR * 0.6;
|
|
613
|
+
if (z > 0 && yy < torsoTop + T * 0.55 && Math.abs(x) < v)
|
|
614
|
+
return "top";
|
|
615
|
+
if (z > 0 && yy < torsoTop + T * 0.6 && Math.abs(x) < v + b.S * 0.16)
|
|
616
|
+
return "lapel";
|
|
617
|
+
return "top2";
|
|
618
|
+
}
|
|
619
|
+
case "jacket":
|
|
620
|
+
case "cardigan":
|
|
621
|
+
return z > 0 && Math.abs(x) < b.S * 0.28 ? (nearNeck ? collar : "top") : "top2";
|
|
622
|
+
default:
|
|
623
|
+
return nearNeck ? collar : "top";
|
|
624
|
+
}
|
|
625
|
+
};
|
|
626
|
+
const period = b.S * 0.17;
|
|
627
|
+
const printed = (p, k) => {
|
|
628
|
+
if (k !== "top" || !look.pattern)
|
|
629
|
+
return k;
|
|
630
|
+
const [x, yy] = p;
|
|
631
|
+
// Vertical stripes follow the cloth around the body (the angle around it), not straight x.
|
|
632
|
+
const around = Math.atan2(x, p[2] + b.D * 0.2) * b.S * 0.9 + b.S * 4;
|
|
633
|
+
if (look.pattern === "pinstripes")
|
|
634
|
+
return Math.abs((around % period) - period / 2) < period * 0.13 ? "stripe" : k;
|
|
635
|
+
if (look.pattern === "stripes")
|
|
636
|
+
return Math.floor((yy - torsoTop) / (T * (look.stripeWidth ?? 0.13))) % 2 ? "stripe" : k;
|
|
637
|
+
if (look.pattern === "polka") {
|
|
638
|
+
// Dots on a grid around the body (every other row shifted).
|
|
639
|
+
const row = Math.floor((yy - torsoTop) / (period * 1.6)), ax = around + (row % 2) * period * 0.8;
|
|
640
|
+
const cx = (Math.floor(ax / (period * 1.6)) + 0.5) * period * 1.6, cy = torsoTop + (row + 0.5) * period * 1.6;
|
|
641
|
+
return Math.hypot(ax - cx, yy - cy) < period * 0.42 ? "stripe" : k;
|
|
642
|
+
}
|
|
643
|
+
return (Math.floor(around / period) + Math.floor((yy - torsoTop) / period)) % 2 ? "stripe" : k;
|
|
644
|
+
};
|
|
645
|
+
// Something in the pocket: the part inside is covered by the pocket.
|
|
646
|
+
const pocketTop = -L - T * 0.76, pocketX = M * b.S * 0.42;
|
|
647
|
+
const pz = look.pocketItem ? (surfaceZ(torso, pocketX, pocketTop + T * 0.06) ?? b.D) : 0;
|
|
648
|
+
const item = look.pocketItem ? box([pocketX, pocketTop + T * 0.035, pz + b.S * 0.03], [b.S * 0.15, T * 0.085, b.S * 0.045], 1.5) : undefined;
|
|
649
|
+
// A sweater over the shoulders: around the back, the sleeves knotted on the chest.
|
|
650
|
+
const sweater = look.sweater
|
|
651
|
+
? union(capsule([-b.S * 0.95, shoulder - T * 0.02, -b.D * 0.35], [b.S * 0.95, shoulder - T * 0.02, -b.D * 0.35], T * 0.1), capsule([-b.S * 0.85, shoulder, b.D * 0.2], [-b.S * 0.1, torsoTop + T * 0.32, b.D * 1.05], T * 0.065), capsule([b.S * 0.85, shoulder, b.D * 0.2], [b.S * 0.1, torsoTop + T * 0.32, b.D * 1.05], T * 0.065), sphere([0, torsoTop + T * 0.33, b.D * 1.08], T * 0.075))
|
|
652
|
+
: undefined;
|
|
653
|
+
const extras = (p, k) => {
|
|
654
|
+
const [x, yy, z] = p;
|
|
655
|
+
if (sweater && sweater(x, yy, z) < 0.8)
|
|
656
|
+
return "sweater";
|
|
657
|
+
if (look.tie && z > 0 && yy > torsoTop + T * 0.06 && yy < -L - T * 0.12) {
|
|
658
|
+
const w = yy < torsoTop + T * 0.13 ? b.S * 0.07 : b.S * (0.06 + 0.06 * Math.min(1, (yy - torsoTop) / T));
|
|
659
|
+
if (Math.abs(x) < w && k !== "top2" && k !== "lapel")
|
|
660
|
+
return "tie";
|
|
661
|
+
}
|
|
662
|
+
if (look.necklace) {
|
|
663
|
+
const d = Math.hypot(x, (z + b.D * 0.15) * 0.9);
|
|
664
|
+
if (yy < torsoTop + T * 0.2 && d > neckR + 2 && d < neckR + 7 && z > -b.D * 0.3) {
|
|
665
|
+
if (look.necklace.kind === "chain")
|
|
666
|
+
return "jewel";
|
|
667
|
+
return Math.floor(Math.atan2(x, z + b.D) * 14 + 100) % 2 ? "jewel" : k;
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
if (look.suspenders && yy > torsoTop + T * 0.04 && Math.abs(Math.abs(x) - b.S * 0.42 - (yy - torsoTop) * 0.06) < b.S * 0.075)
|
|
671
|
+
return "strap";
|
|
672
|
+
return k;
|
|
673
|
+
};
|
|
674
|
+
// A flower in the lapel (the wearer's left, over the heart).
|
|
675
|
+
const flower = (p) => look.boutonniere && p[2] > 0 && Math.hypot(M * p[0] - b.S * 0.48, p[1] - (-L - T * 0.8)) < b.S * 0.11;
|
|
676
|
+
const torsoColor = (p) => (flower(p) ? "flower" : extras(p, torsoBase(p)));
|
|
677
|
+
const torsoBase = (p) => {
|
|
678
|
+
if (item && item(p[0], p[1], p[2]) < 0.8)
|
|
679
|
+
return p[1] > pocketTop ? "pocket" : p[1] < pocketTop - T * 0.032 ? "band" : "item";
|
|
680
|
+
if (look.apron && p[2] > 0 && Math.abs(p[0]) < b.S * 0.72 && p[1] > torsoTop + T * 0.28)
|
|
681
|
+
return "apron";
|
|
682
|
+
if (look.top !== "dress" && p[1] > waist + heavy * T * 0.16)
|
|
683
|
+
return "bottom";
|
|
684
|
+
return printed(p, topKey(p));
|
|
685
|
+
};
|
|
686
|
+
// Skirts and dresses (on the hips).
|
|
687
|
+
let skirt, skirtColor;
|
|
688
|
+
const hem = look.bottom === "longSkirt" ? -L * 0.16 : -L * 0.5;
|
|
689
|
+
if (look.bottom === "skirt" || look.bottom === "longSkirt") {
|
|
690
|
+
skirt = taper(waist - 4, hem, [Math.max(W, H) * 1.08, b.D * 1.06], [H * 1.5 + 10, b.D * 1.5], heavy * b.D * 0.3);
|
|
691
|
+
skirtColor = () => (look.top === "dress" ? "top" : "bottom");
|
|
692
|
+
}
|
|
693
|
+
if (look.apron) {
|
|
694
|
+
const panel = intersect(taper(waist - 2, -L * 0.42, [W * 1.08, b.D * 1.06 + heavy * 6], [H * 1.2, b.D * 1.1], heavy * b.D * 0.4), (x, yy, z) => -z + 2);
|
|
695
|
+
skirt = skirt ? union(skirt, panel) : panel;
|
|
696
|
+
const prev = skirtColor;
|
|
697
|
+
skirtColor = (p) => (p[2] > 4 ? "apron" : prev ? prev(p) : "bottom");
|
|
698
|
+
}
|
|
699
|
+
// Face colours: a rosy nose and cheeks, a shadow of beard.
|
|
700
|
+
const headColor = (p) => {
|
|
701
|
+
const [x, yy, z] = p;
|
|
702
|
+
if (look.flushed && (nose(x, yy, z) < u * 0.02 || (z > 0 && Math.hypot(Math.abs(x) - u * 0.48, yy - (y.eye + u * 0.42)) < u * 0.17)))
|
|
703
|
+
return "flush";
|
|
704
|
+
// Stubble: the chin and jaw below the mouth and the upper lip — not the cheeks.
|
|
705
|
+
if (look.stubble && z > -u * 0.1 && nose(x, yy, z) > u * 0.02 && (yy > y.mouth + u * 0.1 || (yy > y.nose + u * 0.16 && yy < y.mouth - u * 0.06 && Math.abs(x) < u * 0.34)))
|
|
706
|
+
return "stubble";
|
|
707
|
+
return "skin";
|
|
708
|
+
};
|
|
709
|
+
return { shade, sh, chest, belly, hipsZ, hipHalf: Math.max(W * 0.98, H * 0.95), style, b, y, head, headParts, noseParts, headColor, ears, nose, hair, bulk, hairColor, tail, torsoItem: item, bouncy: ["perm", "afro", "puffs", "long", "braids"].includes(look.hair), veil: !!look.veil, neck, neckR, torso: union(torsoCut, ...(item ? [item] : []), ...(sweater ? [sweater] : [])), torsoColor, skirt, skirtColor, eye, mouthW: Math.min(u * F.mouthW, frontHalf(head, y.mouth, u) * 0.82), mouthMargin: F.mouthMargin, pupil: F.pupil, chinRoom: chinBottom(head, y.mouth, u) - y.mouth - u * 0.1 };
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* The torso as a hand keeps out of it (body space, at rest): an ellipsoid over the trunk from the
|
|
713
|
+
* chest to the hips, as wide and deep as the torso is drawn below the shoulders (a belly, wide hips,
|
|
714
|
+
* a sweater, something in the pocket), grown by `margin`.
|
|
715
|
+
*/
|
|
716
|
+
function torsoKeepOut(m, margin) {
|
|
717
|
+
const { b } = m;
|
|
718
|
+
const top = m.y.torsoTop + b.T * 0.2, bottom = m.y.hip;
|
|
719
|
+
let x0 = Infinity, x1 = -Infinity, z0 = Infinity, z1 = -Infinity;
|
|
720
|
+
const sx = b.S / 30, sz = b.D / 20;
|
|
721
|
+
for (let yy = top; yy <= bottom; yy += b.T / 16)
|
|
722
|
+
for (let x = -b.S * 2.5; x <= b.S * 2.5; x += sx)
|
|
723
|
+
for (let z = -b.D * 3; z <= b.D * 3; z += sz)
|
|
724
|
+
if (m.torso(x, yy, z) < 0)
|
|
725
|
+
(x0 = Math.min(x0, x), x1 = Math.max(x1, x), z0 = Math.min(z0, z), z1 = Math.max(z1, z));
|
|
726
|
+
return [[(x0 + x1) / 2, m.y.hip - b.T * 0.48, (z0 + z1) / 2], [(x1 - x0) / 2 + margin, b.T * 0.56, (z1 - z0) / 2 + margin]];
|
|
727
|
+
}
|
|
728
|
+
/**
|
|
729
|
+
* The head as a hand keeps out of it (body space, at rest): the cranium with the hair's bulk round it
|
|
730
|
+
* — one ellipsoid fitted to both (the hair above the jaw: a fall down the back does not count), a
|
|
731
|
+
* little bigger where the box's corners leave hair out (at most 5 %) — then the face's own
|
|
732
|
+
* ellipsoids (jaw, chin), each grown by `margin` (a hand's half thickness), and the nose's as they are
|
|
733
|
+
* (it stands out of the face in profile). Built from the model, so it is the head as drawn for any
|
|
734
|
+
* anatomy (a big head, a perm, a bun, a long nose).
|
|
735
|
+
*/
|
|
736
|
+
function headKeepOut(m, margin) {
|
|
737
|
+
const u = m.b.u;
|
|
738
|
+
const [[cc, cr], ...face] = m.headParts;
|
|
739
|
+
const limit = cc[1] + cr[1] * 0.5;
|
|
740
|
+
let x0 = cc[0] - cr[0], x1 = cc[0] + cr[0], y0 = cc[1] - cr[1], z0 = cc[2] - cr[2], z1 = cc[2] + cr[2];
|
|
741
|
+
const y1 = cc[1] + cr[1];
|
|
742
|
+
const hair = [];
|
|
743
|
+
const step = u / 10;
|
|
744
|
+
for (let yy = cc[1] - u * 2.6; yy <= limit; yy += step)
|
|
745
|
+
for (let x = -u * 2.2; x <= u * 2.2; x += step)
|
|
746
|
+
for (let z = -u * 2.2; z <= u * 2.2; z += step) {
|
|
747
|
+
if (m.bulk(x, yy, z) >= 0)
|
|
748
|
+
continue;
|
|
749
|
+
hair.push([x, yy, z]);
|
|
750
|
+
x0 = Math.min(x0, x);
|
|
751
|
+
x1 = Math.max(x1, x);
|
|
752
|
+
y0 = Math.min(y0, yy);
|
|
753
|
+
z0 = Math.min(z0, z);
|
|
754
|
+
z1 = Math.max(z1, z);
|
|
755
|
+
}
|
|
756
|
+
const c = [(x0 + x1) / 2, (y0 + y1) / 2, (z0 + z1) / 2], rad = [(x1 - x0) / 2, (y1 - y0) / 2, (z1 - z0) / 2];
|
|
757
|
+
let k = 1;
|
|
758
|
+
for (const p of hair)
|
|
759
|
+
k = Math.max(k, Math.hypot((p[0] - c[0]) / rad[0], (p[1] - c[1]) / rad[1], (p[2] - c[2]) / rad[2]));
|
|
760
|
+
k = Math.min(k, 1.05);
|
|
761
|
+
const grown = (r3, s = 1) => [r3[0] * s + margin, r3[1] * s + margin, r3[2] * s + margin];
|
|
762
|
+
// (The nose as drawn: a hand resting on the face — over the eyes, at the mouth — sits in front of it,
|
|
763
|
+
// not a hand's thickness off it.)
|
|
764
|
+
return [[c, grown(rad, k)], ...face.map(([fc, fr]) => [fc, grown(fr)]), ...m.noseParts];
|
|
765
|
+
}
|
|
766
|
+
// ------------------------------------------------------------------ drawing a view
|
|
767
|
+
/** Light from the upper left, in front (view space). */
|
|
768
|
+
const LIGHT = (() => {
|
|
769
|
+
const v = [-0.5, -0.62, 0.6];
|
|
770
|
+
const l = Math.hypot(...v);
|
|
771
|
+
return [v[0] / l, v[1] / l, v[2] / l];
|
|
772
|
+
})();
|
|
773
|
+
/** Cel shading: surfaces turned away from the light more than this are in shadow. */
|
|
774
|
+
const SHADE_AT = -0.02;
|
|
775
|
+
/** Colour regions traced snug to the silhouette (set for a build, as `WEIGHT`). */
|
|
776
|
+
const SNUG = { on: false };
|
|
777
|
+
/**
|
|
778
|
+
* A volume drawn flat, the way a cartoon is painted: the fill, its colour regions, cel shading
|
|
779
|
+
* (the side turned away from the light, cast shadows) and an outline in a dark tone of its colour.
|
|
780
|
+
*/
|
|
781
|
+
export function piece(c, g, o) {
|
|
782
|
+
const field = o.field ?? c.val;
|
|
783
|
+
const outline = fieldPath(field, g);
|
|
784
|
+
if (!outline)
|
|
785
|
+
return "";
|
|
786
|
+
const n = g.w * g.h;
|
|
787
|
+
const key = new Array(n);
|
|
788
|
+
const light = new Float32Array(n).fill(1);
|
|
789
|
+
const keys = new Set([o.base]);
|
|
790
|
+
for (let i = 0; i < n; i++) {
|
|
791
|
+
if (field[i] > 0 || Number.isNaN(c.hit[i * 3]))
|
|
792
|
+
continue;
|
|
793
|
+
const p = [c.hit[i * 3], c.hit[i * 3 + 1], c.hit[i * 3 + 2]];
|
|
794
|
+
key[i] = o.color ? o.color(p) : o.base;
|
|
795
|
+
keys.add(key[i]);
|
|
796
|
+
const nm = normal(o.sdf, p, o.smooth);
|
|
797
|
+
const [nx, ny, nz] = toCam(nm, o.theta);
|
|
798
|
+
light[i] = nx * LIGHT[0] + ny * LIGHT[1] + nz * LIGHT[2] - SHADE_AT;
|
|
799
|
+
if (o.shadow?.(i))
|
|
800
|
+
light[i] = Math.min(light[i], -0.3);
|
|
801
|
+
}
|
|
802
|
+
let out = `<path d="${outline}" fill="palette(${o.base})" fill-rule="evenodd"/>`;
|
|
803
|
+
const snug = o.snug ?? SNUG.on;
|
|
804
|
+
for (const k of keys) {
|
|
805
|
+
if (k === o.base)
|
|
806
|
+
continue;
|
|
807
|
+
const mask = new Float32Array(n);
|
|
808
|
+
// (Snug: the silhouette's own field across its edge, half a cell either side of a region's edge inside.)
|
|
809
|
+
if (snug)
|
|
810
|
+
for (let i = 0; i < n; i++)
|
|
811
|
+
mask[i] = field[i] >= 0 ? field[i] : key[i] === k ? Math.max(field[i], -g.step * 0.5) : g.step * 0.5;
|
|
812
|
+
else
|
|
813
|
+
for (let i = 0; i < n; i++)
|
|
814
|
+
mask[i] = key[i] === k ? -1 : 1;
|
|
815
|
+
const d = fieldPath(mask, g, 4);
|
|
816
|
+
const edged = o.edges?.includes(k);
|
|
817
|
+
if (d)
|
|
818
|
+
out += `<path d="${d}" fill="palette(${k})" fill-rule="evenodd"${edged && !o.edgeKeep ? ` ${stc(k, 1.8)}` : ""}/>`;
|
|
819
|
+
if (d && edged && o.edgeKeep) {
|
|
820
|
+
const e = strokeWhere(mask, g, o.edgeKeep);
|
|
821
|
+
if (e)
|
|
822
|
+
out += `<path d="${e}" fill="none" ${stc(k, 1.8)}/>`;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
// Shadows, region by region (a smooth terminator: the lighting is a continuous field).
|
|
826
|
+
for (const k of o.shade === false ? [] : keys) {
|
|
827
|
+
const f = new Float32Array(n);
|
|
828
|
+
for (let i = 0; i < n; i++)
|
|
829
|
+
f[i] = key[i] === k ? light[i] * 20 : 1;
|
|
830
|
+
const d = fieldPath(f, g, 6);
|
|
831
|
+
if (d)
|
|
832
|
+
out += `<path d="${d}" fill="palette(${k.endsWith("Shade") ? k : `${k}Shade`})" fill-rule="evenodd"/>`;
|
|
833
|
+
}
|
|
834
|
+
const line = o.line ?? o.base;
|
|
835
|
+
if (o.keep) {
|
|
836
|
+
const d = strokeWhere(field, g, o.keep);
|
|
837
|
+
return out + (d ? `<path d="${d}" fill="none" ${stc(line)}/>` : "");
|
|
838
|
+
}
|
|
839
|
+
return out + `<path d="${outline}" fill="none" fill-rule="evenodd" ${stc(line)}/>`;
|
|
840
|
+
}
|
|
841
|
+
/** Open outline pieces of a field, keeping only points where `keep` holds. */
|
|
842
|
+
function strokeWhere(val, g, keep) {
|
|
843
|
+
let d = "";
|
|
844
|
+
for (const loop of contours(val, g.w, g.h)) {
|
|
845
|
+
const n = loop.length;
|
|
846
|
+
const ok = loop.map(([i, j]) => keep(Math.round(i), Math.round(j)));
|
|
847
|
+
if (ok.every(Boolean)) {
|
|
848
|
+
d += " " + curvePath(loop.map(([i, j]) => [g.x0 + i * g.step, g.y0 + j * g.step]), g.step, true);
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
const start = ok.indexOf(false);
|
|
852
|
+
let run = [];
|
|
853
|
+
const flush = () => {
|
|
854
|
+
if (run.length > 3)
|
|
855
|
+
d += " " + curvePath(run, g.step, false);
|
|
856
|
+
run = [];
|
|
857
|
+
};
|
|
858
|
+
for (let s = 1; s <= n; s++) {
|
|
859
|
+
const i = (start + s) % n;
|
|
860
|
+
if (ok[i])
|
|
861
|
+
run.push([g.x0 + loop[i][0] * g.step, g.y0 + loop[i][1] * g.step]);
|
|
862
|
+
else
|
|
863
|
+
flush();
|
|
864
|
+
}
|
|
865
|
+
flush();
|
|
866
|
+
}
|
|
867
|
+
return d.trim();
|
|
868
|
+
}
|
|
869
|
+
function headView(m, vw) {
|
|
870
|
+
const theta = vw.yaw;
|
|
871
|
+
const u = m.b.u;
|
|
872
|
+
const box = { x: [-u * 2, u * 2], y: [m.y.top - u * 0.9, m.y.head + u * (m.veil ? 3.6 : 2.4)], z: [-u * 1.9, u * 1.6] };
|
|
873
|
+
const g = gridFor([box], vw, u / 26);
|
|
874
|
+
const head = cast(m.head, g, vw), hair = cast(m.hair, g, vw), ears = cast(m.ears, g, vw);
|
|
875
|
+
const n = g.w * g.h;
|
|
876
|
+
// In front of the head: hair nearer than the face (spilling a little past its outline, so the
|
|
877
|
+
// head's outline does not show through), and ears that stick out of the face (3/4 view).
|
|
878
|
+
const front = new Float32Array(n), earF = new Float32Array(n);
|
|
879
|
+
for (let i = 0; i < n; i++) {
|
|
880
|
+
// Head and ears together: what the hair can be in front of.
|
|
881
|
+
const hv = Math.min(head.val[i], ears.val[i]), hd = Math.max(head.depth[i], ears.depth[i]);
|
|
882
|
+
const both = hv < 0 && hair.val[i] < 0;
|
|
883
|
+
front[i] = Math.max(hair.val[i], hv - 3.2, both ? hd - hair.depth[i] : -Infinity);
|
|
884
|
+
// (Depths only where the rays hit: a miss is −Infinity, and a difference with it would be
|
|
885
|
+
// ±Infinity — not a field value the outline can be traced through.)
|
|
886
|
+
earF[i] = ears.val[i] < 0 ? Math.max(ears.val[i], head.val[i] < 0 ? head.depth[i] - ears.depth[i] : 1, hair.val[i] < 0 ? hair.depth[i] - ears.depth[i] : -Infinity) : ears.val[i];
|
|
887
|
+
}
|
|
888
|
+
let x0 = Infinity, x1 = -Infinity, y0 = Infinity, y1 = -Infinity;
|
|
889
|
+
for (let j = 0; j < g.h; j++)
|
|
890
|
+
for (let i = 0; i < g.w; i++) {
|
|
891
|
+
const k = j * g.w + i;
|
|
892
|
+
if (head.val[k] < 0 || hair.val[k] < 0 || ears.val[k] < 0) {
|
|
893
|
+
const X = g.x0 + i * g.step, Y = g.y0 + j * g.step;
|
|
894
|
+
x0 = Math.min(x0, X);
|
|
895
|
+
x1 = Math.max(x1, X);
|
|
896
|
+
y0 = Math.min(y0, Y);
|
|
897
|
+
y1 = Math.max(y1, Y);
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
const at = (arr, i, j) => arr[Math.max(0, Math.min(g.h - 1, j)) * g.w + Math.max(0, Math.min(g.w - 1, i))];
|
|
901
|
+
// Cast shadows on the face: under the front hair (the light comes from above, a little left).
|
|
902
|
+
const drop = Math.round((u * 0.12) / g.step), side = Math.round((u * 0.04) / g.step);
|
|
903
|
+
const underHair = (k) => {
|
|
904
|
+
const i = k % g.w, j = Math.floor(k / g.w);
|
|
905
|
+
for (let d = 1; d <= drop; d++)
|
|
906
|
+
if (at(front, i - Math.round((side * d) / drop), j - d) < 0)
|
|
907
|
+
return true;
|
|
908
|
+
return false;
|
|
909
|
+
};
|
|
910
|
+
// A jump in depth: where the nose stands out of the face (outlined there, like a drawn nose).
|
|
911
|
+
const jump = (i, j) => {
|
|
912
|
+
let d = 0;
|
|
913
|
+
for (const [a, b] of [[1, 0], [-1, 0], [0, 1], [0, -1], [2, 0], [-2, 0]])
|
|
914
|
+
d = Math.max(d, Math.abs(at(head.depth, i + a, j + b) - at(head.depth, i, j)));
|
|
915
|
+
return d > g.step * 2;
|
|
916
|
+
};
|
|
917
|
+
// The nose as its own layer over the mouth (filled and shaded like the face, outlined only where
|
|
918
|
+
// it stands out), so the far corner of a mouth seen from three-quarters goes behind it.
|
|
919
|
+
const noseVal = new Float32Array(n).fill(1);
|
|
920
|
+
if (m.nose && theta < 1.6)
|
|
921
|
+
for (let i = 0; i < n; i++)
|
|
922
|
+
if (head.val[i] < 0 && !Number.isNaN(head.hit[i * 3]))
|
|
923
|
+
noseVal[i] = m.nose(head.hit[i * 3], head.hit[i * 3 + 1], head.hit[i * 3 + 2]) - u * 0.015;
|
|
924
|
+
// (Over the hair too: hair at the side of the face never hides the nose.)
|
|
925
|
+
const noseOver = m.nose && theta < 1.6 ? piece(head, g, { shade: m.shade, sdf: m.head, theta: vw, color: m.headColor, base: "skin", field: noseVal, keep: (i, j) => jump(i, j) && at(noseVal, i, j) < u * 0.02 }) : "";
|
|
926
|
+
const tailArt = m.tail ? piece(cast(m.tail.sdf, g, vw), g, { shade: m.shade, sdf: m.tail.sdf, theta: vw, color: m.tail.color, base: "hair", edges: ["accent"] }) : "";
|
|
927
|
+
const tailInFront = !!m.tail && depthOf(m.tail.pivot, vw) > depthOf([0, m.y.head, 0], vw) + u * 0.2;
|
|
928
|
+
// The front hair's outline is drawn against the face only, not where it meets the hair behind.
|
|
929
|
+
const keepHair = (i, j) => !(at(hair.val, i, j) < -2 && at(head.val, i, j) > -1.5) && !(at(hair.val, i + 1, j) < -2 && at(hair.val, i - 1, j) < -2 && at(hair.val, i, j + 1) < -2 && at(hair.val, i, j - 1) < -2 && at(head.val, i, j) > -4);
|
|
930
|
+
return {
|
|
931
|
+
hairBack: piece(hair, g, { shade: m.shade, sdf: m.hair, theta: vw, color: m.hairColor, base: "hair", edges: ["accent", "earItem", "earTip", "hat", "hatDark"], smooth: u * 0.18 }),
|
|
932
|
+
earsBack: piece(ears, g, { shade: m.shade, sdf: m.ears, theta: vw, base: "skin" }),
|
|
933
|
+
head: piece(head, g, { shade: m.shade, sdf: m.head, theta: vw, color: m.headColor, base: "skin", shadow: underHair }),
|
|
934
|
+
noseOver,
|
|
935
|
+
earsFront: theta > 0.2 && theta < 2 ? piece(ears, g, { shade: m.shade, sdf: m.ears, theta: vw, base: "skin", field: earF }) : "",
|
|
936
|
+
hairFront: piece(hair, g, { shade: m.shade, sdf: m.hair, theta: vw, color: m.hairColor, base: "hair", edges: ["accent", "earItem", "earTip", "hat", "hatDark"], field: front, keep: keepHair, smooth: u * 0.18 }),
|
|
937
|
+
tailBack: tailArt && !tailInFront ? tailArt : "",
|
|
938
|
+
tailFront: tailArt && tailInFront ? tailArt : "",
|
|
939
|
+
bounds: [x0, y0, x1, y1],
|
|
940
|
+
};
|
|
941
|
+
}
|
|
942
|
+
function bodyView(m, look, vw) {
|
|
943
|
+
const theta = vw.yaw;
|
|
944
|
+
void theta;
|
|
945
|
+
const { b } = m;
|
|
946
|
+
const box = { x: [-b.S - 30, b.S + 30], y: [m.y.torsoTop - 12, m.y.hip + b.T * 0.3], z: [-b.D * 1.2, b.D * 2.2] };
|
|
947
|
+
const g = gridFor([box], vw, 2);
|
|
948
|
+
// The shirt's hem — where the trousers' colour starts — one ink line (TV proportions: not along
|
|
949
|
+
// the cut at the hips the legs go on from).
|
|
950
|
+
const hem = m.style.proportions === "tv";
|
|
951
|
+
const edges = ["pocket", "apron", "item", "band", "sweater", "lapel", "tie", "strap", ...(["polo", "shirt", "jacket", "cardigan"].includes(look.top) ? ["top2"] : []), "bottom"];
|
|
952
|
+
const tc = cast(m.torso, g, vw);
|
|
953
|
+
// The head and neck shade the top of the chest.
|
|
954
|
+
const neckShadow = (k) => {
|
|
955
|
+
const i = k % g.w, j = Math.floor(k / g.w);
|
|
956
|
+
const X = g.x0 + i * g.step, Y = g.y0 + j * g.step;
|
|
957
|
+
return ((X - 2) / (m.neckR * 1.7)) ** 2 + ((Y - m.y.torsoTop) / (b.T * 0.13)) ** 2 < 1;
|
|
958
|
+
};
|
|
959
|
+
// No line along the torso's bottom (its cut at the hips): the legs, the trousers or a skirt carry
|
|
960
|
+
// on from it (a dress is one piece).
|
|
961
|
+
const cut = m.y.hip + 5 + (hem ? 5 : 0);
|
|
962
|
+
const atCut = (i, j) => {
|
|
963
|
+
// (TV proportions: only the bottom edge itself — the torso's sides are inked down to it, the
|
|
964
|
+
// trousers' outline goes on from there.)
|
|
965
|
+
const R = hem ? 1 : 2, band = hem ? 2.5 : 4 + (look.heavy ?? 0) * b.T * 0.15;
|
|
966
|
+
for (let dj = -R; dj <= R; dj++)
|
|
967
|
+
for (let di = -R; di <= R; di++) {
|
|
968
|
+
const k = (j + dj) * g.w + (i + di);
|
|
969
|
+
// (A belly's lower edge over the trousers is the torso's bottom too.)
|
|
970
|
+
if (k >= 0 && k < g.w * g.h && tc.val[k] < 0 && tc.hit[k * 3 + 1] > cut - band)
|
|
971
|
+
return true;
|
|
972
|
+
}
|
|
973
|
+
return false;
|
|
974
|
+
};
|
|
975
|
+
const torso = piece(tc, g, { shade: m.shade, sdf: m.torso, theta: vw, color: m.torsoColor, base: "top", edges, shadow: neckShadow, keep: (i, j) => !atCut(i, j), ...(hem ? { edgeKeep: (i, j) => !atCut(i, j) } : {}) });
|
|
976
|
+
const nb = { x: [-m.neckR - 4, m.neckR + 4], y: [m.y.torsoTop - 4 + (m.style.proportions === "tv" ? 18 : 0), m.y.head + m.b.u * 0.7], z: [-m.b.u, m.b.u * 0.5] };
|
|
977
|
+
const ng = gridFor([nb], vw, 1.5);
|
|
978
|
+
// The neck is in the head's shadow.
|
|
979
|
+
const neck = piece(cast(m.neck, ng, vw), ng, { shade: m.shade, sdf: m.neck, theta: vw, base: m.shade ? "skinShade" : "skin", line: "skin" });
|
|
980
|
+
let skirt = "";
|
|
981
|
+
if (m.skirt) {
|
|
982
|
+
const sb = { x: [-b.H * 2 - 20, b.H * 2 + 20], y: [m.y.waist - 8, 0], z: [-b.D * 2, b.D * 2.4] };
|
|
983
|
+
const sg = gridFor([sb], vw, 2);
|
|
984
|
+
skirt = piece(cast(m.skirt, sg, vw), sg, { shade: m.shade, sdf: m.skirt, theta: vw, color: m.skirtColor, base: m.skirtColor?.([0, m.y.hip, 0]) ?? "bottom", edges: ["apron"] });
|
|
985
|
+
}
|
|
986
|
+
return { torso, neck, skirt };
|
|
987
|
+
}
|
|
988
|
+
/** Face features of a view: eye variants, pupils, lids, mouth and brow shapes, nose, extras. */
|
|
989
|
+
function faceView(m, look, vw) {
|
|
990
|
+
const theta = vw.yaw;
|
|
991
|
+
const u = m.b.u;
|
|
992
|
+
const s = onSurface(m.head, vw);
|
|
993
|
+
const { x: ex, rx, ry } = m.eye;
|
|
994
|
+
const ey = m.y.eye;
|
|
995
|
+
// Round eyes, fully side-on: the classic cartoon profile keeps its near eye — it, its brow, the
|
|
996
|
+
// mouth, a moustache and glasses are drawn as from a little in front of the side (`PROFILE_EYE`),
|
|
997
|
+
// where they are still on the face, not left out as slivers on its edge.
|
|
998
|
+
const cheat = m.style.eyes === "round" && theta > 1.45 && theta < 1.62;
|
|
999
|
+
const fvw = cheat ? { ...vw, yaw: PROFILE_EYE } : vw;
|
|
1000
|
+
const se = cheat ? onSurface(m.head, fvw) : s;
|
|
1001
|
+
const ring = (cx, cy, ax, ay, lift = 0, n = 28) => Array.from({ length: n }, (_, i) => {
|
|
1002
|
+
const a = (i / n) * Math.PI * 2;
|
|
1003
|
+
return se.point(cx + Math.cos(a) * ax, cy + Math.sin(a) * ay, lift);
|
|
1004
|
+
});
|
|
1005
|
+
const curve = (p) => `M${r(p[0][0])} ${r(p[0][1])}` + p.slice(1).map((q) => ` L${r(q[0])} ${r(q[1])}`).join("");
|
|
1006
|
+
const eyes = [-1, 1].map((side) => side * ex);
|
|
1007
|
+
// An eye turned well away is left out (the classic near-profile: one eye), not drawn as a sliver
|
|
1008
|
+
// on the face's edge.
|
|
1009
|
+
const vis = eyes.map((x, i) => (!cheat || i === 0) && se.visible(x, ey, 0.4));
|
|
1010
|
+
const each = (fn) => eyes.map((x, i) => (vis[i] ? fn(x, i) : "")).join("");
|
|
1011
|
+
const white = (k) => each((x) => {
|
|
1012
|
+
const ring_ = ring(x, ey, rx * k, ry * k);
|
|
1013
|
+
const lid = Array.from({ length: 11 }, (_, i) => {
|
|
1014
|
+
const a = Math.PI + (i / 10) * Math.PI;
|
|
1015
|
+
return se.point(x + Math.cos(a) * rx * k * 1.04, ey + Math.sin(a) * ry * k * 1.04);
|
|
1016
|
+
});
|
|
1017
|
+
const lashes = look.female
|
|
1018
|
+
? (() => {
|
|
1019
|
+
const o = Math.sign(x) || 1;
|
|
1020
|
+
const a = se.point(x + o * rx * k * 0.95, ey - ry * k * 0.35), b = se.point(x + o * rx * k * 1.35, ey - ry * k * 0.7);
|
|
1021
|
+
const c = se.point(x + o * rx * k * 0.75, ey - ry * k * 0.75), d = se.point(x + o * rx * k * 1.05, ey - ry * k * 1.12);
|
|
1022
|
+
return `<path d="M${r(a[0])} ${r(a[1])} L${r(b[0])} ${r(b[1])} M${r(c[0])} ${r(c[1])} L${r(d[0])} ${r(d[1])}" ${st(2.4)}/>`;
|
|
1023
|
+
})()
|
|
1024
|
+
: "";
|
|
1025
|
+
return `<path d="M${pts(ring_)} Z" fill="#fff" ${st(2.4)}/><path d="${curve(lid)}" fill="none" ${st(3.6)}/>` + lashes;
|
|
1026
|
+
});
|
|
1027
|
+
const arc = (dy, bend) => each((x) => {
|
|
1028
|
+
const p = Array.from({ length: 9 }, (_, i) => {
|
|
1029
|
+
const t = -1 + (i / 8) * 2;
|
|
1030
|
+
return se.point(x + t * rx, ey + dy * ry + bend * ry * (1 - t * t));
|
|
1031
|
+
});
|
|
1032
|
+
return `<path d="${curve(p)}" fill="none" ${st(3.6)}/>`;
|
|
1033
|
+
});
|
|
1034
|
+
const pupilR = u * m.pupil;
|
|
1035
|
+
const pupil = (k = 1) => each((x) => {
|
|
1036
|
+
const c = se.point(x, ey + ry * 0.08);
|
|
1037
|
+
// (A dot pupil has no glint.)
|
|
1038
|
+
if (m.style.eyes === "wide" || m.style.eyes === "round")
|
|
1039
|
+
return `<circle cx="${r(c[0])}" cy="${r(c[1])}" r="${r(pupilR * k)}" fill="palette(ink)"/>`;
|
|
1040
|
+
// Big eyes: a large glint up on the light's side and a small one across from it.
|
|
1041
|
+
if (m.style.eyes === "big")
|
|
1042
|
+
return `<circle cx="${r(c[0])}" cy="${r(c[1])}" r="${r(pupilR * k)}" fill="palette(ink)"/><circle cx="${r(c[0] - pupilR * 0.36 * k)}" cy="${r(c[1] - pupilR * 0.38 * k)}" r="${r(pupilR * 0.36 * k)}" fill="#fff"/><circle cx="${r(c[0] + pupilR * 0.42 * k)}" cy="${r(c[1] + pupilR * 0.4 * k)}" r="${r(pupilR * 0.15 * k)}" fill="#fff"/>`;
|
|
1043
|
+
return `<circle cx="${r(c[0])}" cy="${r(c[1])}" r="${r(pupilR * k)}" fill="palette(ink)"/><circle cx="${r(c[0] - pupilR * 0.35 * k)}" cy="${r(c[1] - pupilR * 0.4 * k)}" r="${r(pupilR * 0.32 * k)}" fill="#fff"/>`;
|
|
1044
|
+
});
|
|
1045
|
+
const lids = each((x) => {
|
|
1046
|
+
const up = Array.from({ length: 13 }, (_, i) => {
|
|
1047
|
+
const a = Math.PI + (i / 12) * Math.PI;
|
|
1048
|
+
return se.point(x + Math.cos(a) * rx * 1.08, ey + Math.min(Math.sin(a) * ry * 1.08, -ry * 0.05));
|
|
1049
|
+
});
|
|
1050
|
+
const cut = [se.point(x + rx * 1.08, ey - ry * 0.05), se.point(x - rx * 1.08, ey - ry * 0.05)];
|
|
1051
|
+
return `<path d="M${pts([...up, ...cut])} Z" fill="palette(skin)"/><path d="${curve([cut[1], cut[0]])}" fill="none" ${st(3.2)}/>`;
|
|
1052
|
+
});
|
|
1053
|
+
const dead = each((x) => {
|
|
1054
|
+
const a = se.point(x - rx * 0.6, ey - ry * 0.5), b = se.point(x + rx * 0.6, ey + ry * 0.5), c = se.point(x + rx * 0.6, ey - ry * 0.5), d = se.point(x - rx * 0.6, ey + ry * 0.5);
|
|
1055
|
+
return `<path d="M${r(a[0])} ${r(a[1])} L${r(b[0])} ${r(b[1])} M${r(c[0])} ${r(c[1])} L${r(d[0])} ${r(d[1])}" ${st(3.6)}/>`;
|
|
1056
|
+
});
|
|
1057
|
+
const eyeVariants = {
|
|
1058
|
+
open: white(1),
|
|
1059
|
+
wide: white(1.16),
|
|
1060
|
+
half: white(1),
|
|
1061
|
+
closed: arc(0.1, 0.22),
|
|
1062
|
+
happy: arc(0.2, -0.42),
|
|
1063
|
+
dead,
|
|
1064
|
+
shine: white(1.16) + pupil(1.6),
|
|
1065
|
+
};
|
|
1066
|
+
// Brows (one morph path: two strokes).
|
|
1067
|
+
const by = m.y.brow;
|
|
1068
|
+
// (Round eyes: thin brows of one width, like a stroke of the outline's pen.)
|
|
1069
|
+
const roundEyes = m.style.eyes === "round";
|
|
1070
|
+
const thick = roundEyes ? u * 0.05 : u * 0.085, tip = roundEyes ? 0.7 : 0.25;
|
|
1071
|
+
const brow = (dy, inner, outer) => eyes
|
|
1072
|
+
.map((x, i) => {
|
|
1073
|
+
// The brow of an eye turned away is collapsed onto the other one (same path commands).
|
|
1074
|
+
const shown = vis[i] || !vis[1 - i];
|
|
1075
|
+
const xx = shown ? x : eyes[1 - i];
|
|
1076
|
+
const o = Math.sign(xx);
|
|
1077
|
+
// A filled, tapered stroke: thick at the inner end, thin at the outer one.
|
|
1078
|
+
const a = se.point(xx - o * rx * 0.95, by + dy + inner), c = se.point(xx, by + dy - u * 0.08), e = se.point(xx + o * rx * 1.1, by + dy + outer + u * 0.02);
|
|
1079
|
+
const a2 = se.point(xx - o * rx * 0.95, by + dy + inner + thick), c2 = se.point(xx, by + dy - u * 0.08 + thick * 0.75), e2 = se.point(xx + o * rx * 1.1, by + dy + outer + u * 0.02 + thick * tip);
|
|
1080
|
+
const q = shown ? [a, c, e, e2, c2, a2] : [a, a, a, a, a, a];
|
|
1081
|
+
return `M${r(q[0][0])} ${r(q[0][1])} Q${r(q[1][0])} ${r(q[1][1])} ${r(q[2][0])} ${r(q[2][1])} L${r(q[3][0])} ${r(q[3][1])} Q${r(q[4][0])} ${r(q[4][1])} ${r(q[5][0])} ${r(q[5][1])} Z`;
|
|
1082
|
+
})
|
|
1083
|
+
.join(" ");
|
|
1084
|
+
const k = u * 0.1;
|
|
1085
|
+
const brows = { base: brow(0, 0, 0), shapes: { up: brow(-k * 1.2, 0, 0), sad: brow(-k * 0.4, -k * 0.9, k * 0.6), cross: brow(k * 0.4, k * 1.1, -k * 0.5), smug: brow(-k * 0.3, k * 0.5, -k * 0.4) } };
|
|
1086
|
+
// Mouth.
|
|
1087
|
+
// Each corner stays on the part of the face turned to the camera (turned away, a mouth seen from
|
|
1088
|
+
// the side is short: its far corner goes round the face, out of sight).
|
|
1089
|
+
const corner = (side, my, k) => {
|
|
1090
|
+
let x = side * m.mouthW * k;
|
|
1091
|
+
for (let i = 0; i < 20 && !se.visible(x, my, m.mouthMargin); i++)
|
|
1092
|
+
x *= 0.88;
|
|
1093
|
+
return se.point(x, my);
|
|
1094
|
+
};
|
|
1095
|
+
// The rules: every mouth shape (expressions and the visemes of speech) stays on the face, inside its
|
|
1096
|
+
// outline, and never touches the nose — from every angle. The nose as drawn here (where the face's
|
|
1097
|
+
// surface is the nose's) with a gap around it; the face where a ray goes well into the head.
|
|
1098
|
+
const ng = gridFor([{ x: [-u * 1.2, u * 1.2], y: [m.y.nose - u * 0.4, m.y.mouth + u * 0.7], z: [-u * 1.2, u * 1.6] }], vw, u / 40);
|
|
1099
|
+
const hc = cast(m.head, ng, vw);
|
|
1100
|
+
const noseCells = [];
|
|
1101
|
+
if (m.nose)
|
|
1102
|
+
for (let j = 0; j < ng.h; j++)
|
|
1103
|
+
for (let i = 0; i < ng.w; i++) {
|
|
1104
|
+
const c = j * ng.w + i;
|
|
1105
|
+
if (hc.val[c] < 0 && !Number.isNaN(hc.hit[c * 3]) && m.nose(hc.hit[c * 3], hc.hit[c * 3 + 1], hc.hit[c * 3 + 2]) < u * 0.015)
|
|
1106
|
+
noseCells.push([ng.x0 + i * ng.step, ng.y0 + j * ng.step]);
|
|
1107
|
+
}
|
|
1108
|
+
// A visible gap between the nose and the upper lip.
|
|
1109
|
+
const gap = u * 0.08 + 2;
|
|
1110
|
+
// Near profile the mouth is on the face's edge: the lips may stand a little out of it.
|
|
1111
|
+
const edge = theta > 1.1 ? u * 0.1 : 0;
|
|
1112
|
+
const onFace = (p) => {
|
|
1113
|
+
const i = Math.round((p[0] - ng.x0) / ng.step), j = Math.round((p[1] - ng.y0) / ng.step);
|
|
1114
|
+
return i >= 0 && j >= 0 && i < ng.w && j < ng.h && hc.val[j * ng.w + i] < edge;
|
|
1115
|
+
};
|
|
1116
|
+
let my = m.y.mouth, kL = 1, kR = 1, lift = Math.max(0.6, Math.min(1, m.mouthW / (u * 0.3)));
|
|
1117
|
+
let mid = se.point(0, my)[0];
|
|
1118
|
+
const check = (shapes) => {
|
|
1119
|
+
// Every shape, and every expression with every viseme on top (smiling while talking: the
|
|
1120
|
+
// morphs add up).
|
|
1121
|
+
const base = pathPoints(parsePath(shapes.base));
|
|
1122
|
+
const of = (k) => pathPoints(parsePath(shapes.shapes[k]));
|
|
1123
|
+
const exprs = ["smile", "grin", "frown"].filter((k) => shapes.shapes[k]), visemes = Object.keys(shapes.shapes).filter((k) => !exprs.includes(k));
|
|
1124
|
+
const combos = exprs.flatMap((e) => visemes.map((v) => {
|
|
1125
|
+
const E = of(e), V = of(v);
|
|
1126
|
+
return base.map((b, i) => [b[0] + (E[i][0] - b[0]) + (V[i][0] - b[0]), b[1] + (E[i][1] - b[1]) + (V[i][1] - b[1])]);
|
|
1127
|
+
}));
|
|
1128
|
+
const pts = [shapes.base, ...Object.values(shapes.shapes)].flatMap((d) => pathPoints(parsePath(d))).concat(combos.flat());
|
|
1129
|
+
const v = { noseL: 0, noseR: 0, sideL: 0, sideR: 0, below: 0 };
|
|
1130
|
+
for (const p of pts) {
|
|
1131
|
+
if (noseCells.some((q) => Math.hypot(q[0] - p[0], q[1] - p[1]) < gap))
|
|
1132
|
+
p[0] < mid ? v.noseL++ : v.noseR++;
|
|
1133
|
+
if (!onFace(p)) {
|
|
1134
|
+
if (p[1] > se.point(0, my)[1] + u * 0.12)
|
|
1135
|
+
v.below++;
|
|
1136
|
+
else
|
|
1137
|
+
p[0] < mid ? v.sideL++ : v.sideR++;
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
return v;
|
|
1141
|
+
};
|
|
1142
|
+
const open0 = Math.min((u / 50) * 1.0, m.chinRoom / 27);
|
|
1143
|
+
let open = open0;
|
|
1144
|
+
const build = () => {
|
|
1145
|
+
const style = { symmetric: theta < 0.35 || theta > 2.8, lift };
|
|
1146
|
+
mid = se.point(0, my)[0];
|
|
1147
|
+
const L = corner(-1, my, kL), R = corner(1, my, kR);
|
|
1148
|
+
return { L, R, style, shapes: mouthShapes(L, R, open, style) };
|
|
1149
|
+
};
|
|
1150
|
+
let made = build(), clear = false;
|
|
1151
|
+
// Fix what breaks, cheapest change first: shorten the corner that touches (the nose's side or the
|
|
1152
|
+
// face's edge), flatten the smile, open less, then lower the mouth (never into the chin).
|
|
1153
|
+
for (let i = 0; i < 60; i++) {
|
|
1154
|
+
const v = check(made.shapes);
|
|
1155
|
+
if (!v.noseL && !v.noseR && !v.sideL && !v.sideR && !v.below) {
|
|
1156
|
+
clear = true;
|
|
1157
|
+
break;
|
|
1158
|
+
}
|
|
1159
|
+
// Facing the camera the nose is above the middle of the mouth: shortening the corners does not
|
|
1160
|
+
// clear it — flatten the smile and lower the mouth first.
|
|
1161
|
+
const frontal = theta < 0.35 || theta > 2.8;
|
|
1162
|
+
if (frontal && (v.noseL || v.noseR) && lift > 0.3)
|
|
1163
|
+
lift *= 0.85;
|
|
1164
|
+
else if (frontal && (v.noseL || v.noseR) && my - m.y.mouth < m.chinRoom * 0.4) {
|
|
1165
|
+
my += u * 0.015;
|
|
1166
|
+
open = Math.min(open, (m.chinRoom - (my - m.y.mouth)) / 27);
|
|
1167
|
+
}
|
|
1168
|
+
else if ((v.noseR || v.sideR) && v.noseR + v.sideR >= v.noseL + v.sideL && kR > 0.5)
|
|
1169
|
+
kR *= 0.92;
|
|
1170
|
+
else if ((v.noseL || v.sideL) && kL > 0.5)
|
|
1171
|
+
kL *= 0.92;
|
|
1172
|
+
else if ((v.noseL || v.noseR) && lift > 0.3)
|
|
1173
|
+
lift *= 0.85;
|
|
1174
|
+
else if (v.below && open > open0 * 0.45)
|
|
1175
|
+
open *= 0.9;
|
|
1176
|
+
else if ((v.noseL || v.noseR) && my - m.y.mouth < m.chinRoom * 0.4) {
|
|
1177
|
+
my += u * 0.015;
|
|
1178
|
+
open = Math.min(open, (m.chinRoom - (my - m.y.mouth)) / 27);
|
|
1179
|
+
}
|
|
1180
|
+
else if (v.below && my > m.y.mouth - u * 0.06)
|
|
1181
|
+
my -= u * 0.01;
|
|
1182
|
+
else
|
|
1183
|
+
break;
|
|
1184
|
+
made = build();
|
|
1185
|
+
}
|
|
1186
|
+
const { L, R, style } = made;
|
|
1187
|
+
const mouth = made.shapes;
|
|
1188
|
+
const inside = mouthInside(L, R, open, style);
|
|
1189
|
+
const gape = gapeView(m, vw, { mid: se.point(0, my), nose: se.point(0, m.y.nose)[1], mouth: mouth.base, teeth: inside.teeth.base, tongue: inside.tongue.base }, (p) => noseCells.some((q) => Math.hypot(q[0] - p[0], q[1] - p[1]) < gap));
|
|
1190
|
+
mouth.shapes.gape = gape.mouth;
|
|
1191
|
+
inside.teeth.shapes.gape = gape.teeth;
|
|
1192
|
+
inside.tongue.shapes.gape = gape.tongue;
|
|
1193
|
+
// Nostrils (the nose itself is a volume of the head).
|
|
1194
|
+
const ny = m.y.nose;
|
|
1195
|
+
let nose = "";
|
|
1196
|
+
if (theta < 1.6) {
|
|
1197
|
+
const w = look.nose === "wide" ? u * 0.11 : look.nose === "round" ? u * 0.09 : u * 0.065;
|
|
1198
|
+
for (const sx of [-1, 1]) {
|
|
1199
|
+
const x = sx * w, yy = ny + u * 0.07;
|
|
1200
|
+
if (!s.visible(x, yy))
|
|
1201
|
+
continue;
|
|
1202
|
+
const c = s.point(x, yy, 0.5);
|
|
1203
|
+
nose += `<ellipse cx="${r(c[0])}" cy="${r(c[1])}" rx="${r(u * 0.035 * Math.max(0.45, Math.cos(theta)))}" ry="${r(u * 0.022)}" fill="palette(skinLine)" opacity="0.85"/>`;
|
|
1204
|
+
}
|
|
1205
|
+
}
|
|
1206
|
+
// Extras on the face.
|
|
1207
|
+
let extras = "";
|
|
1208
|
+
if (theta < 1.6 && m.style.blush && !look.flushed)
|
|
1209
|
+
// Soft rosy cheeks under the eyes, flattened by the turn of the face.
|
|
1210
|
+
for (const sx of [-1, 1]) {
|
|
1211
|
+
const x = sx * u * 0.5, yy = ey + ry + u * 0.14;
|
|
1212
|
+
if (!s.visible(x, yy, 0.3))
|
|
1213
|
+
continue;
|
|
1214
|
+
const c = s.point(x, yy, 0.2), e = s.point(x + u * 0.16, yy, 0.2);
|
|
1215
|
+
const rx = Math.max(u * 0.05, Math.hypot(e[0] - c[0], e[1] - c[1]));
|
|
1216
|
+
extras += `<ellipse cx="${r(c[0])}" cy="${r(c[1])}" rx="${r(rx)}" ry="${r(u * 0.085)}" fill="palette(flush)" opacity="0.45"/>`;
|
|
1217
|
+
}
|
|
1218
|
+
if (theta < 1.6) {
|
|
1219
|
+
if (look.freckles)
|
|
1220
|
+
for (const sx of [-1, 1])
|
|
1221
|
+
for (const [dx, dy] of [[0, 0], [0.09, 0.05], [-0.07, 0.07], [0.02, 0.12]]) {
|
|
1222
|
+
const x = sx * u * (0.42 + dx), yy = ey + u * (0.32 + dy);
|
|
1223
|
+
if (!s.visible(x, yy))
|
|
1224
|
+
continue;
|
|
1225
|
+
const p = s.point(x, yy);
|
|
1226
|
+
extras += `<circle cx="${r(p[0])}" cy="${r(p[1])}" r="${r(u * 0.025)}" fill="palette(skinDark)"/>`;
|
|
1227
|
+
}
|
|
1228
|
+
if (look.wrinkles)
|
|
1229
|
+
for (const sx of [-1, 1]) {
|
|
1230
|
+
if (!s.visible(sx * ex, ey + ry * 1.3))
|
|
1231
|
+
continue;
|
|
1232
|
+
const a = s.point(sx * (ex - rx * 0.6), ey + ry * 1.25), c = s.point(sx * ex, ey + ry * 1.45), e = s.point(sx * (ex + rx * 0.6), ey + ry * 1.25);
|
|
1233
|
+
const f0 = s.point(sx * u * 0.28, ny + u * 0.02), f1 = s.point(sx * u * 0.44, m.y.mouth - u * 0.02);
|
|
1234
|
+
extras += `<path d="M${r(a[0])} ${r(a[1])} Q${r(c[0])} ${r(c[1])} ${r(e[0])} ${r(e[1])} M${r(f0[0])} ${r(f0[1])} L${r(f1[0])} ${r(f1[1])}" fill="none" ${st(2)}/>`;
|
|
1235
|
+
}
|
|
1236
|
+
}
|
|
1237
|
+
let over = "";
|
|
1238
|
+
if (theta < 1.6) {
|
|
1239
|
+
if (look.moustache) {
|
|
1240
|
+
const my = m.y.mouth;
|
|
1241
|
+
const shape = [[-0.46, -0.02], [-0.38, -0.14], [-0.2, -0.22], [0, -0.24], [0.2, -0.22], [0.38, -0.14], [0.46, -0.02], [0.3, -0.08], [0.12, -0.08], [0, -0.1], [-0.12, -0.08], [-0.3, -0.08]];
|
|
1242
|
+
over += `<path d="M${pts(shape.map(([x, yy]) => se.point(x * u, my + yy * u, u * 0.05)))} Z" fill="palette(hairDark)" ${st(2.6)}/>`;
|
|
1243
|
+
}
|
|
1244
|
+
if (look.goatee) {
|
|
1245
|
+
const cy = m.y.mouth + u * 0.24;
|
|
1246
|
+
over += `<path d="M${pts(ring(0, cy, u * 0.14, u * 0.11, u * 0.03, 16))} Z" fill="palette(hairDark)" ${st(2.4)}/>`;
|
|
1247
|
+
}
|
|
1248
|
+
if (look.glasses) {
|
|
1249
|
+
// A frame in front of the face, made from the face as drawn: each lens round its eye, as wide
|
|
1250
|
+
// as the eye allows — never past the middle (a bridge between the lenses) nor round the side
|
|
1251
|
+
// of the face — the frame a little out of the face at the eyes' depth and curving back with
|
|
1252
|
+
// it, rigid (a turned face foreshortens the far lens instead of wrapping it round the cheek),
|
|
1253
|
+
// its arm back to the ear.
|
|
1254
|
+
const zAt = (x) => surfaceZ(m.head, x, ey) ?? u * 0.8;
|
|
1255
|
+
const bridge = u * 0.06;
|
|
1256
|
+
const w = Math.max(rx * 0.9, Math.min(rx * 1.35, ex - bridge, Math.max(rx * 1.05, frontHalf(m.head, ey, u) - ex)));
|
|
1257
|
+
const h = ry * 1.05;
|
|
1258
|
+
// The frame's depth: through the face between the eyes and at the eyes, curving back, a little out.
|
|
1259
|
+
const zc = zAt(0), ze = zAt(ex), bend = Math.max(0, zc - ze) / (ex * ex);
|
|
1260
|
+
const at = (x, yy) => project([x, yy, zc - bend * x * x + u * 0.1], fvw);
|
|
1261
|
+
const lens = (x) => Array.from({ length: 24 }, (_, i) => {
|
|
1262
|
+
const a = (i / 24) * Math.PI * 2;
|
|
1263
|
+
const c = Math.cos(a), sn = Math.sin(a);
|
|
1264
|
+
// A rounded square.
|
|
1265
|
+
const kx = Math.sign(c) * Math.abs(c) ** 0.6, ky = Math.sign(sn) * Math.abs(sn) ** 0.6;
|
|
1266
|
+
return at(x + kx * w, ey + ky * h);
|
|
1267
|
+
});
|
|
1268
|
+
const bridgeA = at(-ex + w, ey - ry * 0.2), bridgeM = at(0, ey - ry * 0.25), bridgeB = at(ex - w, ey - ry * 0.2);
|
|
1269
|
+
const tint = look.glasses === "sun" ? `fill="#1e2228" fill-opacity="0.9"` : `fill="#cfe8f5" fill-opacity="0.28"`;
|
|
1270
|
+
over += eyes.map((x, i) => (vis[i] ? `<path d="M${pts(lens(x))} Z" ${tint} ${st(3)}/>` : "")).join("") + `<path d="M${pts(cheat ? [bridgeA, bridgeM] : [bridgeA, bridgeM, bridgeB])}" fill="none" ${st(3)}/>`;
|
|
1271
|
+
if (theta > 0.2) {
|
|
1272
|
+
// (To the near ear, over its top.)
|
|
1273
|
+
const a = at(-ex - w, ey - ry * 0.2), e = project([-u * 0.92, Math.min(ey - ry * 0.1, m.y.ear - u * 0.2), -u * 0.02], vw);
|
|
1274
|
+
over += `<path d="M${r(a[0])} ${r(a[1])} L${r(e[0])} ${r(e[1])}" ${st(3)}/>`;
|
|
1275
|
+
}
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
if (look.earrings) {
|
|
1279
|
+
for (const sx of [-1, 1]) {
|
|
1280
|
+
const p3 = [sx * u * 1.0, m.y.ear + u * 0.3, -u * 0.06];
|
|
1281
|
+
if (theta > 0.2 && theta < 2 && sx > 0)
|
|
1282
|
+
continue; // the far ear is hidden
|
|
1283
|
+
const p = project(p3, vw);
|
|
1284
|
+
extras += `<circle cx="${r(p[0])}" cy="${r(p[1])}" r="${r(u * 0.07)}" fill="palette(accent)" ${st(2)}/>`;
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
const visible = theta < 1.6;
|
|
1288
|
+
return { eyeVariants, pupils: pupil(), lids, brows, mouth, teeth: inside.teeth, tongue: inside.tongue, jaw: gape.jaw, jawShade: gape.jawShade, jawLine: gape.jawLine, nose, extras, over, visible, mouthClear: clear && gape.clear, mouthAt: s.point(0, my, u * 0.04) };
|
|
1289
|
+
}
|
|
1290
|
+
/** Weight of the `gape` morph that draws the jaw fully unhinged (weight 1: a big cartoon gape). */
|
|
1291
|
+
export const GAPE_FULL = 2;
|
|
1292
|
+
/** Degrees the 3D `jaw` bone turns a weight of gape (its occluder follows the drawn jaw down). */
|
|
1293
|
+
export const JAW_TURN = 36;
|
|
1294
|
+
/**
|
|
1295
|
+
* How much of the upper arm (from the shoulder) is drawn under the head whatever its depth (the
|
|
1296
|
+
* cartoon convention of a head drawn over its shoulders); the rest of the arm keeps its true depth.
|
|
1297
|
+
*/
|
|
1298
|
+
export const SHOULDER_UNDER = 0.5;
|
|
1299
|
+
/**
|
|
1300
|
+
* The jaw's occluder: cones from the cheeks and the middle of the face (on the head) to the chin (on
|
|
1301
|
+
* the jaw bone), each thinned until it is inside the head's occluder at rest (the head's volume grown
|
|
1302
|
+
* by `grow`) — they only reach out of it as the jaw drops.
|
|
1303
|
+
*/
|
|
1304
|
+
function jawCones(m, to, grow) {
|
|
1305
|
+
const u = m.b.u, nt = m.y.neckTop;
|
|
1306
|
+
// (Deep in the head and wide there, narrower at the chin: what counts is how much of the picture
|
|
1307
|
+
// they cover once the chin has dropped.)
|
|
1308
|
+
const cones = [
|
|
1309
|
+
...[-1, 0, 1].map((sx) => ({
|
|
1310
|
+
a: [sx * u * 0.45, m.y.mouth - u * 0.22, sx ? -u * 0.05 : u * 0.1],
|
|
1311
|
+
b: [sx * u * 0.4, -u * 0.12, sx ? -u * 0.25 : -u * 0.12],
|
|
1312
|
+
r: [u * 0.45, u * 0.3],
|
|
1313
|
+
})),
|
|
1314
|
+
// (The cheeks round out to the head's width.)
|
|
1315
|
+
...[-1, 1].map((sx) => ({ a: [sx * u * 0.6, m.y.nose, -u * 0.35], b: [sx * u * 0.55, -u * 0.3, -u * 0.6], r: [u * 0.42, u * 0.3] })),
|
|
1316
|
+
];
|
|
1317
|
+
return cones.map(({ a, b, r: r0 }) => {
|
|
1318
|
+
const tip = [to[0] + b[0], to[1] + b[1], to[2] + b[2]];
|
|
1319
|
+
let k = 1;
|
|
1320
|
+
// (Points all round spheres along the cone: a golden-angle spiral.)
|
|
1321
|
+
const inside = () => {
|
|
1322
|
+
for (let h = 0; h <= 1; h += 0.125)
|
|
1323
|
+
for (let i = 0; i < 40; i++) {
|
|
1324
|
+
const c = [a[0] + (tip[0] - a[0]) * h, a[1] + (tip[1] - a[1]) * h, a[2] + (tip[2] - a[2]) * h];
|
|
1325
|
+
const rad = (r0[0] + (r0[1] - r0[0]) * h) * k;
|
|
1326
|
+
const z = 1 - ((i + 0.5) / 40) * 2, q = Math.sqrt(1 - z * z), t = i * 2.39996;
|
|
1327
|
+
if (m.head(c[0] + Math.cos(t) * q * rad, c[1] + Math.sin(t) * q * rad, c[2] + z * rad) > grow * 0.5)
|
|
1328
|
+
return false;
|
|
1329
|
+
}
|
|
1330
|
+
return true;
|
|
1331
|
+
};
|
|
1332
|
+
for (let i = 0; i < 30 && !inside(); i++)
|
|
1333
|
+
k *= 0.92;
|
|
1334
|
+
return { from: { bone: "head", at: [r(a[0]), r(a[1] - nt), r(a[2])] }, to: { bone: "jaw", t: 1, at: b.map(r) }, r: [r(r0[0] * k), r(r0[1] * k)] };
|
|
1335
|
+
});
|
|
1336
|
+
}
|
|
1337
|
+
/**
|
|
1338
|
+
* The gape of a view: the mouth opened to swallow something whole, with the lower head following it.
|
|
1339
|
+
* Everything is drawn for the jaw **unhinged** (weight `GAPE_FULL`) and the `gape` shapes are made
|
|
1340
|
+
* half way there (`morphTowards`): the morph is linear, so weight 1 is a big gape (the mouth ≈ 2.2×
|
|
1341
|
+
* viseme D tall, the chin ≈ 0.45 head radius lower) and weight 2 the unhinged jaw — the mouth as
|
|
1342
|
+
* wide as the head, the chin ≈ 0.9 head radius below its rest — every weight between them (and a
|
|
1343
|
+
* close from 2 to 0) a valid shape.
|
|
1344
|
+
*
|
|
1345
|
+
* The jaw is morph parts over the head's drawing, made from the head's outline in this view (rays
|
|
1346
|
+
* from the middle of the face, between the nose and the mouth, to the outline below): `jawLine`,
|
|
1347
|
+
* the lower outline (at rest exactly on the drawn chin), `jaw`, the skin between that outline and a
|
|
1348
|
+
* band inside the rest chin (no area at rest), and for cel-shaded looks `jawShade`, its side away
|
|
1349
|
+
* from the light. Opening, the lower head stretches down and its cheeks round out to the head's full
|
|
1350
|
+
* width, the old chin covered by the skin.
|
|
1351
|
+
*
|
|
1352
|
+
* The mouth's top lip stays clear of the nose (`touchesNose`, the face's rule: lowered until it
|
|
1353
|
+
* is); below, the mouth goes past the rest chin by design — the jaw drops with it.
|
|
1354
|
+
*/
|
|
1355
|
+
function gapeView(m, vw, at, touchesNose) {
|
|
1356
|
+
const u = m.b.u;
|
|
1357
|
+
const g = gridFor([{ x: [-u * 1.7, u * 1.7], y: [m.y.top - u * 0.1, m.y.head + u * 1.5], z: [-u * 1.7, u * 1.7] }], vw, u / 30);
|
|
1358
|
+
const hc = cast(m.head, g, vw);
|
|
1359
|
+
// The head's field between grid points (negative inside its outline).
|
|
1360
|
+
const field = (X, Y) => {
|
|
1361
|
+
const fi = (X - g.x0) / g.step, fj = (Y - g.y0) / g.step;
|
|
1362
|
+
const i = Math.floor(fi), j = Math.floor(fj);
|
|
1363
|
+
if (i < 0 || j < 0 || i >= g.w - 1 || j >= g.h - 1)
|
|
1364
|
+
return 1;
|
|
1365
|
+
const a = fi - i, b = fj - j, v = (ii, jj) => hc.val[jj * g.w + ii];
|
|
1366
|
+
return (v(i, j) * (1 - a) + v(i + 1, j) * a) * (1 - b) + (v(i, j + 1) * (1 - a) + v(i + 1, j + 1) * a) * b;
|
|
1367
|
+
};
|
|
1368
|
+
// The head's widest reach on each side.
|
|
1369
|
+
let xmin = Infinity, xmax = -Infinity;
|
|
1370
|
+
for (let j = 0; j < g.h; j++)
|
|
1371
|
+
for (let i = 0; i < g.w; i++)
|
|
1372
|
+
if (hc.val[j * g.w + i] < 0) {
|
|
1373
|
+
xmin = Math.min(xmin, g.x0 + i * g.step);
|
|
1374
|
+
xmax = Math.max(xmax, g.x0 + i * g.step);
|
|
1375
|
+
}
|
|
1376
|
+
// The jaw hinges between the nose and the mouth; rays from there to the outline below.
|
|
1377
|
+
const Yj = (at.nose + at.mid[1]) / 2;
|
|
1378
|
+
const cx = Math.max(xmin + u * 0.35, Math.min(xmax - u * 0.35, at.mid[0]));
|
|
1379
|
+
const ray = (phi) => {
|
|
1380
|
+
const dx = Math.cos(phi), dy = Math.sin(phi);
|
|
1381
|
+
let t0 = 0, t1 = 0;
|
|
1382
|
+
for (let t = 0; t < u * 3; t += u / 60) {
|
|
1383
|
+
if (field(cx + dx * t, Yj + dy * t) > 0) {
|
|
1384
|
+
t1 = t;
|
|
1385
|
+
break;
|
|
1386
|
+
}
|
|
1387
|
+
t0 = t;
|
|
1388
|
+
}
|
|
1389
|
+
for (let k = 0; k < 14; k++) {
|
|
1390
|
+
const t = (t0 + t1) / 2;
|
|
1391
|
+
if (field(cx + dx * t, Yj + dy * t) > 0)
|
|
1392
|
+
t1 = t;
|
|
1393
|
+
else
|
|
1394
|
+
t0 = t;
|
|
1395
|
+
}
|
|
1396
|
+
const t = (t0 + t1) / 2;
|
|
1397
|
+
return [cx + dx * t, Yj + dy * t];
|
|
1398
|
+
};
|
|
1399
|
+
const N = 16;
|
|
1400
|
+
const phis = Array.from({ length: N + 1 }, (_, i) => (i / N) * Math.PI);
|
|
1401
|
+
const B = phis.map(ray);
|
|
1402
|
+
// Unhinged: the lower head stretched down by `drop`, its cheeks out to the head's widest (and a
|
|
1403
|
+
// little past it: they round out), from the hinge — where it meets the head's outline — down.
|
|
1404
|
+
const drop = u * 0.9;
|
|
1405
|
+
const b = Math.max(...B.map((q) => q[1])) - Yj + drop;
|
|
1406
|
+
const a0 = [B[0][0] - cx, cx - B[N][0]], a1 = [(xmax - cx) * 1.08, (cx - xmin) * 1.08];
|
|
1407
|
+
const jawAt = (phi) => {
|
|
1408
|
+
const sn = Math.sin(phi), c = Math.cos(phi), k = c >= 0 ? 0 : 1;
|
|
1409
|
+
const a = a0[k] + (a1[k] - a0[k]) * (1 - (1 - Math.min(1, sn * 1.4)) ** 3);
|
|
1410
|
+
return [cx + a * c, Yj + b * sn];
|
|
1411
|
+
};
|
|
1412
|
+
const U = phis.map(jawAt);
|
|
1413
|
+
// Catmull-Rom through points, as cubic segments.
|
|
1414
|
+
const segs = (p) => p.slice(0, -1).map((q, i) => {
|
|
1415
|
+
const p0 = p[Math.max(0, i - 1)], p2 = p[i + 1], p3 = p[Math.min(p.length - 1, i + 2)];
|
|
1416
|
+
return `C${r(q[0] + (p2[0] - p0[0]) / 6)} ${r(q[1] + (p2[1] - p0[1]) / 6)} ${r(p2[0] - (p3[0] - q[0]) / 6)} ${r(p2[1] - (p3[1] - q[1]) / 6)} ${r(p2[0])} ${r(p2[1])}`;
|
|
1417
|
+
}).join(" ");
|
|
1418
|
+
const line = (p) => `M${r(p[0][0])} ${r(p[0][1])} ${segs(p)}`;
|
|
1419
|
+
// The jaw's skin: from its outline up to a band inside the rest chin (covering the drawn chin's
|
|
1420
|
+
// line from the smallest gape on, keeping the head's own drawing and shading above it).
|
|
1421
|
+
const Bin = B.map((q) => {
|
|
1422
|
+
const d = Math.hypot(q[0] - cx, q[1] - Yj) || 1, k = Math.min(u * 0.3, d * 0.6) / d;
|
|
1423
|
+
return [q[0] + (cx - q[0]) * k, q[1] + (Yj - q[1]) * k];
|
|
1424
|
+
});
|
|
1425
|
+
const jawLine = { base: line(B), shapes: { gape: morphTowards(line(B), line(U), GAPE_FULL) } };
|
|
1426
|
+
const jawBase = `${line(B)} ${segs([...B].reverse())} Z`, jawOpen = `${line(U)} ${segs([...Bin].reverse())} Z`;
|
|
1427
|
+
const jaw = { base: jawBase, shapes: { gape: morphTowards(jawBase, jawOpen, GAPE_FULL) } };
|
|
1428
|
+
// Cel shaded looks: the side of the dropped jaw turned away from the light (lower right) in
|
|
1429
|
+
// shadow, up to the rest chin (whose drawing is shaded there too) — none at rest.
|
|
1430
|
+
const inner = U.map((q, i) => {
|
|
1431
|
+
const a = U[Math.max(0, i - 1)], c = U[Math.min(N, i + 1)];
|
|
1432
|
+
const tx = c[0] - a[0], ty = c[1] - a[1], l = Math.hypot(tx, ty) || 1;
|
|
1433
|
+
// (The outward normal: the curve runs right → bottom → left, the outside on its right.)
|
|
1434
|
+
const nx = ty / l, ny = -tx / l;
|
|
1435
|
+
const f = smoothstep(0.05, 0.45, -(nx * LIGHT[0] + ny * LIGHT[1]));
|
|
1436
|
+
return [q[0] + (Bin[i][0] - q[0]) * f, q[1] + (Bin[i][1] - q[1]) * f];
|
|
1437
|
+
});
|
|
1438
|
+
const shadeBase = `${line(B)} ${segs([...B].reverse())} Z`, shadeOpen = `${line(U)} ${segs([...inner].reverse())} Z`;
|
|
1439
|
+
const jawShade = m.shade ? { base: shadeBase, shapes: { gape: morphTowards(shadeBase, shadeOpen, GAPE_FULL) } } : undefined;
|
|
1440
|
+
// The mouth unhinged: its corners on the jaw's sides a third of the way down (pulled down and out
|
|
1441
|
+
// to the head's width), the top lip under the nose, the round lower edge a chin's thickness above
|
|
1442
|
+
// the dropped chin.
|
|
1443
|
+
const margin = u * 0.06, sc = 0.3;
|
|
1444
|
+
const cr = jawAt(Math.asin(sc)), cl = jawAt(Math.PI - Math.asin(sc));
|
|
1445
|
+
const l = [cl[0] + margin, cl[1]], rr = [cr[0] - margin, cr[1]];
|
|
1446
|
+
const bottom = Yj + b - u * 0.16;
|
|
1447
|
+
const sample = (d) => parsePath(d).flatMap((sp) => {
|
|
1448
|
+
const out = [];
|
|
1449
|
+
let p0 = sp.start;
|
|
1450
|
+
for (const [c1, c2, e] of sp.segments) {
|
|
1451
|
+
for (let k = 0; k <= 8; k++) {
|
|
1452
|
+
const t = k / 8, mt = 1 - t;
|
|
1453
|
+
out.push([mt ** 3 * p0[0] + 3 * mt * mt * t * c1[0] + 3 * mt * t * t * c2[0] + t ** 3 * e[0], mt ** 3 * p0[1] + 3 * mt * mt * t * c1[1] + 3 * mt * t * t * c2[1] + t ** 3 * e[1]]);
|
|
1454
|
+
}
|
|
1455
|
+
p0 = e;
|
|
1456
|
+
}
|
|
1457
|
+
return out;
|
|
1458
|
+
});
|
|
1459
|
+
let top = at.mid[1] - u * 0.06, made = gapeMouth(l, rr, top, bottom, { out: (rr[0] - l[0]) * 0.07, teeth: u * 0.13 }), clear = false;
|
|
1460
|
+
for (let i = 0; i < 30; i++) {
|
|
1461
|
+
const half = morphTowards(at.mouth, made.mouth, GAPE_FULL);
|
|
1462
|
+
if (![...sample(made.mouth), ...sample(half)].some(touchesNose)) {
|
|
1463
|
+
clear = true;
|
|
1464
|
+
break;
|
|
1465
|
+
}
|
|
1466
|
+
top += u * 0.015;
|
|
1467
|
+
made = gapeMouth(l, rr, top, bottom, { out: (rr[0] - l[0]) * 0.07, teeth: u * 0.13 });
|
|
1468
|
+
}
|
|
1469
|
+
return {
|
|
1470
|
+
mouth: morphTowards(at.mouth, made.mouth, GAPE_FULL),
|
|
1471
|
+
teeth: morphTowards(at.teeth, made.teeth, GAPE_FULL),
|
|
1472
|
+
tongue: morphTowards(at.tongue, made.tongue, GAPE_FULL),
|
|
1473
|
+
jaw, jawShade, jawLine, clear,
|
|
1474
|
+
};
|
|
1475
|
+
}
|
|
1476
|
+
// ------------------------------------------------------------------ shoes, sleeves, shorts
|
|
1477
|
+
// ------------------------------------------------------------------ the builder
|
|
1478
|
+
/**
|
|
1479
|
+
* Options of `cartoonCharacter`: `pitch` (degrees) — how far above the camera sees the character
|
|
1480
|
+
* (a room camera's pitch, `viewAt`): every drawing is made at that angle.
|
|
1481
|
+
*/
|
|
1482
|
+
/**
|
|
1483
|
+
* The torso as an occluder of a solid: the same ellipsoids as its drawing (`model`), a hair smaller,
|
|
1484
|
+
* its hips ending at the drawing's cut — so a limb behind it is hidden right to the drawing's edge.
|
|
1485
|
+
*/
|
|
1486
|
+
function torsoOccluder(m, look) {
|
|
1487
|
+
const { b } = m;
|
|
1488
|
+
const heavy = look.heavy ?? 0;
|
|
1489
|
+
const T = b.T, L = b.L, k = 0.98;
|
|
1490
|
+
const W = b.W * (look.female ? 0.86 : 1) + heavy * b.W * 0.35, H = b.H * (look.female ? 1.04 : 1) + heavy * b.H * 0.2;
|
|
1491
|
+
const sh = m.sh;
|
|
1492
|
+
const at = (y, z = 0) => ({ bone: "body", at: [0, r(y + L), r(z)] });
|
|
1493
|
+
const ell = (y, z, rad) => ({ from: at(y, z), ellipsoid: rad.map((v) => r(v * k)) });
|
|
1494
|
+
const hipR = T * m.hipsZ[2];
|
|
1495
|
+
// The materials limbs grow out of with no seam (`join`): the torso's colour at the shoulder (a
|
|
1496
|
+
// sleeve out of the shirt) and at the hip (a trouser leg out of the seat).
|
|
1497
|
+
const join = [...new Set([m.torsoColor([b.S * 0.95, m.y.shoulder + T * 0.08, 0]), m.torsoColor([H * 0.9, -L - 4, 0])])];
|
|
1498
|
+
return {
|
|
1499
|
+
join,
|
|
1500
|
+
blend: r(T * 0.12),
|
|
1501
|
+
shapes: [
|
|
1502
|
+
ell(-L - T * 0.68, 0, [b.S * m.chest, T * 0.36, b.D]),
|
|
1503
|
+
ell(-L - T * m.belly[0], heavy * b.D * 0.55, [W, T * m.belly[1], b.D * (1 + heavy * 0.6)]),
|
|
1504
|
+
ell(-L + 5 - hipR, b.D * m.hipsZ[0], [Math.max(W * 0.98, H * 0.95), hipR, b.D * m.hipsZ[1]]),
|
|
1505
|
+
{ from: { bone: "body", at: [r(-b.S + sh * 0.6), r(m.y.shoulder + L), 0] }, to: { bone: "body", at: [r(b.S - sh * 0.6), r(m.y.shoulder + L), 0] }, r: r(sh * k) },
|
|
1506
|
+
],
|
|
1507
|
+
};
|
|
1508
|
+
}
|
|
1509
|
+
/**
|
|
1510
|
+
* The legs, the hips and the skirt as a solid on the 3D skeleton: each leg a thigh and a shin cone
|
|
1511
|
+
* (a round knee between them), shorts or socks over them, the hips between the hip joints, a skirt
|
|
1512
|
+
* draped over the lap. The torso is an occluder (the torso's drawing is under the solid).
|
|
1513
|
+
*/
|
|
1514
|
+
function legsSolid(m, look, j, calf = 1.12) {
|
|
1515
|
+
void j;
|
|
1516
|
+
const { b } = m;
|
|
1517
|
+
const heavy = look.heavy ?? 0;
|
|
1518
|
+
const bare = look.bottom !== "pants";
|
|
1519
|
+
const shorts = look.bottom === "shorts" || look.bottom === "bermuda";
|
|
1520
|
+
const skirted = look.bottom === "skirt" || look.bottom === "longSkirt";
|
|
1521
|
+
const shortsTo = look.bottom === "bermuda" ? 0.9 : 0.45;
|
|
1522
|
+
// TV proportions: the thighs side by side as wide as the hips as drawn (the hip joints half way
|
|
1523
|
+
// out, `hipHalf`); trouser legs straight tubes of nearly even width (no knee, no calf: the cloth hangs
|
|
1524
|
+
// over them), bare legs tapering to a slim ankle.
|
|
1525
|
+
const tv = m.style.proportions === "tv";
|
|
1526
|
+
const R0 = m.hipHalf * 0.5;
|
|
1527
|
+
const thigh = tv ? [r(R0), r(R0 * (bare ? 0.7 : 0.88))] : [r(b.leg[0] * THIGH_R * (1 + heavy * 0.35)), r(b.leg[0] * 0.6)];
|
|
1528
|
+
const shin = tv ? (bare ? [r(R0 * 0.6), r(b.leg[1] * 0.5)] : [r(R0 * 0.88), r(R0 * 0.78)]) : [r(b.leg[0] * 0.52), r(b.leg[1] * 0.5)];
|
|
1529
|
+
if (tv && !bare)
|
|
1530
|
+
calf = 1;
|
|
1531
|
+
const pt = (bone, t = 0, at) => ({ bone, ...(t ? { t } : {}), ...(at ? { at: at.map(r) } : {}) });
|
|
1532
|
+
const paint = (key) => ({ fill: `palette(${key})`, ...(m.shade ? { shade: `palette(${key}Shade)` } : {}), stroke: `palette(${key}Line)`, strokeWidth: lw(SW), join: [key] });
|
|
1533
|
+
const bodies = [];
|
|
1534
|
+
const T = b.T, D = b.D * (1 + heavy * 0.6);
|
|
1535
|
+
bodies.push(torsoOccluder(m, look));
|
|
1536
|
+
// The hips: between the hip joints, a little back (the seat).
|
|
1537
|
+
const hipsKey = look.top === "dress" ? "top" : bare && !shorts && !skirted ? "skin" : "bottom";
|
|
1538
|
+
// Cartoon proportions: the legs grow out of a round seat — the torso's own cross-section at the
|
|
1539
|
+
// hips, just under its drawing (as a skirt's top) — each leg one smooth volume with it (trousers),
|
|
1540
|
+
// or the shorts' legs with it over bare legs: no peg in a socket, no step at the waist.
|
|
1541
|
+
const organic = m.style.proportions === "cartoon" || m.style.proportions === "tv";
|
|
1542
|
+
// The seat is the torso's own hip volume (its occluder's, a little smaller): under the torso's
|
|
1543
|
+
// drawing, it is there only for the legs to blend into — the trousers start at the torso's hem as
|
|
1544
|
+
// wide as it and the thighs part below it, no block or bulge at the hips.
|
|
1545
|
+
const hipsEll = torsoOccluder(m, look).shapes.filter((x) => x.ellipsoid).pop();
|
|
1546
|
+
// (Its lower half, flattened, at the torso's bottom: hidden above it, under the hem it fills the
|
|
1547
|
+
// crotch before the thighs part.)
|
|
1548
|
+
const hf = hipsEll.from, [hx, hy, hz] = hipsEll.ellipsoid;
|
|
1549
|
+
const seat = { from: { ...hf, at: [hf.at[0], r(hf.at[1] + hy), hf.at[2]] }, ellipsoid: [r(hx), r(Math.min(hy * 0.5, b.T * 0.1)), r(hz * 0.94)] };
|
|
1550
|
+
// (TV: a small blend — a big one swells the trouser leg wherever its cones meet, at the knee.)
|
|
1551
|
+
const seatBlend = r(tv ? Math.min(6, thigh[0] * 0.3) : thigh[0] * 0.6);
|
|
1552
|
+
// (Under a skirt the cloth is the hips; otherwise no line where they come out of the torso.)
|
|
1553
|
+
if (!skirted && !organic)
|
|
1554
|
+
bodies.push({ ...paint(hipsKey), blend: 6, seamless: true, shapes: [{ from: pt("legF1", 0, [0, -4, -6]), to: pt("legB1", 0, [0, -4, -6]), r: r(b.H * 0.5 * (1 + heavy * 0.15)) }] });
|
|
1555
|
+
for (const side of ["B", "F"]) {
|
|
1556
|
+
const legKey = bare ? "skin" : "bottom";
|
|
1557
|
+
const withSeat = organic && !bare;
|
|
1558
|
+
bodies.push({ ...paint(legKey), blend: withSeat ? seatBlend : 5, ...(withSeat ? { seamless: true } : {}), shapes: [
|
|
1559
|
+
...(withSeat ? [seat] : []),
|
|
1560
|
+
{ from: pt(`leg${side}1`), to: pt(`leg${side}1`, 1), r: thigh },
|
|
1561
|
+
// (A trouser leg's round end inside the shoe, not past its sole.)
|
|
1562
|
+
{ from: pt(`leg${side}2`), to: pt(`leg${side}2`, tv && !bare ? r(1 - (shin[1] * 0.7) / (b.L * 0.5)) : 0.97), r: shin },
|
|
1563
|
+
// The calf: a swell at the back of the shin, below the knee.
|
|
1564
|
+
...(calf > 1 ? [{ from: pt(`leg${side}2`, 0.18, [0, 0, -shin[0] * 0.25]), to: pt(`leg${side}2`, 0.5, [0, 0, -shin[0] * 0.12]), r: [r(shin[0] * calf), r(shin[0] * (calf - 0.12))] }] : []),
|
|
1565
|
+
] });
|
|
1566
|
+
if (shorts && organic)
|
|
1567
|
+
// The shorts' leg out of the seat, flaring a little to the hem.
|
|
1568
|
+
// (TV: a straight tube just over the thigh, its hem as wide as its top.)
|
|
1569
|
+
bodies.push({ ...paint("bottom"), blend: seatBlend, seamless: true, shapes: [seat, { from: pt(`leg${side}1`, -0.04), to: pt(`leg${side}1`, shortsTo), r: tv ? [r(thigh[0] + 0.8), r(thigh[0] + 0.8)] : [r(thigh[0] + 3), r(thigh[0] + (thigh[1] - thigh[0]) * shortsTo + 6)] }] });
|
|
1570
|
+
else if (shorts)
|
|
1571
|
+
bodies.push({ ...paint("bottom"), shapes: [{ from: pt(`leg${side}1`, -0.04), to: pt(`leg${side}1`, shortsTo), r: [thigh[0] + 3, r(thigh[0] + (thigh[1] - thigh[0]) * shortsTo + 3)] }] });
|
|
1572
|
+
if (look.socks)
|
|
1573
|
+
bodies.push({ ...paint("socks"), strokeWidth: 2.4, shapes: [{ from: pt(`leg${side}2`, 0.8), to: pt(`leg${side}2`, 0.97), r: [r(shin[1] + 1.5), r(shin[1] + 2)] }] });
|
|
1574
|
+
}
|
|
1575
|
+
// Shoes: an upper from the heel to the toe on a sole, flat on the floor standing.
|
|
1576
|
+
// (Never thinner than the ankle: thick limbs get bigger shoes.)
|
|
1577
|
+
const k = Math.max(b.shoe, shin[1] / 9.5);
|
|
1578
|
+
const v = (x, y, z) => [r(x * k), r(y * k), r(z * k)];
|
|
1579
|
+
for (const side of ["B", "F"]) {
|
|
1580
|
+
const foot = `foot${side}`;
|
|
1581
|
+
const sole = (key, h = 2.5, at = v(0, 10, 13)) => ({ ...paint(key), strokeWidth: 2.4, shapes: [{ from: pt(foot, 0, at), box: v(10, h, 25), round: r(Math.min(h, 2.5) * k) }] });
|
|
1582
|
+
if (look.shoes === "flipflops" || look.shoes === "sandals") {
|
|
1583
|
+
bodies.push(sole("shoes", 1.6, v(0, 10.5, 13)));
|
|
1584
|
+
bodies.push({ ...paint("skin"), strokeWidth: 2.4, blend: 3, shapes: [{ from: pt(foot, 0, v(0, 2, -5)), to: pt(foot, 0, v(0, 4.5, 30)), r: [r(8 * k), r(6.5 * k)] }] });
|
|
1585
|
+
bodies.push({ ...paint("shoes"), strokeWidth: 2, shapes: look.shoes === "flipflops"
|
|
1586
|
+
? [{ from: pt(foot, 0, v(-7, 2, 14)), to: pt(foot, 0, v(0, -1, 20)), r: r(2.6 * k) }, { from: pt(foot, 0, v(7, 2, 14)), to: pt(foot, 0, v(0, -1, 20)), r: r(2.6 * k) }]
|
|
1587
|
+
: [-2, 12, 24].map((z) => ({ from: pt(foot, 0, v(-8, 2, z)), to: pt(foot, 0, v(8, 2, z)), r: r(3 * k) })) });
|
|
1588
|
+
}
|
|
1589
|
+
else if (look.shoes === "heels") {
|
|
1590
|
+
bodies.push({ ...paint("shoes"), strokeWidth: 2.4, blend: 3, shapes: [
|
|
1591
|
+
{ from: pt(foot, 0, v(0, -1, -5)), to: pt(foot, 0, v(0, 6, 30)), r: [r(9.5 * k), r(7 * k)] },
|
|
1592
|
+
{ from: pt(foot, 0, v(0, 6, -5)), box: v(3, 6, 3), round: r(1.5 * k) },
|
|
1593
|
+
] });
|
|
1594
|
+
}
|
|
1595
|
+
else if (tv) {
|
|
1596
|
+
// TV proportions: a simple shoe on a flat sole, wider than the ankle (or the trousers' hem).
|
|
1597
|
+
const s = b.shoe, wHalf = Math.max(shin[1] + 4.5, 11.5 * s), len = 26 * s;
|
|
1598
|
+
const at = (y, z) => [0, r(y), r(z)];
|
|
1599
|
+
const top = 12 * s - 4.4 * s + 1.5 * s;
|
|
1600
|
+
bodies.push({ ...paint(look.shoes === "dress" ? "shoes" : "sole"), strokeWidth: 2.4, shapes: [{ from: pt(foot, 0, at(12 * s - 2.2 * s, 9 * s)), box: [r(wHalf), r(2.2 * s), r(len)], round: r(2 * s) }] });
|
|
1601
|
+
// The upper: high round the ankle, lower and a little wider over the toes (one volume).
|
|
1602
|
+
bodies.push({ ...paint("shoes"), strokeWidth: 2.4, blend: r(5 * s), shapes: [
|
|
1603
|
+
{ from: pt(foot, 0, at(top - 8.5 * s, -3 * s)), box: [r(wHalf * 0.9), r(8.5 * s), r(len * 0.5)], round: r(Math.min(7.5 * s, wHalf * 0.8)) },
|
|
1604
|
+
{ from: pt(foot, 0, at(top - 5.5 * s, 17 * s)), box: [r(wHalf * 0.97), r(5.5 * s), r(len * 0.62)], round: r(5.2 * s) },
|
|
1605
|
+
] });
|
|
1606
|
+
}
|
|
1607
|
+
else {
|
|
1608
|
+
bodies.push(sole(look.shoes === "dress" ? "shoes" : "sole"));
|
|
1609
|
+
bodies.push({ ...paint("shoes"), strokeWidth: 2.4, blend: 4, shapes: [{ from: pt(foot, 0, v(0, 1, -7)), to: pt(foot, 0, v(0, 3.5, 32)), r: [r(10.5 * k), r(9 * k)] }] });
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
if (look.cape) {
|
|
1613
|
+
// A cape from the shoulders down the back to the knees, wider at the hem: on the torso's bone,
|
|
1614
|
+
// hanging behind it.
|
|
1615
|
+
// (Thin: a sheet of cloth a little behind the back, wider than the shoulders.)
|
|
1616
|
+
const top = -b.T * 0.9, bottom = b.L * 0.55, t = b.D * 0.12;
|
|
1617
|
+
bodies.push({ ...paint("cape"), blend: 4, shapes: [
|
|
1618
|
+
{ from: pt("body", 0, [0, r((top + bottom) / 2), r(-b.D * (1 + heavy * 0.6) - t * 1.4)]), box: [r(b.S * 1.12), r((bottom - top) / 2), r(t)], round: r(t * 0.9) },
|
|
1619
|
+
] });
|
|
1620
|
+
}
|
|
1621
|
+
if (skirted) {
|
|
1622
|
+
// Cloth from the hips over both thighs (the torso drawing has its top), wider at the hem: an
|
|
1623
|
+
// A-line standing, over the lap seated.
|
|
1624
|
+
const long = look.bottom === "longSkirt";
|
|
1625
|
+
// The cloth hugs the thighs (a valley between the knees) and ends just above them: seated,
|
|
1626
|
+
// the knees show under the hem.
|
|
1627
|
+
// The top: the torso's own cross-section (wide, not as deep), just under its drawing, so the
|
|
1628
|
+
// cloth comes out of it with no step at the waist; then an A-line over the thighs.
|
|
1629
|
+
const kneeR = long ? thigh[1] + b.H * 0.16 : thigh[1] + b.H * 0.14;
|
|
1630
|
+
const hipHalf = Math.max(b.W * 0.98, b.H * 0.95) * (look.female ? 0.9 : 1) + heavy * b.W * 0.3;
|
|
1631
|
+
// TV proportions, long: the hem is flat — the cloth's round ends stop at it (`hemT` along the
|
|
1632
|
+
// shin) and a slab round each shin squares it off, following the legs.
|
|
1633
|
+
const hemR = kneeR + b.H * 0.16, hemT = 0.42, shinLen = b.L * 0.5;
|
|
1634
|
+
const flat = tv && long, endT = flat ? Math.max(0.02, hemT - hemR / shinLen) : hemT, slab = hemR * 0.45;
|
|
1635
|
+
// (TV: a small blend — a simple A-line, not swelling at the knees.)
|
|
1636
|
+
bodies.push({ ...paint(look.top === "dress" ? "top" : "bottom"), blend: r(tv ? Math.min(6, b.H * 0.3) : b.H * 0.3), seamless: true, shapes: [
|
|
1637
|
+
{ from: pt("hips", 0, [0, r(b.H * 0.1), heavy * b.D * 0.3]), box: [r(hipHalf * 0.88), r(b.H * 0.12), r(D * 0.78)], round: r(D * 0.4) },
|
|
1638
|
+
...["F", "B"].flatMap((side) => [
|
|
1639
|
+
{ from: pt(`leg${side}1`, 0.05), to: pt(`leg${side}1`, long ? 1 : 0.9), r: [r(thigh[0] + 5), r(kneeR)] },
|
|
1640
|
+
// (Long: to mid-calf, the hem's round end still above the ankles — the shoes and a bit of
|
|
1641
|
+
// leg show under it, never inside the cloth.)
|
|
1642
|
+
...(long ? [{ from: pt(`leg${side}2`), to: pt(`leg${side}2`, r(endT)), r: [r(kneeR), r(hemR)] }] : []),
|
|
1643
|
+
...(flat ? [{ from: pt(`leg${side}2`, r(hemT - slab / shinLen)), box: [r(hemR), r(slab), r(hemR * 0.92)], round: 3 }] : []),
|
|
1644
|
+
]),
|
|
1645
|
+
// One cloth, not two trouser legs: it spans the gap between the legs as they part (a stride,
|
|
1646
|
+
// a step to the side) — across the thighs, at the knees and, long, at the hem.
|
|
1647
|
+
...[
|
|
1648
|
+
[`legF1`, `legB1`, 0.5, thigh[0] + 5],
|
|
1649
|
+
[`legF1`, `legB1`, long ? 1 : 0.9, kneeR],
|
|
1650
|
+
...(long ? (flat ? [[`legF2`, `legB2`, endT, hemR]] : [[`legF2`, `legB2`, 0.25, kneeR + b.H * 0.08], [`legF2`, `legB2`, 0.42, hemR]]) : []),
|
|
1651
|
+
].map(([a, c, t, rad]) => ({ from: pt(a, t), to: pt(c, t), r: [r(rad), r(rad)] })),
|
|
1652
|
+
] });
|
|
1653
|
+
}
|
|
1654
|
+
return { id: "legs", type: "solid", step: 2, bodies };
|
|
1655
|
+
}
|
|
1656
|
+
export function cartoonCharacter(look, opts = {}) {
|
|
1657
|
+
// Lipstick colours the lips (a thicker outline); the inside of the mouth stays dark.
|
|
1658
|
+
const style = styleOf(opts.style);
|
|
1659
|
+
WEIGHT.w = style.line ?? SW;
|
|
1660
|
+
WEIGHT.uniform = style.line !== undefined;
|
|
1661
|
+
SNUG.on = style.proportions === "tv";
|
|
1662
|
+
try {
|
|
1663
|
+
return buildCartoon(look, opts, style);
|
|
1664
|
+
}
|
|
1665
|
+
finally {
|
|
1666
|
+
WEIGHT.w = SW;
|
|
1667
|
+
WEIGHT.uniform = false;
|
|
1668
|
+
SNUG.on = false;
|
|
1669
|
+
}
|
|
1670
|
+
}
|
|
1671
|
+
function buildCartoon(look, opts, style) {
|
|
1672
|
+
const [LIPS, LIPS_W] = look.lipstick ? ["palette(lips)", lw(3.6)] : ["palette(mouthLine)", lw(2.4)];
|
|
1673
|
+
const anatomy = opts.anatomy ?? styledAnatomy(cartoonAnatomy(look), style, look);
|
|
1674
|
+
const broken = checkAnatomy(anatomy).filter((x) => x.severity === "error");
|
|
1675
|
+
if (broken.length)
|
|
1676
|
+
throw new Error(`${look.name}: anatomy breaks ${broken.length} rule(s): ${broken.map((x) => `${x.rule} — ${x.message}`).join("; ")}`);
|
|
1677
|
+
const m = model(look, false, anatomy, style);
|
|
1678
|
+
const { b } = m;
|
|
1679
|
+
const u = b.u;
|
|
1680
|
+
const pitch = deg(opts.pitch ?? 0);
|
|
1681
|
+
/** A drawn angle seen from the camera's pitch. */
|
|
1682
|
+
const vw = (v) => ({ yaw: VIEWS[v], pitch });
|
|
1683
|
+
/** A drawn angle of the head (nearly level, put back on the neck). */
|
|
1684
|
+
const vh = (v) => headAngle(m, VIEWS[v], pitch);
|
|
1685
|
+
// Joints in body space (the front view), and where the main view (three-quarter) draws them.
|
|
1686
|
+
const j3 = {};
|
|
1687
|
+
const at = (x, y, z = 0) => {
|
|
1688
|
+
const p = project([x, y, z], vw("profile"));
|
|
1689
|
+
return [r(p[0]), r(p[1])];
|
|
1690
|
+
};
|
|
1691
|
+
const joint = (name, x, y, z = 0) => ((j3[name] = [x, y, z]), at(x, y, z));
|
|
1692
|
+
// Joints (front-view model) → the three-quarter skeleton.
|
|
1693
|
+
const tv = style.proportions === "tv";
|
|
1694
|
+
// (TV proportions: the hip joints half way out the hips as drawn, the thighs touching under the
|
|
1695
|
+
// seat — one pair of trousers, parting below the crotch.)
|
|
1696
|
+
const sx = b.S - b.arm[0] * 0.35, hx = tv ? m.hipHalf * 0.5 - 1.5 : b.H * 0.5;
|
|
1697
|
+
const armLen = armLength(anatomy.body);
|
|
1698
|
+
// A slouch: the shoulders a little lower, the elbows bent back, the hands forward.
|
|
1699
|
+
const slouch = style.posture === "slouch";
|
|
1700
|
+
const shY = m.y.shoulder + 4 + (slouch ? b.T * 0.04 : 0);
|
|
1701
|
+
const belly = (look.heavy ?? 0) * b.W * 0.35;
|
|
1702
|
+
const waistX = b.W * (look.female ? 0.86 : 1) + (look.heavy ?? 0) * b.W * 0.35 - belly;
|
|
1703
|
+
const hipX = Math.max(waistX * 0.98, (b.H * (look.female ? 1.04 : 1) + (look.heavy ?? 0) * b.H * 0.2) * 0.95) - belly;
|
|
1704
|
+
const ankle = 12 * b.shoe;
|
|
1705
|
+
// Cartoon proportions hang the arms loose (an A, not two posts along the body): the elbow a little
|
|
1706
|
+
// out, the hand further out and forward, the arm a little bent.
|
|
1707
|
+
// (TV proportions: a little off the body, the hands by the thighs.)
|
|
1708
|
+
const loose = style.proportions === "cartoon";
|
|
1709
|
+
let [eOut, hOut, hDrop, hFwd] = loose ? [armLen * 0.05, armLen * 0.16, 0.95, 20] : tv ? [0, 0, 0.97, 14] : [0, 0, slouch ? 0.93 : 0.98, slouch ? 28 : 16];
|
|
1710
|
+
if (tv) {
|
|
1711
|
+
// Hanging just clear of the torso as drawn (from a little below the shoulder down), so the arm's
|
|
1712
|
+
// outline shows all along its side — no arm melting into a sleeve-coloured side: the elbow and
|
|
1713
|
+
// the hand only as far out as that takes.
|
|
1714
|
+
const half = (yy) => {
|
|
1715
|
+
let x = 0;
|
|
1716
|
+
for (let z = -b.D * 1.6; z <= b.D * 1.6; z += b.D / 8)
|
|
1717
|
+
for (let xx = x; xx < b.S * 3; xx += 1)
|
|
1718
|
+
if (m.torso(xx, yy, z) < 0)
|
|
1719
|
+
x = xx;
|
|
1720
|
+
return x;
|
|
1721
|
+
};
|
|
1722
|
+
const elbowY = shY + armLen * 0.5, handY = shY + armLen * hDrop;
|
|
1723
|
+
const rArm = (t) => b.arm[0] * 0.5 + (b.arm[1] - b.arm[0]) * 0.5 * t;
|
|
1724
|
+
const ex0 = Math.max(sx + 3, waistX + b.arm[0] * 0.55) + belly * 0.5, hx0 = Math.max(sx + 4, hipX + b.arm[1] * 0.6) + belly;
|
|
1725
|
+
let ex = ex0;
|
|
1726
|
+
for (let t = 0.5; t <= 1.001; t += 0.1)
|
|
1727
|
+
ex = Math.max(ex, sx + (half(shY + (elbowY - shY) * t) + rArm(t * 0.5) - sx) / t);
|
|
1728
|
+
let hxx = Math.max(hx0, ex + armLen * 0.02);
|
|
1729
|
+
for (let t = 0.1; t <= 1.001; t += 0.1) {
|
|
1730
|
+
const yy = elbowY + (handY - elbowY) * t;
|
|
1731
|
+
if (yy < m.y.hip)
|
|
1732
|
+
hxx = Math.max(hxx, ex + (half(yy) + rArm(0.5 + t * 0.5) + 1 - ex) / t);
|
|
1733
|
+
}
|
|
1734
|
+
eOut = ex - ex0;
|
|
1735
|
+
hOut = hxx - hx0;
|
|
1736
|
+
}
|
|
1737
|
+
const j = {
|
|
1738
|
+
// Arms at rest hang relaxed: the elbow a little bent, the hand a little forward, out past a belly.
|
|
1739
|
+
// (Outside the torso's sides — waist and hips — so a hanging arm is beside the body from any angle.)
|
|
1740
|
+
shoulderF: joint("shoulderF", -sx, shY), elbowF: joint("elbowF", -Math.max(sx + 3, waistX + b.arm[0] * 0.55) - belly * 0.5 - eOut, shY + armLen * 0.5, slouch ? -9 : -2), handF: joint("handF", -Math.max(sx + 4, hipX + b.arm[1] * 0.6) - belly - hOut, shY + armLen * hDrop, hFwd),
|
|
1741
|
+
shoulderB: joint("shoulderB", sx, shY), elbowB: joint("elbowB", Math.max(sx + 3, waistX + b.arm[0] * 0.55) + belly * 0.5 + eOut, shY + armLen * 0.5, slouch ? -9 : -2), handB: joint("handB", Math.max(sx + 4, hipX + b.arm[1] * 0.6) + belly + hOut, shY + armLen * hDrop, hFwd),
|
|
1742
|
+
hipF: joint("hipF", -hx, m.y.hip), kneeF: joint("kneeF", -hx, m.y.hip / 2, 6), footF: joint("footF", -hx, -ankle),
|
|
1743
|
+
hipB: joint("hipB", hx, m.y.hip), kneeB: joint("kneeB", hx, m.y.hip / 2, 6), footB: joint("footB", hx, -ankle),
|
|
1744
|
+
};
|
|
1745
|
+
const eyeMid = project([0, m.y.eye, u * 0.9], vh("profile"));
|
|
1746
|
+
// Hip to ankle (the gaits' crouch, knee lift and heel kick are fractions of it).
|
|
1747
|
+
const legLen = r(Math.abs(j.footF[1] - j.hipF[1]));
|
|
1748
|
+
const views = Object.fromEntries(Object.keys(VIEWS).map((v) => [v, { head: headView(m, vh(v)), body: bodyView(m, look, vw(v)), face: faceView(m, look, vh(v)) }]));
|
|
1749
|
+
const longSleeves = ["jacket", "cardigan", "blazer"].includes(look.top);
|
|
1750
|
+
const shortSleeves = !longSleeves && !["tank", "overalls", "dress"].includes(look.top) || look.top === "overalls";
|
|
1751
|
+
const bareLegs = look.bottom !== "pants";
|
|
1752
|
+
const shorts = look.bottom === "shorts" || look.bottom === "bermuda";
|
|
1753
|
+
const skirted = look.bottom === "skirt" || look.bottom === "longSkirt";
|
|
1754
|
+
const shortsTo = look.bottom === "bermuda" ? 0.9 : 0.45;
|
|
1755
|
+
// Points of the head for props fitted to the body (a phone at the ear, a cigarette at the mouth),
|
|
1756
|
+
// where each drawn angle puts them: the near ear, the middle of the mouth, between the eyes, the top.
|
|
1757
|
+
const headPoints = Object.fromEntries(Object.keys(VIEWS).map((v) => {
|
|
1758
|
+
const th = vh(v), sp = onSurface(m.head, th);
|
|
1759
|
+
return [v, {
|
|
1760
|
+
// The ear canal: on the near ear, between the height of the eyes and of the nose.
|
|
1761
|
+
ear: project([-u * 0.97, m.y.head + u * 0.22, -u * 0.02], th),
|
|
1762
|
+
// The other ear (the far hand's, when someone turned round keeps holding the phone).
|
|
1763
|
+
earB: project([u * 0.97, m.y.head + u * 0.22, -u * 0.02], th),
|
|
1764
|
+
// (Where the face's rules put the mouth in this angle.)
|
|
1765
|
+
mouth: views[v].face.mouthAt,
|
|
1766
|
+
eye: sp.point(0, m.y.eye, u * 0.04),
|
|
1767
|
+
top: project([0, m.y.top, 0], th),
|
|
1768
|
+
}];
|
|
1769
|
+
}));
|
|
1770
|
+
// The arm's radii at the shoulder, the elbow and the wrist: tube arms one width; TV proportions a
|
|
1771
|
+
// gentle taper.
|
|
1772
|
+
const tube = style.limbs === "tube";
|
|
1773
|
+
const elbowR = r((b.arm[0] + b.arm[1]) * 0.25);
|
|
1774
|
+
const [shoulderR, wristR] = tv ? [r(b.arm[0] * 0.5), r(b.arm[1] * 0.5)] : tube ? [elbowR, elbowR] : [r(b.arm[0] * 0.5), r(b.arm[1] * 0.5)];
|
|
1775
|
+
// TV proportions: hands drawn as one silhouette with the forearm (no line across the wrist), turned
|
|
1776
|
+
// along the forearm at rest.
|
|
1777
|
+
const handOpts = (side) => tv ? { wrist: wristR, cuff: longSleeves, dir: r((Math.atan2(j[`hand${side}`][1] - j[`elbow${side}`][1], j[`hand${side}`][0] - j[`elbow${side}`][0]) * 180) / Math.PI) } : {};
|
|
1778
|
+
const handArt = (side, mirror = false) => cartoonHands(j[`hand${side}`], { r: b.hand * (side === "F" ? 1.2 : 1.16), fill: side === "B" && m.shade ? "palette(skinShade)" : "palette(skin)", line: "palette(skinLine)", stroke: lw(2.2), detail: lw(2.2 * 0.7), merged: style.limbs === "tube" && style.proportions === "cartoon", ...handOpts(side), ...(mirror ? { mirror } : {}) });
|
|
1779
|
+
const art = {
|
|
1780
|
+
...Object.fromEntries(Object.entries(handArt("F")).map(([k, v]) => [`handF_${k}`, v])),
|
|
1781
|
+
...Object.fromEntries(Object.entries(handArt("B")).map(([k, v]) => [`handB_${k}`, v])),
|
|
1782
|
+
// TV proportions: each hand also drawn as the other one (its thumb across the fingers), for the
|
|
1783
|
+
// angles it is seen from its other side (`HAND_MIRROR`).
|
|
1784
|
+
...(tv ? Object.fromEntries(["F", "B"].flatMap((side) => Object.entries(handArt(side, true)).filter(([k]) => k !== "grip").map(([k, v]) => [`hand${side}_${k}M`, v]))) : {}),
|
|
1785
|
+
};
|
|
1786
|
+
// Asymmetric details: a second drawing of the hair and the torso, mirrored on the body, shown when
|
|
1787
|
+
// the character faces left (`side` control).
|
|
1788
|
+
const asym = look.top === "shirt" || !!look.pocketItem || !!look.earItem || look.hair === "sidePart" || !!look.hairLine || !!look.boutonniere;
|
|
1789
|
+
if (asym) {
|
|
1790
|
+
const mm = model(look, true, anatomy, style);
|
|
1791
|
+
for (const v of Object.keys(VIEWS)) {
|
|
1792
|
+
const s = SUFFIX[v], H = headView(mm, vh(v)), B = bodyView(mm, look, vw(v));
|
|
1793
|
+
art[`hairBack${s}M`] = H.hairBack;
|
|
1794
|
+
art[`hairFront${s}M`] = H.hairFront;
|
|
1795
|
+
art[`torso${s}M`] = B.torso;
|
|
1796
|
+
}
|
|
1797
|
+
}
|
|
1798
|
+
for (const v of Object.keys(VIEWS)) {
|
|
1799
|
+
const s = SUFFIX[v], V = views[v];
|
|
1800
|
+
art[`hairBack${s}`] = V.head.hairBack;
|
|
1801
|
+
art[`earsBack${s}`] = V.head.earsBack;
|
|
1802
|
+
art[`head${s}`] = V.head.head;
|
|
1803
|
+
art[`earsFront${s}`] = V.head.earsFront;
|
|
1804
|
+
art[`hairFront${s}`] = V.head.hairFront;
|
|
1805
|
+
art[`tailBack${s}`] = V.head.tailBack;
|
|
1806
|
+
art[`tailFront${s}`] = V.head.tailFront;
|
|
1807
|
+
art[`neck${s}`] = V.body.neck;
|
|
1808
|
+
art[`torso${s}`] = V.body.torso;
|
|
1809
|
+
art[`skirt${s}`] = V.body.skirt;
|
|
1810
|
+
art[`nose${s}`] = V.face.extras;
|
|
1811
|
+
// Over the mouth: the nose (with its nostrils), then a moustache, glasses.
|
|
1812
|
+
art[`over${s}`] = V.head.noseOver + V.face.nose + V.face.over;
|
|
1813
|
+
}
|
|
1814
|
+
const F = views.profile.face;
|
|
1815
|
+
for (const [k, v] of Object.entries(F.eyeVariants))
|
|
1816
|
+
art[`eye_${k}`] = v;
|
|
1817
|
+
art.pupils = F.pupils;
|
|
1818
|
+
art.lids = F.lids;
|
|
1819
|
+
// The arms as solids on the 3D skeleton: the upper arm, the forearm (a round elbow between them),
|
|
1820
|
+
// a short sleeve over the upper arm, a watch — each drawn where it is nearest (a forearm bent
|
|
1821
|
+
// towards the camera in front of the upper arm, from any angle), cel shaded like the legs.
|
|
1822
|
+
const armBodies = (side) => {
|
|
1823
|
+
const pt = (bone, t = 0) => ({ bone, ...(t ? { t } : {}) });
|
|
1824
|
+
const key = (k) => ({ fill: `palette(${k})`, ...(m.shade ? { shade: `palette(${k}Shade)` } : {}), stroke: `palette(${k}Line)`, strokeWidth: lw(SW), join: [k] });
|
|
1825
|
+
const up = `arm${side}1`, lo = `arm${side}2`;
|
|
1826
|
+
// Tube arms: one width from the shoulder to the wrist, the sleeve flush with it (a hem line).
|
|
1827
|
+
const elbow = elbowR, rs = shoulderR, rw = wristR;
|
|
1828
|
+
// The head is drawn over the shoulder (`under`), not over the whole upper arm: the elbow's half
|
|
1829
|
+
// keeps its depth, so an elbow raised in front of the chest (a hand bringing something to the
|
|
1830
|
+
// mouth, under a dropped jaw) is drawn over the chin, not hidden behind it.
|
|
1831
|
+
const mid = r((rs + elbow) / 2);
|
|
1832
|
+
const bodies = [
|
|
1833
|
+
{ ...key(longSleeves ? "top2" : "skin"), blend: 3, shapes: [
|
|
1834
|
+
{ from: pt(up), to: pt(up, SHOULDER_UNDER), r: [rs, mid], under: true },
|
|
1835
|
+
{ from: pt(up, SHOULDER_UNDER), to: pt(up, 1), r: [mid, elbow] },
|
|
1836
|
+
{ from: pt(lo), to: pt(lo, 0.96), r: [elbow, rw] },
|
|
1837
|
+
] },
|
|
1838
|
+
];
|
|
1839
|
+
if (shortSleeves)
|
|
1840
|
+
bodies.push({ ...key("top"), shapes: [{ from: pt(up, -0.04), to: pt(up, 0.42), r: tv ? [r(rs + 1.2), r((rs + elbow) / 2 + 1.6)] : tube ? [r(elbow + 0.8), r(elbow + 0.8)] : [r(b.arm[0] * 0.5 + 5), r(b.arm[0] * 0.5 + 6)], under: true }] });
|
|
1841
|
+
return bodies;
|
|
1842
|
+
};
|
|
1843
|
+
/** A body as an occluder: its shapes hide what is behind them, drawing nothing. */
|
|
1844
|
+
const occluder = (x) => ({ shapes: x.shapes, ...(x.blend ? { blend: x.blend } : {}) });
|
|
1845
|
+
// Both arms in one solid, the torso an occluder: each bit of an arm shows only where it is nearest
|
|
1846
|
+
// (a forearm in front of the chest hidden from behind, one arm over the other).
|
|
1847
|
+
// The head hides them as drawn: its cranium and its face down to the chin (a big head's jaw comes
|
|
1848
|
+
// down past the shoulders), a hair smaller — and, as a cartoon head is drawn over its shoulders,
|
|
1849
|
+
// it is drawn over the shoulder's half of the upper arms and the sleeves (`under`,
|
|
1850
|
+
// `SHOULDER_UNDER`) whatever their depth: a shoulder turned towards the camera never covers the
|
|
1851
|
+
// jaw. The elbow's half and the forearm keep their depth (a phone at the ear, a glass at the mouth,
|
|
1852
|
+
// an elbow raised in front of the chest under a dropped jaw).
|
|
1853
|
+
// It covers the head as drawn, outline included: the drawing is seen nearly level and put back on
|
|
1854
|
+
// the neck (`headAngle`), its chin a little lower than the volume's at the room's pitch — so the
|
|
1855
|
+
// volume grows by the outline and by that difference (the face's depth × the pitch left out).
|
|
1856
|
+
const grow = SW + u * 0.9 * Math.abs(Math.sin(pitch) - Math.sin(pitch * HEAD_PITCH)) + u * 0.02;
|
|
1857
|
+
const headOccluder = { blend: r(u * 0.2), over: r(u * 4), shapes: [...m.headParts, ...m.noseParts].map(([c, rad]) => ({ from: { bone: "head", at: [r(c[0]), r(c[1] - m.y.neckTop), r(c[2])] }, ellipsoid: rad.map((v) => r(v + grow)) })) };
|
|
1858
|
+
// The jaw a gape drops: a 3D bone from a hinge at the back of the head (at the mouth's height) to
|
|
1859
|
+
// the chin, nothing drawn on it (the dropped jaw is a morph of the face: `gapeView`). Turned by the
|
|
1860
|
+
// `jaw` control and the `gape` emotion (`JAW_TURN`° a weight), it takes this occluder down with
|
|
1861
|
+
// the drawn jaw — the cheeks to the chin, as cones from the head to the jaw — so the upper arms
|
|
1862
|
+
// stay behind a dropped chin. At rest it is inside the head's (it changes nothing).
|
|
1863
|
+
const jawFrom = [0, m.y.mouth, -u * 0.5], jawTo = [0, m.y.head + u * anatomy.face.chinY - u * 0.2, u * 0.55];
|
|
1864
|
+
const jawOccluder = { over: r(u * 4), shapes: jawCones(m, jawTo, grow) };
|
|
1865
|
+
// The legs, the hips and a skirt hide the arms too (a hand behind the skirt).
|
|
1866
|
+
const legOccluders = (legsSolid(m, look, j, anatomy.limbs.calf).bodies ?? []).filter((x) => x.fill).map(occluder);
|
|
1867
|
+
// Each arm its own solid (as a cut-out animator keeps the far arm on its own layer): the other arm
|
|
1868
|
+
// an occluder in it. The far arm's (`armsB`) goes behind the whole drawing of the body while its
|
|
1869
|
+
// upper arm is behind the torso (`rig3d.chains`): the torso's own outline then covers where it goes
|
|
1870
|
+
// in — no sliver along the outline, no stump cut off by the torso's volume — and comes back to its
|
|
1871
|
+
// place, composited by depth, when it is not.
|
|
1872
|
+
const armsBSolid = { id: "armsB", type: "solid", step: 2, bodies: [torsoOccluder(m, look), headOccluder, jawOccluder, ...legOccluders, ...armBodies("F").map(occluder), ...armBodies("B")] };
|
|
1873
|
+
const armsSolid = { id: "arms", type: "solid", step: 2, bodies: [torsoOccluder(m, look), headOccluder, jawOccluder, ...legOccluders, ...armBodies("B").map(occluder), ...armBodies("F")] };
|
|
1874
|
+
// A wristwatch: a band round a forearm, its own solid behind the same occluders as the arms (and
|
|
1875
|
+
// the arms themselves), one per wrist. The wearer keeps it on the same wrist whichever way they
|
|
1876
|
+
// face: the near arm facing right, the far arm facing left (the rig mirrored) — `sideSwitch`
|
|
1877
|
+
// (set by the `side` control from the actor's facing) picks the wrist.
|
|
1878
|
+
const watchSolids = look.watch
|
|
1879
|
+
? ["F", "B"].map((side) => ({
|
|
1880
|
+
id: `watch${side}`, type: "solid", step: 2,
|
|
1881
|
+
visibleWhen: { part: "sideSwitch", variant: side === "F" ? "r" : "l" },
|
|
1882
|
+
bodies: [
|
|
1883
|
+
torsoOccluder(m, look), headOccluder, jawOccluder, ...legOccluders, ...armBodies("B").map(occluder), ...armBodies("F").map(occluder),
|
|
1884
|
+
{ fill: "palette(watch)", ...(m.shade ? { shade: "palette(watchShade)" } : {}), stroke: "palette(watchLine)", strokeWidth: lw(2.2), shapes: [{ from: { bone: `arm${side}2`, t: 0.8 }, to: { bone: `arm${side}2`, t: 0.88 }, r: r(b.arm[1] * 0.5 + 2.5) }] },
|
|
1885
|
+
],
|
|
1886
|
+
}))
|
|
1887
|
+
: [];
|
|
1888
|
+
// (The ponytail's bone where the head's drawing puts it.)
|
|
1889
|
+
const tail = m.tail ? [project(m.tail.pivot, vh("profile")), project(m.tail.end, vh("profile"))] : undefined;
|
|
1890
|
+
const parts = [
|
|
1891
|
+
// Which side of the body faces the camera (set by the `side` control): picks the watch's wrist.
|
|
1892
|
+
{ id: "sideSwitch", type: "switch", bone: "root", variants: { r: "", l: "" }, default: "r" },
|
|
1893
|
+
{ id: "shadow", type: "rigid", bone: "ground", art: `<ellipse cx="4" cy="2" rx="${r(b.H * 1.6 + 20)}" ry="${r(8 + b.H * 0.08)}" fill="#000" opacity="0.18"/>` },
|
|
1894
|
+
...(tail ? [{ id: "tailBack", type: "rigid", bone: "tail", art: "tailBack" }] : []),
|
|
1895
|
+
{ id: "hairBack", type: "rigid", bone: "hair", art: "hairBack" },
|
|
1896
|
+
{ id: "neck", type: "rigid", bone: "neck", art: "neck" },
|
|
1897
|
+
{ id: "torso", type: "rigid", bone: "body", art: "torso" },
|
|
1898
|
+
// The legs, the hips and a skirt: volumes on the 3D skeleton, drawn from the view every frame
|
|
1899
|
+
// (cel shaded, a knee over its shin, one thigh over the other), over the torso only where they
|
|
1900
|
+
// are nearer than it.
|
|
1901
|
+
legsSolid(m, look, j, anatomy.limbs.calf),
|
|
1902
|
+
{ id: "earsBack", type: "rigid", bone: "head", art: "earsBack" },
|
|
1903
|
+
{ id: "head", type: "rigid", bone: "head", art: "head" },
|
|
1904
|
+
// The jaw that drops with a gape (`gapeView`): skin over the rest chin, its new lower outline.
|
|
1905
|
+
...jawParts(F, ""),
|
|
1906
|
+
{ id: "nose", type: "rigid", bone: "head", art: "nose" },
|
|
1907
|
+
{ id: "eyes", type: "switch", bone: "head", variants: Object.fromEntries(Object.keys(F.eyeVariants).map((k) => [k, `eye_${k}`])), default: "open" },
|
|
1908
|
+
{ id: "pupils", type: "rigid", bone: "pupils", art: "pupils", visibleWhen: { part: "eyes", variant: ["open", "wide", "half"] } },
|
|
1909
|
+
{ id: "lids", type: "rigid", bone: "head", art: "lids", visibleWhen: { part: "eyes", variant: "half" } },
|
|
1910
|
+
{ id: "mouth", type: "morph", bone: "head", fill: "palette(mouth)", stroke: LIPS, strokeWidth: LIPS_W, attrs: { "stroke-linejoin": "round" }, base: F.mouth.base, shapes: F.mouth.shapes },
|
|
1911
|
+
// Inside the mouth: clipped by its shape (never past the lips).
|
|
1912
|
+
{ id: "tongue", type: "morph", bone: "head", fill: "palette(tongue)", base: F.tongue.base, shapes: F.tongue.shapes, clip: "mouth" },
|
|
1913
|
+
{ id: "teeth", type: "morph", bone: "head", fill: "#ffffff", base: F.teeth.base, shapes: F.teeth.shapes, clip: "mouth" },
|
|
1914
|
+
{ id: "hairFront", type: "rigid", bone: "hair", art: "hairFront" },
|
|
1915
|
+
...(tail ? [{ id: "tailFront", type: "rigid", bone: "tail", art: "tailFront" }] : []),
|
|
1916
|
+
{ id: "earsFront", type: "rigid", bone: "head", art: "earsFront" },
|
|
1917
|
+
// The nose, a moustache, glasses: over the mouth and over the hair at the sides of the face.
|
|
1918
|
+
{ id: "over", type: "rigid", bone: "head", art: "over" },
|
|
1919
|
+
{ id: "brows", type: "morph", bone: "head", fill: "palette(brow)", base: F.brows.base, shapes: F.brows.shapes },
|
|
1920
|
+
armsBSolid,
|
|
1921
|
+
armsSolid,
|
|
1922
|
+
...watchSolids,
|
|
1923
|
+
...["B", "F"].map((side) => ({
|
|
1924
|
+
id: `hand${side}`, type: "switch", bone: `hand${side}`, variants: { open: `hand${side}_open`, fist: `hand${side}_fist`, point: `hand${side}_point`, grip: `hand${side}_grip` }, default: style.hands,
|
|
1925
|
+
// The thumbs towards the body from the front and from behind: the hand on the far side of the
|
|
1926
|
+
// picture there is drawn as its mirror image (a hand holding something keeps its grip's drawing).
|
|
1927
|
+
...(tv ? { alt: [{ when: { part: "view", variant: [...HAND_MIRROR[side]] }, variants: { open: `hand${side}_openM`, fist: `hand${side}_fistM`, point: `hand${side}_pointM` } }] } : {}),
|
|
1928
|
+
})),
|
|
1929
|
+
];
|
|
1930
|
+
// Secondary motion, as in hand-drawn animation: the body jiggles, forearms and the head drag a
|
|
1931
|
+
// little behind and settle (follow-through), a ponytail swings, big hair bounces.
|
|
1932
|
+
const physics = [
|
|
1933
|
+
{ type: "jiggle", bone: "body", stiffness: anatomy.physics.body[0], damping: anatomy.physics.body[1], translate: 0.04, squash: 0.03 },
|
|
1934
|
+
{ type: "spring", bones: ["armF2"], stiffness: anatomy.physics.forearms[0], damping: anatomy.physics.forearms[1], gravity: [0, 0], inertia: 0.35 },
|
|
1935
|
+
{ type: "spring", bones: ["armB2"], stiffness: anatomy.physics.forearms[0], damping: anatomy.physics.forearms[1], gravity: [0, 0], inertia: 0.35 },
|
|
1936
|
+
{ type: "spring", bones: ["head"], stiffness: anatomy.physics.head[0], damping: anatomy.physics.head[1], gravity: [0, 0], inertia: 0.15 },
|
|
1937
|
+
...(tail ? [{ type: "spring", bones: ["tail"], stiffness: anatomy.physics.tail[0], damping: anatomy.physics.tail[1], gravity: [0, 260], inertia: 0.7 }] : []),
|
|
1938
|
+
...(m.bouncy ? [{ type: "jiggle", bone: "hair", stiffness: anatomy.physics.hair[0], damping: anatomy.physics.hair[1], translate: 0.05, squash: 0.06 }] : []),
|
|
1939
|
+
];
|
|
1940
|
+
const skeleton = [
|
|
1941
|
+
{ id: "root" },
|
|
1942
|
+
{ id: "ground" },
|
|
1943
|
+
{ id: "hips", parent: "root", from: [0, r(m.y.hip)], to: [0, r(m.y.hip - 10)], mass: 2 },
|
|
1944
|
+
{ id: "body", parent: "hips", from: [0, r(m.y.hip)], to: [0, r(m.y.torsoTop)], mass: 2 },
|
|
1945
|
+
{ id: "neck", parent: "body", from: [0, r(m.y.torsoTop)], to: [0, r(m.y.neckTop)], mass: 1, limits: { rotation: [-25, 25] } },
|
|
1946
|
+
{ id: "head", parent: "neck", from: [0, r(m.y.neckTop)], to: [0, r(m.y.top)], mass: 1.4, limits: { rotation: [-40, 40] } },
|
|
1947
|
+
{ id: "hair", parent: "head", from: [0, r(m.y.head)], to: [0, r(m.y.top - u * 0.2)], mass: 0.6 },
|
|
1948
|
+
// (The jaw: a 3D bone, nothing drawn on it — see `jawOccluder`.)
|
|
1949
|
+
{ id: "jaw", parent: "head", from: project(jawFrom, vh("profile")).map(r), to: project(jawTo, vh("profile")).map(r), mass: 0.2 },
|
|
1950
|
+
...(tail ? [{ id: "tail", parent: "head", from: [r(tail[0][0]), r(tail[0][1])], to: [r(tail[1][0]), r(tail[1][1])], mass: 0.4 }] : []),
|
|
1951
|
+
// (The pupils' pivot on the drawn eye.)
|
|
1952
|
+
{ id: "pupils", parent: "head", from: [r(eyeMid[0]), r(eyeMid[1])] },
|
|
1953
|
+
...["ear", "earB", "mouth", "eye", "top"].map((k) => ({ id: `${k}Pt`, parent: "head", from: [r(headPoints.profile[k][0]), r(headPoints.profile[k][1])] })),
|
|
1954
|
+
...limbBones({ ...j, toeF: 30 * b.shoe, toeB: 30 * b.shoe }),
|
|
1955
|
+
];
|
|
1956
|
+
const top2 = look.top2 ?? shade(look.topColor, 0.75);
|
|
1957
|
+
const doc = {
|
|
1958
|
+
format: "toon",
|
|
1959
|
+
version: 1,
|
|
1960
|
+
name: look.name,
|
|
1961
|
+
// Seated facing the camera: half the thigh shows; skirts sit with the knees together.
|
|
1962
|
+
// The anatomy it was made from travels with it (edit it and rebuild with `opts.anatomy`).
|
|
1963
|
+
meta: {
|
|
1964
|
+
description: `${look.name}: TV-cartoon human (three-quarter view facing right; front, half, side, away and back views)`,
|
|
1965
|
+
anatomy,
|
|
1966
|
+
// Rules checked on the drawings (the mouth clear of the nose in every drawn angle).
|
|
1967
|
+
anatomyIssues: [
|
|
1968
|
+
...checkAnatomy(anatomy).filter((x) => x.severity === "warning"),
|
|
1969
|
+
...Object.keys(VIEWS).filter((v) => views[v].face.visible && !views[v].face.mouthClear).map((v) => ({ severity: "error", rule: "mouth-nose-view", message: `in the ${v} view a mouth shape still touches the nose or leaves the face` })),
|
|
1970
|
+
],
|
|
1971
|
+
},
|
|
1972
|
+
palette: {
|
|
1973
|
+
ink: INK,
|
|
1974
|
+
skin: look.skin,
|
|
1975
|
+
skinDark: shade(look.skin, 0.86),
|
|
1976
|
+
hair: look.hairColor,
|
|
1977
|
+
hairDark: shade(look.hairColor, 0.72),
|
|
1978
|
+
top: look.topColor,
|
|
1979
|
+
topDark: shade(look.topColor, 0.8),
|
|
1980
|
+
top2,
|
|
1981
|
+
top2Dark: shade(top2, 0.8),
|
|
1982
|
+
pocket: shade(look.topColor, 0.92),
|
|
1983
|
+
bottom: look.bottomColor,
|
|
1984
|
+
bottomDark: shade(look.bottomColor, 0.8),
|
|
1985
|
+
shoes: look.shoeColor ?? (look.shoes === "studded" ? "#232323" : look.shoes === "canvas" ? "#2f6fc0" : look.shoes === "dress" ? "#4a2f22" : "#e84a3c"),
|
|
1986
|
+
sole: "#f4f1ea",
|
|
1987
|
+
socks: look.socks ?? "#ffffff",
|
|
1988
|
+
accent: look.accent ?? "#ff6fa8",
|
|
1989
|
+
apron: look.apron ?? "#ffffff",
|
|
1990
|
+
// The inside of the mouth is always dark; lipstick colours the lips (the mouth's outline).
|
|
1991
|
+
mouth: "#5a1c26",
|
|
1992
|
+
lips: look.lipstick ?? "#5a1c26",
|
|
1993
|
+
tongue: "#d8606a",
|
|
1994
|
+
stripe: look.stripe ?? top2,
|
|
1995
|
+
flush: mix(look.skin, "#e0584f", 0.32),
|
|
1996
|
+
stubble: mix(look.skin, "#5b6577", 0.2),
|
|
1997
|
+
item: look.pocketItem?.color ?? "#f4f1ea",
|
|
1998
|
+
band: look.pocketItem?.band ?? "#c8282e",
|
|
1999
|
+
earItem: look.earItem === "pencil" ? "#f2c230" : "#f7f5ee",
|
|
2000
|
+
hat: look.hatColor ?? "#2f6fc0",
|
|
2001
|
+
hatDark: shade(look.hatColor ?? "#2f6fc0", 0.82),
|
|
2002
|
+
lapel: shade(top2, 0.88),
|
|
2003
|
+
tie: look.tie ?? "#8a1f2b",
|
|
2004
|
+
sweater: look.sweater ?? "#f2c230",
|
|
2005
|
+
jewel: look.necklace?.color ?? (look.necklace?.kind === "chain" ? "#e2b84a" : "#f7f3ea"),
|
|
2006
|
+
strap: look.suspenders ?? "#4a3426",
|
|
2007
|
+
watch: look.watch ?? "#d4af37",
|
|
2008
|
+
earTip: look.earItem === "pencil" ? "#e8d2a8" : "#d9894a",
|
|
2009
|
+
brow: shade(look.hairColor, look.hairColor === "#d8d4cc" ? 0.8 : 0.6),
|
|
2010
|
+
veil: look.veil ?? "#ffffff",
|
|
2011
|
+
flower: look.boutonniere ?? "#e23d5a",
|
|
2012
|
+
cape: look.cape ?? "#c8282e",
|
|
2013
|
+
},
|
|
2014
|
+
art,
|
|
2015
|
+
skeleton,
|
|
2016
|
+
parts,
|
|
2017
|
+
// (Points on the head are projected the way its drawing is: nearly level, put back on the neck.)
|
|
2018
|
+
anchors: {
|
|
2019
|
+
head: { bone: "head", at: project([0, m.y.eye - u * 0.1, u * 0.45], vh("profile")).map(r) },
|
|
2020
|
+
// Where props fit on the head, in every view (the points move with the drawing).
|
|
2021
|
+
...Object.fromEntries(["ear", "earB", "mouth", "eye", "top"].map((k) => [k, { bone: `${k}Pt`, at: [r(headPoints.profile[k][0]), r(headPoints.profile[k][1])] }])),
|
|
2022
|
+
face: { bone: "head", at: project([0, m.y.eye + u * 0.2, u * 0.9], vh("profile")).map(r) },
|
|
2023
|
+
// Held props turn with the hand (a phone at the ear, a bottle tipped to the mouth).
|
|
2024
|
+
hand: { bone: "handF", at: j.handF, turn: 1 },
|
|
2025
|
+
handB: { bone: "handB", at: j.handB, turn: 1 },
|
|
2026
|
+
fist: { bone: "handF", at: [r(j.handF[0] + b.hand * 0.8), j.handF[1]], turn: 1 },
|
|
2027
|
+
},
|
|
2028
|
+
ik: limbIk,
|
|
2029
|
+
// 2.5D: the limbs, the torso and the head posed in 3D and projected for every drawn angle, so a
|
|
2030
|
+
// pose (sitting, a step towards the camera) reads from any side.
|
|
2031
|
+
rig3d: {
|
|
2032
|
+
bones: {
|
|
2033
|
+
hips: { from: [0, r(m.y.hip), 0], to: [0, r(m.y.hip - 10), 0] },
|
|
2034
|
+
body: { from: [0, r(m.y.hip), 0], to: [0, r(m.y.torsoTop), 0] },
|
|
2035
|
+
neck: { from: [0, r(m.y.torsoTop), 0], to: [0, r(m.y.neckTop), 0] },
|
|
2036
|
+
head: { from: [0, r(m.y.neckTop), 0], to: [0, r(m.y.top), 0] },
|
|
2037
|
+
jaw: { from: jawFrom.map(r), to: jawTo.map(r) },
|
|
2038
|
+
armF1: { from: j3.shoulderF, to: j3.elbowF }, armF2: { from: j3.elbowF, to: j3.handF },
|
|
2039
|
+
armB1: { from: j3.shoulderB, to: j3.elbowB }, armB2: { from: j3.elbowB, to: j3.handB },
|
|
2040
|
+
legF1: { from: j3.hipF, to: j3.kneeF }, legF2: { from: j3.kneeF, to: j3.footF },
|
|
2041
|
+
legB1: { from: j3.hipB, to: j3.kneeB }, legB2: { from: j3.kneeB, to: j3.footB },
|
|
2042
|
+
// The feet point forward (level: they turn with the body, not the shin).
|
|
2043
|
+
footF: { from: j3.footF, to: [j3.footF[0], j3.footF[1], r(34 * b.shoe)] },
|
|
2044
|
+
footB: { from: j3.footB, to: [j3.footB[0], j3.footB[1], r(34 * b.shoe)] },
|
|
2045
|
+
},
|
|
2046
|
+
views: Object.fromEntries(Object.keys(VIEWS).map((v) => [v, r((VIEWS[v] * 180) / Math.PI)])),
|
|
2047
|
+
pitch: opts.pitch ?? 0,
|
|
2048
|
+
chains: [
|
|
2049
|
+
// The hands (drawings) by the depth of their forearm.
|
|
2050
|
+
{ bones: ["armF2"], parts: ["handF"], tip: true, margin: 6, behind: "arms" },
|
|
2051
|
+
{ bones: ["armB2"], parts: ["handB"], tip: true, margin: 6, behind: "arms" },
|
|
2052
|
+
// The far arm's drawing, by its upper arm: behind the body's drawing while clearly behind.
|
|
2053
|
+
{ bones: ["armB1"], parts: ["armsB"], margin: 2, limb: false },
|
|
2054
|
+
].map((c) => ({ ...c, parts: c.parts.filter((id) => parts.some((p) => p.id === id)) })),
|
|
2055
|
+
front: "handB",
|
|
2056
|
+
back: "shadow",
|
|
2057
|
+
// The torso as drawn (its occluder's ellipsoids) and the arm's half width: a far arm never lies
|
|
2058
|
+
// along the torso's outline in the picture (the torso clearance).
|
|
2059
|
+
torso: (torsoOccluder(m, look).shapes ?? []).filter((x) => x.ellipsoid).map((x) => ({ bone: "body", at: x.from.at, radii: x.ellipsoid })),
|
|
2060
|
+
torsoGap: r((b.arm[0] + b.arm[1]) * 0.25),
|
|
2061
|
+
// Where a reached hand never goes: into the torso or the head, as drawn (a hand's half thickness
|
|
2062
|
+
// out of them).
|
|
2063
|
+
keepOut: [
|
|
2064
|
+
...[torsoKeepOut(m, b.hand * 0.6)].map(([c, rad]) => ({ bone: "body", at: [r(c[0]), r(c[1] - m.y.hip), r(c[2])], radii: rad.map(r) })),
|
|
2065
|
+
// The head as drawn: the cranium with its hair, the face (a hand rests on its surface — on
|
|
2066
|
+
// the cheek, at the mouth — never inside it).
|
|
2067
|
+
...headKeepOut(m, b.hand * 0.6).map(([c, rad]) => ({ bone: "head", at: [r(c[0]), r(c[1] - m.y.neckTop), r(c[2])], radii: rad.map(r) })),
|
|
2068
|
+
],
|
|
2069
|
+
// Where the director's 3D reaches go: the mouth, the near ear, in front of the chest (a held
|
|
2070
|
+
// thing, a little to the near side).
|
|
2071
|
+
points: {
|
|
2072
|
+
mouth: { bone: "head", at: [0, r(m.y.mouth - m.y.neckTop), r(u * 0.92)] },
|
|
2073
|
+
eye: { bone: "head", at: [0, r(m.y.eye - m.y.neckTop), r(u * 0.88)] },
|
|
2074
|
+
ear: { bone: "head", at: [r(-u * 0.97), r(m.y.ear - m.y.neckTop), 0] },
|
|
2075
|
+
earB: { bone: "head", at: [r(u * 0.97), r(m.y.ear - m.y.neckTop), 0] },
|
|
2076
|
+
chest: { bone: "body", at: [r(-b.S * 0.3), r(-b.T * 0.42), r(b.D * (1 + (look.heavy ?? 0) * 0.6) + b.T * 0.32)] },
|
|
2077
|
+
},
|
|
2078
|
+
},
|
|
2079
|
+
physics,
|
|
2080
|
+
controls: {
|
|
2081
|
+
mouth: { type: "viseme", part: "mouth" },
|
|
2082
|
+
// The face looks along +x: `forward` turns the head bone's rest direction (up the neck) there.
|
|
2083
|
+
look: { type: "aim", targets: [{ bone: "head", forward: aimForward(skeleton, "head"), maxAngle: 14, weight: 0.35 }, { bone: "pupils", mode: "translate", radius: r(u * 0.06) }] },
|
|
2084
|
+
emotion: emotions({
|
|
2085
|
+
dead: { "parts.eyes.variant": "dead", "parts.mouth.morph.D": 0.6, "bones.head.rotation": 10 },
|
|
2086
|
+
determined: { "parts.eyes.variant": "half", "parts.mouth.morph.frown": 0.4, "parts.brows.morph.cross": 1 },
|
|
2087
|
+
calm: { "parts.eyes.variant": "closed", "parts.brows.morph.cross": 0.3 },
|
|
2088
|
+
excited: { "parts.eyes.variant": "shine", "parts.mouth.morph.grin": 0.9, "parts.brows.morph.up": 0.8, "bones.head.rotation": -6 },
|
|
2089
|
+
annoyed: { "parts.eyes.variant": "half", "parts.mouth.morph.frown": 0.5, "parts.brows.morph.cross": 0.6, "bones.head.rotation": 3 },
|
|
2090
|
+
// The cartoon gape (swallow whole, scream, a big yawn): the jaw dropped, eyes wide, brows up.
|
|
2091
|
+
// Weighted past 1 (`{ gape: 2 }`) the jaw unhinges.
|
|
2092
|
+
gape: { "parts.eyes.variant": "wide", "parts.mouth.morph.gape": 1, "bones.jaw.rotation": JAW_TURN, "parts.brows.morph.up": 1, "bones.head.rotation": -4 },
|
|
2093
|
+
}),
|
|
2094
|
+
// The jaw on its own: `gape` (weighted: `{ gape: 2 }` unhinges it) or `shut`.
|
|
2095
|
+
jaw: { type: "pose", poses: { shut: {}, gape: { "parts.mouth.morph.gape": 1, "bones.jaw.rotation": JAW_TURN } } },
|
|
2096
|
+
},
|
|
2097
|
+
behaviors: [
|
|
2098
|
+
{ type: "blink", id: "blink", part: "eyes", open: "open", closed: "closed", interval: [2.2, 4.8], duration: 0.12 },
|
|
2099
|
+
{ type: "breathe", id: "breathe", bone: "body", amount: 0.012, period: 3.4 },
|
|
2100
|
+
],
|
|
2101
|
+
// Gestures with anticipation and overshoot.
|
|
2102
|
+
clips: fluid({
|
|
2103
|
+
...characterClips({
|
|
2104
|
+
// (TV proportions: long legs take long steps — the feet a little under half a leg apart at the
|
|
2105
|
+
// contact, the hips dropping into each step, the arms swinging wide, the head a touch late.)
|
|
2106
|
+
walk: tv
|
|
2107
|
+
? { a: r(0.23 * b.L), lift: r(0.1 * b.L), dur: 0.84, bob: r(0.028 * b.L), lean: 3, armSwing: 34, headLag: 0.06 }
|
|
2108
|
+
: { a: r(26 * (b.L / 200)), lift: r(16 * (b.L / 200)), dur: 0.62, bob: 4, lean: 2, armSwing: 24 },
|
|
2109
|
+
// A cartoon run (`gait` flight): a third of the cycle in the air, long steps (0.3 of the leg
|
|
2110
|
+
// each way while planted: about two legs of travel a cycle), a deep down, the heel kicked up
|
|
2111
|
+
// behind, a runner's arms; 18 frames a cycle at 30 fps.
|
|
2112
|
+
run: { a: r(0.3 * legLen), lift: r(0.14 * legLen), dur: 0.6, bob: r(0.055 * legLen), lean: 12, armSwing: 44, crouch: 0.1, flight: 0.34, heel: 0.45, knee: 0.4, paw: 0.15, armStyle: "run" },
|
|
2113
|
+
jump: 80,
|
|
2114
|
+
hands: true,
|
|
2115
|
+
leg: legLen,
|
|
2116
|
+
foot: r(30 * b.shoe),
|
|
2117
|
+
// (A skirt's runs are cut to it: `skirtRun`.)
|
|
2118
|
+
...(look.bottom === "skirt" ? { skirt: "short" } : look.bottom === "longSkirt" ? { skirt: "long" } : {}),
|
|
2119
|
+
}),
|
|
2120
|
+
...humanClips(),
|
|
2121
|
+
}),
|
|
2122
|
+
};
|
|
2123
|
+
doc.palette = withTones(doc.palette, style.ink ? { ink: style.ink } : {});
|
|
2124
|
+
gestures3d(doc, { u, sh: j3.shoulderF[1], sx: -j3.shoulderF[0], T: b.T, W: b.W, hip: m.y.hip, D: b.D, arm: armLen });
|
|
2125
|
+
return withTurnaround(doc, views, sx, hx, !!tail, headPoints);
|
|
2126
|
+
}
|
|
2127
|
+
/**
|
|
2128
|
+
* Gestures in 3D: the clips that bring a hand somewhere (the ear, the mouth, the chin, the eyes, up
|
|
2129
|
+
* by the head, in front of the chest) get their arm tracks from 3D reaches to body points for this
|
|
2130
|
+
* rig's own proportions — a hand to the ear bends the elbow up beside the head, from any angle, where
|
|
2131
|
+
* a 2D arm swing (made for a profile) would go round the front in 3D. Keys are hand targets at
|
|
2132
|
+
* fractions of the clip; `null` is the arm at rest.
|
|
2133
|
+
*
|
|
2134
|
+
* The arm never goes through the head as drawn (`rig3d.keepOut`: the cranium with its hair, the
|
|
2135
|
+
* face — for any anatomy, a big head as much as a small one). Every key and in-between is placed in
|
|
2136
|
+
* the clip's own pose at that moment (the body's lean, the head's nod: its tracks), the targets on
|
|
2137
|
+
* the head (`head`) moving with it; a target is pushed out of the head to its surface (`touch`: a
|
|
2138
|
+
* hand resting on the face — on the eyes, under the chin) or a little past it; and where the arm
|
|
2139
|
+
* would still cross the head (a hand out of reach above a big head, a forearm across the face) the
|
|
2140
|
+
* nearest place where the hand, the elbow and the middle of the forearm are all clear of it is used.
|
|
2141
|
+
*/
|
|
2142
|
+
function gestures3d(doc, o) {
|
|
2143
|
+
const { u, sh, sx, T, W, hip, D, arm } = o;
|
|
2144
|
+
const P = (n) => rig3dPoint(doc, {}, n) ?? [0, 0, 0];
|
|
2145
|
+
const add = (p, d) => [p[0] + d[0], p[1] + d[1], p[2] + d[2]];
|
|
2146
|
+
const sub = (p, d) => [p[0] - d[0], p[1] - d[1], p[2] - d[2]];
|
|
2147
|
+
const len = (p) => Math.hypot(p[0], p[1], p[2]);
|
|
2148
|
+
const lerp3 = (a, b, t) => [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t, a[2] + (b[2] - a[2]) * t];
|
|
2149
|
+
const mouth = P("mouth"), ear = P("ear"), chest = P("chest"), eye = P("eye");
|
|
2150
|
+
const hold = (p) => [[0, p], [1, p]];
|
|
2151
|
+
const visit = (p, a = 0.3, b = 0.7) => [[0, null], [a, p], [b, p], [1, null]];
|
|
2152
|
+
const earHold = add(lerp3(ear, mouth, 0.45), [0, u * 0.3, 0]);
|
|
2153
|
+
const mv = (m, v) => [m[0] * v[0] + m[1] * v[1] + m[2] * v[2], m[3] * v[0] + m[4] * v[1] + m[5] * v[2], m[6] * v[0] + m[7] * v[1] + m[8] * v[2]];
|
|
2154
|
+
const mvT = (m, v) => [m[0] * v[0] + m[3] * v[1] + m[6] * v[2], m[1] * v[0] + m[4] * v[1] + m[7] * v[2], m[2] * v[0] + m[5] * v[1] + m[8] * v[2]];
|
|
2155
|
+
const keepOut = (doc.rig3d.keepOut ?? []).filter((k) => k.bone === "head");
|
|
2156
|
+
const frames = (pose) => keepOut.flatMap((k) => (pose[k.bone] ? [{ c: add(pose[k.bone].from, mv(pose[k.bone].rot, k.at)), rot: pose[k.bone].rot, rad: k.radii }] : []));
|
|
2157
|
+
const norm = (l, rad) => Math.hypot(l[0] / rad[0], l[1] / rad[1], l[2] / rad[2]);
|
|
2158
|
+
const clearance = (pose, p) => Math.min(Infinity, ...frames(pose).map((f) => norm(mvT(f.rot, sub(p, f.c)), f.rad)));
|
|
2159
|
+
const pushOut = (pose, p, min) => {
|
|
2160
|
+
for (let i = 0; i < 6; i++) {
|
|
2161
|
+
let moved = false;
|
|
2162
|
+
for (const f of frames(pose)) {
|
|
2163
|
+
const l = mvT(f.rot, sub(p, f.c)), q = norm(l, f.rad);
|
|
2164
|
+
if (q >= min)
|
|
2165
|
+
continue;
|
|
2166
|
+
const dir = q < 1e-6 ? [0, 0, 1] : l, k = min / (norm(dir, f.rad) || 1);
|
|
2167
|
+
p = add(f.c, mv(f.rot, [dir[0] * k, dir[1] * k, dir[2] * k]));
|
|
2168
|
+
moved = true;
|
|
2169
|
+
}
|
|
2170
|
+
if (!moved)
|
|
2171
|
+
break;
|
|
2172
|
+
}
|
|
2173
|
+
return p;
|
|
2174
|
+
};
|
|
2175
|
+
const rest = rig3dPose(doc, {});
|
|
2176
|
+
/** A point on the head at rest, where the head is in a pose. */
|
|
2177
|
+
const onHead = (pose, p) => (pose.head && rest.head ? add(pose.head.from, mv(pose.head.rot, sub(p, rest.head.from))) : p);
|
|
2178
|
+
/** A point on the torso at rest, where the torso is in a pose (a lean). */
|
|
2179
|
+
const onBody = (pose, p) => (pose.body && rest.body ? add(pose.body.from, mv(pose.body.rot, sub(p, rest.body.from))) : p);
|
|
2180
|
+
const unit = (p) => {
|
|
2181
|
+
const l = len(p) || 1;
|
|
2182
|
+
return [p[0] / l, p[1] / l, p[2] / l];
|
|
2183
|
+
};
|
|
2184
|
+
const shoulder = (sd) => rest[sd < 0 ? "armF1" : "armB1"].from;
|
|
2185
|
+
const l1 = len(sub(rest.armF1.to, rest.armF1.from)), l2 = len(sub(rest.armF2.to, rest.armF2.from)), reach = l1 + l2;
|
|
2186
|
+
/** The hand `k` of the arm's reach from the shoulder along `dir` (body space, unnormalised). */
|
|
2187
|
+
const along = (sd, dir, k = 0.95) => add(shoulder(sd), unit(dir).map((v) => v * k * reach));
|
|
2188
|
+
// The head as drawn at rest (its keep-outs: the head's rotation is none) and how far a point is out
|
|
2189
|
+
// of its outline in the picture at a view (`yaw`): from the front whatever its depth (an arm level
|
|
2190
|
+
// with the head shows beside it or crosses it), else only where it is in front of the head's middle
|
|
2191
|
+
// there (behind it, the head hides it).
|
|
2192
|
+
const heads = keepOut.map((k) => ({ c: add(rest[k.bone].from, k.at), r: k.radii }));
|
|
2193
|
+
const outline = (p, yaw, frontOnly) => {
|
|
2194
|
+
const cy = Math.cos(yaw), sy = Math.sin(yaw);
|
|
2195
|
+
let q = Infinity;
|
|
2196
|
+
for (const h of heads) {
|
|
2197
|
+
const A = (h.r[0] * cy) ** 2 + (h.r[2] * sy) ** 2, C = h.r[1] ** 2;
|
|
2198
|
+
const x = p[0] - h.c[0], z = p[2] - h.c[2], dx = x * cy + z * sy, dy = p[1] - h.c[1];
|
|
2199
|
+
// (The middle of the head along the line of sight there: the diameter conjugate to it.)
|
|
2200
|
+
if (frontOnly && -x * sy + z * cy <= (sy * cy * (h.r[2] ** 2 - h.r[0] ** 2) * dx) / A)
|
|
2201
|
+
continue;
|
|
2202
|
+
q = Math.min(q, Math.hypot(dx / Math.sqrt(A), dy / Math.sqrt(C)));
|
|
2203
|
+
}
|
|
2204
|
+
return q;
|
|
2205
|
+
};
|
|
2206
|
+
// An arm raised beside the head — fists up in a cheer, a wave, arms thrown up in a jump — as a
|
|
2207
|
+
// cartoonist draws it for this anatomy: up and out, opened out from the vertical by the least angle
|
|
2208
|
+
// (`min`° at least) at which the whole arm (the visible upper arm, the elbow, the forearm, the hand)
|
|
2209
|
+
// is clear of the head as drawn — in 3D by a margin, and in the picture from the angles that face
|
|
2210
|
+
// the camera or turn away from it (`BESIDE_VIEWS`): from the front and the back whatever its depth
|
|
2211
|
+
// (level with the head, it shows beside it or crosses it), from a three-quarter view where it is in
|
|
2212
|
+
// front of the head. A small head on long arms raises them nearly straight up, a big head (big hair)
|
|
2213
|
+
// on short arms out wide by its sides — opened out, never folded back over the skull. (Seen from
|
|
2214
|
+
// nearer a side the near arm points at the camera, over a big head whatever its angle: there the
|
|
2215
|
+
// engine's picture-space face clearance opens it out further — the clips are `clear`.) The hand is
|
|
2216
|
+
// `k` of the arm's reach from the shoulder, leaning `fwd` forward (back when negative), the elbow
|
|
2217
|
+
// bent towards `pole`; the far arm mirrors the near one (a symmetric V). `a` is the angle found.
|
|
2218
|
+
const besides = new Map();
|
|
2219
|
+
// (The places it makes: the arm travels to and from them round its own side — see the in-betweens.)
|
|
2220
|
+
const raised = new WeakSet();
|
|
2221
|
+
const beside = (sd, o) => {
|
|
2222
|
+
const key = JSON.stringify([o.k, o.fwd ?? 0, o.min ?? 0, o.pole(-1)]);
|
|
2223
|
+
let F = besides.get(key);
|
|
2224
|
+
if (!F) {
|
|
2225
|
+
const S = shoulder(-1), f = o.fwd ?? 0;
|
|
2226
|
+
const target = (a) => along(-1, [-Math.sin(deg(a)), -Math.cos(deg(a)), f], o.k);
|
|
2227
|
+
// How clear the arm is (≥ 1: clear by every margin).
|
|
2228
|
+
const clearAt = (a) => {
|
|
2229
|
+
// The elbow where a two-bone reach puts it (bent towards the pole).
|
|
2230
|
+
const T = target(a), d = sub(T, S), dl = len(d), dh = unit(d), pl = o.pole(-1, T), pd = pl[0] * dh[0] + pl[1] * dh[1] + pl[2] * dh[2];
|
|
2231
|
+
const ph = unit([pl[0] - dh[0] * pd, pl[1] - dh[1] * pd, pl[2] - dh[2] * pd]);
|
|
2232
|
+
const c = Math.min(1, Math.max(-1, (l1 * l1 + dl * dl - l2 * l2) / (2 * l1 * dl))), sn = Math.sqrt(1 - c * c);
|
|
2233
|
+
const E = add(S, [l1 * (c * dh[0] + sn * ph[0]), l1 * (c * dh[1] + sn * ph[1]), l1 * (c * dh[2] + sn * ph[2])]);
|
|
2234
|
+
let q = Infinity;
|
|
2235
|
+
for (const p of [...[0.5, 0.625, 0.75, 0.875, 1].map((x) => lerp3(S, E, x)), ...[0.25, 0.5, 0.75, 1].map((x) => lerp3(E, T, x))]) {
|
|
2236
|
+
q = Math.min(q, clearance(rest, p) / 1.12);
|
|
2237
|
+
for (const y of BESIDE_VIEWS) {
|
|
2238
|
+
const level = Math.abs(Math.sin(y)) < 1e-6;
|
|
2239
|
+
q = Math.min(q, outline(p, y, !level) / (level ? 1.08 : 1.05));
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
return q;
|
|
2243
|
+
};
|
|
2244
|
+
// (Out to level at most: the clearest angle there should none clear the head.)
|
|
2245
|
+
let best = { a: o.min ?? 0, q: -Infinity };
|
|
2246
|
+
for (let a = o.min ?? 0; a <= 95; a += 1) {
|
|
2247
|
+
const q = clearAt(a);
|
|
2248
|
+
if (q > best.q)
|
|
2249
|
+
best = { a, q };
|
|
2250
|
+
if (q >= 1)
|
|
2251
|
+
break;
|
|
2252
|
+
}
|
|
2253
|
+
F = { at: target(best.a), a: best.a };
|
|
2254
|
+
besides.set(key, F);
|
|
2255
|
+
}
|
|
2256
|
+
const at = sd < 0 ? [...F.at] : [-F.at[0], F.at[1], F.at[2]];
|
|
2257
|
+
raised.add(at);
|
|
2258
|
+
return { at, a: F.a };
|
|
2259
|
+
};
|
|
2260
|
+
// The raising gestures' places. (A raised arm's elbow bends out and down a little, a lowered one's
|
|
2261
|
+
// down and back by the side — in between, as high as the hand is; a waving one's sticks out to the
|
|
2262
|
+
// side, under the hand.)
|
|
2263
|
+
const raisedPole = (sd, at) => {
|
|
2264
|
+
const h = at ? Math.min(1, Math.max(0, (sh - at[1]) / (reach * 0.4))) : 1;
|
|
2265
|
+
return [sd * (0.3 + 0.7 * h), 1 - 0.4 * h, -0.5 + 0.1 * h];
|
|
2266
|
+
};
|
|
2267
|
+
const wavePole = (sd) => [sd, 0.9, 0];
|
|
2268
|
+
const cheerLo = (sd) => beside(sd, { k: 0.88, fwd: -0.12, pole: raisedPole }).at;
|
|
2269
|
+
// (The pump: the same arm, straighter — as wide, or wider should the straight arm need it.)
|
|
2270
|
+
const cheerHi = (sd) => beside(sd, { k: 0.98, fwd: -0.12, min: beside(-1, { k: 0.88, fwd: -0.12, pole: raisedPole }).a, pole: raisedPole }).at;
|
|
2271
|
+
// A wave: the upper arm out to the side, a little above level and forward, the forearm up from the
|
|
2272
|
+
// elbow and swinging about it like a wiper — in towards the head by the most that keeps the forearm
|
|
2273
|
+
// and the hand clear of the head as drawn (`WAVE_IN`° at most), then `WAVE_SWING`° out from there.
|
|
2274
|
+
const waveElbow = (sd) => add(shoulder(sd), unit([sd, -0.3, 0.12]).map((v) => v * l1));
|
|
2275
|
+
const waveHand = (sd, a) => add(waveElbow(sd), unit([sd * Math.sin(deg(a)), -Math.cos(deg(a)), 0.15]).map((v) => v * l2 * 0.98));
|
|
2276
|
+
let waveA;
|
|
2277
|
+
const waveAngle = () => {
|
|
2278
|
+
if (waveA !== undefined)
|
|
2279
|
+
return waveA;
|
|
2280
|
+
const E = waveElbow(-1);
|
|
2281
|
+
const clearAt = (a) => {
|
|
2282
|
+
let q = Infinity;
|
|
2283
|
+
for (const p of [0.4, 0.6, 0.8, 1].map((x) => lerp3(E, waveHand(-1, a), x))) {
|
|
2284
|
+
q = Math.min(q, clearance(rest, p) / 1.12);
|
|
2285
|
+
// (In the picture from the front and the back only: from three-quarters a waving hand may pass
|
|
2286
|
+
// in front of the hair — the engine's face clearance keeps it off the face there.)
|
|
2287
|
+
for (const y of [VIEWS.front, VIEWS.back])
|
|
2288
|
+
q = Math.min(q, outline(p, y, false) / 1.08);
|
|
2289
|
+
}
|
|
2290
|
+
return q;
|
|
2291
|
+
};
|
|
2292
|
+
let best = { a: -WAVE_IN, q: -Infinity };
|
|
2293
|
+
for (let a = -WAVE_IN; a <= 50; a += 2) {
|
|
2294
|
+
const q = clearAt(a);
|
|
2295
|
+
if (q > best.q)
|
|
2296
|
+
best = { a, q };
|
|
2297
|
+
if (q >= 1)
|
|
2298
|
+
break;
|
|
2299
|
+
}
|
|
2300
|
+
return (waveA = best.a);
|
|
2301
|
+
};
|
|
2302
|
+
const waveKey = (sd, out) => {
|
|
2303
|
+
const at = waveHand(sd, waveAngle() + (out ? WAVE_SWING : 0));
|
|
2304
|
+
raised.add(at);
|
|
2305
|
+
return at;
|
|
2306
|
+
};
|
|
2307
|
+
const waveIn = (sd) => waveKey(sd, false);
|
|
2308
|
+
const waveOut = (sd) => waveKey(sd, true);
|
|
2309
|
+
const thrownUp = (sd) => beside(sd, { k: 0.95, fwd: -0.1, pole: raisedPole }).at;
|
|
2310
|
+
const flail = (sd) => beside(sd, { k: 0.95, fwd: -0.05, pole: raisedPole }).at;
|
|
2311
|
+
const danceUp = (sd) => beside(sd, { k: 0.88, fwd: -0.05, pole: raisedPole }).at;
|
|
2312
|
+
/** The hands swung back behind the hips (a jump's wind-up). */
|
|
2313
|
+
const swungBack = (sd) => along(sd, [sd * 0.25, 0.85, -0.5]);
|
|
2314
|
+
/** Both hands together in front (holding something), `lift` of the way from hanging to level (0: down, 1: straight ahead), the elbows bent (`k` of the reach). */
|
|
2315
|
+
const together = (sd, lift, k = 0.78) => along(sd, [-sd * 0.12, Math.cos(lift * Math.PI / 2), Math.sin(lift * Math.PI / 2)], k);
|
|
2316
|
+
/** The arm out to the side, low and a little forward (for balance: a landing, a windmill's low point). */
|
|
2317
|
+
const outLow = (sd) => along(sd, [sd * 0.75, 0.55, 0.35], 0.92);
|
|
2318
|
+
/** The arms flung up from together in front (a toss's release): apart, up beside the head and a little forward. */
|
|
2319
|
+
const flungUp = (sd) => beside(sd, { k: 0.97, fwd: 0.1, pole: raisedPole }).at;
|
|
2320
|
+
/** Wings: the arms out to the sides, down and up. */
|
|
2321
|
+
const wingDown = (sd) => along(sd, [sd * 0.65, 0.72, 0.15], 0.93);
|
|
2322
|
+
const wingUp = (sd) => along(sd, [sd * 0.92, -0.38, 0.1], 0.93);
|
|
2323
|
+
// `head`: the targets are on the head (they move with its nod); `touch`: the hand rests on it.
|
|
2324
|
+
// `torso`: the targets are on the torso (they lean with it: a body leaning over an edge).
|
|
2325
|
+
// `nod`: the head bent forward (degrees) all through the clip — looking down at a phone in the hands;
|
|
2326
|
+
// `down`: the eyes lowered too (the pupils down, the lids half closed: reading a screen from any view).
|
|
2327
|
+
// Raised arms go up beside the head (`beside`), never across it. `clear`: a new clip whose hands
|
|
2328
|
+
// never go to the face is marked `clear` (the engine keeps its arms off the face in the picture, for
|
|
2329
|
+
// any anatomy and from any angle; the shared clips are marked where they are made, `characterClips`).
|
|
2330
|
+
const G = {
|
|
2331
|
+
// Using a phone (ergonomics studies: held in front of the lower chest, near the middle, about a
|
|
2332
|
+
// forearm's length from the eyes, the elbow down by the side, the head bent towards it). One hand,
|
|
2333
|
+
// its thumb scrolling (the commonest grip); `textingTwo`: both thumbs typing.
|
|
2334
|
+
texting: { duration: 2.4, nod: 24, down: true, F: [[0, [-sx * 0.05, sh + T * 0.48, T * 0.6]], [0.5, [-sx * 0.05, sh + T * 0.48 - 2, T * 0.61]], [1, [-sx * 0.05, sh + T * 0.48, T * 0.6]]], pole: (sd) => [sd * 0.5, 1, -0.5] },
|
|
2335
|
+
// Carrying a box in both hands in front of the belly, the hands on its sides, the elbows bent.
|
|
2336
|
+
carry: { duration: 1.6, clear: true, F: hold([-sx * 0.75, sh + T * 0.78, T * 0.55]), B: hold([sx * 0.75, sh + T * 0.78, T * 0.55]), pole: (sd) => [sd, 1, -0.3] },
|
|
2337
|
+
textingTwo: { duration: 1.2, nod: 24, down: true, F: [[0, [-sx * 0.2, sh + T * 0.48, T * 0.55]], [0.5, [-sx * 0.2, sh + T * 0.48 - 2, T * 0.56]], [1, [-sx * 0.2, sh + T * 0.48, T * 0.55]]], B: [[0, [sx * 0.2, sh + T * 0.48, T * 0.55]], [0.5, [sx * 0.2, sh + T * 0.48 - 2, T * 0.56]], [1, [sx * 0.2, sh + T * 0.48, T * 0.55]]], pole: (sd) => [sd * 0.6, 1, -0.4] },
|
|
2338
|
+
// Holding a game controller in both hands, thumbs busy (in front of the belly, elbows in).
|
|
2339
|
+
gamepad: { duration: 1, clear: true, F: [[0, [-sx * 0.12, sh + T * 0.55, T * 0.5]], [0.5, [-sx * 0.12, sh + T * 0.55 - 3, T * 0.52]], [1, [-sx * 0.12, sh + T * 0.55, T * 0.5]]], B: [[0, [sx * 0.12, sh + T * 0.55, T * 0.5]], [0.5, [sx * 0.12, sh + T * 0.56, T * 0.48]], [1, [sx * 0.12, sh + T * 0.55, T * 0.5]]], pole: (sd) => [sd * 0.7, 1, -0.3] },
|
|
2340
|
+
// Leaning over a pool table: the near hand far forward (the bridge), the far one back by the hip
|
|
2341
|
+
// pushing the cue (strokes).
|
|
2342
|
+
cue: { duration: 1.6, clear: true, body: [[0, 28], [1, 28]], F: hold([-sx * 0.25, sh + T * 0.75, arm * 0.95]), B: [[0, [sx * 0.55, sh + T * 0.9, -T * 0.05]], [0.45, [sx * 0.45, sh + T * 0.85, T * 0.3]], [0.6, [sx * 0.45, sh + T * 0.85, T * 0.3]], [1, [sx * 0.55, sh + T * 0.9, -T * 0.05]]], pole: (sd) => [sd * 0.6, 0.6, -0.6] },
|
|
2343
|
+
// Dancing: hips swaying with a bounce, the hands up in front swinging in turn.
|
|
2344
|
+
groove: { duration: 0.9, clear: true, body: [[0, -6], [0.5, 6], [1, -6]], hips: [[0, 0], [0.25, 6], [0.5, 0], [0.75, 6], [1, 0]], F: [[0, [-sx * 0.9, sh + T * 0.35, T * 0.45]], [0.5, [-sx * 0.7, sh + T * 0.05, T * 0.5]], [1, [-sx * 0.9, sh + T * 0.35, T * 0.45]]], B: [[0, [sx * 0.7, sh + T * 0.05, T * 0.5]], [0.5, [sx * 0.9, sh + T * 0.35, T * 0.45]], [1, [sx * 0.7, sh + T * 0.05, T * 0.5]]], pole: (sd) => [sd, 0.8, -0.3] },
|
|
2345
|
+
phone: { head: true, F: hold(earHold), pole: (sd) => [sd * 0.5, 1, 0.2] },
|
|
2346
|
+
smoke: { head: true, F: [[0, null], [0.3, add(mouth, [0, u * 0.1, u * 0.35])], [0.55, add(mouth, [0, u * 0.1, u * 0.35])], [0.85, [-sx * 1.1, sh + T * 0.45, T * 0.35]], [1, null]], pole: (sd) => [sd * 0.5, 1, 0.2] },
|
|
2347
|
+
drink: { head: true, F: visit(add(mouth, [0, u * 0.3, u * 0.4]), 0.25, 0.75), pole: (sd) => [sd * 0.5, 1, 0.2] },
|
|
2348
|
+
think: { head: true, touch: true, F: hold(add(mouth, [0, u * 0.35, u * 0.2])), pole: (sd) => [sd * 0.4, 1, 0.3] },
|
|
2349
|
+
facepalm: { head: true, touch: true, F: visit(add(eye, [0, 0, u * 0.25])), pole: (sd) => [sd * 0.5, 1, 0.2] },
|
|
2350
|
+
cry: { head: true, touch: true, F: [[0, add(eye, [-u * 0.25, 0, u * 0.3])], [0.5, add(eye, [-u * 0.25, u * 0.06, u * 0.3])], [1, add(eye, [-u * 0.25, 0, u * 0.3])]], B: [[0, add(eye, [u * 0.25, 0, u * 0.3])], [0.5, add(eye, [u * 0.25, u * 0.06, u * 0.3])], [1, add(eye, [u * 0.25, 0, u * 0.3])]], pole: (sd) => [sd * 0.6, 1, 0.2] },
|
|
2351
|
+
// Waving hello: the elbow out to the side at shoulder height, the forearm up, the hand above and
|
|
2352
|
+
// beside the head (never in front of the face) swinging out and back twice a cycle.
|
|
2353
|
+
wave: { F: [[0, waveIn(-1)], [0.25, waveOut(-1)], [0.5, waveIn(-1)], [0.75, waveOut(-1)], [1, waveIn(-1)]], pole: wavePole },
|
|
2354
|
+
// Scared: both hands up in front of the chest, palms out, below the chin.
|
|
2355
|
+
scared: { F: hold([-(sx + u * 0.35), sh + T * 0.12, T * 0.55]), B: hold([sx + u * 0.35, sh + T * 0.12, T * 0.55]), pole: (sd) => [sd * 0.8, 1, -0.2] },
|
|
2356
|
+
clap: { F: [[0, add(chest, [-sx * 0.2, 0, T * 0.15])], [0.5, add(chest, [sx * 0.25 - 4, 0, T * 0.2])], [1, add(chest, [-sx * 0.2, 0, T * 0.15])]], B: [[0, [sx * 0.45, chest[1], chest[2] + T * 0.15]], [0.5, [sx * 0.25 + 4, chest[1], chest[2] + T * 0.2]], [1, [sx * 0.45, chest[1], chest[2] + T * 0.15]]], pole: (sd) => [sd * 0.6, 1, -0.2] },
|
|
2357
|
+
// Cheering: both fists up in a V beside the head, pumped (the elbows straighten on the beat).
|
|
2358
|
+
cheer: { F: [[0, cheerLo(-1)], [0.5, cheerHi(-1)], [1, cheerLo(-1)]], B: [[0, cheerLo(1)], [0.5, cheerHi(1)], [1, cheerLo(1)]], pole: raisedPole },
|
|
2359
|
+
// Pointing up and ahead (at a traffic light across the road): the near arm straight, 30° above level
|
|
2360
|
+
// and a little out (ahead of the face from a side, beside it from the front).
|
|
2361
|
+
pointUp: { F: hold(along(-1, [-0.3, -0.5, 0.8], 0.97)), pole: raisedPole },
|
|
2362
|
+
// Jumping: the arms swing back as the knees bend, are thrown up beside the head on take-off (out
|
|
2363
|
+
// and up round the sides), come down out to the sides for the landing (balance).
|
|
2364
|
+
jump: { F: [[0, null], [0.15, swungBack(-1)], [0.425, thrownUp(-1)], [0.825, outLow(-1)], [1, null]], B: [[0, null], [0.15, swungBack(1)], [0.425, thrownUp(1)], [0.825, outLow(1)], [1, null]], pole: raisedPole },
|
|
2365
|
+
// Throwing something up with both hands: together low in front, a dip, flung up — the hands parting
|
|
2366
|
+
// round the sides to up beside the head (never up across the face) — and back down together.
|
|
2367
|
+
toss: { F: [[0, together(-1, 0.55)], [0.22, together(-1, 0.4)], [0.44, flungUp(-1)], [1, together(-1, 0.5)]], B: [[0, together(1, 0.55)], [0.22, together(1, 0.4)], [0.44, flungUp(1)], [1, together(1, 0.5)]], pole: raisedPole },
|
|
2368
|
+
// Teetering at an edge, leaning out over it: the arms windmill round the sides — up beside the
|
|
2369
|
+
// head, out and down, up again.
|
|
2370
|
+
teeter: { torso: true, F: [[0, null], [0.14, flail(-1)], [0.32, outLow(-1)], [0.5, flail(-1)], [0.68, outLow(-1)], [1, null]], B: [[0, null], [0.14, flail(1)], [0.32, outLow(1)], [0.5, flail(1)], [0.68, outLow(1)], [1, null]], pole: raisedPole },
|
|
2371
|
+
// Flapping like wings: the arms out to the sides, down and up (never above the head).
|
|
2372
|
+
flap: { F: [[0, wingDown(-1)], [0.5, wingUp(-1)], [1, wingDown(-1)]], B: [[0, wingDown(1)], [0.5, wingUp(1)], [1, wingDown(1)]], pole: (sd) => [sd * 0.3, 1, -0.5] },
|
|
2373
|
+
// Dancing: one arm up beside the head while the other is down and out to its side, in turn.
|
|
2374
|
+
dance: { torso: true, F: [[0, danceUp(-1)], [0.5, outLow(-1)], [1, danceUp(-1)]], B: [[0, outLow(1)], [0.5, danceUp(1)], [1, outLow(1)]], pole: raisedPole },
|
|
2375
|
+
// A fighting stance: the lead fist out in front at shoulder height, the rear one by the chest —
|
|
2376
|
+
// both under the chin (the face shows; a guard covering it reads as hiding).
|
|
2377
|
+
stance: { torso: true, F: hold([-sx * 0.55, sh + T * 0.1, arm * 0.62]), B: hold([sx * 0.2, sh + T * 0.22, T * 0.62]), pole: (sd) => [sd * 0.6, 1, -0.2] },
|
|
2378
|
+
shrug: { F: visit([-(sx + u * 0.6), sh + T * 0.55, T * 0.45], 0.35, 0.7), B: visit([sx + u * 0.6, sh + T * 0.55, T * 0.45], 0.35, 0.7), pole: (sd) => [sd * 0.4, 1, -0.4] },
|
|
2379
|
+
point: { F: hold([-sx, sh + T * 0.1, arm * 0.9]), pole: (sd) => [sd * 0.3, 1, -0.2] },
|
|
2380
|
+
present: { F: hold([-(sx + arm * 0.5), sh + T * 0.3, arm * 0.6]), pole: (sd) => [sd * 0.3, 1, -0.3] },
|
|
2381
|
+
talk: { F: [[0, add(chest, [-sx * 0.3, T * 0.15, T * 0.1])], [0.5, add(chest, [-sx * 0.6, T * 0.05, T * 0.18])], [1, add(chest, [-sx * 0.3, T * 0.15, T * 0.1])]], pole: (sd) => [sd * 0.5, 1, -0.4] },
|
|
2382
|
+
sing: { F: [[0, [-sx * 0.9, sh + T * 0.25, T * 0.5]], [0.5, [-sx * 1.2, sh + T * 0.1, T * 0.6]], [1, [-sx * 0.9, sh + T * 0.25, T * 0.5]]], pole: (sd) => [sd * 0.5, 1, -0.4] },
|
|
2383
|
+
read: { F: hold([-sx * 0.35, sh + T * 0.4, T * 0.55]), B: hold([sx * 0.35, sh + T * 0.4, T * 0.55]), pole: (sd) => [sd * 0.5, 1, -0.3] },
|
|
2384
|
+
laugh: { F: hold([-W * 0.5, hip - T * 0.3, D + T * 0.1]), B: hold([W * 0.5, hip - T * 0.3, D + T * 0.1]), pole: (sd) => [sd * 0.6, 1, -0.4] },
|
|
2385
|
+
};
|
|
2386
|
+
for (const [name, g] of Object.entries(G)) {
|
|
2387
|
+
// (New gestures are made here; the others take their arm tracks from here.)
|
|
2388
|
+
const clip = doc.clips?.[name] ?? (g.duration ? (doc.clips[name] = { duration: g.duration, loop: true, ...(g.clear ? { clear: true } : {}), tracks: {} }) : undefined);
|
|
2389
|
+
if (!clip)
|
|
2390
|
+
continue;
|
|
2391
|
+
if (g.body)
|
|
2392
|
+
clip.tracks["bones.body.rotation"] = g.body.map(([f, v]) => [r(f * clip.duration), v, "sineInOut"]);
|
|
2393
|
+
if (g.hips)
|
|
2394
|
+
clip.tracks["bones.hips.y"] = g.hips.map(([f, v]) => [r(f * clip.duration), v, "sineInOut"]);
|
|
2395
|
+
if (g.nod)
|
|
2396
|
+
clip.tracks["bones.head.rotation"] = [[0, g.nod], [r(clip.duration), g.nod]];
|
|
2397
|
+
if (g.down) {
|
|
2398
|
+
clip.tracks["bones.pupils.y"] = [[0, r(u * 0.07)], [r(clip.duration), r(u * 0.07)]];
|
|
2399
|
+
// (The chin tucked: the head lower on the neck reads as looking down from the front too.)
|
|
2400
|
+
clip.tracks["bones.head.y"] = [[0, r(u * 0.14)], [r(clip.duration), r(u * 0.14)]];
|
|
2401
|
+
clip.tracks["parts.eyes.variant"] = [[0, "half"]];
|
|
2402
|
+
}
|
|
2403
|
+
const tracks = Object.fromEntries(Object.entries(clip.tracks).filter(([k]) => !/^bones\.arm[FB][12]\./.test(k)));
|
|
2404
|
+
// The clip's pose of the body and head at a fraction of it (its own tracks: a lean, a nod).
|
|
2405
|
+
const own = Object.entries(tracks).flatMap(([k, keys]) => {
|
|
2406
|
+
const m = /^bones\.([^.]+)\.(rotation|turn|spread)$/.exec(k);
|
|
2407
|
+
return m && doc.rig3d.bones[m[1]] ? [{ bone: m[1], ch: m[2], track: normalizeTrack(keys, clip.loop ? { loop: { duration: clip.duration } } : {}) }] : [];
|
|
2408
|
+
});
|
|
2409
|
+
const poseAt = (f) => {
|
|
2410
|
+
const v = {};
|
|
2411
|
+
for (const { bone, ch, track } of own) {
|
|
2412
|
+
const x = sampleTrack(track, f * clip.duration);
|
|
2413
|
+
if (typeof x === "number")
|
|
2414
|
+
(v[bone] ??= {})[ch] = x;
|
|
2415
|
+
}
|
|
2416
|
+
return v;
|
|
2417
|
+
};
|
|
2418
|
+
const min = g.touch ? 1 : 1.03;
|
|
2419
|
+
for (const side of ["F", "B"]) {
|
|
2420
|
+
const keys = g[side];
|
|
2421
|
+
if (!keys)
|
|
2422
|
+
continue;
|
|
2423
|
+
const sd = side === "F" ? -1 : 1;
|
|
2424
|
+
// The elbow's way for a hand place (a gesture's own, which may change with how high the hand is).
|
|
2425
|
+
const poleAt = (p) => g.pole?.(sd, p) ?? [sd * 0.5, 1, -0.3];
|
|
2426
|
+
const upper = `arm${side}1`, lower = `arm${side}2`;
|
|
2427
|
+
const reach = len(sub(doc.rig3d.bones[upper].to, doc.rig3d.bones[upper].from)) + len(sub(doc.rig3d.bones[lower].to, doc.rig3d.bones[lower].from));
|
|
2428
|
+
const restHand = doc.rig3d.bones[lower].to;
|
|
2429
|
+
const ch = {};
|
|
2430
|
+
const last = {};
|
|
2431
|
+
const put = (f, v, ease = "linear") => {
|
|
2432
|
+
for (const [b, vals] of Object.entries(v))
|
|
2433
|
+
for (const [k, x0] of Object.entries(vals)) {
|
|
2434
|
+
const key = `bones.${b}.${k}`;
|
|
2435
|
+
// The short way round from the previous key.
|
|
2436
|
+
const x = last[key] === undefined ? x0 : x0 + 360 * Math.round((last[key] - x0) / 360);
|
|
2437
|
+
last[key] = x;
|
|
2438
|
+
(ch[key] ??= []).push([r(f * clip.duration), r(x), ease]);
|
|
2439
|
+
}
|
|
2440
|
+
};
|
|
2441
|
+
let guess;
|
|
2442
|
+
const solveIn = (p, values, pl) => reach3d(doc, upper, lower, p, values, pl, guess);
|
|
2443
|
+
// The elbow's other ways (out to the side: up by a big head, the forearm stands beside it).
|
|
2444
|
+
const others = [[sd, 0.5, 0], [sd, 0.15, -0.1], [sd * 0.7, 1, 0.4]];
|
|
2445
|
+
// Where the hand goes for a target in a pose, and the elbow's way: out of the head, the whole
|
|
2446
|
+
// arm clear of it — the target itself, else the nearest clear place (the reach turned out to
|
|
2447
|
+
// the arm's side, up or down, shorter).
|
|
2448
|
+
const place = (p, values) => {
|
|
2449
|
+
const pose = rig3dPose(doc, values);
|
|
2450
|
+
const t0 = pushOut(pose, p, min), pole = poleAt(t0), poles = [pole, ...others];
|
|
2451
|
+
if (!keepOut.length)
|
|
2452
|
+
return { at: t0, pole };
|
|
2453
|
+
const clear = (t, pl) => {
|
|
2454
|
+
const arm3 = rig3dPose(doc, { ...values, ...solveIn(t, values, pl) })[lower];
|
|
2455
|
+
const mid = lerp3(arm3.from, arm3.to, 0.5);
|
|
2456
|
+
return len(sub(arm3.to, t)) < 2 && clearance(pose, arm3.to) >= min - 1e-3 && clearance(pose, mid) >= min && clearance(pose, arm3.from) >= min;
|
|
2457
|
+
};
|
|
2458
|
+
const S = pose[upper].from, d0 = sub(t0, S), l0 = len(d0) || 1;
|
|
2459
|
+
const dir = [d0[0] / l0, d0[1] / l0, d0[2] / l0], rho = Math.min(l0, reach * 0.96);
|
|
2460
|
+
const tries = [{ c: t0, e: 0 }];
|
|
2461
|
+
for (const k of [0.15, 0.3, 0.5, 0.75, 1, 1.5, 2.5])
|
|
2462
|
+
for (const j of [0, -0.25, 0.25, -0.5])
|
|
2463
|
+
for (const s of [1, 0.85, 0.7]) {
|
|
2464
|
+
const d = add(dir, [sd * k, j, 0]), dl = len(d);
|
|
2465
|
+
const c = pushOut(pose, add(S, [(d[0] / dl) * rho * s, (d[1] / dl) * rho * s, (d[2] / dl) * rho * s]), min);
|
|
2466
|
+
if (len(sub(c, S)) <= reach * 0.98)
|
|
2467
|
+
tries.push({ c, e: len(sub(c, t0)) });
|
|
2468
|
+
}
|
|
2469
|
+
tries.sort((x, y) => x.e - y.e);
|
|
2470
|
+
for (const { c } of tries)
|
|
2471
|
+
for (const pl of poles)
|
|
2472
|
+
if (clear(c, pl))
|
|
2473
|
+
return { at: c, pole: pl };
|
|
2474
|
+
return { at: t0, pole };
|
|
2475
|
+
};
|
|
2476
|
+
const zero = { [upper]: { rotation: 0, spread: 0, turn: 0 }, [lower]: { rotation: 0, turn: 0, spread: 0 } };
|
|
2477
|
+
const placed = keys.map(([f, p]) => {
|
|
2478
|
+
if (!p)
|
|
2479
|
+
return null;
|
|
2480
|
+
const values = poseAt(f);
|
|
2481
|
+
const pose = rig3dPose(doc, values);
|
|
2482
|
+
return place(g.head ? onHead(pose, p) : g.torso ? onBody(pose, p) : p, values);
|
|
2483
|
+
});
|
|
2484
|
+
// The hand travels between the keys along an arc in front of the body (the arm swings round
|
|
2485
|
+
// the front, never through the chest or the head) — to or from a hand raised beside the head,
|
|
2486
|
+
// round the arm's own side (up and down beside the head: from a side the arm comes up past the
|
|
2487
|
+
// shoulder, under the head, never sweeping up across the face) — every in-between placed and
|
|
2488
|
+
// solved again.
|
|
2489
|
+
keys.forEach(([f, p], i) => {
|
|
2490
|
+
if (i > 0) {
|
|
2491
|
+
const [f0, p0] = keys[i - 1];
|
|
2492
|
+
const a = placed[i - 1]?.at ?? restHand, b = placed[i]?.at ?? restHand;
|
|
2493
|
+
const dist = Math.hypot(b[0] - a[0], b[1] - a[1], b[2] - a[2]);
|
|
2494
|
+
const round = (!!p0 && raised.has(p0)) || (!!p && raised.has(p));
|
|
2495
|
+
if (dist > 8)
|
|
2496
|
+
for (const k of [0.25, 0.5, 0.75]) {
|
|
2497
|
+
const bulge = Math.sin(k * Math.PI) * dist * 0.25, fk = f0 + (f - f0) * k, values = poseAt(fk);
|
|
2498
|
+
// (Round the side, the arm swings about the shoulder as far out as the keys hold it — its
|
|
2499
|
+
// direction turning out past the side, its reach between theirs — not bent in on a chord.)
|
|
2500
|
+
const S = shoulder(sd), da = sub(a, S), db = sub(b, S), w = Math.sin(k * Math.PI) * 0.6;
|
|
2501
|
+
const d = unit(add(lerp3(unit(da), unit(db), k), [sd * w, 0, w * 0.15]));
|
|
2502
|
+
const at = round ? add(S, d.map((v) => v * (len(da) + (len(db) - len(da)) * k))) : [a[0] + (b[0] - a[0]) * k, a[1] + (b[1] - a[1]) * k, a[2] + (b[2] - a[2]) * k + bulge];
|
|
2503
|
+
const q = place(at, values);
|
|
2504
|
+
put(fk, (guess = solveIn(q.at, values, q.pole)));
|
|
2505
|
+
}
|
|
2506
|
+
}
|
|
2507
|
+
const q = placed[i];
|
|
2508
|
+
put(f, q ? (guess = solveIn(q.at, poseAt(f), q.pole)) : (guess = zero), "sineInOut");
|
|
2509
|
+
});
|
|
2510
|
+
Object.assign(tracks, ch);
|
|
2511
|
+
}
|
|
2512
|
+
doc.clips[name] = { ...clip, tracks };
|
|
2513
|
+
}
|
|
2514
|
+
}
|
|
2515
|
+
/**
|
|
2516
|
+
* Every colour gets a shadow tone (`<key>Shade`, darker and a little cooler) and a line tone
|
|
2517
|
+
* (`<key>Line`, darker than its shadow: coloured lines instead of black ones).
|
|
2518
|
+
*/
|
|
2519
|
+
export function withTones(palette, o = {}) {
|
|
2520
|
+
// Short hex colours (#abc) written out (#aabbcc).
|
|
2521
|
+
palette = Object.fromEntries(Object.entries(palette).map(([k, v]) => [k, longHex(v)]));
|
|
2522
|
+
const out = { ...palette };
|
|
2523
|
+
// One ink for every line (a flat TV cartoon): the `ink` key and every `<key>Line`.
|
|
2524
|
+
if (o.ink)
|
|
2525
|
+
out.ink = longHex(o.ink);
|
|
2526
|
+
// A painter's shadow layer: the colour multiplied by a dusty rose (warm shadows, never grey);
|
|
2527
|
+
// lines multiplied further, a dark tone of the same colour.
|
|
2528
|
+
const multiply = (hex, m) => {
|
|
2529
|
+
const n = parseInt(hex.slice(1), 16);
|
|
2530
|
+
const c = [(n >> 16) & 255, (n >> 8) & 255, n & 255].map((v, i) => Math.round(v * m[i]));
|
|
2531
|
+
return `#${((c[0] << 16) | (c[1] << 8) | c[2]).toString(16).padStart(6, "0")}`;
|
|
2532
|
+
};
|
|
2533
|
+
for (const [k, v] of Object.entries(palette)) {
|
|
2534
|
+
if (!/^#[0-9a-f]{6}$/i.test(v))
|
|
2535
|
+
continue;
|
|
2536
|
+
out[`${k}Shade`] = multiply(v, [0.86, 0.77, 0.8]);
|
|
2537
|
+
out[`${k}Line`] = o.ink ? out.ink : multiply(v, [0.46, 0.34, 0.36]);
|
|
2538
|
+
// A shadow tone drawn as a base colour (a neck in the head's shadow) is outlined like its colour.
|
|
2539
|
+
out[`${k}ShadeLine`] = out[`${k}Line`];
|
|
2540
|
+
}
|
|
2541
|
+
return out;
|
|
2542
|
+
}
|
|
2543
|
+
/** The jaw's parts of a view (`gapeView`): the skin of the dropped lower head, then its outline. */
|
|
2544
|
+
function jawParts(F, s) {
|
|
2545
|
+
return [
|
|
2546
|
+
{ id: `jaw${s}`, type: "morph", bone: "head", fill: "palette(skin)", base: F.jaw.base, shapes: F.jaw.shapes },
|
|
2547
|
+
...(F.jawShade ? [{ id: `jawShade${s}`, type: "morph", bone: "head", fill: "palette(skinShade)", base: F.jawShade.base, shapes: F.jawShade.shapes }] : []),
|
|
2548
|
+
{ id: `jawLine${s}`, type: "morph", bone: "head", fill: "none", stroke: "palette(skinLine)", strokeWidth: lw(SW), attrs: { "stroke-linejoin": "round", "stroke-linecap": "round" }, base: F.jawLine.base, shapes: F.jawLine.shapes },
|
|
2549
|
+
];
|
|
2550
|
+
}
|
|
2551
|
+
/**
|
|
2552
|
+
* The other drawn angles as views (front, half, side, away, back), in turning order: arms and legs
|
|
2553
|
+
* move to where each angle puts them, the face parts of each angle follow the same emotions,
|
|
2554
|
+
* blinks and lip sync.
|
|
2555
|
+
*/
|
|
2556
|
+
function withTurnaround(doc, views, sx, hx, tail, headPoints) {
|
|
2557
|
+
const { stroke: LIPS, strokeWidth: LIPS_W } = doc.parts.find((p) => p.id === "mouth");
|
|
2558
|
+
const cq = Math.cos(Q);
|
|
2559
|
+
const others = Object.keys(VIEWS).filter((v) => v !== "profile");
|
|
2560
|
+
const viewParts = (v) => {
|
|
2561
|
+
const s = SUFFIX[v];
|
|
2562
|
+
const before = (id, part) => ({ before: id, part });
|
|
2563
|
+
const list = [
|
|
2564
|
+
...(tail ? [before("neck", { id: `tailBack${s}`, type: "rigid", bone: "tail", art: `tailBack${s}` })] : []),
|
|
2565
|
+
before("neck", { id: `hairBack${s}`, type: "rigid", bone: "hair", art: `hairBack${s}` }),
|
|
2566
|
+
// The torso under the legs' solid (it is drawn over the torso where the legs are nearer).
|
|
2567
|
+
before("legs", { id: `neck${s}`, type: "rigid", bone: "neck", art: `neck${s}` }),
|
|
2568
|
+
before("legs", { id: `torso${s}`, type: "rigid", bone: "body", art: `torso${s}` }),
|
|
2569
|
+
before("arms", { id: `earsBack${s}`, type: "rigid", bone: "head", art: `earsBack${s}` }),
|
|
2570
|
+
before("arms", { id: `head${s}`, type: "rigid", bone: "head", art: `head${s}` }),
|
|
2571
|
+
...(views[v].face.visible ? jawParts(views[v].face, s).map((p) => before("arms", p)) : []),
|
|
2572
|
+
before("arms", { id: `nose${s}`, type: "rigid", bone: "head", art: `nose${s}` }),
|
|
2573
|
+
];
|
|
2574
|
+
const F = views[v].face;
|
|
2575
|
+
if (F.visible) {
|
|
2576
|
+
for (const [k, art] of Object.entries(F.eyeVariants)) {
|
|
2577
|
+
doc.art[`eye${s}_${k}`] = art;
|
|
2578
|
+
// (Each angle's eyes follow the main eyes' variant: blinks, emotions.)
|
|
2579
|
+
list.push(before("arms", { id: `eye${s}_${k}`, type: "rigid", bone: "head", art: `eye${s}_${k}`, visibleWhen: { part: "eyes", variant: k } }));
|
|
2580
|
+
}
|
|
2581
|
+
doc.art[`pupils${s}`] = F.pupils;
|
|
2582
|
+
doc.art[`lids${s}`] = F.lids;
|
|
2583
|
+
list.push(before("arms", { id: `pupils${s}`, type: "rigid", bone: "pupils", art: `pupils${s}`, visibleWhen: { part: "eyes", variant: ["open", "wide", "half"] } }), before("arms", { id: `lids${s}`, type: "rigid", bone: "head", art: `lids${s}`, visibleWhen: { part: "eyes", variant: "half" } }), before("arms", { id: `mouth${s}`, type: "morph", bone: "head", fill: "palette(mouth)", stroke: LIPS, strokeWidth: LIPS_W, attrs: { "stroke-linejoin": "round" }, base: F.mouth.base, shapes: F.mouth.shapes }), before("arms", { id: `tongue${s}`, type: "morph", bone: "head", fill: "palette(tongue)", base: F.tongue.base, shapes: F.tongue.shapes, clip: `mouth${s}` }), before("arms", { id: `teeth${s}`, type: "morph", bone: "head", fill: "#ffffff", base: F.teeth.base, shapes: F.teeth.shapes, clip: `mouth${s}` }));
|
|
2584
|
+
}
|
|
2585
|
+
list.push(before("arms", { id: `hairFront${s}`, type: "rigid", bone: "hair", art: `hairFront${s}` }), ...(tail ? [before("arms", { id: `tailFront${s}`, type: "rigid", bone: "tail", art: `tailFront${s}` })] : []), before("arms", { id: `earsFront${s}`, type: "rigid", bone: "head", art: `earsFront${s}` }), before("arms", { id: `over${s}`, type: "rigid", bone: "head", art: `over${s}` }));
|
|
2586
|
+
if (F.visible)
|
|
2587
|
+
list.push(before("arms", { id: `brows${s}`, type: "morph", bone: "head", fill: "palette(brow)", base: F.brows.base, shapes: F.brows.shapes }));
|
|
2588
|
+
return list;
|
|
2589
|
+
};
|
|
2590
|
+
const face = ["eyes", "pupils", "lids", "mouth", "teeth", "tongue", "jaw", "jawShade", "jawLine", "brows"].filter((id) => doc.parts.some((p) => p.id === id));
|
|
2591
|
+
// Where an angle puts the shoulders and hips (relative to the three-quarter skeleton).
|
|
2592
|
+
const move = (theta, v) => {
|
|
2593
|
+
const c = Math.cos(theta);
|
|
2594
|
+
const pts = Object.fromEntries(["ear", "earB", "mouth", "eye", "top"].map((k) => [`${k}Pt`, [r(headPoints[v][k][0] - headPoints.profile[k][0]), r(headPoints[v][k][1] - headPoints.profile[k][1])]]));
|
|
2595
|
+
return { armF1: [r(sx * (cq - c)), 0], armB1: [r(sx * (c - cq)), 0], legF1: [r(hx * (cq - c)), 0], legB1: [r(hx * (c - cq)), 0], ...pts };
|
|
2596
|
+
};
|
|
2597
|
+
const jaws = ["jaw", "jawShade", "jawLine"].filter((id) => doc.parts.some((p) => p.id === id));
|
|
2598
|
+
const spec = {
|
|
2599
|
+
eyes: { center: [0, 0], parts: [] },
|
|
2600
|
+
mouth: { center: [0, 0], parts: [] },
|
|
2601
|
+
profileOnly: ["hairBack", "neck", "torso", "earsBack", "head", "earsFront", "nose", "over", "hairFront", ...(tail ? ["tailBack", "tailFront"] : [])],
|
|
2602
|
+
hide: Object.fromEntries(others.map((v) => [v, face])),
|
|
2603
|
+
parts: Object.fromEntries(others.map((v) => [v, viewParts(v)])),
|
|
2604
|
+
move: Object.fromEntries(others.map((v) => [v, move(VIEWS[v], v)])),
|
|
2605
|
+
// (The jaw's parts have only the `gape` shape: the visemes leave them at rest.)
|
|
2606
|
+
mouths: ["teeth", "tongue", ...jaws, ...others.filter((v) => views[v].face.visible).flatMap((v) => ["mouth", "teeth", "tongue", ...jaws].map((k) => `${k}${SUFFIX[v]}`))],
|
|
2607
|
+
order: Object.keys(VIEWS),
|
|
2608
|
+
// (Running across the frame: three-quarters side-on — fully side-on, a big round head hides the eye.)
|
|
2609
|
+
travel: "q64",
|
|
2610
|
+
};
|
|
2611
|
+
const out = withViews(doc, spec);
|
|
2612
|
+
// The mouth control takes the gape as a shape too (`"gape"`, `{ gape: 2 }`: every view's mouth,
|
|
2613
|
+
// teeth, tongue and jaw), and `"shut"` (the rest viseme).
|
|
2614
|
+
out.controls.mouth.map = { shut: "X" };
|
|
2615
|
+
// The limbs are placed by the 3D projection (rig3d): the views' 2D limb moves and foot targets
|
|
2616
|
+
// would push the 3D legs and arms off.
|
|
2617
|
+
for (const pose of Object.values(out.controls.view.poses)) {
|
|
2618
|
+
for (const k of Object.keys(pose))
|
|
2619
|
+
if (/^(ik\.foot[FB]\.[xy]|bones\.(arm|leg)[FB]1\.[xy])$/.test(k))
|
|
2620
|
+
delete pose[k];
|
|
2621
|
+
}
|
|
2622
|
+
// Facing left (the rig mirrored): the mirrored drawings of the asymmetric details (`side`, set by
|
|
2623
|
+
// the scene from the actor's facing).
|
|
2624
|
+
const sideLeft = {};
|
|
2625
|
+
for (const p of out.parts) {
|
|
2626
|
+
if (p.type !== "rigid" || !/^(hairBack|hairFront|torso)[A-Z]?$/.test(p.id) || typeof p.art !== "string" || !out.art[`${p.art}M`])
|
|
2627
|
+
continue;
|
|
2628
|
+
const key = p.art;
|
|
2629
|
+
delete p.art;
|
|
2630
|
+
Object.assign(p, { type: "switch", default: "r", variants: { r: key, l: `${key}M` } });
|
|
2631
|
+
sideLeft[`parts.${p.id}.variant`] = "l";
|
|
2632
|
+
}
|
|
2633
|
+
sideLeft["parts.sideSwitch.variant"] = "l";
|
|
2634
|
+
// (The control exists when anything depends on the side: a mirrored drawing, a part shown by
|
|
2635
|
+
// `sideSwitch` such as a watch.)
|
|
2636
|
+
const sided = out.parts.some((p) => p.visibleWhen?.part === "sideSwitch");
|
|
2637
|
+
if (Object.keys(sideLeft).length > 1 || sided)
|
|
2638
|
+
out.controls.side = { type: "pose", poses: { right: {}, left: sideLeft } };
|
|
2639
|
+
const suffixes = others.filter((v) => views[v].face.visible).map((v) => SUFFIX[v]);
|
|
2640
|
+
for (const pose of [out.controls.emotion, out.controls.jaw].flatMap((c) => Object.values(c.poses))) {
|
|
2641
|
+
for (const [k, v] of Object.entries({ ...pose })) {
|
|
2642
|
+
for (const s of ["", ...suffixes]) {
|
|
2643
|
+
// (The gape drops the jaw too.)
|
|
2644
|
+
if (k.startsWith("parts.mouth.morph."))
|
|
2645
|
+
for (const part of ["mouth", "teeth", "tongue", ...(k === "parts.mouth.morph.gape" ? jaws : [])])
|
|
2646
|
+
if (part !== "mouth" || s)
|
|
2647
|
+
pose[k.replace("parts.mouth.", `parts.${part}${s}.`)] = v;
|
|
2648
|
+
if (k.startsWith("parts.brows.morph.") && s)
|
|
2649
|
+
pose[k.replace("parts.brows.", `parts.brows${s}.`)] = v;
|
|
2650
|
+
}
|
|
2651
|
+
}
|
|
2652
|
+
}
|
|
2653
|
+
withHeadTurn(out, others);
|
|
2654
|
+
return out;
|
|
2655
|
+
}
|
|
2656
|
+
/** Head parts (per view): the head's own drawings, chosen by `headView`. */
|
|
2657
|
+
const HEAD_PART = /^(hairBack|tailBack|earsBack|head|jawLine|jawShade|jaw|nose|eyes|eye|pupils|lids|mouth|teeth|tongue|over|hairFront|tailFront|earsFront|brows)([A-Z]?)(?:_(.+))?$/;
|
|
2658
|
+
/**
|
|
2659
|
+
* The head turns on its own (a listener looking at whoever speaks while sitting facing the TV): a
|
|
2660
|
+
* hidden `headView` switch picks the head's drawings — any drawn angle, or one mirrored (`~q50`:
|
|
2661
|
+
* turned the other way) — the `view` control sets it to the body's view, the `head` control
|
|
2662
|
+
* overrides it.
|
|
2663
|
+
*/
|
|
2664
|
+
function withHeadTurn(out, others) {
|
|
2665
|
+
const viewOf = (suffix) => (suffix ? others.find((v) => SUFFIX[v] === suffix) : "profile");
|
|
2666
|
+
const mirrorable = (v) => VIEWS[v] > 0.01 && VIEWS[v] < Math.PI - 0.01;
|
|
2667
|
+
const names = Object.keys(VIEWS);
|
|
2668
|
+
const allHead = [...names, ...names.filter(mirrorable).map((v) => `~${v}`)];
|
|
2669
|
+
// Mirroring: rigid art wrapped in a flip about the head's centre line (x = 0); morph paths flipped.
|
|
2670
|
+
const flipArt = (key) => {
|
|
2671
|
+
const k = `${key}~`;
|
|
2672
|
+
if (!out.art[k])
|
|
2673
|
+
out.art[k] = `<g transform="scale(-1 1)">${out.art[key] ?? key}</g>`;
|
|
2674
|
+
return k;
|
|
2675
|
+
};
|
|
2676
|
+
const flipPath = (d) => pathToString(withPoints(parsePath(d), pathPoints(parsePath(d)).map(([x, y]) => [-x, y])));
|
|
2677
|
+
const added = [];
|
|
2678
|
+
const morphMirrors = {};
|
|
2679
|
+
const gate = (v) => ({ part: "headView", variant: v });
|
|
2680
|
+
for (const p of out.parts) {
|
|
2681
|
+
const m = HEAD_PART.exec(p.id ?? "");
|
|
2682
|
+
if (!m)
|
|
2683
|
+
continue;
|
|
2684
|
+
const v = viewOf(m[2]);
|
|
2685
|
+
const kind = m[1];
|
|
2686
|
+
const eyeVariant = kind === "eye" ? m[3] : undefined;
|
|
2687
|
+
// Gate on the head view (eye drawings also on the eyes' variant: blinks, emotions).
|
|
2688
|
+
const face = kind === "eye" ? { part: "eyes", variant: eyeVariant } : kind === "pupils" ? { part: "eyes", variant: ["open", "wide", "half"] } : kind === "lids" ? { part: "eyes", variant: "half" } : undefined;
|
|
2689
|
+
p.visibleWhen = face ? { ...face, and: gate(v) } : gate(v);
|
|
2690
|
+
if (!mirrorable(v))
|
|
2691
|
+
continue;
|
|
2692
|
+
const id = `${p.id}~`;
|
|
2693
|
+
let dup;
|
|
2694
|
+
if (p.type === "rigid")
|
|
2695
|
+
dup = { ...p, id, art: flipArt(p.art) };
|
|
2696
|
+
else if (p.type === "morph") {
|
|
2697
|
+
dup = { ...p, id, base: flipPath(p.base), shapes: Object.fromEntries(Object.entries(p.shapes).map(([k, d]) => [k, flipPath(d)])), ...(p.clip ? { clip: `${p.clip}~` } : {}) };
|
|
2698
|
+
morphMirrors[p.id] = id;
|
|
2699
|
+
}
|
|
2700
|
+
else if (p.type === "switch" && kind === "eyes") {
|
|
2701
|
+
// The main eyes switch holds the variant: its mirror is one drawing per variant.
|
|
2702
|
+
for (const [k, key] of Object.entries(p.variants))
|
|
2703
|
+
added.push({ after: p.id, part: { id: `eyes~_${k}`, type: "rigid", bone: p.bone, art: flipArt(key), visibleWhen: { part: "eyes", variant: k, and: gate(`~${v}`) } } });
|
|
2704
|
+
}
|
|
2705
|
+
else if (p.type === "switch")
|
|
2706
|
+
dup = { ...p, id, variants: Object.fromEntries(Object.entries(p.variants).map(([k, key]) => [k, flipArt(key)])) };
|
|
2707
|
+
if (!dup)
|
|
2708
|
+
continue;
|
|
2709
|
+
dup.visibleWhen = face ? { ...face, and: gate(`~${v}`) } : gate(`~${v}`);
|
|
2710
|
+
added.push({ after: p.id, part: dup });
|
|
2711
|
+
}
|
|
2712
|
+
for (const { after, part } of added)
|
|
2713
|
+
out.parts.splice(out.parts.findIndex((q) => q.id === after) + 1, 0, part);
|
|
2714
|
+
out.parts.splice(1, 0, { id: "headView", type: "switch", bone: "root", variants: Object.fromEntries(allHead.map((v) => [v, "viewNone"])), default: "profile" });
|
|
2715
|
+
// The face's opacity gating per view is replaced by the head view.
|
|
2716
|
+
for (const pose of Object.values(out.controls.view.poses)) {
|
|
2717
|
+
for (const k of Object.keys(pose)) {
|
|
2718
|
+
const m = /^parts\.([^.]+)\.opacity$/.exec(k);
|
|
2719
|
+
if (m && HEAD_PART.test(m[1]))
|
|
2720
|
+
delete pose[k];
|
|
2721
|
+
}
|
|
2722
|
+
}
|
|
2723
|
+
for (const [v, pose] of Object.entries(out.controls.view.poses))
|
|
2724
|
+
pose["parts.headView.variant"] = v;
|
|
2725
|
+
// Emotions, visemes and the asymmetric side drive the mirrored parts too.
|
|
2726
|
+
for (const pose of [out.controls.emotion, out.controls.jaw].flatMap((c) => Object.values(c.poses))) {
|
|
2727
|
+
for (const [k, val] of Object.entries({ ...pose })) {
|
|
2728
|
+
const m = /^parts\.([^.]+)\.(morph\..+)$/.exec(k);
|
|
2729
|
+
if (m && morphMirrors[m[1]])
|
|
2730
|
+
pose[`parts.${morphMirrors[m[1]]}.${m[2]}`] = val;
|
|
2731
|
+
}
|
|
2732
|
+
}
|
|
2733
|
+
const mouths = out.controls.mouth.part;
|
|
2734
|
+
out.controls.mouth.part = [...(Array.isArray(mouths) ? mouths : [mouths]), ...Object.entries(morphMirrors).filter(([id]) => /^(mouth|teeth|tongue|jaw)/.test(id)).map(([, id]) => id)];
|
|
2735
|
+
const side = out.controls.side?.poses?.left;
|
|
2736
|
+
if (side)
|
|
2737
|
+
for (const [k, val] of Object.entries({ ...side })) {
|
|
2738
|
+
const m = /^parts\.([^.]+)\.variant$/.exec(k);
|
|
2739
|
+
if (m && out.parts.some((q) => q.id === `${m[1]}~`))
|
|
2740
|
+
side[`parts.${m[1]}~.variant`] = val;
|
|
2741
|
+
}
|
|
2742
|
+
// The head's points (where props fit: the mouth, an ear…) go with the head's drawing, not the
|
|
2743
|
+
// body's view: the head control sets them (override) for its angle, mirrored ones flipped about the
|
|
2744
|
+
// head's middle.
|
|
2745
|
+
const pts = ["ear", "earB", "mouth", "eye", "top"].filter((k) => out.skeleton.some((b) => b.id === `${k}Pt`));
|
|
2746
|
+
const fromX = (k) => (out.skeleton.find((b) => b.id === `${k}Pt`)?.from?.[0] ?? 0);
|
|
2747
|
+
const ptPose = (v) => {
|
|
2748
|
+
const mirror = v.startsWith("~"), base = mirror ? v.slice(1) : v;
|
|
2749
|
+
const vp = (out.controls.view.poses[base] ?? {});
|
|
2750
|
+
const o = {};
|
|
2751
|
+
// (Offsets are in the head bone's frame: turned to setup space to mirror them.)
|
|
2752
|
+
const hb = out.skeleton.find((b) => b.id === "head");
|
|
2753
|
+
const th = Math.atan2(hb.to[1] - hb.from[1], hb.to[0] - hb.from[0]), c = Math.cos(th), sn = Math.sin(th);
|
|
2754
|
+
for (const k of pts) {
|
|
2755
|
+
const dx = vp[`bones.${k}Pt.x`] ?? 0, dy = vp[`bones.${k}Pt.y`] ?? 0;
|
|
2756
|
+
if (!mirror) {
|
|
2757
|
+
o[`bones.${k}Pt.x`] = r(dx);
|
|
2758
|
+
o[`bones.${k}Pt.y`] = r(dy);
|
|
2759
|
+
continue;
|
|
2760
|
+
}
|
|
2761
|
+
const sx = dx * c - dy * sn, sy = dx * sn + dy * c;
|
|
2762
|
+
const mx = -2 * fromX(k) - sx, my = sy;
|
|
2763
|
+
o[`bones.${k}Pt.x`] = r(mx * c + my * sn);
|
|
2764
|
+
o[`bones.${k}Pt.y`] = r(-mx * sn + my * c);
|
|
2765
|
+
}
|
|
2766
|
+
return o;
|
|
2767
|
+
};
|
|
2768
|
+
out.controls.head = { type: "pose", override: true, poses: Object.fromEntries(allHead.map((v) => [v, { "parts.headView.variant": v, ...ptPose(v) }])) };
|
|
2769
|
+
}
|
|
2770
|
+
/**
|
|
2771
|
+
* Director measurements of a cartoon character: the skeleton's (`rigInfo`), its `extent` and
|
|
2772
|
+
* `height` measured from the character as drawn at rest in the main view (`drawnBounds`: a big
|
|
2773
|
+
* head, hair, a cap's peak, a belly — whatever the anatomy draws; the doc was built at its pitch),
|
|
2774
|
+
* the body's half depth from its anatomy.
|
|
2775
|
+
*/
|
|
2776
|
+
export function cartoonInfo(doc, look, opts = {}) {
|
|
2777
|
+
const a = opts.anatomy ?? doc.meta?.anatomy ?? styledAnatomy(cartoonAnatomy(look), opts.style, look);
|
|
2778
|
+
const D = a.body.depth;
|
|
2779
|
+
return rigInfo(doc, { depth: { back: D, front: D * (1 + (look.heavy ?? 0) * 0.6) } });
|
|
2780
|
+
}
|
|
2781
|
+
/**
|
|
2782
|
+
* How things are held (2.5D characters): for each kind of hand-held object, the gesture clip, the
|
|
2783
|
+
* 3D direction of the object's long axis and of its front (body space: x right, y down, z towards the
|
|
2784
|
+
* viewer seen from the front), and its size relative to the holder's scale. From ergonomics studies of
|
|
2785
|
+
* phone use (held in front of the lower chest about a forearm from the eyes, the screen tilted up
|
|
2786
|
+
* towards them, the head bent forward) and from life drawing (the palm and fingers behind the object,
|
|
2787
|
+
* the thumb over its edge: seen from the front or the side you see the phone's back).
|
|
2788
|
+
*
|
|
2789
|
+
* `holdFit(kind, grip, top)` is the `grab` action's `fit` for an object drawn upright with its grip at
|
|
2790
|
+
* `grip` and its top at `top` (its own art's coordinates), its front (`art`) the face the holder looks
|
|
2791
|
+
* at and its `back` drawn as seen from behind.
|
|
2792
|
+
*/
|
|
2793
|
+
export const HOLD = {
|
|
2794
|
+
/** A phone being used: one hand, thumb scrolling (`textingTwo`: both thumbs). */
|
|
2795
|
+
phone: { clip: "texting", dir: [0, -0.8, 0.6], face: [0, -0.6, -0.8], scale: 0.5 },
|
|
2796
|
+
/** A box carried in both hands in front of the belly (`carry`; `boxFit`: its sides on the two hands). */
|
|
2797
|
+
box: { clip: "carry", dir: [1, 0, 0], face: [0, 0, 1], scale: 1 },
|
|
2798
|
+
/** A book or a tablet being read in both hands, a little lower and further than a phone. */
|
|
2799
|
+
book: { clip: "read", dir: [0, -0.7, 0.7], face: [0, -0.7, -0.7], scale: 0.9 },
|
|
2800
|
+
};
|
|
2801
|
+
export function holdFit(kind, grip, top) {
|
|
2802
|
+
const h = HOLD[kind];
|
|
2803
|
+
return [{ point: grip, anchor: "hand" }, { point: top, dir: [...h.dir], face: [...h.face], fixed: true }];
|
|
2804
|
+
}
|
|
2805
|
+
/** The `grab`'s `also` for a two-handed hold (`textingTwo`, `read`): the far hand holds it too. */
|
|
2806
|
+
export const holdBoth = ["handB"];
|
|
2807
|
+
/**
|
|
2808
|
+
* The `grab`'s `fit` for something carried between the two hands (a box, a tray): its left side's
|
|
2809
|
+
* point on the near hand, turned so its right side's point is on the far hand (its own art's
|
|
2810
|
+
* coordinates). Grab it at `hand` with `also: holdBoth`, play `carry`.
|
|
2811
|
+
*/
|
|
2812
|
+
export function boxFit(left, right) {
|
|
2813
|
+
return [{ point: left, anchor: "hand" }, { point: right, anchor: "handB" }];
|
|
2814
|
+
}
|
|
2815
|
+
//# sourceMappingURL=cartoon.js.map
|