dsh-quay-bot 1.0.2 → 1.0.4

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.
Files changed (59) hide show
  1. package/README.md +28 -105
  2. package/botscreen/requirements.txt +1 -1
  3. package/client.js +3 -2
  4. package/cordis.patch.yml +1 -1
  5. package/host/api.js +4 -0
  6. package/host/remote.js +285 -8
  7. package/host/screen.js +22 -86
  8. package/package.json +35 -32
  9. package/remote/bin/dsh-remote.mjs +435 -0
  10. package/remote/package.json +27 -0
  11. package/remote/src/config.mjs +183 -0
  12. package/remote/src/crypto.mjs +220 -0
  13. package/remote/src/keys.mjs +226 -0
  14. package/remote/src/proxy.mjs +1028 -0
  15. package/remote/src/sessions.mjs +163 -0
  16. package/remote/src/shim.js +838 -0
  17. package/remote/src/supervisor.mjs +207 -0
  18. package/remote/src/tunnel.mjs +533 -0
  19. package/remote/src/upstream.mjs +403 -0
  20. package/remote/src/util.mjs +149 -0
  21. package/remote/src/ws.mjs +313 -0
  22. package/LICENSE +0 -21
  23. package/avatar/build.mjs +0 -81
  24. package/avatar/gallery/droid.json +0 -14
  25. package/avatar/gallery/ghost.json +0 -14
  26. package/avatar/gallery/grok-pod.json +0 -14
  27. package/avatar/gallery/guardian.json +0 -14
  28. package/avatar/gallery/mecha.json +0 -14
  29. package/avatar/gallery/mochi.json +0 -14
  30. package/avatar/gallery/neko.json +0 -14
  31. package/avatar/gallery/orbiter.json +0 -14
  32. package/avatar/gallery/relay.json +0 -14
  33. package/avatar/gallery/spark.json +0 -14
  34. package/avatar/src/bot/clay.ts +0 -135
  35. package/avatar/src/bot/effects.ts +0 -100
  36. package/avatar/src/bot/grok-bot.ts +0 -626
  37. package/avatar/src/bot/index.ts +0 -15
  38. package/avatar/src/bot/palettes.ts +0 -62
  39. package/avatar/src/bot/ring.ts +0 -219
  40. package/avatar/src/bot/scheduler.ts +0 -239
  41. package/avatar/src/bot/shapes.ts +0 -277
  42. package/avatar/src/bot/spring.ts +0 -196
  43. package/avatar/src/core/color.ts +0 -92
  44. package/avatar/src/core/eyes.ts +0 -297
  45. package/avatar/src/core/field.ts +0 -482
  46. package/avatar/src/core/frame.ts +0 -655
  47. package/avatar/src/core/fx.ts +0 -375
  48. package/avatar/src/core/index.ts +0 -49
  49. package/avatar/src/core/paint.ts +0 -331
  50. package/avatar/src/core/path.ts +0 -495
  51. package/avatar/src/core/pose.ts +0 -581
  52. package/avatar/src/core/svg.ts +0 -98
  53. package/avatar/src/core/types.ts +0 -153
  54. package/avatar/src/core/validate.ts +0 -296
  55. package/avatar/src/core/volume.ts +0 -681
  56. package/avatar/src/entry.ts +0 -260
  57. package/bin/doctor.mjs +0 -148
  58. package/botscreen/requirements-offline.txt +0 -11
  59. package/market/index.json +0 -41
