@voqalize/avatar 0.2.2 → 0.4.0
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 -661
- package/LICENSE-CC-BY-4.0 +396 -0
- package/README.md +191 -662
- package/assets/README.md +48 -0
- package/assets/tanya.glb +0 -0
- package/assets/tara.glb +0 -0
- package/assets/tushar.glb +0 -0
- package/client/Avatar.tsx +57 -0
- package/client/AvatarClient.ts +818 -0
- package/client/arjun.ts +26 -0
- package/client/createAvatar.ts +177 -0
- package/client/createCanvasAvatar.ts +72 -0
- package/client/index.ts +44 -0
- package/client/internal.ts +108 -0
- package/client/interviewer-female.ts +4 -0
- package/client/interviewer-male.ts +4 -0
- package/client/ishita.ts +26 -0
- package/client/kabir.ts +26 -0
- package/client/meera.ts +26 -0
- package/client/naina.ts +26 -0
- package/client/playout.ts +95 -0
- package/client/professional-female-a.ts +4 -0
- package/client/professional-female-b.ts +4 -0
- package/client/professional-male-a.ts +4 -0
- package/client/professional-male-b.ts +4 -0
- package/client/react.ts +13 -0
- package/client/supports.ts +28 -0
- package/client/three/assets.ts +24 -0
- package/client/three/budgets.ts +27 -0
- package/client/three/holds.ts +64 -0
- package/client/three/internal.ts +54 -0
- package/client/three/motion-limits.json +116 -0
- package/client/three/sequences.ts +281 -0
- package/client/three/tanya.ts +69 -0
- package/client/three/tara-rig.ts +1562 -0
- package/client/three/tara.ts +76 -0
- package/client/three/tushar.ts +65 -0
- package/client/types.ts +235 -0
- package/client/useAvatar.ts +74 -0
- package/client/vikram.ts +26 -0
- package/dist/Avatar.d.ts +44 -0
- package/dist/Avatar.d.ts.map +1 -0
- package/{client/dist → dist}/Avatar.js +2 -2
- package/dist/Avatar.js.map +1 -0
- package/dist/AvatarClient.d.ts +274 -0
- package/dist/AvatarClient.d.ts.map +1 -0
- package/dist/AvatarClient.js +712 -0
- package/dist/AvatarClient.js.map +1 -0
- package/dist/arjun.d.ts +7 -0
- package/dist/arjun.d.ts.map +1 -0
- package/dist/arjun.js +20 -0
- package/dist/arjun.js.map +1 -0
- package/dist/createAvatar.d.ts +135 -0
- package/dist/createAvatar.d.ts.map +1 -0
- package/dist/createAvatar.js +75 -0
- package/dist/createAvatar.js.map +1 -0
- package/dist/createCanvasAvatar.d.ts +22 -0
- package/dist/createCanvasAvatar.d.ts.map +1 -0
- package/dist/createCanvasAvatar.js +47 -0
- package/dist/createCanvasAvatar.js.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +34 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +41 -0
- package/dist/internal.js.map +1 -0
- package/dist/interviewer-female.d.ts +4 -0
- package/dist/interviewer-female.d.ts.map +1 -0
- package/dist/interviewer-female.js +3 -0
- package/dist/interviewer-female.js.map +1 -0
- package/dist/interviewer-male.d.ts +4 -0
- package/dist/interviewer-male.d.ts.map +1 -0
- package/dist/interviewer-male.js +3 -0
- package/dist/interviewer-male.js.map +1 -0
- package/dist/ishita.d.ts +7 -0
- package/dist/ishita.d.ts.map +1 -0
- package/dist/ishita.js +20 -0
- package/dist/ishita.js.map +1 -0
- package/dist/kabir.d.ts +7 -0
- package/dist/kabir.d.ts.map +1 -0
- package/dist/kabir.js +20 -0
- package/dist/kabir.js.map +1 -0
- package/dist/meera.d.ts +7 -0
- package/dist/meera.d.ts.map +1 -0
- package/dist/meera.js +20 -0
- package/dist/meera.js.map +1 -0
- package/dist/naina.d.ts +7 -0
- package/dist/naina.d.ts.map +1 -0
- package/dist/naina.js +20 -0
- package/dist/naina.js.map +1 -0
- package/dist/playout.d.ts +42 -0
- package/dist/playout.d.ts.map +1 -0
- package/dist/playout.js +85 -0
- package/dist/playout.js.map +1 -0
- package/dist/professional-female-a.d.ts +4 -0
- package/dist/professional-female-a.d.ts.map +1 -0
- package/dist/professional-female-a.js +3 -0
- package/dist/professional-female-a.js.map +1 -0
- package/dist/professional-female-b.d.ts +4 -0
- package/dist/professional-female-b.d.ts.map +1 -0
- package/dist/professional-female-b.js +3 -0
- package/dist/professional-female-b.js.map +1 -0
- package/dist/professional-male-a.d.ts +4 -0
- package/dist/professional-male-a.d.ts.map +1 -0
- package/dist/professional-male-a.js +3 -0
- package/dist/professional-male-a.js.map +1 -0
- package/dist/professional-male-b.d.ts +4 -0
- package/dist/professional-male-b.d.ts.map +1 -0
- package/dist/professional-male-b.js +3 -0
- package/dist/professional-male-b.js.map +1 -0
- package/dist/react.d.ts +13 -0
- package/dist/react.d.ts.map +1 -0
- package/dist/react.js +13 -0
- package/dist/react.js.map +1 -0
- package/dist/supports.d.ts +20 -0
- package/dist/supports.d.ts.map +1 -0
- package/dist/supports.js +26 -0
- package/dist/supports.js.map +1 -0
- package/dist/three/assets.d.ts +24 -0
- package/dist/three/assets.d.ts.map +1 -0
- package/dist/three/assets.js +22 -0
- package/dist/three/assets.js.map +1 -0
- package/dist/three/budgets.d.ts +17 -0
- package/dist/three/budgets.d.ts.map +1 -0
- package/dist/three/budgets.js +21 -0
- package/dist/three/budgets.js.map +1 -0
- package/dist/three/holds.d.ts +33 -0
- package/dist/three/holds.d.ts.map +1 -0
- package/dist/three/holds.js +56 -0
- package/dist/three/holds.js.map +1 -0
- package/dist/three/internal.d.ts +52 -0
- package/dist/three/internal.d.ts.map +1 -0
- package/dist/three/internal.js +49 -0
- package/dist/three/internal.js.map +1 -0
- package/dist/three/motion-limits.json +116 -0
- package/dist/three/sequences.d.ts +71 -0
- package/dist/three/sequences.d.ts.map +1 -0
- package/dist/three/sequences.js +262 -0
- package/dist/three/sequences.js.map +1 -0
- package/dist/three/tanya.d.ts +35 -0
- package/dist/three/tanya.d.ts.map +1 -0
- package/dist/three/tanya.js +51 -0
- package/dist/three/tanya.js.map +1 -0
- package/dist/three/tara-rig.d.ts +403 -0
- package/dist/three/tara-rig.d.ts.map +1 -0
- package/dist/three/tara-rig.js +1500 -0
- package/dist/three/tara-rig.js.map +1 -0
- package/dist/three/tara.d.ts +35 -0
- package/dist/three/tara.d.ts.map +1 -0
- package/dist/three/tara.js +58 -0
- package/dist/three/tara.js.map +1 -0
- package/dist/three/tushar.d.ts +31 -0
- package/dist/three/tushar.d.ts.map +1 -0
- package/dist/three/tushar.js +47 -0
- package/dist/three/tushar.js.map +1 -0
- package/dist/types.d.ts +149 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +140 -0
- package/dist/types.js.map +1 -0
- package/dist/useAvatar.d.ts +39 -0
- package/dist/useAvatar.d.ts.map +1 -0
- package/dist/useAvatar.js +35 -0
- package/dist/useAvatar.js.map +1 -0
- package/dist/vikram.d.ts +7 -0
- package/dist/vikram.d.ts.map +1 -0
- package/dist/vikram.js +20 -0
- package/dist/vikram.js.map +1 -0
- package/package.json +110 -25
- package/src/avatar.d.ts +218 -120
- package/src/avatar.js +950 -213
- package/src/behavior.d.ts +42 -0
- package/src/behavior.js +114 -0
- package/src/camera.js +29 -0
- package/src/canvas/author/parts/eye.mjs +722 -0
- package/src/canvas/author/parts/hand.mjs +1156 -0
- package/src/canvas/author/parts/mouth.mjs +741 -0
- package/src/canvas/author/parts/nose.mjs +100 -0
- package/src/canvas/author/parts/skin-detail.mjs +67 -0
- package/src/canvas/author/path.mjs +283 -0
- package/src/canvas/author/rig.mjs +405 -0
- package/src/canvas/avatars/round/face.d.mts +3 -0
- package/src/canvas/avatars/round/face.mjs +1307 -0
- package/src/canvas/create-rig.d.ts +15 -0
- package/src/canvas/create-rig.js +100 -0
- package/src/canvas/data/img/professional-female-a-hair-back.webp +0 -0
- package/src/canvas/data/img/professional-female-a-hair-front.webp +0 -0
- package/src/canvas/data/img/professional-female-a-top-body.webp +0 -0
- package/src/canvas/data/img/professional-female-b-hair-back.webp +0 -0
- package/src/canvas/data/img/professional-female-b-hair-front.webp +0 -0
- package/src/canvas/data/img/professional-female-b-top-body.webp +0 -0
- package/src/canvas/data/img/professional-male-a-hair-back.webp +0 -0
- package/src/canvas/data/img/professional-male-a-hair-front.webp +0 -0
- package/src/canvas/data/img/professional-male-a-top-body.webp +0 -0
- package/src/canvas/data/img/professional-male-b-hair-back.webp +0 -0
- package/src/canvas/data/img/professional-male-b-hair-front.webp +0 -0
- package/src/canvas/data/img/professional-male-b-top-body.webp +0 -0
- package/src/canvas/data/img/round-m3-hair-back.webp +0 -0
- package/src/canvas/data/img/round-m3-hair-front.webp +0 -0
- package/src/canvas/data/img/round-m3-top-body.webp +0 -0
- package/src/canvas/data/img/round-w1-hair-back.webp +0 -0
- package/src/canvas/data/img/round-w1-hair-front.webp +0 -0
- package/src/canvas/data/img/round-w1-top-body.webp +0 -0
- package/src/canvas/data/interviewer-female.rig.json +1 -0
- package/src/canvas/data/interviewer-male.rig.json +1 -0
- package/src/canvas/data/professional-female-a.rig.json +1 -0
- package/src/canvas/data/professional-female-b.rig.json +1 -0
- package/src/canvas/data/professional-male-a.rig.json +1 -0
- package/src/canvas/data/professional-male-b.rig.json +1 -0
- package/src/canvas/src/live.js +508 -0
- package/src/canvas/src/render2d.js +218 -0
- package/src/canvas/src/rig.js +297 -0
- package/src/canvas/src/vocab.js +96 -0
- package/src/clips.js +18 -7
- package/src/conformance.js +119 -0
- package/src/emotions.js +8 -5
- package/src/face-core.js +27 -1
- package/src/face-myna.d.ts +7 -0
- package/src/face-myna.js +192 -131
- package/src/face-peep-control-plane.js +167 -0
- package/src/face-peep.d.ts +7 -0
- package/src/face-peep.js +274 -116
- package/src/face-wren.d.ts +7 -0
- package/src/face-wren.js +17 -19
- package/src/faces.d.ts +12 -0
- package/src/faces.js +53 -0
- package/src/gaze.js +434 -58
- package/src/hand.js +120 -91
- package/src/head.js +134 -0
- package/src/idle.js +227 -119
- package/src/interjections.js +200 -31
- package/src/params.js +8 -4
- package/src/perform.js +7 -9
- package/src/prosody.js +647 -0
- package/src/rig.d.ts +24 -0
- package/src/rig.js +32 -0
- package/src/speech-timing.js +23 -0
- package/src/visemes.js +110 -24
- package/client/dist/Avatar.d.ts +0 -27
- package/client/dist/Avatar.d.ts.map +0 -1
- package/client/dist/Avatar.js.map +0 -1
- package/client/dist/AvatarClient.d.ts +0 -139
- package/client/dist/AvatarClient.d.ts.map +0 -1
- package/client/dist/AvatarClient.js +0 -223
- package/client/dist/AvatarClient.js.map +0 -1
- package/client/dist/index.d.ts +0 -22
- package/client/dist/index.d.ts.map +0 -1
- package/client/dist/index.js +0 -22
- package/client/dist/index.js.map +0 -1
- package/client/dist/types.d.ts +0 -86
- package/client/dist/types.d.ts.map +0 -1
- package/client/dist/types.js +0 -31
- package/client/dist/types.js.map +0 -1
- package/client/dist/useAvatar.d.ts +0 -42
- package/client/dist/useAvatar.d.ts.map +0 -1
- package/client/dist/useAvatar.js +0 -51
- package/client/dist/useAvatar.js.map +0 -1
- package/client/src/Avatar.tsx +0 -33
- package/client/src/AvatarClient.ts +0 -270
- package/client/src/index.ts +0 -22
- package/client/src/types.ts +0 -104
- package/client/src/useAvatar.ts +0 -85
- package/docs/contract-avatar.md +0 -371
- package/docs/contract-protocol.md +0 -461
|
@@ -0,0 +1,1562 @@
|
|
|
1
|
+
|
|
2
|
+
/**
|
|
3
|
+
* tara's renderer: the `AvatarRig` contract (`apply(frame)` / `destroy()`)
|
|
4
|
+
* over the Blender-authored GLB.
|
|
5
|
+
*
|
|
6
|
+
* The whole file is one idea — **the pose channel is the interface, and every
|
|
7
|
+
* mapping here is a translation of one channel into the one control that
|
|
8
|
+
* renders it.** `scripts/morphs.py` authored the shape keys under the library's
|
|
9
|
+
* own channel names precisely so this file never has to interpret a viseme, a
|
|
10
|
+
* state or an emotion; it receives a fully mixed pose and moves geometry.
|
|
11
|
+
*
|
|
12
|
+
* Three kinds of control, in the order they appear below:
|
|
13
|
+
*
|
|
14
|
+
* morph targets the face itself — lips, jaw, lids, brows, and the mouth
|
|
15
|
+
* interior that has to choreograph with them
|
|
16
|
+
* head group `headYaw` / `headPitch` / `headRoll`, as a rotation of the
|
|
17
|
+
* parts that ride the skull about the jaw-angle pivot
|
|
18
|
+
* eye globes `pupilX` / `pupilY`, as a rotation of the eyeball, because
|
|
19
|
+
* the iris is painted onto a sphere and cannot slide
|
|
20
|
+
*
|
|
21
|
+
* and a fourth, for the body: `shoulderL/R` are morph targets on the torso
|
|
22
|
+
* shell like any face channel, and `breath`, `torsoLean` and `torsoTurn` are
|
|
23
|
+
* each one transform of a group — a swell, a scale, a sway (see `BODY`).
|
|
24
|
+
*
|
|
25
|
+
* An asset may add a fifth: expression maps, which change the face's *light*
|
|
26
|
+
* where a smile or a raised brow would, because moving the geometry cannot
|
|
27
|
+
* (see `expressive`).
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
import { REST } from "../internal.js";
|
|
31
|
+
import type { AvatarFrame, AvatarRig, RigPose } from "../internal.js";
|
|
32
|
+
import * as THREE from "three";
|
|
33
|
+
import { GLTFLoader } from "three/addons/loaders/GLTFLoader.js";
|
|
34
|
+
import { ASSETS } from "./assets.js";
|
|
35
|
+
import { HARD_BUDGET, pixelRatioFor } from "./budgets.js";
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The camera, from `scripts/render_frame.py`. It is orthographic and that is
|
|
39
|
+
* forced by the albedo rather than chosen: the atlas is a front-orthographic
|
|
40
|
+
* projection of the shell, so an atlas texel sits at face-space (u, v) whatever
|
|
41
|
+
* depth the triangle carrying it has. A perspective camera disagrees by
|
|
42
|
+
* (offset from the axis) x (depth / distance) — which put the hair rim 3 px
|
|
43
|
+
* above the hairline it is textured to and opened a black band across the
|
|
44
|
+
* forehead. `build_tara.setup_scene` has the measurement.
|
|
45
|
+
*/
|
|
46
|
+
const FRAME = { bottom: -0.52, top: 1.46 };
|
|
47
|
+
const FRAME_HEIGHT = FRAME.top - FRAME.bottom;
|
|
48
|
+
const FRAME_CENTRE = (FRAME.top + FRAME.bottom) / 2;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Head motion, in degrees at the channel's own clamp of ±1.4.
|
|
52
|
+
*
|
|
53
|
+
* These are the program's *working* envelope, mapped so that a channel pinned to
|
|
54
|
+
* its limit lands exactly on it. That is the point of scaling by the clamp
|
|
55
|
+
* rather than by 1: the mixer cannot ask for more than the envelope allows, and
|
|
56
|
+
* the hard ceiling stays unreachable by construction instead of by a second
|
|
57
|
+
* clamp nobody runs.
|
|
58
|
+
*
|
|
59
|
+
* **It opened on 2026-09-12, from yaw ±6°, pitch ±5°, roll ±3°.** That
|
|
60
|
+
* envelope made Busso's neutral-speech numbers unreachable: his mean
|
|
61
|
+
* per-sentence pitch *range* is 9.5°, which is 95 % of everything ±5° could
|
|
62
|
+
* ever produce, so speaking alone would have swung the channel corner to corner
|
|
63
|
+
* and a deliberate nod on top of it would have had nowhere left to go. What had
|
|
64
|
+
* held it there was the asset, not anatomy — the neck's follow was linear and
|
|
65
|
+
* drew a second jawline past a few degrees — and once that follow became exact
|
|
66
|
+
* (`NECK_QUAD` below) pitch could open to 24 and roll to 8.
|
|
67
|
+
*
|
|
68
|
+
* Yaw is still the smallest because it is the one axis this rig is genuinely
|
|
69
|
+
* constrained on: the albedo is a front-orthographic projection of a 0.34-deep
|
|
70
|
+
* shell, and a large turn is where that reads as a cardboard cutout rather than
|
|
71
|
+
* a head. **It opened from 9° to 15° on 2026-09-18.** The cutout was measured
|
|
72
|
+
* rather than assumed — a ladder rendered at 9/12/15/18/21/25/30 and read at
|
|
73
|
+
* crop on all three characters is clean to 21° on tara and to 18° on tanya and
|
|
74
|
+
* tushar. 9° was therefore set at half of where the artefact actually begins,
|
|
75
|
+
* and the stiffness the owner reported on tanya's turns was that margin, not
|
|
76
|
+
* her asset. 15° keeps 3° of headroom on the tightest of the three.
|
|
77
|
+
*
|
|
78
|
+
* This is not for speech: Busso wants ±1.15° of yaw in neutral conversation and
|
|
79
|
+
* always did. It is for a head that turns to *look* at something, which is what
|
|
80
|
+
* mocap drives and what pegged the channel — a real 25° turn still saturates at
|
|
81
|
+
* 15°, so this widens the envelope without making it generous.
|
|
82
|
+
*
|
|
83
|
+
* And it is not the angle a pose may be *held* at, which is a stricter question
|
|
84
|
+
* with its own measurement per character (`motion-limits.json`, applied through
|
|
85
|
+
* `holds.ts`): a turn that returns is forgiven what a sustained one is not. The
|
|
86
|
+
* two numbers differ by about 3x on yaw and neither is a correction of the
|
|
87
|
+
* other.
|
|
88
|
+
*
|
|
89
|
+
* **Editing these needs no rebuild, but it is not free.** The neck's fields
|
|
90
|
+
* carry no angle, so `tara.glb` cannot go stale against them. What a number
|
|
91
|
+
* here does move:
|
|
92
|
+
*
|
|
93
|
+
* - Every clip is authored in channel units, so a degree here re-sizes every
|
|
94
|
+
* clip driving that axis. `test/nods.test.ts` bands *pitch* only — `down`,
|
|
95
|
+
* `up`, `upFirst` — and computes `yawPP` without ever asserting it. A yaw
|
|
96
|
+
* change moves nothing there; a pitch change moves four tests.
|
|
97
|
+
* - `head_parallax.py` and `validate_morphs.py` quote their gates at this
|
|
98
|
+
* envelope. `validate_morphs` reads `morphs.head_envelope()`, but
|
|
99
|
+
* `head_parallax.POSES` hardcoded `yaw 9` until 2026-09-18 and would have
|
|
100
|
+
* gone on grading 9° while the rig shipped 15° — a gate defending a number
|
|
101
|
+
* nothing used. It derives both angles from here now.
|
|
102
|
+
* - The yaw twist's two fields are an expansion in the angle, so their error
|
|
103
|
+
* grows as θ²/6. Against the exact rotation at the maximum ramp
|
|
104
|
+
* (`NECK_TWIST` = 0.5) that is 0.31 % at 9°, 0.85 % at 15°, 1.23 % at 18°
|
|
105
|
+
* (`morphs.neck_twist`). The note here used to read as a wall at 9°; it is
|
|
106
|
+
* not one — 15° costs under a percent of a displacement that is itself a
|
|
107
|
+
* fraction of the neck's radius.
|
|
108
|
+
*
|
|
109
|
+
* TARA-SPECIFIC: each number is her reach before an artefact shows — yaw by
|
|
110
|
+
* the cutout, pitch by the neck fold that starts to crease at 24° chin-up.
|
|
111
|
+
* Both were measured on the shipping surface, one axis at a time. This is still
|
|
112
|
+
* one shared pair of
|
|
113
|
+
* constants for all three characters, which holds only because 15° is inside
|
|
114
|
+
* every one of them; the first character that wants more than its neighbours
|
|
115
|
+
* forces the envelope onto `TaraRigOptions` as a per-character fact. A second
|
|
116
|
+
* avatar measures its own with the audit.
|
|
117
|
+
*/
|
|
118
|
+
// Exported through `internal.ts` for the instruments that need to put a real
|
|
119
|
+
// angle *into* a channel, which is this scaling run backwards. The mocap
|
|
120
|
+
// instrument kept its own copy for want of that export and said in a comment
|
|
121
|
+
// that the copy would lie the day the envelope moved; it moved on 2026-09-18.
|
|
122
|
+
export const HEAD_CLAMP = 1.4;
|
|
123
|
+
export const HEAD_DEG = { yaw: 15, pitch: 24, roll: 8 };
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Where the head turns about, from `build_tara.PIVOT`, in glTF's Y-up frame:
|
|
127
|
+
* Blender (x, y, z) exports as (x, z, −y). v 0.36 is the jaw angle and the
|
|
128
|
+
* earlobe, and it sits a fifth of a face height *behind* the face plane —
|
|
129
|
+
* a pivot on the surface spins the face in place, where a real yaw swings the
|
|
130
|
+
* chin across as well as around, which is most of what makes a small turn read.
|
|
131
|
+
*/
|
|
132
|
+
const PIVOT = new THREE.Vector3(0.0, 0.36, -0.22);
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Where the head *tilts* about, from `morphs.ROLL_PIVOT`: the midline just
|
|
136
|
+
* above the chin. A roll is a bend of the whole neck, so its centre is far
|
|
137
|
+
* below the ear, and a drawn head sells it by holding the chin and swinging
|
|
138
|
+
* the crown — Live2D's sample rigs tilt about this same point. About PIVOT
|
|
139
|
+
* instead, the chin swung 7 px the other way at 8° and the head read as a
|
|
140
|
+
* pendulum hung from the ears.
|
|
141
|
+
*
|
|
142
|
+
* It sits inside the yaw and pitch, so a turned head still tilts about its own
|
|
143
|
+
* chin. TARA-SPECIFIC: see `morphs.ROLL_PIVOT` for what fixed the height and
|
|
144
|
+
* what a second avatar supplies.
|
|
145
|
+
*/
|
|
146
|
+
const ROLL_PIVOT = new THREE.Vector3(0.0, 0.05, -0.22);
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* What the head takes with it, from `build_tara.HEAD_PARTS`. `Body` stays
|
|
150
|
+
* behind, and so does `Neck` — but the neck is not *static*: it carries
|
|
151
|
+
* `headYaw` / `headPitch` / `headRoll` morph targets of its own, ramped from
|
|
152
|
+
* full under the jaw to nothing at the collar (`morphs.neck_targets`). Pitch and
|
|
153
|
+
* roll are the same rotation this group gets; yaw is a twist about the neck's
|
|
154
|
+
* own axis at half the angle, which keeps the neck's outline where it is
|
|
155
|
+
* (`morphs.neck_twist`). They need no code here at all, which is the
|
|
156
|
+
* whole reason they are morphs: they are named for pose channels that rest at
|
|
157
|
+
* 0, so the loop below drives them like any other channel and the influence law
|
|
158
|
+
* hands them the raw pose value.
|
|
159
|
+
*
|
|
160
|
+
* Without it a turn dragged the skull's jaw rim across a throat that had not
|
|
161
|
+
* moved, and the rim landed mid-neck as a second jawline — invisible at the
|
|
162
|
+
* 400 × 300 tile, obvious at a 3× crop.
|
|
163
|
+
*/
|
|
164
|
+
const HEAD_PARTS = ["Head", "Ears", "Hair", "Eye_L", "Eye_R", "Cavity",
|
|
165
|
+
"Teeth_Upper", "Teeth_Lower", "Tongue"];
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* The body, in face-space units (glTF y is face-space v) and degrees.
|
|
169
|
+
*
|
|
170
|
+
* Every mechanism is the SVG faces' (`face-core.poseTransforms`); the
|
|
171
|
+
* amplitudes are set from the anatomy, which on this face is ~8.8 px per
|
|
172
|
+
* centimetre at the 400 × 300 tile (crown to chin is 1.39 units of ~24 cm),
|
|
173
|
+
* and land at about peep's travel as a share of the same tile. The first cut
|
|
174
|
+
* was 0.6 of peep, on the theory that a photograph shows a millimetre a line
|
|
175
|
+
* drawing cannot. Measured in a 30-second listening hold it moved the
|
|
176
|
+
* shoulders 2 px, under 1 % of the tile, and read as a still with a tremor:
|
|
177
|
+
* the head was moving more than the body carrying it. Anatomy is the floor,
|
|
178
|
+
* not a fraction of a cartoon — these now put a listening hold at 4-5 px at
|
|
179
|
+
* the shoulders, still slow, and still well under the 1.5 Hz ceiling.
|
|
180
|
+
*
|
|
181
|
+
* breath A swell, not a slide (`docs/research-biomechanics.md` §6.1): the
|
|
182
|
+
* torso scales about a point 0.35 of a frame below the frame, as peep's
|
|
183
|
+
* does about its hem, so the shoulder line comes up 2.4 px at full
|
|
184
|
+
* inhale against a lower edge that moves two-thirds of that, and the
|
|
185
|
+
* chest widens 2 px a side. Quiet breathing changes chest
|
|
186
|
+
* circumference 2-3 %; 1.2 % wide is the calm end of that in linear
|
|
187
|
+
* scale, and the rise is a little more because in this crop — about
|
|
188
|
+
* 5 cm of chest below the collar — what a breath shows is the upper
|
|
189
|
+
* ribs and clavicles lifting as much as the rib cage widening. The
|
|
190
|
+
* neck and head ride the lift at the collar, derived rather than tuned
|
|
191
|
+
* (peep's `neckLift`), so the neck cannot telescope: ~2.4 px, the
|
|
192
|
+
* 2-3 mm a seated head really moves with a breath.
|
|
193
|
+
* lean `torsoLean` as a deformation of the trunk, on the shell itself
|
|
194
|
+
* (`morphs.torso_targets`) — hem pinned at the frame's lower edge, the
|
|
195
|
+
* shoulders spreading and tipping as they come nearer. Only the head's
|
|
196
|
+
* *ride* is here: above the collar the field is flat, so the neck and
|
|
197
|
+
* head take a pure translation of `leanRide` and nothing else. That is
|
|
198
|
+
* Live2D's measured behaviour rather than a simplification — body angle
|
|
199
|
+
* moves every head part by 1.00 ± 0.02 and adds no differential motion
|
|
200
|
+
* inside the head (`docs/research-torso-motion.md` § 8 item 4).
|
|
201
|
+
*
|
|
202
|
+
* It replaced a uniform `figure.scale.setScalar()`, which was peep's
|
|
203
|
+
* `LEAN_SCALE` carried onto photographic geometry and, with the
|
|
204
|
+
* orthographic camera outside the group it scaled, was arithmetically a
|
|
205
|
+
* zoom: fit the displacement as a linear map and its singular values
|
|
206
|
+
* came back equal to three decimals with no residual, at every lean the
|
|
207
|
+
* mixer produces. What it looked like was the owner's report — the
|
|
208
|
+
* shoulders swelling and dropping in half a second. The crown travelled
|
|
209
|
+
* 4.56× what the eyes did, which is a head being scaled, not carried.
|
|
210
|
+
* A headless audit of what the crown travels against the eyes is that
|
|
211
|
+
* measurement, and its gates are what this change had to turn green.
|
|
212
|
+
* sway `torsoTurn` as the seated body's inverted pendulum: the whole figure
|
|
213
|
+
* rolls about the hips, ~45 cm below the collar, so the trunk shifts
|
|
214
|
+
* sideways and tips by a fraction of a degree together. peep slides
|
|
215
|
+
* its torso under a head that stays; a photograph cannot, because a
|
|
216
|
+
* neck joins them, so here the head rides the trunk and is rolled back
|
|
217
|
+
* level by exactly the trunk's tilt — its roll is `headRoll`'s alone,
|
|
218
|
+
* as a person shifting their weight goes on holding their eyes level on
|
|
219
|
+
* the person they are listening to. One degree at full `torsoTurn`: a
|
|
220
|
+
* weight shift (0.16-0.42) moves the collar 1.1-2.9 px, and following
|
|
221
|
+
* a full head turn ~7 px — 1.7 cm at the collar, what a seated body
|
|
222
|
+
* shifting onto one hip really does.
|
|
223
|
+
*/
|
|
224
|
+
const BODY = {
|
|
225
|
+
swellPivot: FRAME.bottom - 0.35 * FRAME_HEIGHT,
|
|
226
|
+
rise: 0.016,
|
|
227
|
+
widen: 0.012,
|
|
228
|
+
/** `landmarks.SHOULDER.top`: where the neck meets the torso. */
|
|
229
|
+
collar: -0.24,
|
|
230
|
+
/**
|
|
231
|
+
* The head group's rigid drop at a full lean. `morphs.py` parses this and
|
|
232
|
+
* plateaus `torsoLean`'s field at it above the collar, so the body's
|
|
233
|
+
* deformation and the head's transform are one number and meet without a
|
|
234
|
+
* seam — the same arrangement `lift.position.y` already has with the breath.
|
|
235
|
+
*/
|
|
236
|
+
leanRide: 0.021,
|
|
237
|
+
hip: -2.9,
|
|
238
|
+
swayDeg: 1.0,
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Gaze, as iris travel in face heights at `pupilX/Y` = 1, converted to a globe
|
|
243
|
+
* rotation on load. Authored as travel, not as an angle, because travel is what
|
|
244
|
+
* anyone can measure off the render and the angle is an artefact of this
|
|
245
|
+
* build's globe radius (0.24, some 4x anatomical — the sphere is a curved
|
|
246
|
+
* backing for a painted iris, not an eyeball).
|
|
247
|
+
*
|
|
248
|
+
* 0.042 across is ~7 mm on a face 165 mm tall, the iris travel of a ~35°
|
|
249
|
+
* version, and 6.4 px at the 400 px tile; the first value (0.026, ~4 px) was
|
|
250
|
+
* under the threshold where a look to the screen edge reads as a look rather
|
|
251
|
+
* than a drift. Vertical is shorter because a person looking up or down
|
|
252
|
+
* carries most of it with the lids and the head.
|
|
253
|
+
*/
|
|
254
|
+
const GAZE_TRAVEL = { x: 0.042, y: 0.026 };
|
|
255
|
+
/** `head_mesh.GLOBE_R`. The arc a rotation moves the iris through is r·θ. */
|
|
256
|
+
const GLOBE_RADIUS = 0.24;
|
|
257
|
+
/** `face_texture.EYE_EXTENT`'s span: one eye tile covers 0.30 face heights. */
|
|
258
|
+
const EYE_TILE = 0.3;
|
|
259
|
+
|
|
260
|
+
/** Degrees per pose unit: the globe turns `pupil * GAZE_TRAVEL / GLOBE_RADIUS`
|
|
261
|
+
* radians, and the head reaches `HEAD_DEG` at the clamp.
|
|
262
|
+
*
|
|
263
|
+
* Exported through `internal.ts` for the same reason as the head envelope: an
|
|
264
|
+
* instrument that asks for "eyes on the camera through a head turn" is running
|
|
265
|
+
* this conversion backwards, and a second copy of it would be a second thing to
|
|
266
|
+
* update when the eye tile or the globe changes. */
|
|
267
|
+
export const EYE_DEG = { x: (GAZE_TRAVEL.x / GLOBE_RADIUS) * 180 / Math.PI, y: (GAZE_TRAVEL.y / GLOBE_RADIUS) * 180 / Math.PI };
|
|
268
|
+
const HEAD_UNIT_DEG = { x: HEAD_DEG.yaw / HEAD_CLAMP, y: HEAD_DEG.pitch / HEAD_CLAMP };
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* The mixer's per-rig calibration for tara, passed by `tara.ts` and the motion
|
|
272
|
+
* audit so both measure the same face. A pose unit is an angle here and a
|
|
273
|
+
* pixel count on an SVG face, so the speech layer's amplitudes are tuned per
|
|
274
|
+
* rig rather than in the library: `prosodyHeadGain` sizes speech-rhythm head
|
|
275
|
+
* motion against this face's own motion envelope.
|
|
276
|
+
*
|
|
277
|
+
* `oculomotor` is the eye-head system sized for this face (`gaze.js`). Her eye
|
|
278
|
+
* turns 10° a pupil unit and her head 6.4° of yaw a head unit, and the shared
|
|
279
|
+
* look table — drawn for a line face, whose pupils cross most of an eye — put
|
|
280
|
+
* every look in the eyes: a thinking look away was the iris parked in the
|
|
281
|
+
* corner of the socket for two thirds of the state, which is side-eye, not
|
|
282
|
+
* thought. Here the head carries about 60 % of a look and the eyes land a third
|
|
283
|
+
* of the way off centre, where a real eye-head shift leaves them (Freedman &
|
|
284
|
+
* Sparks; Pejsa & Andrist). The comment on each target is its world angle,
|
|
285
|
+
* x right and y down.
|
|
286
|
+
*
|
|
287
|
+
* vor Real gain in the light is close to 1. A little under leaves the
|
|
288
|
+
* head some say, so a nod carries the eyes a touch with it rather
|
|
289
|
+
* than pinning them to the lens. Vertically it is well under
|
|
290
|
+
* (2026-09-15): her pitch is a shell tipping on a photograph and
|
|
291
|
+
* reads as a fraction of what it is, so the eyes' full answer to
|
|
292
|
+
* it read as the eyes moving on their own — at 0.8, THINKING's
|
|
293
|
+
* up-look rolled the iris to the lid with white beneath it, and
|
|
294
|
+
* the reply that followed dropped it into a downcast look with the
|
|
295
|
+
* lid riding down, while the head was still coming back. At 0.35
|
|
296
|
+
* the eyes mostly go where the head takes them.
|
|
297
|
+
* range How far the reflex may carry the eye in the socket. Unlimited,
|
|
298
|
+
* an up-look's onset put the whole 8° in the eye before the neck
|
|
299
|
+
* moved — the iris pinned under the lid, white beneath it, which
|
|
300
|
+
* on this photograph reads as an eye-roll. Up 0.45 is 2.8° and
|
|
301
|
+
* down 0.5 is 3.1°; the head covers the rest, as it does once the
|
|
302
|
+
* eye nears its effective range. Both were looser (0.55, 0.85)
|
|
303
|
+
* and a gap between turns hit both ends of them every time.
|
|
304
|
+
* lidFollow Down 0.38 keeps the upper lid on the iris: the lid travels
|
|
305
|
+
* 0.068 face units per lid unit and the iris 0.026 per pupil unit.
|
|
306
|
+
* Up a little less, so an upward look opens the eye a hair instead
|
|
307
|
+
* of dragging the lid along with it.
|
|
308
|
+
* avert A conversational look away keeps under half its size in the
|
|
309
|
+
* eyes and gives the neck 0.6 of it — on this face the eyes alone
|
|
310
|
+
* could only make it a glance sideways.
|
|
311
|
+
* head The follow's launch and cruise, in head units. A real head
|
|
312
|
+
* reaches a 7-8° shift in about 0.4 s, its speed scaling with the
|
|
313
|
+
* size of the shift; the shared amble took a thinking look 1.2 s,
|
|
314
|
+
* and the reflex, doing its job, held the eyes in the corner of
|
|
315
|
+
* the socket the whole way.
|
|
316
|
+
*
|
|
317
|
+
* `trunkFollow` is 0.3, under the line faces' 0.45. Live2D's face-tracking
|
|
318
|
+
* sample gives the body a third of the head's yaw (BodyAngleX 10 against
|
|
319
|
+
* AngleX 30; docs/research-head-rotation.md § 5 item 6), and that is the
|
|
320
|
+
* benchmark to sit at, not above. Here it matters for a reason a line face
|
|
321
|
+
* does not have: the neck's outline holds under a twist by construction
|
|
322
|
+
* (`morphs.neck_twist`), so the trunk's sway is what is left moving it — a
|
|
323
|
+
* quarter to a third of it at the yaw peak in the recorded call, read as the
|
|
324
|
+
* neck sliding. TARA-SPECIFIC in its evidence only: a second Blender avatar
|
|
325
|
+
* starts from 0.3 and checks its own outline at crop.
|
|
326
|
+
*/
|
|
327
|
+
export const TARA_TUNING = {
|
|
328
|
+
prosodyHeadGain: 1.0, prosodyFaceGain: 1, saccadeGain: 2.4, aversionGain: 1.8,
|
|
329
|
+
trunkFollow: 0.3,
|
|
330
|
+
// **The speaking face's upper half, sized for a photograph.** A reviewer read
|
|
331
|
+
// her speech as delivered "with zero emotion or movement in her eyes or
|
|
332
|
+
// forehead", against the same reviewer's praise for `CANT_HEAR`. The two are
|
|
333
|
+
// the same channels at different values, and the gap is measurable in the
|
|
334
|
+
// expression maps: `CANT_HEAR` above is knit 0.14/0.45 plus inner 0.22/0.5,
|
|
335
|
+
// about 0.75 of map weight, where speech averaged 0.19.
|
|
336
|
+
//
|
|
337
|
+
// `floor` is the larger half of the answer. `browRaise` is split by sign into
|
|
338
|
+
// two maps and *both* read as nothing near zero, so the shared [-0.14, 0.18]
|
|
339
|
+
// range spends most phrases in a dead band recruiting no light at all. The
|
|
340
|
+
// band is skipped rather than the range widened, because what carries the
|
|
341
|
+
// read is the sign a phrase commits to, not how far it goes — and the ceiling
|
|
342
|
+
// stays under a beat's 0.34 for the reason `POSE.brow` gives.
|
|
343
|
+
//
|
|
344
|
+
// `forms` moves draws off the plain raise and onto the inner lift, which on
|
|
345
|
+
// this asset is the only brow shape besides the raise that recruits light at
|
|
346
|
+
// all (`EXPRESSION_WEIGHT`: there is no map for `browAngle`, and its 0.026 of
|
|
347
|
+
// travel is the smallest of the three). `prosody.js` makes the inner lift the
|
|
348
|
+
// rarest form deliberately, because a face that keeps lifting its inner brows
|
|
349
|
+
// reads as worried — that rule is written for the line faces, and the face it
|
|
350
|
+
// is being relaxed for holds `browInner` at 0.22 for the whole of `CANT_HEAR`
|
|
351
|
+
// and was praised for it. Still a transient on a 0.55 s envelope, never a
|
|
352
|
+
// held shape: the prohibition is on the hold, not the event.
|
|
353
|
+
//
|
|
354
|
+
// TARA-SPECIFIC, and fork debt: on a driver of her own these are three
|
|
355
|
+
// constants beside the research comment, not an option on a shared mixer.
|
|
356
|
+
brows: {
|
|
357
|
+
range: [-0.28, 0.24],
|
|
358
|
+
floor: 0.11,
|
|
359
|
+
forms: [
|
|
360
|
+
{ p: 0.38, inner: 0.00, angle: 0.00 },
|
|
361
|
+
{ p: 0.34, inner: 0.26, angle: 0.00 },
|
|
362
|
+
{ p: 0.28, inner: 0.00, angle: 0.30 },
|
|
363
|
+
],
|
|
364
|
+
},
|
|
365
|
+
states: {
|
|
366
|
+
// The shared WORKING pose without its AU4 brows. On peep, brows-down is
|
|
367
|
+
// what makes reading read as effort rather than a blank face; on a
|
|
368
|
+
// photograph, over lids that are already following the eyes down, it
|
|
369
|
+
// closes them to a squint the owner read as straining at the screen, not
|
|
370
|
+
// working. Her reading scan carries the state instead: eyes off the user,
|
|
371
|
+
// stepping along a line, the way a person at their own display looks.
|
|
372
|
+
// TARA-SPECIFIC: a photographic face with a deeper lid crease may want
|
|
373
|
+
// some of the knit back; judge it at crop against LISTENING.
|
|
374
|
+
WORKING: {
|
|
375
|
+
pose: { headPitch: 0.04, lidL: -0.08, lidR: -0.08, shoulderL: 0.06, shoulderR: 0.06 },
|
|
376
|
+
},
|
|
377
|
+
// Straining to hear, without the squint. Its AU7 lifts her lower lid and
|
|
378
|
+
// pushes the cheek up under it: at 0.75 the eyes closed to slits over a
|
|
379
|
+
// dark band, and under brows at -0.45, with the eyes countered into the
|
|
380
|
+
// corner of the socket (`USER_EAR` below), she read as giving the user a
|
|
381
|
+
// suspicious side-eye. The lean in and the ear offered carry the state,
|
|
382
|
+
// which the shared comment already says of them. The brows keep a small
|
|
383
|
+
// knit with the inner ends up: effort that is also asking. The mouth is
|
|
384
|
+
// pressed at a photograph's scale; the shared -0.22 corners clear peep's
|
|
385
|
+
// drawn smile, and hers rests neutral. TARA-SPECIFIC: the squint morph's
|
|
386
|
+
// cheek push is what darkens, so a face built without one might keep a
|
|
387
|
+
// little squint — check the band under the eye at crop.
|
|
388
|
+
//
|
|
389
|
+
// The lean is attentive-sized, not the shared 0.70. Until 2026-09-16 her
|
|
390
|
+
// lean scaled the whole figure about mid-face (`BODY`), so 0.70 plus the
|
|
391
|
+
// engage add was a 4.4% zoom arriving on torsoLean's 0.24 s tau: the
|
|
392
|
+
// shoulders swelled and dropped in half a second, read by the owner as a
|
|
393
|
+
// lurch nothing like a lean. 0.22 sits in the research's sustained band
|
|
394
|
+
// (+0.15–0.25, research-biomechanics.md §6.3) and the ear and chin carry
|
|
395
|
+
// the rest.
|
|
396
|
+
//
|
|
397
|
+
// The lean deforms the trunk now, which is exactly the condition the old
|
|
398
|
+
// note here predicted might afford more. It is left at 0.22 on purpose: the
|
|
399
|
+
// cut was made by eye at crop, and putting it back is the same kind of
|
|
400
|
+
// judgement rather than a consequence of the field changing. TARA-SPECIFIC,
|
|
401
|
+
// and the thing to re-judge first if she reads as under-committed.
|
|
402
|
+
CANT_HEAR: {
|
|
403
|
+
pose: {
|
|
404
|
+
torsoLean: 0.22, headPitch: 0.10,
|
|
405
|
+
browRaiseL: -0.14, browRaiseR: -0.14, browInnerL: 0.22, browInnerR: 0.18,
|
|
406
|
+
mouthPress: 0.40, mouthCornerL: -0.10, mouthCornerR: -0.10,
|
|
407
|
+
},
|
|
408
|
+
},
|
|
409
|
+
// Hunting for a control, without the squint: the same lower lid and cheek
|
|
410
|
+
// at 0.40 left the eyes half-lidded from below over a dark band. The hunt
|
|
411
|
+
// is the wander and the flick; the face only has to be not smiling, and
|
|
412
|
+
// on a mouth that rests neutral that is a small press, not peep's -0.25
|
|
413
|
+
// corners. TARA-SPECIFIC, the same way as CANT_HEAR.
|
|
414
|
+
SEARCHING_SCREEN: {
|
|
415
|
+
pose: { mouthPress: 0.40, mouthCornerL: -0.08, mouthCornerR: -0.08,
|
|
416
|
+
browRaiseL: -0.10, browRaiseR: -0.06 },
|
|
417
|
+
},
|
|
418
|
+
},
|
|
419
|
+
oculomotor: {
|
|
420
|
+
angles: { eye: EYE_DEG, head: HEAD_UNIT_DEG },
|
|
421
|
+
vor: { x: 0.8, y: 0.35 },
|
|
422
|
+
range: { x: 0.8, up: 0.45, down: 0.5 },
|
|
423
|
+
lidFollow: { down: 0.38, up: 0.30 },
|
|
424
|
+
avert: { eye: 0.45, head: 0.6 },
|
|
425
|
+
head: { accel: 16, speed: 3.5 },
|
|
426
|
+
targets: {
|
|
427
|
+
// 2.0° right, 2.5° down: her own display, read level and a little to
|
|
428
|
+
// the side (WORKING, and OFFLINE's wait). The upper lid follows the eye
|
|
429
|
+
// down (`lidFollow`), and a photographed eye 6.8° down — where this
|
|
430
|
+
// sat — hooded to a lid of 0.25-0.43 against listening's 0.14, read as
|
|
431
|
+
// a squint rather than reading. Here it holds 0.12-0.18 through the
|
|
432
|
+
// scan, and being off the user to the side is what says "busy".
|
|
433
|
+
// TARA-SPECIFIC: the depth that hoods is her lid crease's.
|
|
434
|
+
OWN_SCREEN: { px: 0.14, py: 0.24, hx: 0.10, hy: 0.06 },
|
|
435
|
+
// 2.6° of head turn toward the user's side and 3.1° of roll, the eyes
|
|
436
|
+
// countered 2.0° back onto them: the ear offered, contact held from
|
|
437
|
+
// inside the socket. The shared 0.42 counter held her iris against the
|
|
438
|
+
// corner of the socket with white on one side, which on a photograph is
|
|
439
|
+
// side-eye; the roll is the cue that says "ear", so it keeps its size.
|
|
440
|
+
USER_EAR: { px: -0.20, py: 0.05, hx: 0.40, hy: 0.02, roll: 0.55 },
|
|
441
|
+
// 2.8° up, nearly all of it the head — the same total the shared target
|
|
442
|
+
// gives on her, redistributed. The shared split puts 1.0° on the eyes,
|
|
443
|
+
// and the middle of a screen is the one place a hunt keeps returning to,
|
|
444
|
+
// so it is the worst place to sit with the iris off-centre.
|
|
445
|
+
SCREEN_CENTER: { px: 0.00, py: -0.06, hx: 0.00, hy: -0.14 },
|
|
446
|
+
// 9.3° to the side, 1.5° up, the head carrying over half. The shared
|
|
447
|
+
// ones put the eyes at the edge of her socket (0.78 of 0.8) — white on
|
|
448
|
+
// one side again, and a hunt that reads as shifty. SEARCHING_SCREEN,
|
|
449
|
+
// REVIEWING_SCREEN and DISTRACTED look here.
|
|
450
|
+
SCREEN_LEFT: { px: -0.42, py: -0.10, hx: -0.80, hy: -0.05 },
|
|
451
|
+
SCREEN_RIGHT: { px: 0.42, py: -0.10, hx: 0.80, hy: -0.05 },
|
|
452
|
+
// 6.4° up, three quarters of it the head. The shared target is 9.6° on
|
|
453
|
+
// her — higher than her own AWAY_THINKING, so it read as looking over
|
|
454
|
+
// the monitor rather than at the top of it, and it made the two longest
|
|
455
|
+
// hops in SEARCHING_SCREEN's set (14.6° from SCREEN_WORK, 12.4° from
|
|
456
|
+
// either side) on a face whose looks are all scaled to a head-and-
|
|
457
|
+
// shoulders crop.
|
|
458
|
+
SCREEN_TOP: { px: 0.00, py: -0.26, hx: 0.00, hy: -0.28 },
|
|
459
|
+
// 7.0° left, 3.2° down, for the same reason, split evenly.
|
|
460
|
+
SCREEN_WORK: { px: -0.35, py: 0.18, hx: -0.55, hy: 0.12 },
|
|
461
|
+
// 7.8° left, 8.1° up.
|
|
462
|
+
AWAY_THINKING: { px: -0.30, py: -0.40, hx: -0.75, hy: -0.33, roll: 0.08 },
|
|
463
|
+
// 7.6° right, 7.4° up.
|
|
464
|
+
AWAY_RIGHT: { px: 0.30, py: -0.36, hx: 0.72, hy: -0.30, roll: -0.06 },
|
|
465
|
+
// 6.8° left, 5.5° down: shallower than the up-looks, since down on a
|
|
466
|
+
// face this real is the one read as downcast.
|
|
467
|
+
AWAY_DOWN: { px: -0.28, py: 0.45, hx: -0.62, hy: 0.16, roll: 0.04 },
|
|
468
|
+
// 7.8° right, level.
|
|
469
|
+
AWAY_SIDE: { px: 0.32, py: 0.08, hx: 0.72, hy: 0.02, roll: -0.03 },
|
|
470
|
+
},
|
|
471
|
+
},
|
|
472
|
+
} as const;
|
|
473
|
+
|
|
474
|
+
/**
|
|
475
|
+
* The eye material, taught to hold its socket still while the globe turns.
|
|
476
|
+
*
|
|
477
|
+
* The eye atlas is two tiles (`face_texture.write_eye_atlas`): the globe, and
|
|
478
|
+
* a linear multiplier holding what the lids and the socket put on the eye —
|
|
479
|
+
* the shadow under the upper lid, the caruncle, the shading into each corner.
|
|
480
|
+
* A real eye turns under all of that, and when it was baked into the globe a
|
|
481
|
+
* sideways look swung the caruncle into the middle of the white. So the
|
|
482
|
+
* globe's texel is read where the surface point *was* and the socket's where
|
|
483
|
+
* it *is*: the planar UV of the rotated position, which is the unrotated UV
|
|
484
|
+
* plus the rotation's displacement through the UV's own gradient. One extra
|
|
485
|
+
* texture read on two small meshes; no pass, no draw call.
|
|
486
|
+
*/
|
|
487
|
+
function socketed(base: THREE.MeshStandardMaterial, side: number,
|
|
488
|
+
gaze: { value: THREE.Matrix3 }, deep: boolean) {
|
|
489
|
+
const material = base.clone();
|
|
490
|
+
// d(u)/dx and d(v)/dy of `head_mesh.eye_uvs`: the globe is half the atlas
|
|
491
|
+
// wide, the right eye reads it mirrored, and glTF flips v.
|
|
492
|
+
const socket = { value: new THREE.Vector2((0.5 * -side) / EYE_TILE, -1 / EYE_TILE) };
|
|
493
|
+
material.onBeforeCompile = (shader) => {
|
|
494
|
+
shader.uniforms.uGaze = gaze;
|
|
495
|
+
shader.uniforms.uSocket = socket;
|
|
496
|
+
// The globe's hidden skirt turns at the frame's motion depth like the skin
|
|
497
|
+
// that hides it; its visible cap carries a field of exactly zero, so the
|
|
498
|
+
// socket, the iris and gaze below are unaffected by this. It turns at the
|
|
499
|
+
// *frame's* rotation and not its own, which is what `undoGaze` reads back
|
|
500
|
+
// out of `uGaze` — hence this sitting after that uniform is bound.
|
|
501
|
+
if (deep) turnDeep(shader, true);
|
|
502
|
+
shader.vertexShader = shader.vertexShader
|
|
503
|
+
.replace("#include <uv_pars_vertex>",
|
|
504
|
+
"#include <uv_pars_vertex>\nuniform mat3 uGaze;\nuniform vec2 uSocket;\nvarying vec2 vSocketUv;")
|
|
505
|
+
.replace("#include <uv_vertex>",
|
|
506
|
+
"#include <uv_vertex>\nvSocketUv = vMapUv + vec2(0.5, 0.0)"
|
|
507
|
+
+ " + uSocket * ((uGaze * position).xy - position.xy);");
|
|
508
|
+
shader.fragmentShader = shader.fragmentShader
|
|
509
|
+
.replace("#include <uv_pars_fragment>", "#include <uv_pars_fragment>\nvarying vec2 vSocketUv;")
|
|
510
|
+
.replace("#include <map_fragment>",
|
|
511
|
+
"vec3 socket = 2.0 * texture2D( map, vSocketUv ).rgb;\n#include <map_fragment>\ndiffuseColor.rgb *= socket;")
|
|
512
|
+
.replace("#include <emissivemap_fragment>",
|
|
513
|
+
"#include <emissivemap_fragment>\ntotalEmissiveRadiance *= socket;");
|
|
514
|
+
};
|
|
515
|
+
material.customProgramCacheKey = () => (deep ? "tara-eye-socket-deep" : "tara-eye-socket");
|
|
516
|
+
return material;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
/** `cavityShade`'s two measured heights, in the cavity patch's own normalised
|
|
520
|
+
* rest height — 0 at the bottom of its bounding box, 1 at the top, morph deltas
|
|
521
|
+
* included, because `computeBoundingBox` counts them and the shader normalises
|
|
522
|
+
* by that same call.
|
|
523
|
+
*
|
|
524
|
+
* Both were read off a ruler build that painted the normalised height into the
|
|
525
|
+
* emissive term, where the output bypasses the lamps and decodes straight back
|
|
526
|
+
* to the height that produced it. It found two things worth keeping.
|
|
527
|
+
*
|
|
528
|
+
* `seam` is the height a *closed* mouth shows: pixel-weighted 0.805 on tara and
|
|
529
|
+
* 0.808 on tushar, near enough identical to be one constant rather than a
|
|
530
|
+
* per-character tuning. It is deliberately the pivot — see `cavityShade`.
|
|
531
|
+
*
|
|
532
|
+
* `falloff` is sized against the band an *open* mouth exposes, and that band is
|
|
533
|
+
* why this is measured rather than guessed: it is 0.70..0.89, the top fifth of
|
|
534
|
+
* the patch, with nothing below it even at jaw 1 and mouthOpen 1 — the patch
|
|
535
|
+
* runs far past the aperture on purpose, so that its lower edge can chase the
|
|
536
|
+
* lip without ever reaching the chin (`build_tara`, the cavity's lower edge). A
|
|
537
|
+
* ramp laid across the whole patch would put a fifth of its range in the only
|
|
538
|
+
* part anyone sees, which is the mistake the lower arch's `floor` made one
|
|
539
|
+
* commit ago by being sized against its tile instead of its visible band.
|
|
540
|
+
*
|
|
541
|
+
* At 8 the multiplier runs 0.51 at the top of that band to 2.32 at the bottom.
|
|
542
|
+
* Neither clamp engages anywhere the aperture reaches; they are there so that a
|
|
543
|
+
* future morph exposing more of the patch cannot blow the exponential up.
|
|
544
|
+
*/
|
|
545
|
+
const CAVITY_SHADE = { seam: 0.805, falloff: 8, min: 0.35, max: 2.6 };
|
|
546
|
+
|
|
547
|
+
/**
|
|
548
|
+
* The inside of the mouth, shaded by how far the light has to reach into it.
|
|
549
|
+
*
|
|
550
|
+
* Both video reviewers called the open mouth "a dark void", and a luminance
|
|
551
|
+
* profile says why in one number: through viseme D's aperture the cavity
|
|
552
|
+
* renders ten consecutive rows inside a single level of 255 — 49.6 down to 48.7
|
|
553
|
+
* on tara, 49.3 to 48.6 on tushar — between an upper arch at 232 and a lower
|
|
554
|
+
* one at 163. Every other band in that column moves tens of levels per row. The
|
|
555
|
+
* complaint is not that the interior is too dark, then. It is that it is the
|
|
556
|
+
* one surface on this face with no variation in it at all, and a
|
|
557
|
+
* constant-valued region reads as a hole cut in the head rather than as a space
|
|
558
|
+
* behind it.
|
|
559
|
+
*
|
|
560
|
+
* It is flat because it is the only mouth surface that is genuinely *lit*.
|
|
561
|
+
* `build_tara.flat_material` gives it no emissive term, where the teeth beside
|
|
562
|
+
* it carry `emissiveFactor` 0.75 and their photograph's own light with it — so
|
|
563
|
+
* all of the cavity comes from the lamps, and those are 0.62π of ambient
|
|
564
|
+
* against a patch whose normal barely turns. Ambient on a constant normal is a
|
|
565
|
+
* constant.
|
|
566
|
+
*
|
|
567
|
+
* This is authored rather than sampled, which is the wrong way round for this
|
|
568
|
+
* repo and worth saying why: the reference is a *smile*, and a smile shows no
|
|
569
|
+
* interior — the same fact that left the lower arch with no enamel to copy.
|
|
570
|
+
* There is no photograph of this mouth's inside to project, so the choice is an
|
|
571
|
+
* authored gradient or the flat colour, and it is kept modest for it.
|
|
572
|
+
*
|
|
573
|
+
* Authored rather than derived, too. The true form factor from a flat backdrop
|
|
574
|
+
* to the aperture in front of it is *brightest at the centre*, which is exactly
|
|
575
|
+
* backwards: a real mouth is darkest in the middle because it is a tunnel
|
|
576
|
+
* there, and this one is a curtain — `build_tara` parks it in front of the
|
|
577
|
+
* teeth so a closed mouth has something dark to show, and walks it back past
|
|
578
|
+
* them as the jaw drops. So this shades the mouth it stands for, not the
|
|
579
|
+
* geometry it is drawn on.
|
|
580
|
+
*
|
|
581
|
+
* The pivot is what makes that safe. `CAVITY_SHADE.seam` is the height the
|
|
582
|
+
* closed mouth shows, so the multiplier is 1.0 there by construction and the
|
|
583
|
+
* rest pose barely moves: measured over the whole mouth region, at most 4
|
|
584
|
+
* levels of 255 on tara and 3 on tushar. Not nothing, and not worth claiming as
|
|
585
|
+
* nothing — but it matters that it is small, because "a closed mouth is a dark
|
|
586
|
+
* line" is this surface's first job, and `landmarks.PALETTE.cavity` is the
|
|
587
|
+
* colour of that line and stays the authority on it.
|
|
588
|
+
*
|
|
589
|
+
* Opening the mouth reveals the rest: darker above the seam, lighter below it.
|
|
590
|
+
* Where those two halves actually land is not symmetric and not the same on the
|
|
591
|
+
* two characters, because what hides the cavity is the upper arch, and the
|
|
592
|
+
* arches differ. Down the middle of tara's mouth the arch reaches to roughly
|
|
593
|
+
* the seam, so the centre gets the lighter half nearly alone — the ten flat
|
|
594
|
+
* rows above become 50 at the top of the aperture rising to 83 at its bottom,
|
|
595
|
+
* which is the floor the aperture faces. The darker half surfaces instead in
|
|
596
|
+
* two lobes flanking the arch, where the aperture runs wider than the teeth do
|
|
597
|
+
* and so exposes cavity above the seam: −4 levels on tara, −5 on tushar. Those
|
|
598
|
+
* lobes are the commissures, and their being the deepest part of the mouth is
|
|
599
|
+
* right for a reason this shader did not plan — it falls out of a vertical ramp
|
|
600
|
+
* meeting a curved arch.
|
|
601
|
+
*
|
|
602
|
+
* tushar gets the darker half down the centre as well, 49.3 to 43.6, because
|
|
603
|
+
* his teeth are narrower — `TEETH.half_width` 0.110 against tara's 0.132 — so
|
|
604
|
+
* his aperture exposes cavity above the seam in the middle too. One constant,
|
|
605
|
+
* two characters, two different-looking mouths, and the difference between them
|
|
606
|
+
* is the arch's width showing through. That is the argument for the constant
|
|
607
|
+
* staying shared rather than being tuned per character: it is already reading a
|
|
608
|
+
* per-character fact, just not one of its own.
|
|
609
|
+
*/
|
|
610
|
+
function cavityShade(base: THREE.MeshStandardMaterial, lo: number, hi: number) {
|
|
611
|
+
const material = base.clone();
|
|
612
|
+
const span = { value: new THREE.Vector2(lo, (hi - lo) || 1) };
|
|
613
|
+
const shade = {
|
|
614
|
+
value: new THREE.Vector4(CAVITY_SHADE.seam, CAVITY_SHADE.falloff,
|
|
615
|
+
CAVITY_SHADE.min, CAVITY_SHADE.max),
|
|
616
|
+
};
|
|
617
|
+
material.onBeforeCompile = (shader) => {
|
|
618
|
+
shader.uniforms.uCavitySpan = span;
|
|
619
|
+
shader.uniforms.uCavityShade = shade;
|
|
620
|
+
shader.vertexShader = shader.vertexShader
|
|
621
|
+
.replace("#include <common>",
|
|
622
|
+
"#include <common>\nvarying float vCavityAt;\nuniform vec2 uCavitySpan;")
|
|
623
|
+
// `position`, not `transformed`: `morphs.cavity_targets` translates this
|
|
624
|
+
// patch back and drops its lower edge as the jaw opens, and this shading
|
|
625
|
+
// is painted *on* the surface — so it has to ride that, not be swept
|
|
626
|
+
// across it. The raw attribute is the rest frame, before the morphs.
|
|
627
|
+
.replace("#include <begin_vertex>",
|
|
628
|
+
"#include <begin_vertex>\nvCavityAt = (position.y - uCavitySpan.x) / uCavitySpan.y;");
|
|
629
|
+
shader.fragmentShader = shader.fragmentShader
|
|
630
|
+
.replace("#include <common>",
|
|
631
|
+
"#include <common>\nvarying float vCavityAt;\nuniform vec4 uCavityShade;")
|
|
632
|
+
// No `emissivemap_fragment` half, unlike `jawShadow` and `socketed`:
|
|
633
|
+
// those modulate surfaces that emit 75% of a photograph verbatim, and
|
|
634
|
+
// this one has no emissive term at all. The diffuse is the whole output.
|
|
635
|
+
.replace("#include <map_fragment>",
|
|
636
|
+
"#include <map_fragment>\n"
|
|
637
|
+
+ "float cavityK = exp(uCavityShade.y * (uCavityShade.x - vCavityAt));\n"
|
|
638
|
+
+ "diffuseColor.rgb *= clamp(cavityK, uCavityShade.z, uCavityShade.w);");
|
|
639
|
+
};
|
|
640
|
+
material.customProgramCacheKey = () => "tara-cavity-shade";
|
|
641
|
+
return material;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/** `scripts/head_mesh.MOTION_DEPTH_ATTR`, as GLTFLoader names it: lowercased. */
|
|
645
|
+
const MOTION_DEPTH = "_motion_depth";
|
|
646
|
+
|
|
647
|
+
/**
|
|
648
|
+
* The head's frame, taught to turn as if it sat deeper than it does.
|
|
649
|
+
*
|
|
650
|
+
* Rotated at their real depth, the ears, crown, side hair and outline move
|
|
651
|
+
* nearly as far as the nose, and a nod reads as the whole head dropping
|
|
652
|
+
* (docs/research-head-rotation.md § 1). A real head's frame sits near the axis
|
|
653
|
+
* and barely moves, and that differential is what reads as rotation. Each
|
|
654
|
+
* vertex of the skin, hair and ears carries a Δz toward the viewer
|
|
655
|
+
* (`head_mesh.motion_depth`, ≤ 0 and zero across the features), and this moves
|
|
656
|
+
* it on screen by the rotated Δz: the view-space xy of modelView · (0, 0, Δz).
|
|
657
|
+
* Depth, draw order and lighting keep the real position. At rest that xy is
|
|
658
|
+
* exactly zero through the orthographic camera, so the drawing is untouched.
|
|
659
|
+
*
|
|
660
|
+
* The attribute decides, not the mesh name: the neck shares the skin material
|
|
661
|
+
* and has no field, which is why each head shell gets a clone. The same clone
|
|
662
|
+
* wears the expression maps, when the asset has them: the three shells are
|
|
663
|
+
* exactly the ones textured from the face atlas the maps are registered to.
|
|
664
|
+
*/
|
|
665
|
+
/**
|
|
666
|
+
* The vertex half of `motionDepth`, which the eye's socket shader needs too.
|
|
667
|
+
*
|
|
668
|
+
* It is a free function rather than inheritance because `Material.copy` does not
|
|
669
|
+
* carry `onBeforeCompile`: cloning a motion-depth material and giving the clone
|
|
670
|
+
* a second injection drops the first one silently, with no error and a rest pose
|
|
671
|
+
* that looks right. The two are composed by hand instead.
|
|
672
|
+
*/
|
|
673
|
+
function turnDeep(shader: { vertexShader: string }, undoGaze = false) {
|
|
674
|
+
// `undoGaze` is for the globes, and without it the cure draws a worse defect
|
|
675
|
+
// than the one it removes. A globe is *rotated* for gaze, so its own
|
|
676
|
+
// `modelViewMatrix` carries that rotation: building the offset from it tilts
|
|
677
|
+
// (0, 0, Δz) into the screen plane and slides the hidden skirt out past the
|
|
678
|
+
// temple on a look alone, head square on, where the skin it hides behind has
|
|
679
|
+
// not moved at all. The frame's rotation is the one the skirt must follow, and
|
|
680
|
+
// `uGaze` is exactly the extra rotation to take back out. A rotation's inverse
|
|
681
|
+
// is its transpose, and a vector multiplied from the left is the transpose
|
|
682
|
+
// multiply, so this needs no second uniform and no `transpose()`.
|
|
683
|
+
const depth = `vec3(0.0, 0.0, ${MOTION_DEPTH})`;
|
|
684
|
+
shader.vertexShader = shader.vertexShader
|
|
685
|
+
.replace("#include <common>", `#include <common>\nattribute float ${MOTION_DEPTH};`)
|
|
686
|
+
.replace("#include <project_vertex>",
|
|
687
|
+
"#include <project_vertex>\n"
|
|
688
|
+
+ `mvPosition.xy += (modelViewMatrix * vec4(${undoGaze ? `${depth} * uGaze` : depth}, 0.0)).xy;\n`
|
|
689
|
+
+ "gl_Position = projectionMatrix * mvPosition;");
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
function motionDepth(base: THREE.MeshStandardMaterial, expression?: Expression) {
|
|
693
|
+
const material = base.clone();
|
|
694
|
+
material.onBeforeCompile = (shader) => {
|
|
695
|
+
turnDeep(shader);
|
|
696
|
+
if (!expression) return;
|
|
697
|
+
const n = expression.names.length;
|
|
698
|
+
Object.assign(shader.uniforms, expression.uniforms);
|
|
699
|
+
shader.fragmentShader = shader.fragmentShader
|
|
700
|
+
.replace("#include <common>", `#include <common>\n${EXPRESSION_UNIFORMS(n)}`)
|
|
701
|
+
.replace("#include <map_fragment>", `#include <map_fragment>\n${EXPRESSION_FRAGMENT(n)}`)
|
|
702
|
+
.replace("#include <emissivemap_fragment>",
|
|
703
|
+
"#include <emissivemap_fragment>\ntotalEmissiveRadiance *= expression;");
|
|
704
|
+
};
|
|
705
|
+
material.customProgramCacheKey = () =>
|
|
706
|
+
(expression ? `tara-motion-depth-expression-${expression.names.length}` : "tara-motion-depth");
|
|
707
|
+
return material;
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
/**
|
|
711
|
+
* Expression maps: the light a smile or a raised brow changes, which the
|
|
712
|
+
* geometry cannot.
|
|
713
|
+
*
|
|
714
|
+
* A pixel of this face is 75% photograph, emitted, and 25% lit (the lamps,
|
|
715
|
+
* below), so the most a morph can change by moving skin is a quarter of its
|
|
716
|
+
* shading — and the shading it moves is the neutral photograph's, which has no
|
|
717
|
+
* nasolabial fold to deepen and no forehead line to show. A cheek morph
|
|
718
|
+
* measured 0.00% of the tile changed. So an asset can carry, per expression, a
|
|
719
|
+
* grey ratio taken from a photograph of the same face making it
|
|
720
|
+
* (`scripts/expression_maps.py`): the fold's shadow, the cheek's lift into the
|
|
721
|
+
* light, the lines of a raised brow, the two creases of a knit one. Here each
|
|
722
|
+
* is raised to the power of its weight and multiplies the albedo's diffuse and
|
|
723
|
+
* emitted halves alike, as the jaw's shadow does — so at weight 0 it is exactly
|
|
724
|
+
* 1 and the face is the photograph, and at 1 it is the expression's light.
|
|
725
|
+
*
|
|
726
|
+
* Read at the texel's *rest* position, which is where the build registered it:
|
|
727
|
+
* the morph that lifts the cheek carries the lifted cheek's light up with its
|
|
728
|
+
* texture, so light and shape arrive together without the shader knowing
|
|
729
|
+
* where anything went.
|
|
730
|
+
*
|
|
731
|
+
* Per side, because the channels are. The weights cross over at the midline
|
|
732
|
+
* rather than switching there, so a one-sided smile does not cut its fold's
|
|
733
|
+
* light off in a line down the philtrum.
|
|
734
|
+
*
|
|
735
|
+
* The maps are the one thing in the asset nothing draws: they ride on a
|
|
736
|
+
* carrier mesh (`build_tara.py`, "Expression") that exists to get the texture
|
|
737
|
+
* into the GLB, and is taken out of the scene on load. An asset without one is
|
|
738
|
+
* a face whose light never changes, which is every asset built before these.
|
|
739
|
+
*/
|
|
740
|
+
const EXPRESSION_SPLIT = 0.03;
|
|
741
|
+
/**
|
|
742
|
+
* Each map's weight, from one side's channels, as a fraction of the channel
|
|
743
|
+
* value at which it is at its photograph's full strength. The smile's is the
|
|
744
|
+
* mixer's broad smile (0.48-0.58 in the happy states) with room above it; a
|
|
745
|
+
* raise at the mixer's highest (0.42) gets most of the lines; a knit at
|
|
746
|
+
* `CANT_HEAR`'s −0.14 gets a third of the crease and the deepest frown the
|
|
747
|
+
* mixer asks for (−0.45) gets all of it; the inner brow's concern is full at
|
|
748
|
+
* its 0.55 and a press at the 0.40-0.55 most states hold gets most of the chin.
|
|
749
|
+
*
|
|
750
|
+
* A map named here that the asset lacks is simply not read, and a map the
|
|
751
|
+
* asset has that is not named here stays at weight 0: an asset newer than its
|
|
752
|
+
* rig loses the light the rig cannot place, not the face.
|
|
753
|
+
* TARA-SPECIFIC: judged on tushar's maps at the 400 × 300 tile, the only ones
|
|
754
|
+
* that exist.
|
|
755
|
+
*/
|
|
756
|
+
const EXPRESSION_WEIGHT: Record<string, (at: (channel: string) => number) => number> = {
|
|
757
|
+
smile: (at) => at("mouthCorner") / 0.8,
|
|
758
|
+
raise: (at) => at("browRaise") / 0.6,
|
|
759
|
+
knit: (at) => -at("browRaise") / 0.45,
|
|
760
|
+
inner: (at) => at("browInner") / 0.5,
|
|
761
|
+
press: (at) => at("mouthPress") / 0.7,
|
|
762
|
+
};
|
|
763
|
+
const EXPRESSION_UNIFORMS = (n: number) => [
|
|
764
|
+
"uniform sampler2D uExpression;", "uniform vec4 uExpressionTile;", "uniform vec2 uExpressionU;",
|
|
765
|
+
"uniform vec4 uExpressionStrip;", "uniform float uExpressionStep;",
|
|
766
|
+
"uniform float uExpressionScale;",
|
|
767
|
+
`uniform float uExpressionL[${n}];`, `uniform float uExpressionR[${n}];`,
|
|
768
|
+
].join("\n");
|
|
769
|
+
// Every tap is read whatever its weight: a texture read under a branch that
|
|
770
|
+
// differs across the midline has no derivatives to choose its mip from.
|
|
771
|
+
const EXPRESSION_FRAGMENT = (n: number) => [
|
|
772
|
+
"vec2 expressionAt = clamp(uExpressionTile.xz + uExpressionTile.yw * vMapUv, 0.0, 1.0);",
|
|
773
|
+
"vec2 expressionUv = uExpressionStrip.zw + expressionAt * uExpressionStrip.xy;",
|
|
774
|
+
"float expressionSide = smoothstep(",
|
|
775
|
+
` -${EXPRESSION_SPLIT}, ${EXPRESSION_SPLIT}, uExpressionU.x + uExpressionU.y * vMapUv.x);`,
|
|
776
|
+
"float expressionLog = 0.0;",
|
|
777
|
+
`for (int i = 0; i < ${n}; i++) {`,
|
|
778
|
+
" float ratio = texture2D(uExpression, expressionUv + vec2(float(i) * uExpressionStep, 0.0)).r;",
|
|
779
|
+
" expressionLog += mix(uExpressionL[i], uExpressionR[i], expressionSide)",
|
|
780
|
+
" * log(max(ratio * uExpressionScale, 0.01));",
|
|
781
|
+
"}",
|
|
782
|
+
"float expression = exp(expressionLog);",
|
|
783
|
+
"diffuseColor.rgb *= expression;",
|
|
784
|
+
].join("\n");
|
|
785
|
+
|
|
786
|
+
interface Expression {
|
|
787
|
+
readonly map: THREE.Texture;
|
|
788
|
+
readonly names: readonly string[];
|
|
789
|
+
readonly left: Float32Array;
|
|
790
|
+
readonly right: Float32Array;
|
|
791
|
+
readonly uniforms: Record<string, THREE.IUniform>;
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/** The maps from their carrier's node, or `null` for an asset whose carrier
|
|
795
|
+
* does not say what this rig reads — a face with no expression maps is a
|
|
796
|
+
* correct face, and a map read with the wrong layout is not. */
|
|
797
|
+
function expressive(carrier: THREE.Mesh): Expression | null {
|
|
798
|
+
const { expression_tile: tile, expression_u: u, expression_maps: names,
|
|
799
|
+
expression_layout: layout, expression_unity: unity } = carrier.userData;
|
|
800
|
+
const map = (carrier.material as THREE.MeshStandardMaterial).map;
|
|
801
|
+
if (!map || !Array.isArray(tile) || !Array.isArray(u) || !Array.isArray(layout)
|
|
802
|
+
|| typeof unity !== "number" || !Array.isArray(names) || !names.length) return null;
|
|
803
|
+
// A ratio, not a colour. The loader tags every base colour sRGB.
|
|
804
|
+
map.colorSpace = THREE.NoColorSpace;
|
|
805
|
+
map.needsUpdate = true;
|
|
806
|
+
const [width, height, pad, stripWidth, stripHeight] = layout as number[];
|
|
807
|
+
const left = new Float32Array(names.length);
|
|
808
|
+
const right = new Float32Array(names.length);
|
|
809
|
+
return {
|
|
810
|
+
map, names: names.map(String), left, right,
|
|
811
|
+
uniforms: {
|
|
812
|
+
uExpression: { value: map },
|
|
813
|
+
uExpressionTile: { value: new THREE.Vector4(tile[0], tile[1], tile[2], tile[3]) },
|
|
814
|
+
uExpressionU: { value: new THREE.Vector2(u[0], u[1]) },
|
|
815
|
+
uExpressionStrip: { value: new THREE.Vector4(width / stripWidth, height / stripHeight,
|
|
816
|
+
pad / stripWidth, pad / stripHeight) },
|
|
817
|
+
uExpressionStep: { value: (width + pad) / stripWidth },
|
|
818
|
+
uExpressionScale: { value: 255 / unity },
|
|
819
|
+
uExpressionL: { value: left },
|
|
820
|
+
uExpressionR: { value: right },
|
|
821
|
+
},
|
|
822
|
+
};
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* The neck, taught to wear the jaw's shadow where the jaw is.
|
|
827
|
+
*
|
|
828
|
+
* The photograph paints the shadow the chin casts on the throat, and a painted
|
|
829
|
+
* shadow stays where it was painted: under a 9° turn the jaw crossed the top of
|
|
830
|
+
* the neck by 8 px and its shadow did not move, which read as the neck sliding
|
|
831
|
+
* out from under the head. So the
|
|
832
|
+
* build lifts it out of the albedo into a ratio tile
|
|
833
|
+
* (`project_albedo.lift_jaw_shadow`), and this puts it back from the head's
|
|
834
|
+
* frame: each neck fragment finds the point of the *turned* head in front of it
|
|
835
|
+
* — the view ray met with the plane the jaw's rim turns in — and reads the
|
|
836
|
+
* ratio at that point's rest position. At rest that point is the fragment's
|
|
837
|
+
* own, so the drawing is the photograph; under a turn the shadow's edge rides
|
|
838
|
+
* the rim, whatever the neck's own follow is doing.
|
|
839
|
+
*
|
|
840
|
+
* The tile lives in the albedo atlas (`face_texture.JAW_SHADOW_AT`), the way
|
|
841
|
+
* the eye's socket multiplier lives beside its globe, so it is one more read of
|
|
842
|
+
* a texture already bound and no draw call. Its edges are white — no shadow —
|
|
843
|
+
* and the lookup is clamped to it, so a ray that lands past the tile (the
|
|
844
|
+
* throat's far side under a hard turn) reads "no shadow" rather than the hair
|
|
845
|
+
* or the iris the atlas keeps beside it.
|
|
846
|
+
*/
|
|
847
|
+
function jawShadow(base: THREE.MeshStandardMaterial, uv: number[], extent: number[],
|
|
848
|
+
rimZ: number, headInverse: { value: THREE.Matrix4 }) {
|
|
849
|
+
const material = base.clone();
|
|
850
|
+
const tile = { value: new THREE.Vector4(uv[0], uv[1], uv[2], uv[3]) };
|
|
851
|
+
const bounds = { value: new THREE.Vector4(extent[0], extent[1], extent[2], extent[3]) };
|
|
852
|
+
const rim = { value: rimZ };
|
|
853
|
+
material.onBeforeCompile = (shader) => {
|
|
854
|
+
shader.uniforms.uHeadInverse = headInverse;
|
|
855
|
+
shader.uniforms.uJawTile = tile;
|
|
856
|
+
shader.uniforms.uJawBounds = bounds;
|
|
857
|
+
shader.uniforms.uJawRim = rim;
|
|
858
|
+
shader.vertexShader = shader.vertexShader
|
|
859
|
+
.replace("#include <common>", "#include <common>\nvarying vec3 vJawView;")
|
|
860
|
+
.replace("#include <project_vertex>", "#include <project_vertex>\nvJawView = mvPosition.xyz;");
|
|
861
|
+
shader.fragmentShader = shader.fragmentShader
|
|
862
|
+
.replace("#include <common>",
|
|
863
|
+
"#include <common>\nvarying vec3 vJawView;\nuniform mat4 uHeadInverse;\n"
|
|
864
|
+
+ "uniform vec4 uJawTile;\nuniform vec4 uJawBounds;\nuniform float uJawRim;")
|
|
865
|
+
.replace("#include <map_fragment>",
|
|
866
|
+
"#include <map_fragment>\n"
|
|
867
|
+
+ "vec3 jawFrom = (uHeadInverse * vec4(vJawView, 1.0)).xyz;\n"
|
|
868
|
+
+ "vec3 jawRay = (uHeadInverse * vec4(0.0, 0.0, 1.0, 0.0)).xyz;\n"
|
|
869
|
+
+ "vec2 jawAt = jawFrom.xy + jawRay.xy * ((uJawRim - jawFrom.z) / jawRay.z);\n"
|
|
870
|
+
+ "jawAt = clamp(jawAt, uJawBounds.xz, uJawBounds.yw);\n"
|
|
871
|
+
+ "vec3 jawShadow = texture2D(map, uJawTile.xz + uJawTile.yw * jawAt).rgb;\n"
|
|
872
|
+
+ "diffuseColor.rgb *= jawShadow;")
|
|
873
|
+
.replace("#include <emissivemap_fragment>",
|
|
874
|
+
"#include <emissivemap_fragment>\ntotalEmissiveRadiance *= jawShadow;");
|
|
875
|
+
};
|
|
876
|
+
material.customProgramCacheKey = () => "tara-jaw-shadow";
|
|
877
|
+
return material;
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
/** Lamps, standing in for the four area lights `build_tara.setup_scene` uses.
|
|
881
|
+
*
|
|
882
|
+
* The albedo is a photograph and already holds this face's light, so 75% of it
|
|
883
|
+
* is emitted verbatim (`emissiveFactor` in the GLB) and only the remaining 25%
|
|
884
|
+
* is handed to these. They exist for the quarter that makes a yaw read as a
|
|
885
|
+
* head turning rather than a picture sliding; their sum along the face normal
|
|
886
|
+
* is π, which is exactly the irradiance that renders that quarter back at full
|
|
887
|
+
* albedo — so the rest frame is the reference photograph, and only a turned
|
|
888
|
+
* head departs from it.
|
|
889
|
+
*/
|
|
890
|
+
const AMBIENT = 0.62 * Math.PI;
|
|
891
|
+
const KEY = 0.26 * Math.PI;
|
|
892
|
+
const WRAP = 0.10 * Math.PI;
|
|
893
|
+
|
|
894
|
+
/** Frames drawn per second, capped rather than left at the display's rate.
|
|
895
|
+
*
|
|
896
|
+
* `setAnimationLoop` is `requestAnimationFrame`, so uncapped this face is drawn
|
|
897
|
+
* as fast as the viewer's hardware refreshes — 60 on most panels, 120 on a
|
|
898
|
+
* ProMotion Mac or a current flagship phone. That is the wrong way round: the
|
|
899
|
+
* device most likely to care about the battery is the one that would draw the
|
|
900
|
+
* most, and it buys nothing, because idle motion here is deliberately held
|
|
901
|
+
* under ~1.5 Hz (CLAUDE.md) and the head's travel is a few degrees, slowly, in
|
|
902
|
+
* a 400 × 300 tile. 30 samples that twenty times a cycle. Character animation
|
|
903
|
+
* ships lipsync at 24 for a living.
|
|
904
|
+
*
|
|
905
|
+
* Measured on an M1 over four paired reps against `peep`, which is the SVG
|
|
906
|
+
* avatar that already ships: uncapped at 60 the 3-D face cost 13.8 points of
|
|
907
|
+
* one core more than peep; capped at 30 the difference was inside the noise
|
|
908
|
+
* (−0.9 points over three reps). The cap is most of the runtime cost of being
|
|
909
|
+
* 3-D at all.
|
|
910
|
+
*
|
|
911
|
+
* It throttles *drawing* only. The mixer runs its own rAF loop and keeps its
|
|
912
|
+
* own clock, so cue timing is exactly as accurate as it was — what drops is how
|
|
913
|
+
* often that clock is looked at, not how well it is kept.
|
|
914
|
+
*/
|
|
915
|
+
const RENDER_FPS = 30;
|
|
916
|
+
const MIN_FRAME_MS = 1000 / RENDER_FPS;
|
|
917
|
+
|
|
918
|
+
export interface TaraRigOptions {
|
|
919
|
+
/** Called once the GLB is in the scene, for a capture tool that must wait. */
|
|
920
|
+
readonly onReady?: () => void;
|
|
921
|
+
/** The character's GLB, when it is not tara. The second-character seam: every
|
|
922
|
+
* build fact the rig reads (jaw-shadow tile, rim depth, morph names) travels
|
|
923
|
+
* in the GLB's own extras, so a character built by the same scripts needs
|
|
924
|
+
* nothing else. Tara's tuning is still applied, which is the experiment. */
|
|
925
|
+
readonly url?: string;
|
|
926
|
+
/** `false` leaves an asset's expression maps unread, for a capture tool
|
|
927
|
+
* comparing the face with and without them. */
|
|
928
|
+
readonly expression?: boolean;
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
const radians = (deg: number) => (deg * Math.PI) / 180;
|
|
932
|
+
|
|
933
|
+
/** The renderer, or `null` where the browser will not give a context.
|
|
934
|
+
*
|
|
935
|
+
* Three.js throws out of the constructor rather than returning anything, and it
|
|
936
|
+
* has already written its own line to the console by then; that line is kept
|
|
937
|
+
* because it names the underlying reason, which this one does not. */
|
|
938
|
+
function webglRenderer(): THREE.WebGLRenderer | null {
|
|
939
|
+
try {
|
|
940
|
+
return new THREE.WebGLRenderer({ antialias: true, alpha: true });
|
|
941
|
+
} catch {
|
|
942
|
+
return null;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
/**
|
|
947
|
+
* Where a lid channel's influence reaches a shut eye. Not at 1, because the
|
|
948
|
+
* mixer never asks for 1: a blink is a 0.11–0.15 s triangle, the lid channel's
|
|
949
|
+
* 18 ms smoothing rounds its peak off, and this rig draws at 30 fps, so the
|
|
950
|
+
* frame a viewer actually sees peaks at influence 0.74 on the median blink and
|
|
951
|
+
* 0.66 at the fifth percentile. A lid morph that shut only at 1 left every
|
|
952
|
+
* blink a quarter open — the lid came down and the iris was still there.
|
|
953
|
+
*
|
|
954
|
+
* So above `LID_KNEE` the influence is eased up to meet 1 at `LID_SHUT`, and
|
|
955
|
+
* held there: past that point the lid has landed on the lower one. Below the
|
|
956
|
+
* knee it is untouched, and that is every lid held part-way on purpose — the
|
|
957
|
+
* lowered lid of a degraded link, the lid following a downward gaze — so those
|
|
958
|
+
* look exactly as they did. The ease is quadratic from the knee, so both the
|
|
959
|
+
* value and its slope are continuous there.
|
|
960
|
+
*
|
|
961
|
+
* `scripts/morphs.py:influence` parses both numbers out of this file.
|
|
962
|
+
*/
|
|
963
|
+
/**
|
|
964
|
+
* The lower lid follows the eye down. Its retractor is tied to the inferior
|
|
965
|
+
* rectus, so a look down pulls the lower margin down with it by a millimetre
|
|
966
|
+
* or two — the upper lid's half of this is the mixer's `lidBias`, and a face
|
|
967
|
+
* whose upper lid follows while the lower one stays reads as a drowsy droop
|
|
968
|
+
* rather than a glance. Driven through the squint target run backwards,
|
|
969
|
+
* which is the lower lid and nothing else: at the notes gaze (`pupilY` 0.72)
|
|
970
|
+
* the margin drops ~0.006, about 1.7 px at the 400 px tile.
|
|
971
|
+
*/
|
|
972
|
+
const LOWER_LID_FOLLOW = 0.35;
|
|
973
|
+
|
|
974
|
+
const LID_KNEE = 0.35;
|
|
975
|
+
const LID_SHUT = 0.66;
|
|
976
|
+
|
|
977
|
+
/**
|
|
978
|
+
* A squint's influence rises faster than its channel. The mixer's values are
|
|
979
|
+
* set where a line face's lower lid reads — a smile's squint is 0.30 — and a
|
|
980
|
+
* photographic lower lid rising 0.30 of its travel is a pixel or two, so the
|
|
981
|
+
* squint that makes a smile real was not there. A power curve lifts the small
|
|
982
|
+
* values into sight. The lower-lid follow, which runs this target backwards,
|
|
983
|
+
* is added after it and stays linear.
|
|
984
|
+
*
|
|
985
|
+
* TARA-SPECIFIC. The curve needs a ceiling as well as a lift, because the
|
|
986
|
+
* squint stacks and the mouth does not. A *silent* smile takes squint from
|
|
987
|
+
* three layers at once — an approval clip, the encouraging emotion and
|
|
988
|
+
* prosody's warmth — which reach 0.51 together, while the smile map saturates
|
|
989
|
+
* at a mouth corner of 0.8 and no layer drives the jaw, so the last third of a
|
|
990
|
+
* smile arrives as narrowing eyes over lips that cannot part any further. On a
|
|
991
|
+
* line face that reads as warmth. On a photograph it reads as sedation: a
|
|
992
|
+
* reviewer watching a recorded call read those two moments as the avatar
|
|
993
|
+
* falling asleep or heavily medicated, and named the eyes, not the mouth. The
|
|
994
|
+
* ceiling is where the face stops reading drugged, judged at crop. The knee is
|
|
995
|
+
* low enough that an ordinary one-layer smile is untouched (0.22 renders
|
|
996
|
+
* 0.402, against 0.403 with no ceiling at all), and the approach is
|
|
997
|
+
* exponential rather than a clamp so the slope is continuous where the two
|
|
998
|
+
* meet and the lower lid never visibly sticks.
|
|
999
|
+
*/
|
|
1000
|
+
const SQUINT_CURVE = 0.6;
|
|
1001
|
+
const SQUINT_KNEE = 0.2;
|
|
1002
|
+
const SQUINT_CEIL = 0.54;
|
|
1003
|
+
|
|
1004
|
+
const squintCurve = (i: number): number => {
|
|
1005
|
+
if (i <= 0) return i;
|
|
1006
|
+
if (i <= SQUINT_KNEE) return i ** SQUINT_CURVE;
|
|
1007
|
+
const knee = SQUINT_KNEE ** SQUINT_CURVE;
|
|
1008
|
+
const slope = SQUINT_CURVE * SQUINT_KNEE ** (SQUINT_CURVE - 1);
|
|
1009
|
+
const head = SQUINT_CEIL - knee;
|
|
1010
|
+
return SQUINT_CEIL - head * Math.exp((-slope * (i - SQUINT_KNEE)) / head);
|
|
1011
|
+
};
|
|
1012
|
+
|
|
1013
|
+
const lidClosure = (i: number): number => {
|
|
1014
|
+
if (i <= LID_KNEE) return i;
|
|
1015
|
+
if (i >= LID_SHUT) return 1;
|
|
1016
|
+
const t = (i - LID_KNEE) / (LID_SHUT - LID_KNEE);
|
|
1017
|
+
return i + (1 - LID_SHUT) * t * t;
|
|
1018
|
+
};
|
|
1019
|
+
|
|
1020
|
+
/**
|
|
1021
|
+
* A channel's morph influence. One line — and the lid and squint curves above — and the
|
|
1022
|
+
* same one `scripts/morphs.py:influence` uses, so a Blender preview and the
|
|
1023
|
+
* browser pose the face identically.
|
|
1024
|
+
*
|
|
1025
|
+
* It is allowed to go negative, which is what lets one target serve a
|
|
1026
|
+
* bidirectional channel: `mouthCornerL` at −1.4 is the smile target run
|
|
1027
|
+
* backwards into a frown, and `lidL` below its 0.12 rest opens the eye wider
|
|
1028
|
+
* than neutral. A rig that clamped this at 0 would silently delete the negative
|
|
1029
|
+
* half of six channels.
|
|
1030
|
+
*/
|
|
1031
|
+
/**
|
|
1032
|
+
* The neck's follow targets, and the one place a morph is not driven by
|
|
1033
|
+
* `influence`.
|
|
1034
|
+
*
|
|
1035
|
+
* `scripts/morphs.neck_targets` authors two fields per head axis — `A P` and
|
|
1036
|
+
* `A^2 P` for that axis's skew matrix — because a rigid rotation is exactly
|
|
1037
|
+
* `sin(th)` of the first plus `1 - cos(th)` of the second. A single field scaled
|
|
1038
|
+
* by the pose is the *linear* approximation of that, and it stretches the neck
|
|
1039
|
+
* away from the pivot by `radius x (1 - cos th)`: fine at six degrees, five
|
|
1040
|
+
* pixels at the envelope a nod that lands actually needs.
|
|
1041
|
+
*
|
|
1042
|
+
* So under pitch and roll the throat tracks the skull exactly at any angle,
|
|
1043
|
+
* and — the part worth having — the asset stops depending on the envelope.
|
|
1044
|
+
* These fields carry no angle, so `HEAD_DEG` below is a runtime number that can
|
|
1045
|
+
* move without leaving `tara.glb` stale. Yaw's pair is a partial twist rather
|
|
1046
|
+
* than the skull's own rotation, and the build weights its two fields so these
|
|
1047
|
+
* same two influences drive it (`morphs.neck_twist`).
|
|
1048
|
+
*/
|
|
1049
|
+
const NECK_QUAD = "_q";
|
|
1050
|
+
const HEAD_AXIS: Record<string, keyof typeof HEAD_DEG> = {
|
|
1051
|
+
headYaw: "yaw", headPitch: "pitch", headRoll: "roll",
|
|
1052
|
+
};
|
|
1053
|
+
|
|
1054
|
+
const neckInfluence = (channel: string, pose: RigPose): number | null => {
|
|
1055
|
+
const quad = channel.endsWith(NECK_QUAD);
|
|
1056
|
+
const axis = HEAD_AXIS[quad ? channel.slice(0, -NECK_QUAD.length) : channel];
|
|
1057
|
+
if (!axis) return null;
|
|
1058
|
+
const value = pose[quad ? channel.slice(0, -NECK_QUAD.length) : channel] ?? 0;
|
|
1059
|
+
const radians = ((value / HEAD_CLAMP) * HEAD_DEG[axis] * Math.PI) / 180;
|
|
1060
|
+
return quad ? 1 - Math.cos(radians) : Math.sin(radians);
|
|
1061
|
+
};
|
|
1062
|
+
|
|
1063
|
+
/**
|
|
1064
|
+
* The hair's roll, which is the one thing in this rig that is not a function of
|
|
1065
|
+
* the pose alone.
|
|
1066
|
+
*
|
|
1067
|
+
* A hank that hangs past the jaw is lying on a shoulder, and a shoulder does not
|
|
1068
|
+
* tilt when the head does. Rolled rigidly with the skull it lifts off the collar
|
|
1069
|
+
* and the page shows through behind it, so the shell gives up `hold` of the
|
|
1070
|
+
* roll at its lowest rows and none at the crown, graded by `morphs.hair_hold`.
|
|
1071
|
+
* That is the static half and it is what fixes the gap.
|
|
1072
|
+
*
|
|
1073
|
+
* The other half is why roll read as a hinge at all. A rigid rotation about a
|
|
1074
|
+
* fixed point is a hinge — there is nothing else in it — and what a real head
|
|
1075
|
+
* tilt has that this lacked is hair that arrives late and settles. Live2D gives
|
|
1076
|
+
* every hank a spring for exactly this (`docs/research-head-rotation.md` § 3.1:
|
|
1077
|
+
* mobility ~0.95, delay 0.8-0.9, one clear overshoot), so this is a spring on
|
|
1078
|
+
* the hair's own angle chasing the share of the roll it agrees to take.
|
|
1079
|
+
*
|
|
1080
|
+
* It is on the hair and not on the head's channels on purpose. The mixer's
|
|
1081
|
+
* per-channel time constants are shared with the SVG faces and every clip in the
|
|
1082
|
+
* library is authored pre-compensated for them, so a spring on `headRoll` would
|
|
1083
|
+
* silently re-time every nod ever authored. Secondary motion on a shell that
|
|
1084
|
+
* only this renderer has costs nothing outside it.
|
|
1085
|
+
*
|
|
1086
|
+
* 1.5 Hz is the band the library already keeps gesture under, and a hank of hair
|
|
1087
|
+
* on a real head swings near it (a 7 cm pendulum is 1.9 Hz); the damping is a
|
|
1088
|
+
* single visible overshoot, settling inside 0.8 s. Faster reads as a flick and
|
|
1089
|
+
* slower as wet hair.
|
|
1090
|
+
*/
|
|
1091
|
+
/**
|
|
1092
|
+
* `hold` is the share of the head's roll the hanging hair declines to take, and
|
|
1093
|
+
* `hz`/`damping` are how it gets there. 1.5 Hz is the ceiling the repo's idle
|
|
1094
|
+
* constraint sets on *driven* oscillation; a settle is a one-shot and could
|
|
1095
|
+
* defensibly go faster, but there is no reason to spend the exemption: what
|
|
1096
|
+
* unhinges the roll is the hair arriving late, not the ring. At 0.65 it trails
|
|
1097
|
+
* by 93% of its travel a frame in, overshoots 5% and is inside 5% of the hold in
|
|
1098
|
+
* 450 ms — well within a phrase's hold.
|
|
1099
|
+
*/
|
|
1100
|
+
export const HAIR_ROLL = { hold: 0.85, hz: 1.5, damping: 0.65 };
|
|
1101
|
+
|
|
1102
|
+
/**
|
|
1103
|
+
* One step of `HAIR_ROLL`'s spring: semi-implicit, the rate taking the frame's
|
|
1104
|
+
* acceleration before the angle takes the rate. Explicit Euler rings at this
|
|
1105
|
+
* stiffness and 30 fps; this does not, which is the only reason the order of
|
|
1106
|
+
* those two lines is worth a sentence.
|
|
1107
|
+
*
|
|
1108
|
+
* Exported for `test/nods.test.ts`, because settle time and overshoot are
|
|
1109
|
+
* numbers and not something a still frame can show. It is not part of the
|
|
1110
|
+
* package's surface — `packages/avatar/client/tara.ts` is.
|
|
1111
|
+
*/
|
|
1112
|
+
export const hairRollStep = (angle: number, rate: number, target: number, dt: number) => {
|
|
1113
|
+
const w = 2 * Math.PI * HAIR_ROLL.hz;
|
|
1114
|
+
const next = rate + (w * w * (target - angle) - 2 * HAIR_ROLL.damping * w * rate) * dt;
|
|
1115
|
+
return { angle: angle + next * dt, rate: next };
|
|
1116
|
+
};
|
|
1117
|
+
|
|
1118
|
+
/** The hair pair's two influences, from the *extra* angle the shell is turned
|
|
1119
|
+
* by — the same `sin` / `1 - cos` terms the neck's fields are driven with. */
|
|
1120
|
+
const hairInfluence = (channel: string, extra: number): number | null => {
|
|
1121
|
+
if (channel === "headRoll") return Math.sin(extra);
|
|
1122
|
+
if (channel === "headRoll" + NECK_QUAD) return 1 - Math.cos(extra);
|
|
1123
|
+
return null;
|
|
1124
|
+
};
|
|
1125
|
+
|
|
1126
|
+
const influence = (channel: string, value: number): number => {
|
|
1127
|
+
const rest = (REST as Record<string, number>)[channel] ?? 0;
|
|
1128
|
+
const i = (value - rest) / (1 - rest);
|
|
1129
|
+
if (channel === "lidL" || channel === "lidR") return lidClosure(i);
|
|
1130
|
+
return channel === "squintL" || channel === "squintR" ? squintCurve(i) : i;
|
|
1131
|
+
};
|
|
1132
|
+
|
|
1133
|
+
/** One side's weights for the asset's expression maps, in its order. A
|
|
1134
|
+
* channel with no side (`mouthPress`) weighs the same on both. */
|
|
1135
|
+
const expressionWeights = (pose: RigPose, side: "L" | "R", expression: Expression,
|
|
1136
|
+
into: Float32Array) => {
|
|
1137
|
+
const at = (channel: string) => {
|
|
1138
|
+
const name = pose[channel + side] === undefined ? channel : channel + side;
|
|
1139
|
+
const value = pose[name];
|
|
1140
|
+
return value === undefined ? 0 : influence(name, value);
|
|
1141
|
+
};
|
|
1142
|
+
expression.names.forEach((map, i) => {
|
|
1143
|
+
const weight = EXPRESSION_WEIGHT[map]?.(at) ?? 0;
|
|
1144
|
+
into[i] = Math.min(Math.max(weight, 0), 1);
|
|
1145
|
+
});
|
|
1146
|
+
};
|
|
1147
|
+
|
|
1148
|
+
// `options` is `unknown` in the contract, and stays `unknown` here: the mixer
|
|
1149
|
+
// passes `rigOptions` through verbatim and has no way to know any rig's shape.
|
|
1150
|
+
export function createTaraRig(mount: HTMLElement, options?: unknown): AvatarRig {
|
|
1151
|
+
const { onReady, url = ASSETS.tara, expression: readExpression = true } =
|
|
1152
|
+
(options ?? {}) as TaraRigOptions;
|
|
1153
|
+
const scene = new THREE.Scene();
|
|
1154
|
+
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 100);
|
|
1155
|
+
camera.position.set(0, FRAME_CENTRE, 6);
|
|
1156
|
+
camera.lookAt(0, FRAME_CENTRE, 0);
|
|
1157
|
+
|
|
1158
|
+
const renderer = webglRenderer();
|
|
1159
|
+
// No context, no face — and that has to be the whole of it. `createAvatar` is
|
|
1160
|
+
// synchronous and returns `{ destroy }`, so a consumer has nothing to catch:
|
|
1161
|
+
// anything thrown here lands in *their* window and takes the call page with
|
|
1162
|
+
// it, over a browser condition that is nobody's defect. WebGL is unavailable
|
|
1163
|
+
// more often than it looks — a driver on a blocklist, a hardened profile, a
|
|
1164
|
+
// remote desktop — and the right outcome is a call that still has audio,
|
|
1165
|
+
// captions and states, with an empty tile where the head would be.
|
|
1166
|
+
//
|
|
1167
|
+
// `warn` rather than `error` on purpose: `[avatar]` console errors mean a
|
|
1168
|
+
// defect in this package, and the capture tools fail a run on any of them.
|
|
1169
|
+
// This one says the environment cannot draw, which is a different sentence.
|
|
1170
|
+
if (!renderer) {
|
|
1171
|
+
console.warn("[avatar] no WebGL context; this character will not render in this browser");
|
|
1172
|
+
return { apply() { /* nothing to pose */ }, destroy() { /* nothing to release */ } };
|
|
1173
|
+
}
|
|
1174
|
+
renderer.outputColorSpace = THREE.SRGBColorSpace;
|
|
1175
|
+
// `setSize(…, false)` below leaves CSS alone, so the canvas has to be told to
|
|
1176
|
+
// fill the mount. Without this it displays at its backing-store size, which on
|
|
1177
|
+
// a 2x screen is a head twice the tile, cropped to its top-left quarter —
|
|
1178
|
+
// invisible at devicePixelRatio 1, which is what every capture tool ran at.
|
|
1179
|
+
renderer.domElement.style.cssText = "display:block;width:100%;height:100%";
|
|
1180
|
+
mount.append(renderer.domElement);
|
|
1181
|
+
|
|
1182
|
+
scene.add(new THREE.AmbientLight(0xffffff, AMBIENT));
|
|
1183
|
+
const key = new THREE.DirectionalLight(0xffffff, KEY);
|
|
1184
|
+
key.position.set(0, 1.0, 3.0);
|
|
1185
|
+
scene.add(key);
|
|
1186
|
+
for (const x of [-1.9, 1.9]) {
|
|
1187
|
+
const wrap = new THREE.DirectionalLight(0xffffff, WRAP);
|
|
1188
|
+
wrap.position.set(x, 1.0, 2.5);
|
|
1189
|
+
scene.add(wrap);
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
// The head swings as a group; the neck and torso do not (see HEAD_PARTS).
|
|
1193
|
+
// Parenting to a pivot object rather than rotating each part is not a
|
|
1194
|
+
// convenience — the parts have to rotate about a shared point, and a per-mesh
|
|
1195
|
+
// rotation about each mesh's own origin would slide them apart.
|
|
1196
|
+
//
|
|
1197
|
+
// The body's two transforms nest outside it, one group each (see BODY):
|
|
1198
|
+
// `trunk` sways everything, and `lift` carries the neck and head on the
|
|
1199
|
+
// breath the torso takes inside `trunk` — and on the lean's rigid share,
|
|
1200
|
+
// which reaches the head the same way for the same reason.
|
|
1201
|
+
//
|
|
1202
|
+
// There were three. The outermost was `figure`, and it existed only to scale
|
|
1203
|
+
// the whole character for `torsoLean`; with the camera a sibling rather than
|
|
1204
|
+
// a child, that was a zoom and not a lean. The trunk deforms on the shell now
|
|
1205
|
+
// (`morphs.torso_targets`), so the group has no work left and is gone rather
|
|
1206
|
+
// than left behind as an identity transform for someone to wonder about.
|
|
1207
|
+
const trunk = new THREE.Group();
|
|
1208
|
+
const lift = new THREE.Group();
|
|
1209
|
+
const head = new THREE.Group();
|
|
1210
|
+
head.position.copy(PIVOT);
|
|
1211
|
+
// Yaw and pitch turn `head`; roll turns `tilt`, which rides inside them at
|
|
1212
|
+
// the chin (ROLL_PIVOT), so the parts hang from `tilt`.
|
|
1213
|
+
const tilt = new THREE.Group();
|
|
1214
|
+
tilt.position.subVectors(ROLL_PIVOT, PIVOT);
|
|
1215
|
+
scene.add(trunk);
|
|
1216
|
+
trunk.add(lift);
|
|
1217
|
+
lift.add(head);
|
|
1218
|
+
head.add(tilt);
|
|
1219
|
+
let torso: THREE.Object3D | null = null;
|
|
1220
|
+
|
|
1221
|
+
let destroyed = false;
|
|
1222
|
+
let pending: RigPose | null = null;
|
|
1223
|
+
let loaded = false;
|
|
1224
|
+
let warnedAboutRuntimeBudget = false;
|
|
1225
|
+
// Every mesh that owns morph targets, paired with the dictionary three.js
|
|
1226
|
+
// built from the GLB's `extras.targetNames` — i.e. the pose channel names
|
|
1227
|
+
// `scripts/morphs.py` authored them under.
|
|
1228
|
+
const morphed: THREE.Mesh[] = [];
|
|
1229
|
+
const eyes: THREE.Object3D[] = [];
|
|
1230
|
+
// Both globes turn together, so one rotation serves both sockets.
|
|
1231
|
+
const gaze = { value: new THREE.Matrix3() };
|
|
1232
|
+
const turn = new THREE.Matrix4();
|
|
1233
|
+
let expression: Expression | null = null;
|
|
1234
|
+
// The `Hair` shell, on a character whose hair hangs low enough to have the
|
|
1235
|
+
// roll pair; null on one whose hair stops beside the temple (`HAIR_ROLL`).
|
|
1236
|
+
let hairMesh: THREE.Mesh | null = null;
|
|
1237
|
+
// The head's roll in the asset's own frame — what both the neck's follow and
|
|
1238
|
+
// the hair's are authored about — and the hair's own, which chases it.
|
|
1239
|
+
let headRollRad = 0;
|
|
1240
|
+
let hairRollRad = 0;
|
|
1241
|
+
let hairRate = 0;
|
|
1242
|
+
let hairSeeded = false;
|
|
1243
|
+
|
|
1244
|
+
/** The share of the head's roll the hair settles at. */
|
|
1245
|
+
const hairTarget = () => headRollRad * (1 - HAIR_ROLL.hold);
|
|
1246
|
+
|
|
1247
|
+
const writeHair = () => {
|
|
1248
|
+
const dictionary = hairMesh?.morphTargetDictionary;
|
|
1249
|
+
const influences = hairMesh?.morphTargetInfluences;
|
|
1250
|
+
if (!dictionary || !influences) return;
|
|
1251
|
+
const extra = hairRollRad - headRollRad;
|
|
1252
|
+
for (const [channel, index] of Object.entries(dictionary)) {
|
|
1253
|
+
const term = hairInfluence(channel, extra);
|
|
1254
|
+
if (term !== null) influences[index] = term;
|
|
1255
|
+
}
|
|
1256
|
+
};
|
|
1257
|
+
|
|
1258
|
+
const stepHair = (dt: number) => {
|
|
1259
|
+
if (!hairMesh || !hairSeeded) return;
|
|
1260
|
+
({ angle: hairRollRad, rate: hairRate } =
|
|
1261
|
+
hairRollStep(hairRollRad, hairRate, hairTarget(), dt));
|
|
1262
|
+
};
|
|
1263
|
+
|
|
1264
|
+
const resize = () => {
|
|
1265
|
+
const width = Math.max(1, mount.clientWidth);
|
|
1266
|
+
const height = Math.max(1, mount.clientHeight || Math.round((width * 3) / 4));
|
|
1267
|
+
renderer.setPixelRatio(pixelRatioFor(width, height, window.devicePixelRatio));
|
|
1268
|
+
renderer.setSize(width, height, false);
|
|
1269
|
+
// The framed *height* is fixed and the width follows the mount, so a tile
|
|
1270
|
+
// of the wrong aspect shows more or less background rather than a face of
|
|
1271
|
+
// the wrong shape. The atlas cannot be stretched: it is a photograph.
|
|
1272
|
+
// Symmetric about the camera, which is *already* at the frame's centre
|
|
1273
|
+
// height. Offsetting the frustum by that centre as well applies it twice:
|
|
1274
|
+
// the face rendered 0.47 face heights low, at exactly the right size, which
|
|
1275
|
+
// reads as a framing choice rather than as the arithmetic error it was.
|
|
1276
|
+
const halfHeight = FRAME_HEIGHT / 2;
|
|
1277
|
+
const halfWidth = (halfHeight * width) / height;
|
|
1278
|
+
camera.top = halfHeight;
|
|
1279
|
+
camera.bottom = -halfHeight;
|
|
1280
|
+
camera.left = -halfWidth;
|
|
1281
|
+
camera.right = halfWidth;
|
|
1282
|
+
camera.updateProjectionMatrix();
|
|
1283
|
+
};
|
|
1284
|
+
const observer = new ResizeObserver(resize);
|
|
1285
|
+
observer.observe(mount);
|
|
1286
|
+
resize();
|
|
1287
|
+
|
|
1288
|
+
const applyPose = (pose: RigPose) => {
|
|
1289
|
+
headRollRad = radians(((pose.headRoll ?? 0) / HEAD_CLAMP) * HEAD_DEG.roll);
|
|
1290
|
+
// The first pose is a starting point, not a movement: seed the hair where it
|
|
1291
|
+
// would have settled, so a tool that sets one pose and screenshots it gets
|
|
1292
|
+
// the hold with no transient, and only a *change* of roll swings the hank.
|
|
1293
|
+
if (!hairSeeded) { hairSeeded = true; hairRollRad = hairTarget(); hairRate = 0; }
|
|
1294
|
+
for (const mesh of morphed) {
|
|
1295
|
+
const dictionary = mesh.morphTargetDictionary;
|
|
1296
|
+
const influences = mesh.morphTargetInfluences;
|
|
1297
|
+
if (!dictionary || !influences) continue;
|
|
1298
|
+
const neck = mesh.name === "Neck";
|
|
1299
|
+
const hair = mesh === hairMesh;
|
|
1300
|
+
for (const [channel, index] of Object.entries(dictionary)) {
|
|
1301
|
+
// The hair carries the head's roll channel too, and it is neither a
|
|
1302
|
+
// channel value nor the head's own angle: it is how much *further* than
|
|
1303
|
+
// the skull this shell is turned, which is negative while it holds and
|
|
1304
|
+
// swings either side of that while it settles (`HAIR_ROLL`).
|
|
1305
|
+
if (hair) {
|
|
1306
|
+
const term = hairInfluence(channel, hairRollRad - headRollRad);
|
|
1307
|
+
if (term !== null) { influences[index] = term; continue; }
|
|
1308
|
+
}
|
|
1309
|
+
// The neck's head targets are the rotation's two terms, not a channel
|
|
1310
|
+
// scaled by the influence law. Only on the neck: the same three channel
|
|
1311
|
+
// names on the head group are a rigid transform, and nowhere else.
|
|
1312
|
+
if (neck) {
|
|
1313
|
+
const term = neckInfluence(channel, pose);
|
|
1314
|
+
if (term !== null) { influences[index] = term; continue; }
|
|
1315
|
+
}
|
|
1316
|
+
const value = pose[channel];
|
|
1317
|
+
const follow = channel.startsWith("squint") ? LOWER_LID_FOLLOW * Math.max(pose.pupilY ?? 0, 0) : 0;
|
|
1318
|
+
if (value === undefined && !follow) continue;
|
|
1319
|
+
influences[index] = (value === undefined ? 0 : influence(channel, value)) - follow;
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
// Blender's Z is face-space v, so its yaw is about Z, its pitch about X and
|
|
1323
|
+
// its roll about Y. The export maps Blender (x, y, z) to glTF (x, z, −y),
|
|
1324
|
+
// so Blender +Z *is* glTF +Y and Blender +X is glTF +X: yaw and pitch carry
|
|
1325
|
+
// across with their sign intact. Only roll changes sign, because Blender +Y
|
|
1326
|
+
// is glTF −Z, and that is a statement about two axes and not about the
|
|
1327
|
+
// channel.
|
|
1328
|
+
//
|
|
1329
|
+
// Yaw was negated here as well until 2026-09-10, on the belief that the
|
|
1330
|
+
// export flips the handedness of a turn. It does not — both frames are
|
|
1331
|
+
// right-handed — and the cost was a head that turned toward the viewer's
|
|
1332
|
+
// *left* on a positive `headYaw`, against `params.js`'s stated sign. It
|
|
1333
|
+
// survived because the same negation was in `build_tara.pose_head`, so the
|
|
1334
|
+
// Blender preview and the browser agreed with each other and only disagreed
|
|
1335
|
+
// with the library. `gaze.js` is what makes it a defect rather than a
|
|
1336
|
+
// convention: it hands `pupilX` and `headYaw` the same aversion term, so
|
|
1337
|
+
// tara's eyes went one way and her head went the other.
|
|
1338
|
+
//
|
|
1339
|
+
// YXZ because that is the order a neck composes in — yaw carrying the pitch
|
|
1340
|
+
// — rather than the order three.js defaults to.
|
|
1341
|
+
// The body first, because the head's roll is stated against it.
|
|
1342
|
+
//
|
|
1343
|
+
// Positive `torsoTurn` takes the trunk to the viewer's right, which about a
|
|
1344
|
+
// pivot below it is a clockwise roll as the camera sees it: negative about
|
|
1345
|
+
// glTF +Z, the axis pointing at the camera.
|
|
1346
|
+
const sway = -radians((pose.torsoTurn ?? 0) * BODY.swayDeg);
|
|
1347
|
+
trunk.rotation.z = sway;
|
|
1348
|
+
trunk.position.set(BODY.hip * Math.sin(sway), BODY.hip * (1 - Math.cos(sway)), 0);
|
|
1349
|
+
// `torsoLean` itself is not applied here at all — it is two morph targets on
|
|
1350
|
+
// the shell, driven by the generic loop above like any other channel that
|
|
1351
|
+
// rests at 0. What is left for the rig is the head's ride: above the collar
|
|
1352
|
+
// the field is flat, so the neck and head take a pure translation and the
|
|
1353
|
+
// head does not deform under a body lean (research § 8 item 4). `morphs.py`
|
|
1354
|
+
// parses `leanRide`, so this is the same number the field plateaus at.
|
|
1355
|
+
const breath = pose.breath ?? 0;
|
|
1356
|
+
if (torso) {
|
|
1357
|
+
torso.scale.set(1 + breath * BODY.widen, 1 + breath * BODY.rise, 1);
|
|
1358
|
+
torso.position.y = -BODY.swellPivot * breath * BODY.rise;
|
|
1359
|
+
}
|
|
1360
|
+
lift.position.y = breath * BODY.rise * (BODY.collar - BODY.swellPivot)
|
|
1361
|
+
- (pose.torsoLean ?? 0) * BODY.leanRide;
|
|
1362
|
+
|
|
1363
|
+
head.rotation.order = "YXZ";
|
|
1364
|
+
head.rotation.set(
|
|
1365
|
+
radians(((pose.headPitch ?? 0) / HEAD_CLAMP) * HEAD_DEG.pitch),
|
|
1366
|
+
radians(((pose.headYaw ?? 0) / HEAD_CLAMP) * HEAD_DEG.yaw),
|
|
1367
|
+
0,
|
|
1368
|
+
);
|
|
1369
|
+
// Less the trunk's roll, so the head's world roll is `headRoll` and
|
|
1370
|
+
// nothing else. Exact about Z alone; with the yaw and pitch it composes
|
|
1371
|
+
// with, the error is the product of two sub-degree angles.
|
|
1372
|
+
tilt.rotation.z = -headRollRad - sway;
|
|
1373
|
+
for (const globe of eyes) {
|
|
1374
|
+
globe.rotation.order = "YXZ";
|
|
1375
|
+
globe.rotation.set(
|
|
1376
|
+
(pose.pupilY ?? 0) * GAZE_TRAVEL.y / GLOBE_RADIUS,
|
|
1377
|
+
(pose.pupilX ?? 0) * GAZE_TRAVEL.x / GLOBE_RADIUS,
|
|
1378
|
+
0,
|
|
1379
|
+
);
|
|
1380
|
+
}
|
|
1381
|
+
if (eyes.length) gaze.value.setFromMatrix4(turn.makeRotationFromEuler(eyes[0].rotation));
|
|
1382
|
+
if (expression) {
|
|
1383
|
+
expressionWeights(pose, "L", expression, expression.left);
|
|
1384
|
+
expressionWeights(pose, "R", expression, expression.right);
|
|
1385
|
+
}
|
|
1386
|
+
};
|
|
1387
|
+
|
|
1388
|
+
new GLTFLoader().load(url, (gltf) => {
|
|
1389
|
+
if (destroyed) return;
|
|
1390
|
+
trunk.add(gltf.scene);
|
|
1391
|
+
// Collect the morphed meshes *before* reparenting: every one of them is a
|
|
1392
|
+
// head part, so a traverse of `gltf.scene` after the move finds only the
|
|
1393
|
+
// neck and the torso and the whole face goes rigid — silently, because a
|
|
1394
|
+
// rig with nothing to drive still renders a perfectly good rest pose.
|
|
1395
|
+
gltf.scene.traverse((object) => {
|
|
1396
|
+
const mesh = object as THREE.Mesh;
|
|
1397
|
+
if (mesh.isMesh && mesh.morphTargetDictionary) morphed.push(mesh);
|
|
1398
|
+
// Kept aside as well: its roll pair is driven by a clock and not only by a
|
|
1399
|
+
// pose, so the render loop has to reach it between poses (`HAIR_ROLL`).
|
|
1400
|
+
if (mesh.isMesh && mesh.name === "Hair" && mesh.morphTargetDictionary) hairMesh = mesh;
|
|
1401
|
+
});
|
|
1402
|
+
// Out of the scene before anything draws it: it carries the maps, and is
|
|
1403
|
+
// one triangle behind the body that nothing should pay a draw call for.
|
|
1404
|
+
const carrier = gltf.scene.getObjectByName("Expression") as THREE.Mesh | undefined;
|
|
1405
|
+
if (carrier) {
|
|
1406
|
+
carrier.removeFromParent();
|
|
1407
|
+
expression = readExpression ? expressive(carrier) : null;
|
|
1408
|
+
if (!expression) (carrier.material as THREE.MeshStandardMaterial).map?.dispose();
|
|
1409
|
+
carrier.geometry.dispose();
|
|
1410
|
+
(carrier.material as THREE.Material).dispose();
|
|
1411
|
+
}
|
|
1412
|
+
for (const name of HEAD_PARTS) {
|
|
1413
|
+
const part = gltf.scene.getObjectByName(name);
|
|
1414
|
+
// Reparenting moves the object into the tilt's frame, whose origin is
|
|
1415
|
+
// ROLL_PIVOT, so subtract that to leave the part where it was authored.
|
|
1416
|
+
// `attach()` would do this from the world matrix, which has not been
|
|
1417
|
+
// computed yet at load.
|
|
1418
|
+
if (part) {
|
|
1419
|
+
part.position.sub(ROLL_PIVOT);
|
|
1420
|
+
tilt.add(part);
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
// One clone per source material, shared by every shell that carries the
|
|
1424
|
+
// field; a mesh without it keeps the material it came with.
|
|
1425
|
+
const turned = new Map<THREE.Material, THREE.MeshStandardMaterial>();
|
|
1426
|
+
head.traverse((object) => {
|
|
1427
|
+
const mesh = object as THREE.Mesh;
|
|
1428
|
+
if (!mesh.isMesh || !mesh.geometry.getAttribute(MOTION_DEPTH)) return;
|
|
1429
|
+
const base = mesh.material as THREE.MeshStandardMaterial;
|
|
1430
|
+
const own = turned.get(base) ?? motionDepth(base, expression ?? undefined);
|
|
1431
|
+
turned.set(base, own);
|
|
1432
|
+
mesh.material = own;
|
|
1433
|
+
});
|
|
1434
|
+
// The neck rides the breath with the head; the torso *is* the breath. Both
|
|
1435
|
+
// are authored in world space with identity nodes, and `lift` sits at the
|
|
1436
|
+
// origin until a pose arrives, so moving the neck needs no correction.
|
|
1437
|
+
const neck = gltf.scene.getObjectByName("Neck") as THREE.Mesh | undefined;
|
|
1438
|
+
if (neck) lift.add(neck);
|
|
1439
|
+
// The shadow's map comes from the build (`build_tara.py`, the neck); an
|
|
1440
|
+
// asset without it keeps the shadow painted on, as every build did before.
|
|
1441
|
+
const skull = head.getObjectByName("Head");
|
|
1442
|
+
const { jaw_shadow_uv: jawUv, jaw_shadow_extent: jawExtent, jaw_shadow_rim_z: jawRim } =
|
|
1443
|
+
neck?.userData ?? {};
|
|
1444
|
+
if (neck && skull && Array.isArray(jawUv) && Array.isArray(jawExtent) && typeof jawRim === "number") {
|
|
1445
|
+
const headInverse = { value: new THREE.Matrix4() };
|
|
1446
|
+
neck.material = jawShadow(neck.material as THREE.MeshStandardMaterial, jawUv, jawExtent,
|
|
1447
|
+
jawRim, headInverse);
|
|
1448
|
+
neck.onBeforeRender = (_renderer, _scene, camera) => {
|
|
1449
|
+
headInverse.value.multiplyMatrices(camera.matrixWorldInverse, skull.matrixWorld).invert();
|
|
1450
|
+
};
|
|
1451
|
+
}
|
|
1452
|
+
torso = gltf.scene.getObjectByName("Body") ?? null;
|
|
1453
|
+
for (const name of ["Eye_L", "Eye_R"]) {
|
|
1454
|
+
const globe = head.getObjectByName(name);
|
|
1455
|
+
if (!globe) continue;
|
|
1456
|
+
// The globe's geometry is authored in world space with no node transform,
|
|
1457
|
+
// so it has to be rotated about its own centre rather than about the
|
|
1458
|
+
// origin. Re-centre the geometry once and put the offset on the node; the
|
|
1459
|
+
// catchlight is already a child and rides the globe, which is what a
|
|
1460
|
+
// reflection on a wet cornea does.
|
|
1461
|
+
const geometry = (globe as THREE.Mesh).geometry;
|
|
1462
|
+
geometry.computeBoundingSphere();
|
|
1463
|
+
const centre = geometry.boundingSphere?.center.clone() ?? new THREE.Vector3();
|
|
1464
|
+
geometry.translate(-centre.x, -centre.y, -centre.z);
|
|
1465
|
+
globe.position.add(centre);
|
|
1466
|
+
for (const child of globe.children) child.position.sub(centre);
|
|
1467
|
+
const mesh = globe as THREE.Mesh;
|
|
1468
|
+
// Whether the globe turns deep is the attribute's to say, not the name's —
|
|
1469
|
+
// the same rule the frame's shells are found by above. An asset built
|
|
1470
|
+
// before the globes carried the field keeps the socket shader alone.
|
|
1471
|
+
mesh.material = socketed(mesh.material as THREE.MeshStandardMaterial,
|
|
1472
|
+
name === "Eye_L" ? -1 : 1, gaze,
|
|
1473
|
+
mesh.geometry.getAttribute(MOTION_DEPTH) !== undefined);
|
|
1474
|
+
eyes.push(globe);
|
|
1475
|
+
}
|
|
1476
|
+
// The mouth's inside, which is otherwise the one flat-lit surface on this
|
|
1477
|
+
// face. Found by *mesh* name rather than material name: `flat_material`
|
|
1478
|
+
// hard-codes a `tara_` prefix, so tushar's cavity material is called
|
|
1479
|
+
// `tara_cavity` as well, and the mesh is what distinguishes it.
|
|
1480
|
+
const cavity = head.getObjectByName("Cavity") as THREE.Mesh | undefined;
|
|
1481
|
+
if (cavity) {
|
|
1482
|
+
cavity.geometry.computeBoundingBox();
|
|
1483
|
+
const box = cavity.geometry.boundingBox;
|
|
1484
|
+
if (box) {
|
|
1485
|
+
cavity.material = cavityShade(cavity.material as THREE.MeshStandardMaterial,
|
|
1486
|
+
box.min.y, box.max.y);
|
|
1487
|
+
}
|
|
1488
|
+
}
|
|
1489
|
+
// A rig with no morph targets still renders a perfectly good rest pose, so
|
|
1490
|
+
// the failure mode of losing them is a face that simply never moves — which
|
|
1491
|
+
// a still frame cannot show. Say so, loudly enough that the Gate B capture
|
|
1492
|
+
// (which fails on any `[avatar]` console error) catches it.
|
|
1493
|
+
if (!morphed.length) console.error("[avatar] this character has no morph targets; the face will not move", url);
|
|
1494
|
+
loaded = true;
|
|
1495
|
+
if (pending) applyPose(pending);
|
|
1496
|
+
onReady?.();
|
|
1497
|
+
}, undefined, (error: unknown) => console.error("[avatar] could not load this character", url, error));
|
|
1498
|
+
|
|
1499
|
+
// The tolerance is not a fudge factor, it is the whole of what makes the cap
|
|
1500
|
+
// land on 30. rAF fires on the panel's own grid, so the elapsed time is only
|
|
1501
|
+
// ever a multiple of the refresh interval and lands *near* 33.3 ms rather than
|
|
1502
|
+
// on it: 33.33 on a 60 Hz panel, and either side of it under any timestamp
|
|
1503
|
+
// jitter. A bare `elapsed < MIN_FRAME_MS` therefore rejects the frame it wants
|
|
1504
|
+
// and waits for the next one — 50 ms, i.e. 20 fps, not 30. Four milliseconds
|
|
1505
|
+
// is under half the interval of every rate worth caring about (8.3 at 120,
|
|
1506
|
+
// 11.1 at 90, 16.7 at 60), so it can never admit two frames where one belongs,
|
|
1507
|
+
// and it puts 60, 90 and 120 Hz all on 30 fps.
|
|
1508
|
+
const GRID_TOLERANCE_MS = 4;
|
|
1509
|
+
let lastFrameMs = 0;
|
|
1510
|
+
renderer.setAnimationLoop(() => {
|
|
1511
|
+
const now = performance.now();
|
|
1512
|
+
const elapsed = now - lastFrameMs;
|
|
1513
|
+
if (elapsed < MIN_FRAME_MS - GRID_TOLERANCE_MS) return;
|
|
1514
|
+
lastFrameMs = now;
|
|
1515
|
+
// The hair lags the skull and settles behind it, which is a state and not a
|
|
1516
|
+
// function of the pose, so it advances on the clock. Clamped, because a tab
|
|
1517
|
+
// that was in the background hands back a gap and not a frame.
|
|
1518
|
+
stepHair(Math.min(elapsed / 1000, 0.1));
|
|
1519
|
+
writeHair();
|
|
1520
|
+
renderer.render(scene, camera);
|
|
1521
|
+
if (!warnedAboutRuntimeBudget &&
|
|
1522
|
+
(renderer.info.render.calls > HARD_BUDGET.drawCalls ||
|
|
1523
|
+
renderer.info.render.triangles > HARD_BUDGET.triangles)) {
|
|
1524
|
+
warnedAboutRuntimeBudget = true;
|
|
1525
|
+
console.warn("[avatar] runtime budget exceeded", {
|
|
1526
|
+
calls: renderer.info.render.calls,
|
|
1527
|
+
triangles: renderer.info.render.triangles,
|
|
1528
|
+
});
|
|
1529
|
+
}
|
|
1530
|
+
});
|
|
1531
|
+
|
|
1532
|
+
return {
|
|
1533
|
+
apply(frame: AvatarFrame) {
|
|
1534
|
+
// The mixer smooths every channel before it gets here (each has its own
|
|
1535
|
+
// τ, `docs/internal-rig.md`), so the rig eases nothing itself. Poses that
|
|
1536
|
+
// arrive before the GLB does are not queued, only remembered: the newest
|
|
1537
|
+
// one is the only one that was ever going to be shown.
|
|
1538
|
+
if (!loaded) { pending = frame.pose; return; }
|
|
1539
|
+
applyPose(frame.pose);
|
|
1540
|
+
},
|
|
1541
|
+
destroy() {
|
|
1542
|
+
if (destroyed) return;
|
|
1543
|
+
destroyed = true;
|
|
1544
|
+
observer.disconnect();
|
|
1545
|
+
renderer.setAnimationLoop(null);
|
|
1546
|
+
scene.traverse((object) => {
|
|
1547
|
+
const mesh = object as THREE.Mesh;
|
|
1548
|
+
mesh.geometry?.dispose();
|
|
1549
|
+
for (const item of Array.isArray(mesh.material) ? mesh.material : [mesh.material]) {
|
|
1550
|
+
const material = item as THREE.MeshStandardMaterial | undefined;
|
|
1551
|
+
material?.map?.dispose();
|
|
1552
|
+
material?.emissiveMap?.dispose();
|
|
1553
|
+
material?.dispose();
|
|
1554
|
+
}
|
|
1555
|
+
});
|
|
1556
|
+
// A uniform, not a material's map, so the traverse above never meets it.
|
|
1557
|
+
expression?.map.dispose();
|
|
1558
|
+
renderer.dispose();
|
|
1559
|
+
renderer.domElement.remove();
|
|
1560
|
+
},
|
|
1561
|
+
};
|
|
1562
|
+
}
|