@kodax-ai/botui 0.0.0-stage → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,206 @@
1
+ // Solid geometry for the 3D engine.
2
+ //
3
+ // Model space: x right, y down, z away from the viewer. Every solid rests on
4
+ // y = 1, is roughly 2 units tall, and shows its face toward -z.
5
+ //
6
+ // Three ways to make a solid:
7
+ // poly(points) convex polyhedron from its corner points
8
+ // lathe(profile) surface of revolution around the y axis
9
+ // extrude(outline) a flat outline given thickness, face on the front
10
+ //
11
+ // Each returns {verts, faces, at, nrm, k, foot}. `at(u, v)` maps a point of the
12
+ // face drawing (eyes) onto the surface, `nrm(u, v)` is the surface normal there,
13
+ // `k` scales the face drawing to fit, `foot` sizes the ground shadow.
14
+
15
+ export const TAU = Math.PI * 2;
16
+ export const dot = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
17
+ const sub = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
18
+ const cross = (a, b) => [a[1] * b[2] - a[2] * b[1], a[2] * b[0] - a[0] * b[2], a[0] * b[1] - a[1] * b[0]];
19
+ const unit = a => { const l = Math.hypot(a[0], a[1], a[2]) || 1; return [a[0] / l, a[1] / l, a[2] / l]; };
20
+ const range = (n, from = 0) => Array.from({length: n}, (_, i) => from + i);
21
+ const SKIN = 0.02; // how far the face drawing floats above the surface
22
+
23
+ // A face is a loop of vertex indices. `hint` is any vector on the outside.
24
+ // Faces on a higher `layer` are painted after all faces on lower layers.
25
+ function makeFace(verts, idx, hint, layer = 0) {
26
+ const n = [0, 0, 0], c = [0, 0, 0];
27
+ idx.forEach((m, i) => {
28
+ const a = verts[m], b = verts[idx[(i + 1) % idx.length]];
29
+ n[0] += (a[1] - b[1]) * (a[2] + b[2]); n[1] += (a[2] - b[2]) * (a[0] + b[0]); n[2] += (a[0] - b[0]) * (a[1] + b[1]);
30
+ c[0] += a[0] / idx.length; c[1] += a[1] / idx.length; c[2] += a[2] / idx.length;
31
+ });
32
+ const out = unit(n);
33
+ return {idx, c, layer, n: dot(out, hint) < 0 ? out.map(x => -x) : out};
34
+ }
35
+
36
+ function finish(verts, faces, anchor, k) {
37
+ const low = verts.filter(v => v[1] > 0.7).map(v => Math.abs(v[0]));
38
+ return {verts, faces, k, at: anchor.at, nrm: anchor.nrm, foot: Math.min(1.15, Math.max(0.35, ...low))};
39
+ }
40
+
41
+ // The face drawing lies on a plane through `c` with normal `n` (n has no x part).
42
+ function flatAnchor(c, n) {
43
+ const down = [0, -n[2], n[1]];
44
+ return {
45
+ at: (u, v) => [c[0] + u + n[0] * SKIN, c[1] + v * down[1] + n[1] * SKIN, c[2] + v * down[2] + n[2] * SKIN],
46
+ nrm: () => n,
47
+ };
48
+ }
49
+
50
+ // Convex polyhedron. Every plane through three corners that keeps all other
51
+ // corners on one side is a face. The face drawing goes on the face that points
52
+ // most directly at the viewer; `drop` slides it down that face.
53
+ function poly(points, {k = 1, drop = 0} = {}) {
54
+ const faces = [], count = points.length;
55
+ for (let i = 0; i < count; i++) for (let j = i + 1; j < count; j++) for (let m = j + 1; m < count; m++) {
56
+ let n = cross(sub(points[j], points[i]), sub(points[m], points[i]));
57
+ if (Math.hypot(...n) < 1e-9) continue;
58
+ n = unit(n);
59
+ let d = dot(n, points[i]), above = 0, below = 0;
60
+ for (const p of points) { const side = dot(n, p) - d; if (side > 1e-6) above++; else if (side < -1e-6) below++; }
61
+ if (above && below) continue;
62
+ if (above) { n = n.map(x => -x); d = -d; }
63
+ if (faces.some(f => dot(f.n, n) > 0.99999)) continue;
64
+ const idx = range(count).filter(q => Math.abs(dot(n, points[q]) - d) < 1e-6);
65
+ const c = idx.reduce((s, q) => s.map((x, axis) => x + points[q][axis] / idx.length), [0, 0, 0]);
66
+ const a = unit(sub(points[idx[0]], c)), b = cross(n, a);
67
+ const angle = q => Math.atan2(dot(sub(points[q], c), b), dot(sub(points[q], c), a));
68
+ faces.push(makeFace(points, idx.sort((p, q) => angle(p) - angle(q)), n));
69
+ }
70
+ const front = faces.filter(f => Math.abs(f.n[0]) < 1e-6 && f.n[1] < 1e-6).sort((a, b) => a.n[2] - b.n[2])[0];
71
+ const c = [front.c[0], front.c[1] - drop * front.n[2], front.c[2] + drop * front.n[1]];
72
+ return finish(points, faces, flatAnchor(c, front.n), k);
73
+ }
74
+
75
+ // Surface of revolution. `profile` lists [radius, y] from top to bottom; a
76
+ // radius of 0 is a point on the axis. The face drawing wraps around the surface
77
+ // at height `eyeY`.
78
+ function lathe(profile, seg, {k = 1, eyeY = 0} = {}) {
79
+ const verts = [], rows = [];
80
+ for (const [r, y] of profile) {
81
+ if (r < 1e-6) { rows.push([verts.length]); verts.push([0, y, 0]); continue; }
82
+ rows.push(range(seg, verts.length));
83
+ for (let j = 0; j < seg; j++) { const a = j * TAU / seg; verts.push([r * Math.sin(a), y, -r * Math.cos(a)]); }
84
+ }
85
+ const faces = [];
86
+ for (let i = 0; i + 1 < profile.length; i++) {
87
+ const A = rows[i], B = rows[i + 1], dr = profile[i + 1][0] - profile[i][0], dy = profile[i + 1][1] - profile[i][1];
88
+ if (A.length === 1 && B.length === 1) continue;
89
+ for (let j = 0; j < seg; j++) {
90
+ const j2 = (j + 1) % seg, mid = (j + 0.5) * TAU / seg;
91
+ const idx = A.length === 1 ? [A[0], B[j], B[j2]] : B.length === 1 ? [A[j], A[j2], B[0]] : [A[j], A[j2], B[j2], B[j]];
92
+ faces.push(makeFace(verts, idx, [Math.sin(mid) * dy, -dr, -Math.cos(mid) * dy]));
93
+ }
94
+ }
95
+ if (rows[0].length > 1) faces.push(makeFace(verts, rows[0], [0, -1, 0]));
96
+ if (rows[rows.length - 1].length > 1) faces.push(makeFace(verts, rows[rows.length - 1], [0, 1, 0]));
97
+
98
+ // Radius and outward slope of the profile at height y.
99
+ const sample = y => {
100
+ for (let i = 0; i + 1 < profile.length; i++) {
101
+ const [r0, y0] = profile[i], [r1, y1] = profile[i + 1];
102
+ if (y1 - y0 < 1e-6 || y < y0 || y > y1) continue;
103
+ const len = Math.hypot(y1 - y0, r1 - r0);
104
+ return [r0 + (r1 - r0) * (y - y0) / (y1 - y0), (y1 - y0) / len, -(r1 - r0) / len];
105
+ }
106
+ return [0.5, 1, 0];
107
+ };
108
+ const wrap = (u, v) => { const [r, out, up] = sample(eyeY + v), a = u / Math.max(r, 0.3); return [r, out, up, Math.sin(a), Math.cos(a)]; };
109
+ return finish(verts, faces, {
110
+ at: (u, v) => { const [r, out, up, s, c] = wrap(u, v), R = r + SKIN * out; return [R * s, eyeY + v + SKIN * up, -R * c]; },
111
+ nrm: (u, v) => { const [, out, up, s, c] = wrap(u, v); return [s * out, up, -c * out]; },
112
+ }, k);
113
+ }
114
+
115
+ // A flat outline ([x, y] points, y down, any scale) given thickness. It is
116
+ // scaled to fit, centred, and stood on y = 1. `eye` places the face drawing as
117
+ // a fraction of the outline's height, measured from the top.
118
+ function extrude(outline, {depth = 0.32, k = 1, eye = 0.5, size = 1} = {}) {
119
+ const xs = outline.map(p => p[0]), ys = outline.map(p => p[1]);
120
+ const minX = Math.min(...xs), maxX = Math.max(...xs), minY = Math.min(...ys), maxY = Math.max(...ys);
121
+ const s = size * 2 / Math.max(maxX - minX, maxY - minY);
122
+ const pts = outline.map(([x, y]) => [(x - (minX + maxX) / 2) * s, (y - maxY) * s + 1]);
123
+ const n = pts.length, top = 1 - (maxY - minY) * s;
124
+ const verts = [...pts.map(p => [p[0], p[1], -depth]), ...pts.map(p => [p[0], p[1], depth])];
125
+ const area = pts.reduce((sum, p, i) => sum + p[0] * pts[(i + 1) % n][1] - pts[(i + 1) % n][0] * p[1], 0);
126
+ const faces = pts.map((p, i) => {
127
+ const j = (i + 1) % n, dx = pts[j][0] - p[0], dy = pts[j][1] - p[1];
128
+ return makeFace(verts, [i, j, n + j, n + i], area > 0 ? [dy, -dx, 0] : [-dy, dx, 0]);
129
+ });
130
+ faces.push(makeFace(verts, range(n), [0, 0, -1], 1), makeFace(verts, range(n, n), [0, 0, 1], 1));
131
+ return finish(verts, faces, flatAnchor([0, top + eye * (1 - top), -depth], [0, 0, -1]), k);
132
+ }
133
+
134
+ // ---- helpers for describing shapes ----
135
+ const ring = (n, r, y, rot = 0) => range(n).map(i => { const a = rot + i * TAU / n; return [r * Math.sin(a), y, -r * Math.cos(a)]; });
136
+ const box = (w, top, d) => [-w, w].flatMap(x => [top, 1].flatMap(y => [-d, d].map(z => [x, y, z])));
137
+ const arc = (n, fn) => range(n + 1).map(i => fn(i / n));
138
+ const polar = (n, fn) => range(n).map(i => { const a = i * TAU / n, r = fn(a, i); return [r * Math.cos(a), r * Math.sin(a)]; });
139
+ const superellipse = (a, b, p) => th => Math.pow(Math.pow(Math.abs(Math.cos(th) / a), p) + Math.pow(Math.abs(Math.sin(th) / b), p), -1 / p);
140
+ const near = (a, b) => Math.abs(Math.atan2(Math.sin(a - b), Math.cos(a - b)));
141
+
142
+ function dodecahedron() {
143
+ const f = (1 + Math.sqrt(5)) / 2, raw = [];
144
+ for (const a of [-1, 1]) for (const b of [-1, 1]) { for (const c of [-1, 1]) raw.push([a, b, c]); raw.push([0, a / f, b * f], [a / f, b * f, 0], [a * f, 0, b / f]); }
145
+ // Stand it on a face, then turn one of the upper faces toward the viewer.
146
+ const down = unit([0, 1, f]), X = [1, 0, 0], Z = cross(X, down);
147
+ const stood = raw.map(v => [dot(v, X), dot(v, down), dot(v, Z)]), inradius = Math.max(...stood.map(v => v[1]));
148
+ const flip = poly(stood).nrm()[2] > -0.5 ? -1 : 1;
149
+ return stood.map(v => [v[0] * flip * 0.85 / inradius, v[1] * 0.85 / inradius + 0.15, v[2] * flip * 0.85 / inradius]);
150
+ }
151
+
152
+ // ---- the catalogue ----
153
+ // `d` is the level of detail: how many segments go around a curve.
154
+ const rings = d => (d >= 20 ? 10 : d >= 14 ? 7 : 5);
155
+ const DEFS = {
156
+ // Polyhedra
157
+ cube: ['立方', 'poly', () => poly(box(1, -1, 1))],
158
+ brick: ['方砖', 'poly', () => poly(box(0.74, -1.25, 0.6), {k: 0.72})],
159
+ tetra: ['四面体', 'poly', () => poly([[0, -1.15, 0], ...ring(3, 1.4, 1, TAU / 6)], {k: 0.7})],
160
+ pyramid: ['金字塔', 'poly', () => poly([[0, -1.1, 0], ...ring(4, Math.SQRT2, 1, TAU / 8)], {k: 0.7, drop: 0.05})],
161
+ prism: ['三棱柱', 'poly', () => poly([...ring(3, 1.3, -1, TAU / 6), ...ring(3, 1.3, 1, TAU / 6)], {k: 0.85})],
162
+ hexcol: ['六棱柱', 'poly', () => poly([...ring(6, 1.1, -1, TAU / 12), ...ring(6, 1.1, 1, TAU / 12)], {k: 0.7})],
163
+ wedge: ['楔形', 'poly', () => poly([-0.95, 0.95].flatMap(x => [[x, 1, -0.9], [x, 1, 0.55], [x, -1.1, 0.55]]), {k: 0.8})],
164
+ gem: ['宝石', 'poly', () => poly([[0, -1.15, 0], [0, 1, 0], ...ring(4, 0.85 * Math.SQRT2, 0.1, TAU / 8)], {k: 0.6, drop: 0.1})],
165
+ crystal: ['水晶', 'poly', () => poly([[0, -1.4, 0], ...ring(6, 0.82, -0.8, TAU / 12), ...ring(6, 0.82, 0.5, TAU / 12), [0, 1, 0]], {k: 0.5})],
166
+ dodeca: ['十二面体', 'poly', () => poly(dodecahedron(), {k: 0.6})],
167
+ // Surfaces of revolution
168
+ ball: ['球', 'lathe', d => lathe(arc(rings(d), t => [1.1 * Math.sin(Math.PI * t), -0.1 - 1.1 * Math.cos(Math.PI * t)]), d, {eyeY: -0.1})],
169
+ egg: ['蛋', 'lathe', d => lathe(arc(rings(d), t => { const c = Math.cos(Math.PI * t); return [0.86 * Math.sin(Math.PI * t) * (1 - 0.18 * c), -0.15 - 1.15 * c]; }), d, {eyeY: 0, k: 0.85})],
170
+ capsule: ['胶囊', 'lathe', d => { const R = 0.78, q = Math.ceil(rings(d) / 2); return lathe([...arc(q, t => [R * Math.sin(Math.PI * t / 2), -0.5 - R * Math.cos(Math.PI * t / 2)]), ...arc(q, t => [R * Math.cos(Math.PI * t / 2), 0.22 + R * Math.sin(Math.PI * t / 2)])], d, {eyeY: -0.2, k: 0.8}); }],
171
+ thumb: ['圆头柱', 'lathe', d => { const R = 0.85; return lathe([...arc(Math.ceil(rings(d) / 2), t => [R * Math.sin(Math.PI * t / 2), -0.3 - R * Math.cos(Math.PI * t / 2)]), [R, 1]], d, {eyeY: 0.05, k: 0.85}); }],
172
+ dome: ['圆顶', 'lathe', d => lathe(arc(Math.ceil(rings(d) * 0.7), t => [1.1 * Math.sin(Math.PI * t / 2), 1 - 1.55 * Math.cos(Math.PI * t / 2)]), d, {eyeY: 0.35, k: 0.8})],
173
+ drop: ['水滴', 'lathe', d => lathe(arc(rings(d), t => { const th = Math.PI * t; return [1.5 * Math.sin(th) * Math.sin(th / 2) ** 2, 1 - 1.2 * (1 + Math.cos(th))]; }), d, {eyeY: 0.3, k: 0.8})],
174
+ can: ['圆柱', 'lathe', d => lathe([[0.9, -1], [0.9, 1]], d, {k: 0.85})],
175
+ cone: ['圆锥', 'lathe', d => lathe([[0, -1.2], [1.05, 1]], d, {eyeY: 0.3, k: 0.7})],
176
+ cup: ['杯', 'lathe', d => lathe([[1.05, -0.85], [0.7, 1]], d, {eyeY: 0, k: 0.75})],
177
+ top: ['陀螺', 'lathe', d => lathe([[0.55, -1.05], [1, -0.85], [1, -0.2], [0, 1]], d, {eyeY: -0.52, k: 0.6})],
178
+ bell: ['铃', 'lathe', d => lathe([[0, -1.2], [0.42, -1.08], [0.68, -0.75], [0.78, -0.2], [0.86, 0.5], [1.1, 1]], d, {eyeY: 0.1, k: 0.7})],
179
+ mushroom: ['蘑菇', 'lathe', d => lathe([...arc(Math.ceil(rings(d) / 2), t => [1.15 * Math.sin(Math.PI * t / 2), -0.15 - 1.05 * Math.cos(Math.PI * t / 2)]), [0.5, -0.15], [0.6, 1]], d, {eyeY: 0.48, k: 0.55})],
180
+ pawn: ['小兵', 'lathe', d => lathe([...arc(rings(d), t => [0.58 * Math.sin(Math.PI * t * 0.86), -0.72 - 0.58 * Math.cos(Math.PI * t * 0.86)]), [0.3, -0.1], [0.95, 0.85], [0.95, 1]], d, {eyeY: -0.72, k: 0.5})],
181
+ // Outlines with thickness
182
+ coin: ['圆片', 'badge', d => extrude(polar(d * 2, () => 1), {k: 0.85})],
183
+ tile: ['方片', 'badge', d => extrude(polar(d * 2, superellipse(1, 1, 4.5)), {k: 0.9})],
184
+ heart: ['爱心', 'badge', d => extrude(range(d * 2).map(i => { const t = i * TAU / (d * 2); return [16 * Math.sin(t) ** 3, -(13 * Math.cos(t) - 5 * Math.cos(2 * t) - 2 * Math.cos(3 * t) - Math.cos(4 * t))]; }), {k: 0.75, eye: 0.4, size: 1.1})],
185
+ star: ['星星', 'badge', () => extrude(range(10).map(i => { const a = -Math.PI / 2 + i * Math.PI / 5, r = i % 2 ? 0.56 : 1.15; return [r * Math.cos(a), r * Math.sin(a)]; }), {k: 0.55, eye: 0.55, size: 1.15})],
186
+ flower: ['花', 'badge', d => extrude(polar(Math.max(36, d * 3), a => 1 + 0.2 * Math.cos(6 * (a + Math.PI / 2))), {k: 0.8, size: 1.1})],
187
+ shield: ['盾', 'badge', d => { const side = arc(d, t => { const y = -1 + 2 * t; return [y < -0.2 ? 1 : Math.pow(Math.cos((y + 0.2) / 1.2 * Math.PI / 2), 0.7), y]; }); return extrude([...side, ...side.slice(0, -1).reverse().map(([x, y]) => [-x, y])], {k: 0.8, eye: 0.36}); }],
188
+ plus: ['加号', 'badge', () => { const a = 0.42; return extrude([[-a, -1], [a, -1], [a, -a], [1, -a], [1, a], [a, a], [a, 1], [-a, 1], [-a, a], [-1, a], [-1, -a], [-a, -a]], {k: 0.75}); }],
189
+ bubble: ['气泡', 'badge', d => { const n = d * 2, body = superellipse(1.2, 0.85, 3.5), tail = Math.round(2.1 / TAU * n); return extrude(polar(n, (a, i) => i === tail ? 1.55 : body(a)), {k: 0.85, eye: 0.4, size: 1.12}); }],
190
+ ghost: ['幽灵', 'badge', d => extrude([...arc(d, t => [0.95 * Math.cos(Math.PI + Math.PI * t), -0.05 + 0.95 * Math.sin(Math.PI + Math.PI * t)]), ...arc(d, t => [0.95 - 1.9 * t, 0.88 + 0.12 * Math.cos(t * TAU * 3)])], {k: 0.85, eye: 0.42})],
191
+ cat: ['猫', 'badge', () => { const head = superellipse(1.05, 0.88, 2.6); return extrude(polar(48, a => head(a) * (1 + 0.6 * Math.max(0, 1 - Math.min(near(a, -Math.PI / 2 + 0.6545), near(a, -Math.PI / 2 - 0.6545)) / 0.33))), {k: 0.85, eye: 0.62, size: 1.1}); }],
192
+ house: ['小屋', 'badge', () => extrude([[0, -1.15], [1.1, -0.2], [0.85, -0.2], [0.85, 1], [-0.85, 1], [-0.85, -0.2], [-1.1, -0.2]], {k: 0.7, eye: 0.66, size: 1.08})],
193
+ blob: ['软团', 'badge', d => extrude(polar(Math.max(30, d * 2), a => 1 + 0.1 * Math.sin(3 * a + 1) + 0.06 * Math.sin(5 * a + 2.3) + 0.05 * Math.cos(2 * a)), {k: 0.85, eye: 0.48})],
194
+ gear: ['齿轮', 'badge', () => extrude(polar(48, a => Math.cos(8 * a) > 0 ? 1.08 : 0.86), {k: 0.75})],
195
+ };
196
+
197
+ export const SHAPES = Object.entries(DEFS).map(([name, [zh, group]]) => ({name, zh, group}));
198
+ export const GROUPS = {poly: '多面体', lathe: '旋转体', badge: '厚片'};
199
+
200
+ const cache = new Map();
201
+ /** Geometry for a named solid. `detail` is the number of segments around a curve. */
202
+ export function solid(name, detail = 14) {
203
+ const def = DEFS[name] || DEFS.cube, key = (DEFS[name] ? name : 'cube') + '/' + (def[1] === 'poly' ? 0 : detail);
204
+ if (!cache.has(key)) cache.set(key, def[2](detail));
205
+ return cache.get(key);
206
+ }
@@ -0,0 +1,28 @@
1
+ // BotUI solids theme: shaded 3D solids with eyes.
2
+ //
3
+ // import {createAvatar} from './themes/solids/index.js';
4
+ // const bot = createAvatar({identity: 'research-bot', size: 36});
5
+ // sidebar.append(bot.el);
6
+ // bot.setState('thinking');
7
+
8
+ import {SolidAvatar, COLORS} from './avatar.js';
9
+ import {STATES} from '../../core/states.js';
10
+ import {GESTURES} from './motion.js';
11
+ import {SHAPES} from './geometry.js';
12
+ import {EYE_STYLES, EXPRESSIONS} from './face.js';
13
+
14
+ export {SolidAvatar, COLORS, BIRTH_COLORS, HUES, identityFrom} from './avatar.js';
15
+ export {STATES, STATE_GROUPS, ALIASES, resolveState} from '../../core/states.js';
16
+ export {setAttention} from '../../core/core.js';
17
+ export {GESTURES} from './motion.js';
18
+ export {SHAPES, GROUPS} from './geometry.js';
19
+ export {EYE_STYLES, EXPRESSIONS} from './face.js';
20
+
21
+ /** Create an avatar. See SolidAvatar for the options. Append `.el` to the page. */
22
+ export function createAvatar(options) { return new SolidAvatar(options); }
23
+
24
+ /** This theme, in the form core/registry.js expects. */
25
+ export const theme = {
26
+ name: 'solids', zh: '立体家族', create: createAvatar,
27
+ catalog: () => ({shapes: SHAPES, colors: Object.keys(COLORS), eyes: Object.keys(EYE_STYLES), states: Object.keys(STATES), gestures: Object.keys(GESTURES), expressions: Object.keys(EXPRESSIONS)}),
28
+ };
@@ -0,0 +1,114 @@
1
+ // How the solids act out each state, and the one-shot gestures.
2
+ //
3
+ // A pose is `(avatar, t, look) => targets`, returning only what differs from
4
+ // REST. `look` is where the avatar's attention is, each axis about -1..1.
5
+ // Targets: yaw, pitch, roll (radians); sy (vertical stretch); lift (down is
6
+ // positive); scale; gx, gy (eyes sliding on the face); expr; sat; blush;
7
+ // rate and stiff (how fast it gets there); orbit (speed of circling dots);
8
+ // mark (a glyph beside the head); ring ('progress' or 'spin'); bars (sound
9
+ // bars); still (no blinking).
10
+
11
+ import {TAU, smooth, bump} from '../../core/core.js';
12
+
13
+ // The resting turn and tilt every pose is built on. A page may change it to try other angles.
14
+ export const BASE = {yaw: 0.3, pitch: -0.26};
15
+ export const REST = {get yaw() { return BASE.yaw; }, get pitch() { return BASE.pitch; }, roll: 0, sy: 1, lift: 0, scale: 1, gx: 0, gy: 0, expr: 'neutral', sat: 1, blush: 0, rate: 9, stiff: 260, orbit: 0, mark: null, ring: null, bars: false, still: false};
16
+
17
+ const sin = Math.sin;
18
+ const beat = (t, f) => Math.abs(sin(t * f));
19
+ const talk = t => Math.abs(sin(t * 4.2) * Math.cos(t * 1.7));
20
+ const float = t => 2 * sin(t * 1.7);
21
+ // Positive yaw turns the face to the viewer's left and negative pitch tilts it up, so a look to the right or down subtracts yaw and adds pitch.
22
+ // The rest pose already tilts up, so looking up gets less travel than looking down.
23
+ const facing = (l, yaw = BASE.yaw, pitch = BASE.pitch) => ({yaw: yaw - l.x * 0.9, pitch: pitch + l.y * (l.y < 0 ? 0.4 : 0.6), gx: l.x * 0.16, gy: l.y * 0.16});
24
+ const SHOW = ['nod', 'spin', 'wink', 'jump', 'wobble', 'tilt', 'flip', 'dance', 'glance', 'stretch'];
25
+ const PLAY = ['wink', 'wobble', 'spin', 'dance'];
26
+
27
+ export const POSES = {
28
+ // ---- the working day ----
29
+ idle: {pose: (a, t, l) => ({...facing(l), lift: float(t)})},
30
+ listening: {enter: a => a.hop(60), pose: (a, t, l) => ({...facing(l, 0, -0.4), scale: 1.06, roll: 0.07 * sin(t * 0.9), lift: 1.5 * sin(t * 2.4), expr: 'wide'})},
31
+ thinking: {pose: (a, t) => ({yaw: 0.75 * sin(t * 1.15), pitch: 0.2 + 0.08 * sin(t * 2.3), roll: 0.1 * sin(t * 0.8), gx: 0.16 * sin(t * 1.15), gy: -0.2, lift: float(t), rate: 6, orbit: 1})},
32
+ streaming: {pose: (a, t) => ({yaw: 0.25 + 0.3 * sin(t * 1.3), sy: 0.8 + 0.42 * talk(t), lift: float(t), rate: 20, stiff: 700, mark: 'dots'})},
33
+ searching: {pose: (a, t) => { if (a.every(1.1)) a.pulse(); return {yaw: 0.9 * sin(t * 1.6), pitch: -0.2 + 0.12 * sin(t * 0.9), gx: 0.14 * sin(t * 1.6), gy: 0.05 * sin(t * 2.7), lift: float(t), expr: 'inspecting', mark: 'search', rate: 7}; }},
34
+ reading: {pose: (a, t) => { const u = (t * 0.9) % 1, line = Math.floor(t * 0.9) % 3; return {yaw: 0.5 * (u - 0.5), pitch: -0.44 + 0.06 * line, gx: 0.2 * (u - 0.5), gy: 0.1 + 0.04 * line, lift: float(t) * 0.5, rate: 10}; }},
35
+ working: {pose: (a, t) => ({yaw: BASE.yaw + 0.14 * sin(t * 2.2), pitch: -0.5, roll: 0.07 * sin(t * 7), lift: -2.5 * beat(t, 7), gx: 0.14 * sin(t * 3.1), gy: 0.12, expr: 'focus'})},
36
+ coding: {pose: (a, t) => ({yaw: BASE.yaw + 0.1 * sin(t * 1.9), pitch: -0.45, lift: -1.8 * beat(t, 9), sy: 1 - 0.04 * beat(t, 9), gx: 0.1 * sin(t * 2.3), gy: 0.12, expr: 'focus', mark: 'code'})},
37
+ running: {pose: (a, t) => { if (a.every(0.26)) a.emit('dust'); return {yaw: 0.55, pitch: -0.2, roll: 0.14 + 0.05 * sin(t * 14), lift: -7 * beat(t, 7), sy: 1 + 0.08 * sin(t * 14), gx: 0.12, expr: 'focus', rate: 12}; }},
38
+ computing: {pose: (a, t) => ({yaw: BASE.yaw + 0.25 * sin(t * 2), pitch: -0.05, roll: 0.05 * sin(t * 3.3), gy: -0.16, gx: 0.1 * sin(t * 4), lift: float(t), expr: 'focus', orbit: 2.3})},
39
+ controlling: {pose: (a, t) => { if (a.every(0.77)) a.nudge(); const k = Math.floor(t * 1.3), x = sin(k * 12.9898) * 0.9, y = sin(k * 78.233) * 0.6; return {yaw: BASE.yaw + x * 0.7, pitch: -0.26 - y * 0.4, gx: x * 0.15, gy: y * 0.12, lift: float(t), expr: 'focus', mark: 'cursor', rate: 14}; }},
40
+ asking: {enter: a => a.play('tilt'), pose: (a, t, l) => { if (a.every(3.2)) a.hop(70); return {...facing(l, 0.1, -0.3), roll: 0.2 * Math.sign(sin(t * 1.1)), lift: float(t), expr: 'wide', mark: 'ask'}; }},
41
+ blocked: {pose: () => ({yaw: 0.3, pitch: -0.22, sy: 0.55, lift: 12, expr: 'closed', sat: 0.25, rate: 5, stiff: 120})},
42
+ finished: {enter: a => { a.play('spin'); a.hop(150); a.burst('spark', 8); }, pose: (a, t) => ({yaw: 0.3, pitch: -0.3, lift: float(t), expr: 'happy'})},
43
+ error: {enter: a => { a.play('shake'); a.express('cross', 1300); }, pose: () => ({yaw: 0.15, pitch: -0.2, roll: 0.12, sy: 0.9, lift: 5, gy: 0.1, expr: 'sad', sat: 0.6})},
44
+ sleeping: {pose: (a, t) => { if (a.every(1.3)) a.emit('z'); return {yaw: 0.35, pitch: -0.42, roll: 0.14, sy: 0.88 + 0.05 * sin(t * 1.2), lift: 8, expr: 'sleep', sat: 0.85, rate: 3, stiff: 60}; }},
45
+ waking: {enter: a => { a.express('sleepy', 1100); a.play('stretch'); }, pose: (a, t, l) => ({...facing(l), lift: float(t), expr: 'wide'})},
46
+
47
+ // ---- moods ----
48
+ happy: {pose: (a, t, l) => ({...facing(l), lift: -3 * beat(t, 3.2), roll: 0.08 * sin(t * 3.2), expr: 'happy', blush: 0.5})},
49
+ excited: {enter: a => a.hop(120), pose: (a, t, l) => { if (a.every(1.6)) a.burst('spark', 4); return {...facing(l, 0.2, -0.32), lift: -7 * beat(t, 6), sy: 1 + 0.07 * sin(t * 12), scale: 1.06, expr: 'sparkle', blush: 0.4}; }},
50
+ laughing: {pose: (a, t) => ({yaw: BASE.yaw, pitch: -0.5, lift: -2.5 * beat(t, 11), sy: 1 + 0.07 * sin(t * 22), roll: 0.06 * sin(t * 5), expr: 'happy', blush: 0.6, stiff: 700, rate: 14})},
51
+ proud: {pose: (a, t) => { if (a.every(2.2)) a.burst('spark', 3); return {yaw: 0.35, pitch: -0.55, sy: 1.07, scale: 1.06, lift: float(t) * 0.5, expr: 'calm', blush: 0.3}; }},
52
+ curious: {pose: (a, t, l) => ({...facing(l, 0.1, -0.3), roll: 0.22 * Math.sign(sin(t * 0.8)), scale: 1.07, lift: float(t), expr: 'inspecting'})},
53
+ playful: {pose: (a, t, l) => { if (a.every(2.4)) a.play(PLAY[a.beat++ % PLAY.length]); return {...facing(l), roll: 0.16 * sin(t * 2.6), lift: -4 * beat(t, 2.6), expr: 'happy'}; }},
54
+ shy: {pose: (a, t) => ({yaw: 0.75, pitch: 0.05, roll: -0.08, scale: 0.94, gx: 0.12, gy: 0.14, lift: 3 + float(t) * 0.4, expr: 'shy', blush: 1, rate: 5})},
55
+ surprised: {enter: a => a.play('startle'), pose: (a, t, l) => ({...facing(l, 0.2, -0.4), scale: 1.08, sy: 1.06, lift: -2, expr: 'surprised'})},
56
+ suspicious: {pose: (a, t) => ({yaw: 0.6, pitch: -0.2, roll: -0.1, gx: -0.16, gy: 0.02 * sin(t), lift: float(t) * 0.5, expr: 'skeptic', rate: 4})},
57
+ angry: {pose: (a, t, l) => { if (a.every(1.5)) a.squash(2); return {...facing(l, 0.15, -0.2), roll: 0.035 * sin(t * 38), sy: 0.94, scale: 1.04, lift: 2, expr: 'angry', mark: 'anger', rate: 12}; }},
58
+ confused: {pose: (a, t) => ({yaw: 0.5 * sin(t * 0.9), pitch: -0.2, roll: 0.2 * sin(t * 0.9 + 1), gx: 0.1 * sin(t * 1.8), lift: float(t), expr: 'confused', mark: 'ask'})},
59
+ sad: {pose: a => { if (a.every(2.4)) a.emit('tear'); return {yaw: 0.15, pitch: 0.12, sy: 0.92, lift: 6, gy: 0.14, roll: -0.06, expr: 'sad', sat: 0.7, rate: 4, stiff: 120}; }},
60
+ drowsy: {pose: (a, t) => { const u = (t * 0.35) % 1, nod = u < 0.8 ? smooth(u / 0.8) : 1 - smooth((u - 0.8) / 0.2); return {yaw: 0.25, pitch: -0.3 + 0.5 * nod, roll: 0.12 * nod, lift: 3 + 4 * nod, expr: 'sleepy', sat: 0.9, rate: 8}; }},
61
+ bored: {pose: (a, t) => { if (a.every(4)) a.squash(1.5); return {yaw: 0.5, pitch: -0.15, roll: 0.2, sy: 0.94 + 0.03 * sin(t * 0.8), lift: 5, gx: 0.15 * sin(t * 0.4), gy: -0.1, expr: 'sleepy', rate: 3, stiff: 80}; }},
62
+ scared: {pose: (a, t) => ({yaw: -0.3, pitch: -0.38, roll: 0.03 * sin(t * 45), sy: 0.9, scale: 0.92, lift: 3, gx: 0.1 * sin(t * 7), gy: -0.05, expr: 'surprised', sat: 0.8, mark: 'sweat', rate: 14})},
63
+ celebrating: {enter: a => { a.play('jump'); a.burst('confetti', 14); }, pose: (a, t) => { if (a.every(1.15)) { a.play(a.beat++ % 2 ? 'jump' : 'spin'); a.burst('confetti', 10); } return {yaw: 0.25, pitch: -0.3, lift: float(t), expr: 'sparkle'}; }},
64
+ approving: {enter: a => a.play('nod'), pose: (a, t, l) => { if (a.every(1.7)) a.play('nod'); return {...facing(l), lift: float(t), expr: 'happy', mark: 'check'}; }},
65
+ loving: {pose: (a, t, l) => { if (a.every(0.9)) a.emit('heart'); return {...facing(l, 0.15, -0.3), roll: 0.1 * sin(t * 2), lift: -2 * beat(t, 2), scale: 1.04, expr: 'love', blush: 0.8}; }},
66
+
67
+ // ---- signals: what is going on, shown with something around the body ----
68
+ orbit: {pose: (a, t) => ({yaw: BASE.yaw + 0.3 * sin(t * 1.3), gx: 0.14 * Math.cos(t * 2.6), gy: 0.08 * sin(t * 2.6), lift: float(t), orbit: 1.6})},
69
+ radar: {pose: (a, t) => { if (a.every(0.9)) a.pulse(); return {yaw: 1.0 * sin(t * 1.1), pitch: -0.3, lift: float(t), expr: 'wide', rate: 8}; }},
70
+ progress: {pose: (a, t, l) => ({...facing(l, 0.2, -0.3), lift: float(t) * 0.5, expr: 'focus', ring: 'progress'})},
71
+ loading: {pose: (a, t) => ({yaw: BASE.yaw + 0.15 * sin(t * 2), lift: float(t), gy: -0.08, ring: 'spin'})},
72
+ spawning: {enter: a => { a.p.scale = 0.15; a.vel.scale = 0; for (let i = 0; i < 8; i++) a.emit('in'); }, pose: (a, t) => { if (a.every(0.2) && a.since < 2.4) a.emit('in'); return {scale: Math.min(1, 0.3 + a.since * 0.32), yaw: BASE.yaw + Math.max(0, 2.4 - a.since) * 1.2, lift: float(t), expr: a.since < 1.6 ? 'closed' : 'wide', rate: 12}; }},
73
+ dictating: {pose: (a, t) => ({yaw: BASE.yaw + 0.2 * sin(t * 1.1), sy: 0.92 + 0.2 * talk(t), lift: float(t), expr: 'calm', bars: true, rate: 20, stiff: 700})},
74
+ sending: {pose: (a, t) => { if (a.every(0.2)) a.emit('out'); return {yaw: 0.45, pitch: -0.45, lift: float(t) - 2, gx: 0.1, gy: -0.15, expr: 'focus'}; }},
75
+ receiving: {pose: (a, t) => { if (a.every(0.13)) a.emit('in'); return {yaw: BASE.yaw + 0.1 * sin(t * 2), pitch: -0.35, scale: 1 + 0.03 * sin(t * 9), lift: float(t), expr: 'wide'}; }},
76
+ uploading: {pose: (a, t) => { if (a.every(0.28)) a.emit('up'); return {yaw: BASE.yaw, pitch: -0.5, gy: -0.18, sy: 1.05, lift: -2 + float(t), mark: 'up'}; }},
77
+ writing: {pose: (a, t) => { const u = (t * 0.7) % 1; return {yaw: -0.15 + 0.4 * u, pitch: -0.5, gx: 0.18 * (u - 0.5), gy: 0.15, lift: -1.2 * beat(t, 8), expr: 'focus', mark: 'pen'}; }},
78
+ alerting: {enter: a => a.play('startle'), pose: (a, t, l) => { if (a.every(1.3)) a.play('startle'); return {...facing(l, 0.2, -0.3), scale: 1.05, lift: float(t), expr: 'wide', mark: 'bang'}; }},
79
+ bouncing: {enter: a => a.play('jump'), pose: (a, t, l) => { if (a.every(0.85)) a.play('jump'); return {...facing(l), expr: 'happy'}; }},
80
+ 'powering-down': {pose: () => ({yaw: 0.3, pitch: 0.1, sy: 0.7, scale: 0.82, lift: 14, expr: 'closed', sat: 0.12, rate: 2, stiff: 40, still: true})},
81
+
82
+ // ---- product moments ----
83
+ humming: {pose: (a, t) => { if (a.every(0.6)) a.emit('note'); return {yaw: BASE.yaw + 0.25 * sin(t * 1.5), roll: 0.14 * sin(t * 3), lift: -2 * beat(t, 3), expr: 'calm', blush: 0.3}; }},
84
+ notifying: {enter: a => a.hop(110), pose: (a, t, l) => { if (a.every(2)) a.hop(110); return {...facing(l), scale: 1.03, lift: float(t), expr: 'wide', mark: 'badge'}; }},
85
+ dragging: {pose: (a, t) => ({yaw: BASE.yaw + 0.3 * sin(t * 2.3), pitch: -0.1, roll: 0.25 * sin(t * 3.1), sy: 1.16, lift: -8, expr: 'surprised', stiff: 120})},
86
+ greeting: {enter: a => { a.hop(120); a.play('wave'); }, pose: (a, t, l) => { if (a.every(2.6)) a.play('wave'); return {...facing(l, 0.1, -0.3), lift: float(t), scale: 1.04, expr: 'happy'}; }},
87
+
88
+ // ---- showing it off ----
89
+ logo: {pose: () => ({yaw: 0.5, pitch: -0.32, rate: 6, still: true})},
90
+ showcase: {pose: (a, t, l) => { if (a.every(1.8)) a.play(SHOW[a.beat++ % SHOW.length]); return {...facing(l), lift: float(t)}; }},
91
+ turntable: {pose: (a, t) => ({yaw: t * 1.3, pitch: -0.3, rate: 30})},
92
+ };
93
+
94
+ // `move(u, g)` adds this gesture's offsets at progress u (0..1) to g.
95
+ export const GESTURES = {
96
+ nod: {zh: '点头', dur: 0.7, move: (u, g) => { g.pitch -= 0.38 * sin(u * TAU * 2) * (1 - u); }},
97
+ shake: {zh: '摇头', dur: 0.75, move: (u, g) => { g.yaw += 0.55 * sin(u * TAU * 2.5) * (1 - u); }},
98
+ tilt: {zh: '歪头', dur: 1.3, move: (u, g) => { g.roll += 0.32 * smooth(Math.min(1, u * 4)) * smooth(Math.min(1, (1 - u) * 4)); }},
99
+ glance: {zh: '张望', dur: 1.6, move: (u, g) => { g.yaw += 0.8 * sin(u * TAU) * Math.sqrt(bump(u)); g.gx += 0.12 * sin(u * TAU); }},
100
+ wink: {zh: '眨眼', dur: 0.6, expr: 'wink', move: (u, g) => { g.roll += 0.12 * bump(u); }},
101
+ wave: {zh: '招手', dur: 1.1, expr: 'happy', move: (u, g) => { g.roll += 0.3 * sin(u * TAU * 3) * Math.sqrt(bump(u)); g.lift -= 4 * bump(u); }},
102
+ bow: {zh: '鞠躬', dur: 1.2, expr: 'closed', move: (u, g) => { g.pitch += 0.75 * bump(u) ** 2; g.sy -= 0.08 * bump(u); }},
103
+ spin: {zh: '转圈', dur: 0.75, move: (u, g) => { g.yaw += TAU * smooth(u); g.sy += 0.1 * bump(u); }},
104
+ flip: {zh: '空翻', dur: 0.85, expr: 'happy', move: (u, g) => { g.pitch -= TAU * smooth(u); g.lift -= 13 * bump(u); }, end: a => a.squash(3)},
105
+ roll: {zh: '侧翻', dur: 0.9, expr: 'happy', move: (u, g) => { g.roll += TAU * smooth(u); g.lift -= 9 * bump(u); }, end: a => a.squash(2)},
106
+ jump: {zh: '跳', dur: 0.8, expr: 'happy', move: (u, g) => { if (u < 0.22) { g.sy -= 0.28 * bump(u / 0.22); return; } const v = (u - 0.22) / 0.78; g.lift -= 72 * v * (1 - v); g.sy += 0.14 * bump(v * 2); }, end: a => a.squash(4)},
107
+ bounce: {zh: '弹一下', dur: 0.5, start: a => { a.hop(170); a.squash(3); }, move: () => {}},
108
+ wobble: {zh: '果冻', dur: 0.9, start: a => a.squash(5), move: (u, g) => { g.roll += 0.16 * sin(u * TAU * 3) * (1 - u); }},
109
+ shiver: {zh: '发抖', dur: 0.9, expr: 'squint', move: (u, g) => { g.roll += 0.05 * sin(u * 60) * bump(u); g.sy -= 0.06 * bump(u); }},
110
+ startle: {zh: '一惊', dur: 0.5, expr: 'surprised', move: (u, g) => { g.lift -= 9 * (1 - u) ** 2; g.scale += 0.14 * (1 - u); }},
111
+ stretch: {zh: '伸懒腰', dur: 1.2, expr: 'closed', move: (u, g) => { g.sy += 0.36 * bump(u) ** 2 - 0.12 * bump(Math.min(1, u * 5)); g.pitch += 0.25 * bump(u); g.roll += 0.08 * sin(u * TAU); }},
112
+ dance: {zh: '跳舞', dur: 2.4, expr: 'happy', move: (u, g) => { const e = Math.sqrt(bump(u)); g.roll += 0.22 * sin(u * TAU * 4) * e; g.lift -= 6 * Math.abs(sin(u * TAU * 4)) * e; g.yaw += 0.5 * sin(u * TAU * 2) * e; }},
113
+ };
114
+ export const FIDGETS = ['tilt', 'glance', 'wink', 'stretch', 'nod'];
@@ -0,0 +1,37 @@
1
+ // What the body turns into for a signal state when `signals: 'morph'` is set.
2
+ //
3
+ // Each entry is (t, progress) -> {d, w, dots, scale, fade}. `d` is a path that
4
+ // is stroked `w` wide with round caps, `dots` are filled circles [x, y, r].
5
+ // Coordinates are centred on the avatar and stay within about 26 units.
6
+
7
+ const TAU = Math.PI * 2, {sin, cos, abs, max} = Math;
8
+ const f = v => Math.round(v * 10) / 10;
9
+
10
+ export const SYMBOLS = {
11
+ // A core with two satellites.
12
+ orbit: t => ({dots: [[0, 0, 8], ...[0, 1].map(i => { const a = t * 2.6 + i * Math.PI; return [22 * cos(a), 9 * sin(a), 4.5 - 1.5 * sin(a)]; })]}),
13
+ // A dish outline with a sweeping hand.
14
+ radar: t => { const a = t * 2.4; return {d: `M-24,0A24,24 0 1,1 24,0A24,24 0 1,1 -24,0M0,0L${f(20 * cos(a))},${f(20 * sin(a))}`, w: 4.5, dots: [[0, 0, 4.5]]}; },
15
+ // A bar that fills; the unfilled part is a row of small dots.
16
+ progress: (t, p) => { const u = p ?? (t * 0.22) % 1, x = -24 + 48 * u; return {d: `M-24,0H${f(x)}`, w: 11, dots: [-12, 0, 12, 24].filter(v => v > x + 5).map(v => [v, 0, 2.6])}; },
17
+ // Three dots taking turns to hop.
18
+ loading: t => ({dots: [0, 1, 2].map(i => [-17 + i * 17, -11 * max(0, sin(t * 6 - i * 0.9)), 6.5])}),
19
+ // Dots gathering into a growing core.
20
+ spawning: t => { const u = (t * 0.8) % 1; return {dots: [[0, 0, 4 + 7 * u], ...[0, 1, 2, 3, 4, 5].map(i => { const a = i * TAU / 6 + t * 0.6; return [24 * (1 - u) * cos(a), 24 * (1 - u) * sin(a), 4.5 * (1 - u * 0.6)]; })]}; },
21
+ // A voice waveform.
22
+ dictating: t => ({d: [0, 1, 2, 3, 4].map(i => { const h = 4 + 18 * abs(sin(t * 6 + i * 1.3) * cos(t * 2.1 + i)); return `M${-22 + i * 11},${f(-h)}V${f(h)}`; }).join(''), w: 6.5}),
23
+ // An arrow leaving up and to the right, with a trail.
24
+ sending: t => { const k = 4 * sin(t * 5); return {d: `M${f(-12 + k)},${f(12 - k)}L${f(16 + k)},${f(-16 - k)}M${f(-2 + k)},${f(-16 - k)}H${f(16 + k)}V${f(2 - k)}`, w: 7, dots: [[-21, 21, 3.4 + 1.2 * sin(t * 5)]]}; },
25
+ // An arrow dropping into a tray.
26
+ receiving: t => { const y = 4 * sin(t * 5); return {d: `M0,${f(-24 + y)}V${f(2 + y)}M-10,${f(-8 + y)}L0,${f(2 + y)}L10,${f(-8 + y)}M-21,10V21H21V10`, w: 6.5}; },
27
+ // An arrow rising to a line.
28
+ uploading: t => { const y = 4 * sin(t * 5); return {d: `M-19,-23H19M0,${f(24 + y)}V${f(-8 + y)}M-11,${f(3 + y)}L0,${f(-8 + y)}L11,${f(3 + y)}`, w: 6.5}; },
29
+ // A line of handwriting drawn from left to right.
30
+ writing: t => { const u = (t * 0.6) % 1, n = Math.max(1, Math.round(24 * u)), at = i => [-24 + 2 * i, 7 * sin(i * 0.8)]; let d = ''; for (let i = 0; i <= n; i++) d += (i ? 'L' : 'M') + f(at(i)[0]) + ',' + f(at(i)[1]); return {d, w: 5, dots: [[...at(n), 6]]}; },
31
+ // An exclamation mark that throbs.
32
+ alerting: t => ({d: 'M0,-23V3', w: 10, dots: [[0, 19, 6]], scale: 1 + 0.12 * max(0, sin(t * 7))}),
33
+ // A ball bouncing on the ground.
34
+ bouncing: t => ({d: 'M-17,25H17', w: 4, dots: [[0, 12 - 30 * abs(sin(t * 4)), 9]]}),
35
+ // A standby sign, breathing slowly.
36
+ 'powering-down': t => ({d: 'M-10,-14A18,18 0 1,0 10,-14M0,-25V-6', w: 5.5, fade: 0.5 + 0.2 * sin(t * 1.6), scale: 0.8}),
37
+ };