@@ -1,681 +0,0 @@
1
- /**
2
- * 扁平轮廓的隐含体积。
3
- *
4
- * 定义本身始终是 2D 路径。这里把每条填充子路径变成一个“部件”,并给它一个
5
- * 前表面高度场 h(x, y)(背面关于 z = 0 镜像):
6
- *
7
- * - inflate:圆角板。高度由到轮廓的距离决定(椭圆边圆角),于是侧面是弧的,正面是平的。
8
- * - revolve-y:绕部件自身竖直中心线的旋转体(杯子、灯泡),从精确扫描线宽度得来。
9
- * - revolve-x:绕整个身体共享的水平轴旋转(哑铃:杆成圆柱,配重成圆片)。
10
- *
11
- * 由高度场把部件一次性切成两叠普通截面路径:z 切片(朝向观察者的平面,h 的等值面)
12
- * 与 x 切片(朝向侧面的平面,h 的列剖面)。单色渲染时,一叠的并集就是这个实体的剪影,
13
- * 渲染器选择更朝向观察者的那一叠,因此永远不会看到平面的“侧面”。
14
- * 这里全部是纯函数,并由渲染器缓存。
15
- */
16
- import { contourLoops, loopBox, loopsToPath, runRange, simplifyLoop, RUN, type Field, type Grid } from './field.ts';
17
- import { bounds, edgeBands, isInside, type Bounds, type Point } from './path.ts';
18
- import { EYE_H, EYE_W, GAZE_X, GAZE_Y, analyzeBody, drawnEyesFit, eyeCenters, eyeClearance, gazeRange, type BodyAnalysis, type GazeRange } from './validate.ts';
19
- import type { AvatarDefinition, AvatarEyes } from './types.ts';
20
-
21
- export type PartKind = 'inflate' | 'revolve-x' | 'revolve-y';
22
-
23
- export interface VolumeSlice {
24
- /** 'z':平面 z = at,路径坐标 (x, y);'x':平面 x = at,路径坐标 (u = -z, y)。 */
25
- axis: 'z' | 'x';
26
- at: number;
27
- d: string;
28
- loops: Point[][];
29
- evenOdd: boolean;
30
- box: { x: number; y: number; w: number; h: number };
31
- /** z 切片:该截面到轮廓的最小距离(核心与厚壁为 0)。 */
32
- inset: number;
33
- }
34
-
35
- export interface VolumePart {
36
- kind: PartKind;
37
- d: string;
38
- polys: Point[][];
39
- bounds: Bounds;
40
- center: Point;
41
- thickness: number;
42
- depth: number;
43
- rxy: number;
44
- rz: number;
45
- axis?: number;
46
- zSlices: VolumeSlice[];
47
- xSlices: VolumeSlice[];
48
- height: (x: number, y: number) => number;
49
- }
50
-
51
- export interface EyePlane {
52
- x: number;
53
- y: number;
54
- z: number;
55
- dzdx: number;
56
- dzdy: number;
57
- }
58
-
59
- export interface Volume {
60
- parts: VolumePart[];
61
- bounds: Bounds;
62
- eyePart: number;
63
- /** 每只眼睛下方最高的表面,以及凝视行程,用于贴图映射。 */
64
- eyeZ: [number, number];
65
- eyeScale: number;
66
- eyePlanes: [EyePlane, EyePlane];
67
- }
68
-
69
- export type Vec3 = [number, number, number];
70
-
71
- /** 单个膨胀部件允许的最大厚度。 */
72
- export const MAX_PART_DEPTH = 30;
73
- /** 身体有余量时,绘制眼睛最多比契约的 4×11 药丸大这么多。 */
74
- export const EYE_BOOST = 1.45;
75
- /** 放大前每只绘制眼睛与轮廓保持的间隙。 */
76
- export const EYE_BOOST_PAD = 3;
77
- /** 生成式头像的设计目标:放大后的眼睛静止时保持这么多净空。 */
78
- export const FACE_ROOM_TARGET = 4.5;
79
- const MAX_X_SLICES = 40;
80
- const THIN = 4;
81
- const THIN_REACH = 5;
82
- const THIN_MARGIN = 2;
83
- const FINE_Z = 0.6;
84
-
85
- /** 点到多边形的最近边距离。 */
86
- export function polygonEdgeDistance(polys: Point[][], x: number, y: number): number {
87
- let best = Infinity;
88
- for (const poly of polys) {
89
- for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
90
- const ax = poly[j][0];
91
- const ay = poly[j][1];
92
- const bx = poly[i][0];
93
- const by = poly[i][1];
94
- const dx = bx - ax;
95
- const dy = by - ay;
96
- const len = dx * dx + dy * dy;
97
- let t = len > 0 ? ((x - ax) * dx + (y - ay) * dy) / len : 0;
98
- t = t < 0 ? 0 : t > 1 ? 1 : t;
99
- const ex = ax + t * dx - x;
100
- const ey = ay + t * dy - y;
101
- const d = ex * ex + ey * ey;
102
- if (d < best) best = d;
103
- }
104
- }
105
- return Math.sqrt(best);
106
- }
107
-
108
- /** 到轮廓的向内距离,填充之外为 0。 */
109
- export function insideDistance(polys: Point[][], x: number, y: number): number {
110
- return isInside(polys, x, y) ? polygonEdgeDistance(polys, x, y) : 0;
111
- }
112
-
113
- /** 深度 z 处的轮廓收缩量(椭圆圆角)。 */
114
- export function erosionAt(part: Pick<VolumePart, 'depth' | 'rxy' | 'rz'>, z: number): number {
115
- const half = part.depth / 2;
116
- if (part.rz <= 0) return 0;
117
- const u = (Math.abs(z) - (half - part.rz)) / part.rz;
118
- if (u <= 0) return 0;
119
- return part.rxy * (1 - Math.sqrt(Math.max(0, 1 - Math.min(1, u) ** 2)));
120
- }
121
-
122
- /** 膨胀部件表面达到 `level` 深度时向内缩进多少(surfaceZ 的反函数)。 */
123
- export function insetAt(part: Pick<VolumePart, 'depth' | 'rxy' | 'rz'>, level: number): number {
124
- const half = part.depth / 2;
125
- if (part.rxy <= 0 || part.rz <= 0 || level <= half - part.rz) return 0;
126
- if (level >= half) return part.rxy;
127
- const s = (level - (half - part.rz)) / part.rz;
128
- return part.rxy * (1 - Math.sqrt(Math.max(0, 1 - s * s)));
129
- }
130
-
131
- /** 膨胀部件在“向内 `dist` 处”的前表面深度。 */
132
- export function surfaceZ(part: Pick<VolumePart, 'depth' | 'rxy' | 'rz'>, dist: number): number {
133
- const half = part.depth / 2;
134
- if (part.rxy <= 0 || dist >= part.rxy) return half;
135
- const k = 1 - Math.max(0, dist) / part.rxy;
136
- return half - part.rz + Math.sqrt(Math.max(0, 1 - k * k)) * part.rz;
137
- }
138
-
139
- /** 膨胀部件的深度启发式:和它一样厚,但细长部件最多给到厚度的 2.4 倍,读作圆盘。 */
140
- export function partDepth(thickness: number, b: Bounds, volume: number): number {
141
- const long = Math.max(b.maxX - b.minX, b.maxY - b.minY);
142
- const raw = 0.8 * Math.max(thickness, Math.min(0.4 * long, 2.4 * thickness));
143
- return Math.min(MAX_PART_DEPTH, Math.max(3, raw)) * volume;
144
- }
145
-
146
- /** 直线(y = s 或 x = s)与多边形的交点,升序。 */
147
- function crossings(polys: Point[][], rows: boolean, s: number): number[] {
148
- const out: number[] = [];
149
- const a = rows ? 1 : 0;
150
- const b = rows ? 0 : 1;
151
- for (const poly of polys) {
152
- for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) {
153
- const p = poly[j];
154
- const q = poly[i];
155
- if ((p[a] <= s && q[a] > s) || (q[a] <= s && p[a] > s)) out.push(p[b] + ((s - p[a]) * (q[b] - p[b])) / (q[a] - p[a]));
156
- }
157
- }
158
- return out.sort((m, n) => m - n);
159
- }
160
-
161
- interface Profile {
162
- start: number;
163
- step: number;
164
- r: Float64Array;
165
- }
166
-
167
- /**
168
- * 如果每一行(或列)都以 `center` 为中心只交一段,返回该段的一半宽度剖面;
169
- * 否则返回 null(不是旋转体)。
170
- */
171
- function revolveProfile(polys: Point[][], rows: boolean, lo: number, hi: number, center: number): Profile | null {
172
- const step = 0.2;
173
- const n = Math.max(2, Math.ceil((hi - lo) / step) + 1);
174
- const r = new Float64Array(n);
175
- let span = 0;
176
- const mids: number[] = [];
177
- for (let k = 0; k < n; k++) {
178
- const s = Math.min(hi - 0.01, Math.max(lo + 0.01, lo + k * step));
179
- const xs = crossings(polys, rows, s);
180
- if (xs.length === 0) continue;
181
- if (xs.length !== 2) return null;
182
- r[k] = (xs[1] - xs[0]) / 2;
183
- span = Math.max(span, xs[1] - xs[0]);
184
- mids.push((xs[0] + xs[1]) / 2);
185
- }
186
- const tol = Math.max(0.8, 0.04 * span);
187
- if (!mids.length || mids.some((m) => Math.abs(m - center) > tol)) return null;
188
- return { start: lo, step, r };
189
- }
190
-
191
- function radiusAt(p: Profile, s: number): number {
192
- const t = (s - p.start) / p.step;
193
- if (t < 0 || t > p.r.length - 1) return 0;
194
- const k = Math.min(p.r.length - 2, Math.floor(t));
195
- return p.r[k] + (p.r[k + 1] - p.r[k]) * (t - k);
196
- }
197
-
198
- interface Surface {
199
- kind: PartKind;
200
- fillet: { depth: number; rxy: number; rz: number };
201
- profile: Profile | null;
202
- axis: number;
203
- v: number;
204
- }
205
-
206
- /** 部件内部一点的前表面高度;`sd` 是它到轮廓的距离(仅 inflate 使用)。 */
207
- function surfaceHeight(s: Surface, x: number, y: number, sd: number): number {
208
- if (s.kind === 'inflate') return surfaceZ(s.fillet, sd);
209
- const p = s.profile as Profile;
210
- return s.kind === 'revolve-x'
211
- ? s.v * Math.sqrt(Math.max(0, radiusAt(p, x) ** 2 - (y - s.axis) ** 2))
212
- : s.v * Math.sqrt(Math.max(0, radiusAt(p, y) ** 2 - (x - s.axis) ** 2));
213
- }
214
-
215
- /**
216
- * 身体是否像“侧看的车床件”:至少两个部件、宽大于高、关于 y = axis 镜像对称且每个部件都居中于它。
217
- */
218
- export function revolveAxis(body: BodyAnalysis): number | null {
219
- const b = body.bounds;
220
- const outlines = body.subpaths.filter((s) => !s.hole);
221
- if (outlines.length < 2 || b.maxX - b.minX < 1.1 * (b.maxY - b.minY)) return null;
222
- const axis = (b.minY + b.maxY) / 2;
223
- for (const o of outlines) {
224
- const ob = bounds(o.polys);
225
- if (Math.abs((ob.minY + ob.maxY) / 2 - axis) > 1.5) return null;
226
- }
227
- let filled = 0;
228
- let off = 0;
229
- const xs: number[] = [];
230
- for (let x = b.minX + 0.5; x < b.maxX; x += 1) xs.push(x);
231
- const top = new Uint8Array(xs.length);
232
- const bottom = new Uint8Array(xs.length);
233
- const index = edgeBands(body.polys);
234
- for (let y = b.minY + 0.5; y < axis; y += 1) {
235
- index.insideRow(y, xs, top);
236
- index.insideRow(2 * axis - y, xs, bottom);
237
- for (let k = 0; k < xs.length; k++) {
238
- if (top[k] || bottom[k]) filled++;
239
- if (top[k] !== bottom[k]) off++;
240
- }
241
- }
242
- return filled > 0 && off / filled < 0.02 ? axis : null;
243
- }
244
-
245
- interface CellBox {
246
- i0: number;
247
- i1: number;
248
- j0: number;
249
- j1: number;
250
- }
251
-
252
- type LevelCut = (level: number, box?: CellBox) => Point[][];
253
-
254
- /** 膨胀部件的截面:高度随到轮廓的距离增长,所以等值面就是“向内缩进量”的等距线。 */
255
- function insetCuts(g: Grid, sd: Float64Array, fillet: Surface['fillet']): LevelCut {
256
- const whole: Field = { ...g, values: sd };
257
- const range = runRange(sd, g.nx, g.ny);
258
- const flat = new Uint8Array(range.min.length);
259
- return (level, box) => {
260
- const drop = insetAt(fillet, level);
261
- if (!box) {
262
- for (let k = 0; k < flat.length; k++) flat[k] = range.min[k] - drop >= 0 || range.max[k] - drop < 0 ? 1 : 0;
263
- return contourLoops(whole, drop, flat);
264
- }
265
- // 带框的截面在自身样本加一圈外环上求,外环的值被压到负(裁切线)。
266
- const i0 = Math.max(0, box.i0 - 1);
267
- const i1 = Math.min(g.nx - 1, box.i1 + 1);
268
- const j0 = Math.max(0, box.j0 - 1);
269
- const j1 = Math.min(g.ny - 1, box.j1 + 1);
270
- const nx = i1 - i0 + 1;
271
- const ny = j1 - j0 + 1;
272
- const values = new Float64Array(nx * ny);
273
- for (let j = 0; j < ny; j++) {
274
- for (let i = 0; i < nx; i++) {
275
- const s = sd[(j + j0) * g.nx + i + i0];
276
- const v = Math.min(s - drop, s);
277
- const clip = g.step * Math.min(i + i0 - box.i0, box.i1 - i - i0, j + j0 - box.j0, box.j1 - j - j0);
278
- values[j * nx + i] = Math.min(v, clip);
279
- }
280
- }
281
- return contourLoops({ values, nx, ny, x0: g.x0 + i0 * g.step, y0: g.y0 + j0 * g.step, step: g.step }, 0);
282
- };
283
- }
284
-
285
- /** 旋转体部件的截面:高度 ≥ level 处满足 t² ≤ r² − (level / v)²,该差值除以 2r 近似距离。 */
286
- function revolveCuts(g: Grid, sd: Float64Array, surface: Surface): LevelCut {
287
- const { nx, ny } = g;
288
- const { v, axis } = surface;
289
- const p = surface.profile as Profile;
290
- const alongX = surface.kind === 'revolve-x';
291
- const base = new Float64Array(nx * ny);
292
- const over = new Float64Array(nx * ny);
293
- for (let j = 0; j < ny; j++) {
294
- const y = g.y0 + j * g.step;
295
- for (let i = 0; i < nx; i++) {
296
- const x = g.x0 + i * g.step;
297
- const r = radiusAt(p, alongX ? x : y);
298
- const tt = alongX ? y - axis : x - axis;
299
- base[j * nx + i] = r * r - tt * tt;
300
- over[j * nx + i] = 2 * Math.max(r, 0.5);
301
- }
302
- }
303
- const values = new Float64Array(nx * ny);
304
- const rise = runRange(base, nx, ny);
305
- const edge = runRange(sd, nx, ny);
306
- const runs = Math.ceil((nx - 1) / RUN);
307
- const flat = new Uint8Array(rise.min.length);
308
- return (level) => {
309
- const drop = (level / Math.max(v, 1e-6)) ** 2;
310
- for (let k = 0; k < flat.length; k++) {
311
- const out = rise.max[k] - drop < 0 || edge.max[k] < 0;
312
- const within = rise.min[k] - drop >= 0 && edge.min[k] >= 0;
313
- flat[k] = out || within ? 1 : 0;
314
- }
315
- for (let k = 0; k < flat.length; k++) {
316
- if (flat[k]) continue;
317
- const r = k % runs;
318
- const j = (k - r) / runs;
319
- for (let q = j * nx + r * RUN, end = j * nx + Math.min(nx - 1, (r + 1) * RUN); q <= end; q++) {
320
- values[q] = Math.min((base[q] - drop) / over[q], sd[q]);
321
- values[q + nx] = Math.min((base[q + nx] - drop) / over[q + nx], sd[q + nx]);
322
- }
323
- }
324
- return contourLoops({ values, nx, ny, x0: g.x0, y0: g.y0, step: g.step }, 0, flat);
325
- };
326
- }
327
-
328
- interface ThinBox extends CellBox {
329
- top: number;
330
- }
331
-
332
- /**
333
- * 细部周围的框( deepened by THIN_MARGIN,重叠则合并),单位是网格样本。
334
- * 细部指“在 THIN_REACH 内没有半径 ≥ THIN 的内切圆”的地方:气泡尾巴、叶片、提环。
335
- */
336
- function thinBoxes(g: Grid, sd: Float64Array, h: Float64Array): ThinBox[] {
337
- const { nx, ny, step } = g;
338
- const reach = Math.ceil(THIN_REACH / step);
339
- const wide = new Uint8Array(nx * ny);
340
- const near = new Uint8Array(nx * ny);
341
- const count = new Int32Array(Math.max(nx, ny) + 1);
342
- for (let j = 0; j < ny; j++) {
343
- for (let i = 0; i < nx; i++) count[i + 1] = count[i] + (sd[j * nx + i] >= THIN ? 1 : 0);
344
- for (let i = 0; i < nx; i++) wide[j * nx + i] = count[Math.min(nx, i + reach + 1)] > count[Math.max(0, i - reach)] ? 1 : 0;
345
- }
346
- for (let i = 0; i < nx; i++) {
347
- for (let j = 0; j < ny; j++) count[j + 1] = count[j] + wide[j * nx + i];
348
- for (let j = 0; j < ny; j++) near[j * nx + i] = count[Math.min(ny, j + reach + 1)] > count[Math.max(0, j - reach)] ? 1 : 0;
349
- }
350
- const thin = (k: number) => sd[k] > 0 && !near[k];
351
- const seen = new Uint8Array(nx * ny);
352
- const grow = Math.ceil(THIN_MARGIN / step);
353
- const boxes: ThinBox[] = [];
354
- for (let start = 0; start < nx * ny; start++) {
355
- if (seen[start] || !thin(start)) continue;
356
- const box = { i0: nx, i1: -1, j0: ny, j1: -1, top: 0 };
357
- let cells = 0;
358
- const stack = [start];
359
- seen[start] = 1;
360
- while (stack.length) {
361
- const k = stack.pop() as number;
362
- const i = k % nx;
363
- const j = (k - i) / nx;
364
- cells++;
365
- box.top = Math.max(box.top, h[k]);
366
- box.i0 = Math.min(box.i0, i);
367
- box.i1 = Math.max(box.i1, i);
368
- box.j0 = Math.min(box.j0, j);
369
- box.j1 = Math.max(box.j1, j);
370
- for (const n of [i > 0 ? k - 1 : -1, i < nx - 1 ? k + 1 : -1, j > 0 ? k - nx : -1, j < ny - 1 ? k + nx : -1]) {
371
- if (n >= 0 && !seen[n] && thin(n)) {
372
- seen[n] = 1;
373
- stack.push(n);
374
- }
375
- }
376
- }
377
- if (cells * step * step < 2) continue;
378
- boxes.push({
379
- i0: Math.max(0, box.i0 - grow),
380
- i1: Math.min(nx - 1, box.i1 + grow),
381
- j0: Math.max(0, box.j0 - grow),
382
- j1: Math.min(ny - 1, box.j1 + grow),
383
- top: box.top,
384
- });
385
- }
386
- for (let a = 0; a < boxes.length; a++) {
387
- for (let b = a + 1; b < boxes.length; b++) {
388
- const p = boxes[a];
389
- const q = boxes[b];
390
- if (p.i0 > q.i1 || q.i0 > p.i1 || p.j0 > q.j1 || q.j0 > p.j1) continue;
391
- boxes[a] = {
392
- i0: Math.min(p.i0, q.i0),
393
- i1: Math.max(p.i1, q.i1),
394
- j0: Math.min(p.j0, q.j0),
395
- j1: Math.max(p.j1, q.j1),
396
- top: Math.max(p.top, q.top),
397
- };
398
- boxes.splice(b, 1);
399
- b = a;
400
- }
401
- }
402
- return boxes;
403
- }
404
-
405
- /**
406
- * 网格列 c0..c1 的侧剖面,画在该 slab 的列 `at` 上:坐标 (u = −z, y),对 u 对称。
407
- * 每一行取 slab 内的最大高度,于是侧着看时这些平面叠起来正好是实体的侧影,
408
- * 而不是每个斜率处一根齿。行段与轮廓相接处取该处真实表面高度收尾。
409
- */
410
- function columnLoops(g: Grid, c0: number, c1: number, sdIn: Float64Array, hIn: Float64Array, surface: Surface): Point[][] {
411
- const loops: Point[][] = [];
412
- const sd = new Float64Array(g.ny).fill(-Infinity);
413
- const h = new Float64Array(g.ny);
414
- for (let j = 0; j < g.ny; j++) {
415
- for (let c = c0; c <= c1; c++) {
416
- const k = j * g.nx + c;
417
- if (sdIn[k] > sd[j]) sd[j] = sdIn[k];
418
- if (sdIn[k] > 0 && hIn[k] > h[j]) h[j] = hIn[k];
419
- }
420
- }
421
- const rim = (inside: number, outside: number): Point => {
422
- const yIn = g.y0 + inside * g.step;
423
- const y = yIn + ((g.y0 + outside * g.step - yIn) * sd[inside]) / (sd[inside] - sd[outside]);
424
- let top = 0;
425
- for (let c = c0; c <= c1; c++) top = Math.max(top, surfaceHeight(surface, g.x0 + c * g.step, y, 0));
426
- return [Math.min(h[inside], top), y];
427
- };
428
- let j = 0;
429
- while (j < g.ny) {
430
- if (sd[j] <= 0) {
431
- j++;
432
- continue;
433
- }
434
- const j0 = j;
435
- while (j < g.ny && sd[j] > 0) j++;
436
- const j1 = j - 1;
437
- const right: Point[] = [];
438
- if (j0 > 0) right.push(rim(j0, j0 - 1));
439
- else right.push([h[j0], g.y0 + j0 * g.step]);
440
- for (let k = j0; k <= j1; k++) right.push([h[k], g.y0 + k * g.step]);
441
- if (j1 < g.ny - 1) right.push(rim(j1, j1 + 1));
442
- else right.push([h[j1], g.y0 + j1 * g.step]);
443
- let top = 0;
444
- for (const p of right) top = Math.max(top, p[0]);
445
- if (top < 0.05) continue;
446
- const loop: Point[] = right.slice();
447
- for (let k = right.length - 1; k >= 0; k--) loop.push([-right[k][0], right[k][1]]);
448
- loops.push(simplifyLoop(loop, 0.05));
449
- }
450
- return loops;
451
- }
452
-
453
- /** 深度层级:在圆角段上按角度均分。 */
454
- function levels(flat: number, round: number, steps: number): number[] {
455
- const out: number[] = [];
456
- for (let k = 1; k < steps; k++) out.push(flat + round * Math.sin((k / steps) * (Math.PI / 2)));
457
- return out;
458
- }
459
-
460
- /** 构建隐含体积。`volume` 缩放所有深度(0 = 扁平)。 */
461
- export function buildVolume(def: AvatarDefinition, volume = 1, body: BodyAnalysis = analyzeBody(def.body.path)): Volume {
462
- const outlines = body.subpaths.filter((s) => !s.hole);
463
- const holes = body.subpaths.filter((s) => s.hole);
464
- const v = Number.isFinite(volume) ? Math.max(0, volume) : 1;
465
- const xAxis = revolveAxis(body);
466
-
467
- const parts: VolumePart[] = outlines.map((o) => {
468
- const cuts = holes.filter((h) => h.probe && isInside(o.polys, h.probe[0], h.probe[1]));
469
- const polys = [...o.polys, ...cuts.flatMap((h) => h.polys)];
470
- const b = bounds(o.polys);
471
- const center: Point = [(b.minX + b.maxX) / 2, (b.minY + b.maxY) / 2];
472
- const minDim = Math.min(b.maxX - b.minX, b.maxY - b.minY);
473
- const step = Math.max(0.25, Math.min(0.45, minDim / 24));
474
-
475
- let kind: PartKind = 'inflate';
476
- let profile: Profile | null = null;
477
- if (xAxis !== null) {
478
- profile = revolveProfile(polys, false, b.minX, b.maxX, xAxis);
479
- if (profile) kind = 'revolve-x';
480
- } else if (def.id === 'shape-sphere' || def.id === 'sphere' || (outlines.length > 1 && cuts.length === 0)) {
481
- profile = revolveProfile(polys, true, b.minY, b.maxY, center[0]);
482
- if (profile) kind = 'revolve-y';
483
- }
484
-
485
- const index = edgeBands(polys);
486
- let thickness: number;
487
- let depth: number;
488
- let rxy = 0;
489
- let rz = 0;
490
- let axis = 0;
491
- if (kind === 'inflate' || !profile) {
492
- // 内切半径:网格采样上最大的向内距离。
493
- const pad = 1;
494
- const tStep = Math.max(step, 0.6);
495
- const nx = Math.ceil((b.maxX - b.minX + 2 * pad) / tStep) + 1;
496
- const ny = Math.ceil((b.maxY - b.minY + 2 * pad) / tStep) + 1;
497
- const xs: number[] = [];
498
- for (let i = 0; i < nx; i++) xs.push(b.minX - pad + i * tStep);
499
- const row = new Uint8Array(nx);
500
- let widest = 0;
501
- for (let j = 0; j < ny; j++) {
502
- const y = b.minY - pad + j * tStep;
503
- index.insideRow(y, xs, row);
504
- for (let i = 0; i < nx; i++) {
505
- if (!row[i]) continue;
506
- const d = polygonEdgeDistance(polys, xs[i], y);
507
- if (d > widest) widest = d;
508
- }
509
- }
510
- thickness = 2 * widest;
511
- depth = partDepth(thickness, b, v);
512
- rxy = Math.min(thickness / 2, depth / 2) * 0.9;
513
- rz = (depth / 2) * 0.85;
514
- } else {
515
- axis = kind === 'revolve-x' ? (xAxis as number) : center[0];
516
- const radius = Math.max(...profile.r);
517
- thickness = 2 * radius;
518
- depth = 2 * radius * v;
519
- }
520
-
521
- const surface: Surface = { kind, fillet: { depth, rxy, rz }, profile, axis, v };
522
- const filled = edgeBands(polys);
523
- const height = (x: number, y: number) =>
524
- filled.winding(x, y) !== 0 ? surfaceHeight(surface, x, y, kind === 'inflate' ? polygonEdgeDistance(polys, x, y) : 0) : 0;
525
-
526
- const core: VolumeSlice = {
527
- axis: 'z',
528
- at: 0,
529
- d: o.d + cuts.map((h) => h.d).join(''),
530
- loops: polys,
531
- evenOdd: false,
532
- box: { x: b.minX, y: b.minY, w: b.maxX - b.minX, h: b.maxY - b.minY },
533
- inset: 0,
534
- };
535
- const zSlices: VolumeSlice[] = [core];
536
- const xSlices: VolumeSlice[] = [];
537
- const half = depth / 2;
538
-
539
- if (half > 0.05) {
540
- // 采样网格:带符号距离与高度。
541
- const pad = 1;
542
- const nx = Math.ceil((b.maxX - b.minX + 2 * pad) / step) + 1;
543
- const ny = Math.ceil((b.maxY - b.minY + 2 * pad) / step) + 1;
544
- const g: Grid = { x0: b.minX - pad, y0: b.minY - pad, step, nx, ny };
545
- const xs: number[] = [];
546
- for (let i = 0; i < nx; i++) xs.push(g.x0 + i * step);
547
- const sd = new Float64Array(nx * ny);
548
- const h = new Float64Array(nx * ny);
549
- const inside = new Uint8Array(nx);
550
- for (let j = 0; j < ny; j++) {
551
- const y = g.y0 + j * step;
552
- index.insideRow(y, xs, inside);
553
- for (let i = 0; i < nx; i++) {
554
- const k = j * nx + i;
555
- const d = polygonEdgeDistance(polys, xs[i], y);
556
- if (inside[i]) {
557
- sd[k] = d;
558
- h[k] = surfaceHeight(surface, xs[i], y, kind === 'inflate' ? d : 0);
559
- } else {
560
- sd[k] = -d;
561
- }
562
- }
563
- }
564
-
565
- const steps =
566
- kind === 'inflate' ? Math.max(4, Math.min(14, Math.round(rz / 0.8))) : Math.max(5, Math.min(18, Math.round(half / 1.1)));
567
- const ls = kind === 'inflate' ? [half - rz, ...levels(half - rz, rz, steps)] : levels(0, half, steps);
568
- const cut: LevelCut = kind === 'inflate' ? insetCuts(g, sd, surface.fillet) : revolveCuts(g, sd, surface);
569
- const addLevel = (level: number, box?: CellBox) => {
570
- const loops = cut(level, box);
571
- if (!loops.length) return false;
572
- let inset = Infinity;
573
- let k = 0;
574
- for (const loop of loops) for (const p of loop) if (k++ % 3 === 0) inset = Math.min(inset, polygonEdgeDistance(polys, p[0], p[1]));
575
- const slice = { axis: 'z' as const, d: loopsToPath(loops), loops, evenOdd: true, box: loopBox(loops), inset };
576
- zSlices.push({ ...slice, at: level }, { ...slice, at: -level });
577
- return true;
578
- };
579
- const used = [0];
580
- for (const level of ls) if (level >= 0.02 && addLevel(level)) used.push(level);
581
- // 细长而深的鳍:在层级之间补截面(含直壁),每个裁到自己的框内。
582
- const thin = kind === 'inflate' ? thinBoxes(g, sd, h) : [];
583
- for (const box of thin) {
584
- for (let k = 1; k < used.length && used[k - 1] < box.top; k++) {
585
- const a = used[k - 1];
586
- const gap = used[k] - a;
587
- const m = Math.ceil(gap / FINE_Z - 1e-9);
588
- for (let q = 1; q < m && a + (gap * q) / m < box.top; q++) addLevel(a + (gap * q) / m, box);
589
- }
590
- }
591
- zSlices.sort((m, n) => m.at - n.at);
592
-
593
- // 每 every 个网格列一个平面,代表它周围的整个 slab。
594
- const width = b.maxX - b.minX;
595
- const every = Math.max(1, Math.round(Math.max(1.1, width / MAX_X_SLICES) / step));
596
- const before = Math.floor((every - 1) / 2);
597
- for (let i = 1; i < nx - 1; i += every) {
598
- const loops = columnLoops(g, Math.max(0, i - before), Math.min(nx - 1, i - before + every - 1), sd, h, surface);
599
- if (loops.length) {
600
- xSlices.push({ axis: 'x', at: g.x0 + i * step, d: loopsToPath(loops), loops, evenOdd: true, box: loopBox(loops), inset: 0 });
601
- }
602
- }
603
- }
604
-
605
- return {
606
- kind,
607
- d: core.d,
608
- polys,
609
- bounds: b,
610
- center,
611
- thickness,
612
- depth,
613
- rxy,
614
- rz,
615
- axis: kind === 'revolve-x' ? (xAxis as number) : kind === 'revolve-y' ? center[0] : undefined,
616
- zSlices,
617
- xSlices,
618
- height,
619
- };
620
- });
621
-
622
- const eyeScale = boostedEyeScale(body, def.eyes);
623
- const centers = eyeCenters(def.eyes);
624
- let eyePart = 0;
625
- let bestZ = -Infinity;
626
- parts.forEach((part, i) => {
627
- const zs = centers.map(([x, y]) => part.height(x, y));
628
- if (zs.some((z) => z <= 0)) return;
629
- const z = Math.min(...zs);
630
- if (z > bestZ) {
631
- bestZ = z;
632
- eyePart = i;
633
- }
634
- });
635
- const host = parts[eyePart];
636
- const w = (EYE_W * eyeScale) / 2 + GAZE_X * eyeScale;
637
- const hh = (EYE_H * eyeScale) / 2 + GAZE_Y * eyeScale;
638
- const eyeZ = centers.map(([x, y]) => {
639
- let top = 0;
640
- for (const fx of [-1, -0.5, 0, 0.5, 1]) {
641
- for (const fy of [-1, -0.5, 0, 0.5, 1]) top = Math.max(top, host ? host.height(x + fx * w, y + fy * hh) : 0);
642
- }
643
- return top;
644
- }) as [number, number];
645
-
646
- const eyePlanes = centers.map(([x, y]) => {
647
- if (!host) return { x, y, z: 0, dzdx: 0, dzdy: 0 };
648
- const e = 0.75;
649
- const dzdx = (host.height(x + e, y) - host.height(x - e, y)) / (2 * e);
650
- const dzdy = (host.height(x, y + e) - host.height(x, y - e)) / (2 * e);
651
- return { x, y, z: host.height(x, y), dzdx, dzdy };
652
- }) as [EyePlane, EyePlane];
653
-
654
- return { parts, bounds: body.bounds, eyePart, eyeZ, eyeScale, eyePlanes };
655
- }
656
-
657
- /** 不超过 EYE_BOOST × 定义值、且绘制眼睛仍与轮廓保持 EYE_BOOST_PAD 的最大眼睛倍率。 */
658
- export function boostedEyeScale(body: BodyAnalysis, eyes: AvatarEyes): number {
659
- for (const k of [EYE_BOOST, 1.38, 1.3, 1.22, 1.15, 1.08]) {
660
- if (!drawnEyesFit(body.polys, { ...eyes, scale: eyes.scale * k }, 0, 0, EYE_BOOST_PAD)) return eyes.scale * k;
661
- }
662
- return eyes.scale;
663
- }
664
-
665
- export interface FaceRoom {
666
- boost: number;
667
- clearance: number;
668
- gaze: GazeRange;
669
- ok: boolean;
670
- }
671
-
672
- /** 渲染器实际绘制时的脸部余量。 */
673
- export function faceRoom(def: AvatarDefinition, body: BodyAnalysis = analyzeBody(def.body.path)): FaceRoom {
674
- const scale = boostedEyeScale(body, def.eyes);
675
- const eyes = { ...def.eyes, scale };
676
- const clearance = eyeClearance(body.polys, eyes);
677
- const gaze = gazeRange(body.polys, eyes);
678
- const boost = Math.round((scale / def.eyes.scale) * 100) / 100;
679
- const ok = boost >= EYE_BOOST - 1e-9 && clearance >= FACE_ROOM_TARGET && Math.min(gaze.left, gaze.right, gaze.up, gaze.down) === 1;
680
- return { boost, clearance, gaze, ok };
681
- }