| 1 | import { DOT_BODY } from './dot-body.js'; |
| 2 | import { DOT_PIXEL_BODY } from './dot-pixel-body.js'; |
| 3 | import type { OwnerActivityKind } from './pet-engine.js'; |
| 4 | |
| 5 | export type FormKind = Exclude<OwnerActivityKind, 'tool'> | 'waiting' | 'done'; |
| 6 | export type FormStyle = 'smooth' | 'pixel'; |
| 7 | export interface FormPoint { |
| 8 | x: number; |
| 9 | y: number; |
| 10 | lit: number; |
| 11 | fade: number; |
| 12 | } |
| 13 | |
| 14 | const TAU = Math.PI * 2; |
| 15 | const DEG = Math.PI / 180; |
| 16 | const N = DOT_BODY.length; |
| 17 | |
| 18 | const clamp = (v: number, lo = 0, hi = 1): number => (v < lo ? lo : v > hi ? hi : v); |
| 19 | const smoothstep = (e0: number, e1: number, x: number): number => { |
| 20 | const t = clamp((x - e0) / (e1 - e0)); |
| 21 | return t * t * (3 - 2 * t); |
| 22 | }; |
| 23 | const mix = (a: number, b: number, t: number): number => a + (b - a) * t; |
| 24 | |
| 25 | export const FORM_HUE: Record<FormKind, number> = { |
| 26 | executing: 208, |
| 27 | editing: 170, |
| 28 | reading: 206, |
| 29 | searching: 210, |
| 30 | testing: 168, |
| 31 | browsing: 212, |
| 32 | computer: 214, |
| 33 | memory: 264, |
| 34 | thinking: 44, |
| 35 | responding: 172, |
| 36 | delegating: 172, |
| 37 | waiting: 38, |
| 38 | done: 166, |
| 39 | }; |
| 40 | export const FORM_SCALE = 1.0; |
| 41 | export const FORM_STILL_TIME = 1.25; |
| 42 | |
| 43 | type Pt = readonly [number, number]; |
| 44 | type Poly = readonly Pt[]; |
| 45 | |
| 46 | function pointInPoly(x: number, y: number, poly: Poly): boolean { |
| 47 | let inside = false; |
| 48 | for (let i = 0, j = poly.length - 1; i < poly.length; j = i++) { |
| 49 | const xi = poly[i]![0]; |
| 50 | const yi = poly[i]![1]; |
| 51 | const xj = poly[j]![0]; |
| 52 | const yj = poly[j]![1]; |
| 53 | if ((yi > y) !== (yj > y) && x < ((xj - xi) * (y - yi)) / (yj - yi) + xi) inside = !inside; |
| 54 | } |
| 55 | return inside; |
| 56 | } |
| 57 | |
| 58 | function polySdf(x: number, y: number, poly: Poly): number { |
| 59 | let best = Infinity; |
| 60 | for (let i = 0; i < poly.length - 1; i++) { |
| 61 | const ax = poly[i]![0]; |
| 62 | const ay = poly[i]![1]; |
| 63 | const bx = poly[i + 1]![0]; |
| 64 | const by = poly[i + 1]![1]; |
| 65 | const dx = bx - ax; |
| 66 | const dy = by - ay; |
| 67 | const len2 = dx * dx + dy * dy; |
| 68 | const t = len2 > 1e-9 ? clamp(((x - ax) * dx + (y - ay) * dy) / len2) : 0; |
| 69 | const px = ax + dx * t; |
| 70 | const py = ay + dy * t; |
| 71 | const d2 = (x - px) * (x - px) + (y - py) * (y - py); |
| 72 | if (d2 < best) best = d2; |
| 73 | } |
| 74 | const d = Math.sqrt(best); |
| 75 | return pointInPoly(x, y, poly) ? -d : d; |
| 76 | } |
| 77 | |
| 78 | const sdCircle = (x: number, y: number, cx: number, cy: number, r: number): number => Math.hypot(x - cx, y - cy) - r; |
| 79 | const sdRing = (x: number, y: number, cx: number, cy: number, r: number, th: number): number => Math.abs(Math.hypot(x - cx, y - cy) - r) - th; |
| 80 | |
| 81 | const sdEllipse = (x: number, y: number, cx: number, cy: number, rx: number, ry: number, rotRad = 0): number => { |
| 82 | const dx = x - cx; |
| 83 | const dy = y - cy; |
| 84 | const c = Math.cos(rotRad); |
| 85 | const s = Math.sin(rotRad); |
| 86 | const u = dx * c + dy * s; |
| 87 | const v = -dx * s + dy * c; |
| 88 | return (Math.hypot(u / rx, v / ry) - 1) * Math.min(rx, ry); |
| 89 | }; |
| 90 | |
| 91 | const sdBox = (x: number, y: number, cx: number, cy: number, hw: number, hh: number, r = 0, rotRad = 0): number => { |
| 92 | const dx = x - cx; |
| 93 | const dy = y - cy; |
| 94 | const c = Math.cos(rotRad); |
| 95 | const s = Math.sin(rotRad); |
| 96 | const qx = Math.abs(dx * c + dy * s) - (hw - r); |
| 97 | const qy = Math.abs(-dx * s + dy * c) - (hh - r); |
| 98 | return Math.hypot(Math.max(qx, 0), Math.max(qy, 0)) + Math.min(Math.max(qx, qy), 0) - r; |
| 99 | }; |
| 100 | |
| 101 | const sdSegment = (x: number, y: number, ax: number, ay: number, bx: number, by: number, r: number): number => { |
| 102 | const pax = x - ax; |
| 103 | const pay = y - ay; |
| 104 | const bax = bx - ax; |
| 105 | const bay = by - ay; |
| 106 | const h = clamp((pax * bax + pay * bay) / (bax * bax + bay * bay || 1)); |
| 107 | return Math.hypot(pax - bax * h, pay - bay * h) - r; |
| 108 | }; |
| 109 | |
| 110 | const sdArc = (x: number, y: number, cx: number, cy: number, r: number, halfTh: number, midDeg: number, spanDeg: number): number => { |
| 111 | const dx = x - cx; |
| 112 | const dy = y - cy; |
| 113 | const mid = midDeg * DEG; |
| 114 | const halfSpan = spanDeg * 0.5 * DEG; |
| 115 | let da = Math.atan2(dy, dx) - mid; |
| 116 | while (da > Math.PI) da -= TAU; |
| 117 | while (da < -Math.PI) da += TAU; |
| 118 | if (Math.abs(da) <= halfSpan) return Math.abs(Math.hypot(dx, dy) - r) - halfTh; |
| 119 | const a1 = mid - halfSpan; |
| 120 | const a2 = mid + halfSpan; |
| 121 | return Math.min( |
| 122 | Math.hypot(dx - r * Math.cos(a1), dy - r * Math.sin(a1)), |
| 123 | Math.hypot(dx - r * Math.cos(a2), dy - r * Math.sin(a2)), |
| 124 | ) - halfTh; |
| 125 | }; |
| 126 | |
| 127 | const opUnion = (...ds: number[]): number => Math.min(...ds); |
| 128 | const opSmoothUnion = (d1: number, d2: number, k: number): number => { |
| 129 | const h = clamp(0.5 + (0.5 * (d2 - d1)) / k); |
| 130 | return mix(d2, d1, h) - k * h * (1 - h); |
| 131 | }; |
| 132 | const opSub = (d1: number, d2: number): number => Math.max(d1, -d2); |
| 133 | |
| 134 | interface Sample { |
| 135 | d: number; |
| 136 | zone: number; |
| 137 | part: number; |
| 138 | } |
| 139 | |
| 140 | function sdMiniWhale(x: number, y: number, cx: number, cy: number, scale: number): Sample { |
| 141 | const lx = (x - cx) / scale; |
| 142 | const ly = (y - cy) / scale; |
| 143 | const head = sdEllipse(lx, ly, 0.25, 0.01, 0.38, 0.22, 0.05); |
| 144 | const back = sdEllipse(lx, ly, -0.07, 0.0, 0.35, 0.165, -0.10); |
| 145 | const dorsal = polySdf(lx, ly, [[-0.16, -0.14], [-0.03, -0.14], [-0.18, -0.29], [-0.16, -0.14]]); |
| 146 | const peduncle = sdSegment(lx, ly, -0.20, -0.01, -0.49, -0.04, 0.076); |
| 147 | const flukeUp = sdEllipse(lx, ly, -0.60, -0.19, 0.175, 0.062, -0.74); |
| 148 | const flukeDn = sdEllipse(lx, ly, -0.59, 0.11, 0.165, 0.058, 0.66); |
| 149 | const pectoral = sdEllipse(lx, ly, 0.10, 0.20, 0.15, 0.052, 0.50); |
| 150 | const body = opSmoothUnion(opSmoothUnion(head, back, 0.09), peduncle, 0.07); |
| 151 | const full = opUnion(body, dorsal, flukeUp, flukeDn, pectoral); |
| 152 | const eye = sdCircle(lx, ly, 0.37, -0.02, 0.056); |
| 153 | return { |
| 154 | d: opSub(full, eye) * scale, |
| 155 | zone: ly > 0.03 && lx > -0.05 ? 1 : 0, |
| 156 | part: lx < -0.35 ? 1 : 0, |
| 157 | }; |
| 158 | } |
| 159 | |
| 160 | type CompileKey = Exclude<FormKind, 'delegating'> | 'delegating2' | 'delegating3'; |
| 161 | |
| 162 | function evalSdf(kind: CompileKey, x: number, y: number): Sample { |
| 163 | switch (kind) { |
| 164 | case 'thinking': { |
| 165 | const dome = sdCircle(x, y, 0, -0.14, 0.39); |
| 166 | const neck = sdBox(x, y, 0, 0.18, 0.21, 0.13, 0.04); |
| 167 | const cavity = opSmoothUnion(sdCircle(x, y, 0, -0.15, 0.255), sdBox(x, y, 0, 0.10, 0.105, 0.10, 0.03), 0.05); |
| 168 | const shell = opSub(opSmoothUnion(dome, neck, 0.08), cavity); |
| 169 | const filament = opUnion( |
| 170 | sdSegment(x, y, 0, 0.09, 0, -0.13, 0.044), |
| 171 | sdSegment(x, y, 0, -0.13, -0.09, -0.22, 0.038), |
| 172 | sdSegment(x, y, 0, -0.13, 0.09, -0.22, 0.038), |
| 173 | ); |
| 174 | const base1 = sdBox(x, y, 0, 0.39, 0.19, 0.044, 0.03); |
| 175 | const base2 = sdBox(x, y, 0, 0.52, 0.13, 0.042, 0.03); |
| 176 | let rays = Infinity; |
| 177 | let rayIdx = 0; |
| 178 | const angles = [-155, -118, -90, -62, -25]; |
| 179 | for (let i = 0; i < angles.length; i++) { |
| 180 | const a = angles[i]! * DEG; |
| 181 | const dr = sdSegment(x, y, Math.cos(a) * 0.51, -0.14 + Math.sin(a) * 0.51, Math.cos(a) * 0.67, -0.14 + Math.sin(a) * 0.67, 0.040); |
| 182 | if (dr < rays) { |
| 183 | rays = dr; |
| 184 | rayIdx = i + 2; |
| 185 | } |
| 186 | } |
| 187 | const d = opUnion(shell, filament, base1, base2, rays); |
| 188 | if (filament <= shell && filament <= rays) return { d, zone: 2, part: 1 }; |
| 189 | if (rays <= shell && rays <= base1 && rays <= base2) return { d, zone: 2, part: rayIdx }; |
| 190 | return { d, zone: y > 0.3 ? 1 : 0, part: 0 }; |
| 191 | } |
| 192 | case 'reading': { |
| 193 | const gapX = 0.052; |
| 194 | const left = polySdf(x, y, [[-0.78, -0.44], [-gapX, -0.34], [-gapX, 0.52], [-0.78, 0.42], [-0.78, -0.44]]); |
| 195 | const right = polySdf(x, y, [[gapX, -0.34], [0.78, -0.44], [0.78, 0.42], [gapX, 0.52], [gapX, -0.34]]); |
| 196 | let cuts = Infinity; |
| 197 | for (const ry of [-0.20, 0.03, 0.26]) { |
| 198 | cuts = Math.min(cuts, sdSegment(x, y, -0.63, ry - 0.04, -0.19, ry + 0.02, 0.036), sdSegment(x, y, 0.19, ry + 0.02, 0.63, ry - 0.04, 0.036)); |
| 199 | } |
| 200 | const pages = opSub(opUnion(left, right), cuts); |
| 201 | const cover = opUnion(sdSegment(x, y, -0.76, 0.54, 0, 0.62, 0.026), sdSegment(x, y, 0, 0.62, 0.76, 0.54, 0.026)); |
| 202 | return { d: opUnion(pages, cover), zone: Math.abs(x) < 0.18 ? 1 : 0, part: x > 0 ? 1 : 0 }; |
| 203 | } |
| 204 | case 'searching': { |
| 205 | const lcx = -0.10; |
| 206 | const lcy = -0.10; |
| 207 | const lr = 0.38; |
| 208 | const rim = sdRing(x, y, lcx, lcy, lr, 0.072); |
| 209 | const handle = sdSegment(x, y, lcx + lr * 0.73, lcy + lr * 0.73, 0.62, 0.62, 0.082); |
| 210 | const glint = sdArc(x, y, lcx, lcy, lr * 0.62, 0.028, -132, 68); |
| 211 | const glass = opUnion(rim, handle, glint); |
| 212 | let lines = Infinity; |
| 213 | for (const [x0, ly, x1] of [[-0.72, -0.58, -0.34], [-0.72, -0.34, -0.56], [-0.72, 0.16, -0.52], [-0.72, 0.40, -0.18]] as const) { |
| 214 | lines = Math.min(lines, sdSegment(x, y, x0, ly, x1, ly, 0.026)); |
| 215 | } |
| 216 | const cleanLines = opSub(lines, sdCircle(x, y, lcx, lcy, lr + 0.11)); |
| 217 | const d = opUnion(glass, cleanLines); |
| 218 | if (glass <= cleanLines) return { d, zone: glint < rim ? 2 : 1, part: 1 }; |
| 219 | return { d, zone: 0, part: 0 }; |
| 220 | } |
| 221 | case 'editing': { |
| 222 | const ang = -45 * DEG; |
| 223 | const dx = x - 0.04; |
| 224 | const dy = y + 0.06; |
| 225 | const u = dx * Math.cos(ang) + dy * Math.sin(ang); |
| 226 | const v = -dx * Math.sin(ang) + dy * Math.cos(ang); |
| 227 | const barrel = Math.max(Math.abs(u - 0.06) - 0.36, Math.abs(v) - 0.135); |
| 228 | const eraser = sdBox(u, v, 0.52, 0, 0.085, 0.135, 0.04); |
| 229 | const nib = polySdf(u, v, [[-0.34, -0.135], [-0.34, 0.135], [-0.63, 0], [-0.34, -0.135]]); |
| 230 | const gaps = Math.min(sdBox(u, v, -0.32, 0, 0.026, 0.16), sdBox(u, v, 0.43, 0, 0.026, 0.16)); |
| 231 | const pencil = opSub(opUnion(barrel, eraser, nib), gaps); |
| 232 | const line = sdSegment(x, y, -0.44, 0.58, 0.62, 0.58, 0.040); |
| 233 | const d = opUnion(pencil, line); |
| 234 | if (line < pencil) return { d, zone: 2, part: 2 }; |
| 235 | return { d, zone: u < -0.34 ? 2 : u > 0.43 ? 1 : 0, part: 1 }; |
| 236 | } |
| 237 | case 'executing': { |
| 238 | const jx = 0.25; |
| 239 | const jy = -0.25; |
| 240 | const jaw = opSub(sdCircle(x, y, jx, jy, 0.30), sdBox(x, y, jx + 0.16, jy - 0.16, 0.24, 0.135, 0.02, -45 * DEG)); |
| 241 | const shaft = sdSegment(x, y, 0.06, -0.06, -0.38, 0.38, 0.108); |
| 242 | const tail = opSub(sdCircle(x, y, -0.44, 0.44, 0.19), sdCircle(x, y, -0.44, 0.44, 0.085)); |
| 243 | const wrench = opSub(opUnion(opSmoothUnion(jaw, shaft, 0.05), tail), sdCircle(x, y, jx + 0.03, jy - 0.03, 0.128)); |
| 244 | const nut = sdCircle(x, y, jx + 0.03, jy - 0.03, 0.066); |
| 245 | const d = opUnion(wrench, nut); |
| 246 | if (nut < wrench) return { d, zone: 2, part: 2 }; |
| 247 | return { d, zone: jaw < shaft ? 1 : 0, part: 1 }; |
| 248 | } |
| 249 | case 'testing': { |
| 250 | const lip = sdSegment(x, y, -0.18, -0.60, 0.18, -0.60, 0.046); |
| 251 | const neckOuter = sdBox(x, y, 0, -0.40, 0.135, 0.20, 0.02); |
| 252 | const coneOuter = polySdf(x, y, [[-0.135, -0.22], [0.135, -0.22], [0.58, 0.58], [-0.58, 0.58], [-0.135, -0.22]]); |
| 253 | const outer = opSmoothUnion(opUnion(lip, neckOuter, coneOuter), sdBox(x, y, 0, 0.56, 0.56, 0.06, 0.05), 0.04); |
| 254 | const inner = opUnion(sdBox(x, y, 0, -0.38, 0.074, 0.24), polySdf(x, y, [[-0.074, -0.20], [0.074, -0.20], [0.27, 0.16], [-0.27, 0.16], [-0.074, -0.20]])); |
| 255 | const cuts = opUnion(sdCircle(x, y, -0.15, 0.39, 0.062), sdCircle(x, y, 0.16, 0.45, 0.070), sdCircle(x, y, 0.02, 0.28, 0.048)); |
| 256 | const flask = opSub(opSub(outer, inner), cuts); |
| 257 | const b1 = sdCircle(x, y, -0.02, -0.75, 0.048); |
| 258 | const b2 = sdCircle(x, y, 0.09, -0.44, 0.038); |
| 259 | const d = opUnion(flask, b1, b2); |
| 260 | if (b1 < flask || b2 < flask) return { d, zone: 2, part: 2 }; |
| 261 | const liq = y > 0.16 && y < 0.54 && Math.abs(x) < 0.48; |
| 262 | return { d, zone: liq ? 1 : 0, part: liq ? 1 : 0 }; |
| 263 | } |
| 264 | case 'browsing': { |
| 265 | const R = 0.64; |
| 266 | const outer = sdRing(x, y, 0, 0, R, 0.056); |
| 267 | const clip = sdCircle(x, y, 0, 0, R + 0.02); |
| 268 | const eq = Math.max(sdSegment(x, y, -R, 0, R, 0, 0.038), clip); |
| 269 | const lat1 = Math.max(sdSegment(x, y, -R * 0.86, -0.32, R * 0.86, -0.32, 0.032), clip); |
| 270 | const lat2 = Math.max(sdSegment(x, y, -R * 0.86, 0.32, R * 0.86, 0.32, 0.032), clip); |
| 271 | const mer = opSub(sdEllipse(x, y, 0, 0, 0.33, R, 0), sdEllipse(x, y, 0, 0, 0.25, R - 0.06, 0)); |
| 272 | const d = opUnion(outer, eq, lat1, lat2, mer); |
| 273 | const isMer = mer <= outer && mer <= eq && mer <= lat1 && mer <= lat2; |
| 274 | return { d, zone: isMer ? 2 : 1, part: isMer ? (x >= 0 ? 1 : 2) : 0 }; |
| 275 | } |
| 276 | case 'computer': { |
| 277 | const arrow = polySdf(x, y, [[-0.24, -0.48], [-0.24, 0.34], [-0.04, 0.16], [0.10, 0.48], [0.23, 0.42], [0.09, 0.10], [0.34, 0.10], [-0.24, -0.48]]); |
| 278 | const ripple = sdArc(x, y, -0.24, -0.48, 0.22, 0.034, -125, 105); |
| 279 | const bar = sdBox(x, y, 0.38, 0.56, 0.26, 0.066, 0.034); |
| 280 | const d = opUnion(arrow, ripple, bar); |
| 281 | if (ripple < arrow && ripple < bar) return { d, zone: 2, part: 2 }; |
| 282 | if (bar < arrow) return { d, zone: 0, part: 0 }; |
| 283 | return { d, zone: 1, part: 1 }; |
| 284 | } |
| 285 | case 'memory': { |
| 286 | const dia = (cy: number) => polySdf(x, y, [[0, cy - 0.24], [0.64, cy], [0, cy + 0.24], [-0.64, cy], [0, cy - 0.24]]); |
| 287 | const top = dia(-0.36); |
| 288 | const mid = opSub(dia(0.04), dia(-0.11) - 0.058); |
| 289 | const bot = opSub(dia(0.44), dia(0.29) - 0.058); |
| 290 | const d = opUnion(top, mid, bot); |
| 291 | if (top <= mid && top <= bot) return { d, zone: 2, part: 1 }; |
| 292 | if (mid <= bot) return { d, zone: 1, part: 2 }; |
| 293 | return { d, zone: 0, part: 3 }; |
| 294 | } |
| 295 | case 'responding': { |
| 296 | const bubble = opUnion(sdEllipse(x, y, 0, -0.06, 0.66, 0.46), polySdf(x, y, [[-0.36, 0.24], [-0.48, 0.62], [-0.08, 0.34], [-0.36, 0.24]])); |
| 297 | let holes = Infinity; |
| 298 | let cores = Infinity; |
| 299 | let idx = 0; |
| 300 | const xs = [-0.28, 0, 0.28]; |
| 301 | for (let i = 0; i < 3; i++) { |
| 302 | holes = Math.min(holes, sdCircle(x, y, xs[i]!, -0.06, 0.115)); |
| 303 | const dc = sdCircle(x, y, xs[i]!, -0.06, 0.056); |
| 304 | if (dc < cores) { |
| 305 | cores = dc; |
| 306 | idx = i + 1; |
| 307 | } |
| 308 | } |
| 309 | const shell = opSub(bubble, holes); |
| 310 | const d = opUnion(shell, cores); |
| 311 | if (cores < shell) return { d, zone: 2, part: idx }; |
| 312 | return { d, zone: y < -0.2 ? 1 : 0, part: 0 }; |
| 313 | } |
| 314 | case 'delegating2': { |
| 315 | const w1 = sdMiniWhale(x, y, -0.10, -0.30, 0.72); |
| 316 | const w2 = sdMiniWhale(x, y, 0.16, 0.30, 0.76); |
| 317 | if (w1.d <= w2.d) return { d: w1.d, zone: w1.zone, part: 1 + w1.part }; |
| 318 | return { d: w2.d, zone: w2.zone === 1 ? 2 : 1, part: 3 + w2.part }; |
| 319 | } |
| 320 | case 'delegating3': { |
| 321 | const w1 = sdMiniWhale(x, y, -0.10, -0.46, 0.62); |
| 322 | const w2 = sdMiniWhale(x, y, 0.22, 0.01, 0.70); |
| 323 | const w3 = sdMiniWhale(x, y, -0.06, 0.48, 0.60); |
| 324 | if (w1.d <= w2.d && w1.d <= w3.d) return { d: w1.d, zone: w1.zone, part: 1 + w1.part }; |
| 325 | if (w2.d <= w3.d) return { d: w2.d, zone: w2.zone === 1 ? 2 : 1, part: 3 + w2.part }; |
| 326 | return { d: w3.d, zone: w3.zone, part: 5 + w3.part }; |
| 327 | } |
| 328 | case 'waiting': { |
| 329 | const crown = sdCircle(x, y, 0, -0.58, 0.072); |
| 330 | const dome = sdCircle(x, y, 0, -0.16, 0.34); |
| 331 | const skirt = polySdf(x, y, [[-0.34, -0.16], [0.34, -0.16], [0.50, 0.32], [-0.50, 0.32], [-0.34, -0.16]]); |
| 332 | const lip = sdBox(x, y, 0, 0.33, 0.56, 0.062, 0.04); |
| 333 | const bell = opUnion(crown, opSmoothUnion(opSub(dome, sdBox(x, y, 0, 0.6, 1, 0.28)), skirt, 0.06), lip); |
| 334 | const clapper = sdCircle(x, y, 0, 0.54, 0.098); |
| 335 | const arcs = opUnion(sdArc(x, y, 0, -0.04, 0.66, 0.036, 180, 50), sdArc(x, y, 0, -0.04, 0.66, 0.036, 0, 50)); |
| 336 | const d = opUnion(bell, clapper, arcs); |
| 337 | if (clapper < bell && clapper < arcs) return { d, zone: 2, part: 2 }; |
| 338 | if (arcs < bell) return { d, zone: 2, part: 3 }; |
| 339 | return { d, zone: Math.abs(y - 0.33) < 0.09 ? 2 : 1, part: 1 }; |
| 340 | } |
| 341 | case 'done': { |
| 342 | const s1 = sdSegment(x, y, -0.50, 0.04, -0.12, 0.42, 0.118); |
| 343 | const s2 = sdSegment(x, y, -0.12, 0.42, 0.54, -0.34, 0.118); |
| 344 | return { d: Math.min(s1, s2), zone: s2 < s1 ? 2 : 1, part: 1 }; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | interface CompiledSlot { |
| 350 | x: number; |
| 351 | y: number; |
| 352 | zone: number; |
| 353 | part: number; |
| 354 | rim: number; |
| 355 | u: number; |
| 356 | } |
| 357 | |
| 358 | function sampleHexLattice(kind: CompileKey, pitch: number): Array<CompiledSlot & { priority: number }> { |
| 359 | const inset = 0.008; |
| 360 | const G = 88; |
| 361 | const step = 1.84 / G; |
| 362 | const vals = new Float32Array((G + 1) * (G + 1)); |
| 363 | for (let j = 0; j <= G; j++) { |
| 364 | const y = -0.92 + j * step; |
| 365 | for (let i = 0; i <= G; i++) { |
| 366 | vals[j * (G + 1) + i] = evalSdf(kind, -0.92 + i * step, y).d + inset; |
| 367 | } |
| 368 | } |
| 369 | const raw: [number, number][] = []; |
| 370 | const interp = (x1: number, y1: number, v1: number, x2: number, y2: number, v2: number): [number, number] => { |
| 371 | const t = clamp(v1 / (v1 - v2 || 1e-9)); |
| 372 | return [x1 + (x2 - x1) * t, y1 + (y2 - y1) * t]; |
| 373 | }; |
| 374 | for (let j = 0; j < G; j++) { |
| 375 | const y0 = -0.92 + j * step; |
| 376 | const y1 = y0 + step; |
| 377 | for (let i = 0; i < G; i++) { |
| 378 | const x0 = -0.92 + i * step; |
| 379 | const x1 = x0 + step; |
| 380 | const v00 = vals[j * (G + 1) + i]!; |
| 381 | const v10 = vals[j * (G + 1) + i + 1]!; |
| 382 | const v01 = vals[(j + 1) * (G + 1) + i]!; |
| 383 | const v11 = vals[(j + 1) * (G + 1) + i + 1]!; |
| 384 | const mask = (v00 < 0 ? 1 : 0) | (v10 < 0 ? 2 : 0) | (v11 < 0 ? 4 : 0) | (v01 < 0 ? 8 : 0); |
| 385 | if (mask === 0 || mask === 15) continue; |
| 386 | const edges: [number, number][] = []; |
| 387 | if ((v00 < 0) !== (v10 < 0)) edges.push(interp(x0, y0, v00, x1, y0, v10)); |
| 388 | if ((v10 < 0) !== (v11 < 0)) edges.push(interp(x1, y0, v10, x1, y1, v11)); |
| 389 | if ((v11 < 0) !== (v01 < 0)) edges.push(interp(x1, y1, v11, x0, y1, v01)); |
| 390 | if ((v01 < 0) !== (v00 < 0)) edges.push(interp(x0, y1, v01, x0, y0, v00)); |
| 391 | if (edges.length >= 2) raw.push([(edges[0]![0] + edges[1]![0]) * 0.5, (edges[0]![1] + edges[1]![1]) * 0.5]); |
| 392 | } |
| 393 | } |
| 394 | raw.sort((a, b) => Math.atan2(a[1], a[0]) - Math.atan2(b[1], b[0])); |
| 395 | const rims: Array<CompiledSlot & { priority: number }> = []; |
| 396 | const minR2 = (pitch * 0.88) ** 2; |
| 397 | for (const [px, py] of raw) { |
| 398 | let ok = true; |
| 399 | for (let k = 0; k < rims.length; k++) { |
| 400 | const dx = px - rims[k]!.x; |
| 401 | const dy = py - rims[k]!.y; |
| 402 | if (dx * dx + dy * dy < minR2) { |
| 403 | ok = false; |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | if (!ok) continue; |
| 408 | const s = evalSdf(kind, px, py); |
| 409 | rims.push({ x: px, y: py, zone: s.zone, part: s.part, rim: 1, u: 0, priority: 2 }); |
| 410 | } |
| 411 | const out = [...rims]; |
| 412 | const minI2 = (pitch * 0.74) ** 2; |
| 413 | const dy = pitch * 0.8660254; |
| 414 | let row = 0; |
| 415 | for (let y = -0.90; y <= 0.90; y += dy, row++) { |
| 416 | const xOff = (row & 1) ? pitch * 0.5 : 0; |
| 417 | for (let x = -0.90 + xOff; x <= 0.90; x += pitch) { |
| 418 | const s = evalSdf(kind, x, y); |
| 419 | if (s.d > -inset) continue; |
| 420 | let near = false; |
| 421 | for (let k = 0; k < rims.length; k++) { |
| 422 | const dx = x - rims[k]!.x; |
| 423 | const ddy = y - rims[k]!.y; |
| 424 | if (dx * dx + ddy * ddy < minI2) { |
| 425 | near = true; |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | if (near) continue; |
| 430 | const rim = Math.exp(-Math.max(0, -s.d - inset) / 0.085); |
| 431 | out.push({ x, y, zone: s.zone, part: s.part, rim, u: 0, priority: 1 + rim * 0.5 }); |
| 432 | } |
| 433 | } |
| 434 | return out; |
| 435 | } |
| 436 | |
| 437 | const BODIES: Record<FormStyle, readonly (readonly number[])[]> = { smooth: DOT_BODY, pixel: DOT_PIXEL_BODY }; |
| 438 | const HOME_RANKS: Record<FormStyle, number[]> = { |
| 439 | smooth: DOT_BODY.map((p, i) => [p[0] * 0.78 + p[1] * 0.62, i] as const).sort((a, b) => a[0] - b[0] || a[1] - b[1]).map((v) => v[1]), |
| 440 | pixel: DOT_PIXEL_BODY.map((p, i) => [p[0] * 0.78 + p[1] * 0.62, i] as const).sort((a, b) => a[0] - b[0] || a[1] - b[1]).map((v) => v[1]), |
| 441 | }; |
| 442 | |
| 443 | function compileKey(kind: CompileKey): { pitch: number; slots: CompiledSlot[] } { |
| 444 | let lo = 0.015; |
| 445 | let hi = 0.085; |
| 446 | let best: Array<CompiledSlot & { priority: number }> = []; |
| 447 | let bestPitch = (lo + hi) * 0.5; |
| 448 | for (let iter = 0; iter < 12; iter++) { |
| 449 | const mid = (lo + hi) * 0.5; |
| 450 | const pts = sampleHexLattice(kind, mid); |
| 451 | if (!best.length || Math.abs(pts.length - N) < Math.abs(best.length - N)) { |
| 452 | best = pts; |
| 453 | bestPitch = mid; |
| 454 | } |
| 455 | if (pts.length > N) lo = mid; |
| 456 | else hi = mid; |
| 457 | } |
| 458 | if (best.length > N) { |
| 459 | const rims = best.filter((p) => p.priority >= 2); |
| 460 | const ints = best.filter((p) => p.priority < 2); |
| 461 | if (rims.length >= N) { |
| 462 | const st = rims.length / N; |
| 463 | best = Array.from({ length: N }, (_, i) => rims[Math.min(rims.length - 1, Math.floor(i * st))]!); |
| 464 | } else { |
| 465 | const need = N - rims.length; |
| 466 | const st = ints.length / need; |
| 467 | best = [...rims, ...Array.from({ length: need }, (_, i) => ints[Math.min(ints.length - 1, Math.floor(i * st))]!)]; |
| 468 | } |
| 469 | } else if (best.length > 0 && best.length < N) { |
| 470 | const baseLen = best.length; |
| 471 | for (let i = baseLen; i < N; i++) best.push({ ...best[(i * 73) % baseLen]! }); |
| 472 | } |
| 473 | best.sort((a, b) => (a.x * 0.78 + a.y * 0.62) - (b.x * 0.78 + b.y * 0.62)); |
| 474 | return { |
| 475 | pitch: bestPitch, |
| 476 | slots: best.map((p, idx) => ({ x: p.x, y: p.y, zone: p.zone, part: p.part, rim: p.rim, u: idx / (N - 1) })), |
| 477 | }; |
| 478 | } |
| 479 | |
| 480 | const CACHE = new Map<string, { pitch: number; slots: CompiledSlot[] }>(); |
| 481 | |
| 482 | export function getFormLayout(kind: FormKind, style: FormStyle, agents = 3): { pitch: number; slots: CompiledSlot[] } { |
| 483 | const key: CompileKey = kind === 'delegating' ? (agents >= 3 ? 'delegating3' : 'delegating2') : kind; |
| 484 | const cacheKey = `${style}:${key}`; |
| 485 | const hit = CACHE.get(cacheKey); |
| 486 | if (hit) return hit; |
| 487 | const raw = compileKey(key); |
| 488 | const rank = HOME_RANKS[style]; |
| 489 | const byHome = new Array<CompiledSlot>(N); |
| 490 | for (let r = 0; r < N; r++) { |
| 491 | byHome[rank[r]!] = raw.slots[r]!; |
| 492 | } |
| 493 | void BODIES; |
| 494 | const layout = { pitch: raw.pitch, slots: byHome }; |
| 495 | CACHE.set(cacheKey, layout); |
| 496 | return layout; |
| 497 | } |
| 498 | |
| 499 | export function formPoint(kind: FormKind, style: FormStyle, i: number, t: number, agents = 3): FormPoint { |
| 500 | const s = getFormLayout(kind, style, agents).slots[i]!; |
| 501 | const { x: px, y: py, zone, part, rim, u } = s; |
| 502 | |
| 503 | switch (kind) { |
| 504 | case 'thinking': { |
| 505 | if (part === 1) return { x: px, y: py + 0.004 * Math.sin(t * 4.2), lit: 0.92, fade: 0.95 }; |
| 506 | if (part >= 2) { |
| 507 | const idx = part - 2; |
| 508 | const w = 0.5 + 0.5 * Math.sin(t * 4.2 - idx * 0.65); |
| 509 | const dx = px; |
| 510 | const dy = py + 0.14; |
| 511 | const len = Math.hypot(dx, dy) || 1; |
| 512 | return { x: px + (dx / len) * 0.032 * w, y: py + (dy / len) * 0.032 * w, lit: 0.88, fade: 0.55 + 0.45 * w }; |
| 513 | } |
| 514 | return { x: px, y: py + 0.004 * Math.sin(t * 2.1), lit: 0.35 + rim * 0.35, fade: 0.78 + rim * 0.22 }; |
| 515 | } |
| 516 | case 'reading': { |
| 517 | const scanY = -0.32 + ((t * 0.42) % 1) * 0.76; |
| 518 | const beam = Math.exp(-(((py - scanY) * 7.5) ** 2)); |
| 519 | const leaf = (part === 1 ? -0.016 : 0.016) * Math.sin(t * 2.2); |
| 520 | return { x: px, y: py + leaf * Math.abs(px), lit: 0.28 + rim * 0.32 + beam * 0.45, fade: 0.74 + rim * 0.24 }; |
| 521 | } |
| 522 | case 'searching': { |
| 523 | const ox = 0.10 * Math.sin(t * 1.6); |
| 524 | const oy = 0.07 * Math.sin(t * 2.3 + 0.8); |
| 525 | if (part === 1) return { x: px + ox, y: py + oy, lit: zone === 2 ? 0.92 : 0.45 + rim * 0.35, fade: 0.90 }; |
| 526 | const glow = Math.exp(-(((Math.hypot(px - (-0.10 + ox), py - (-0.10 + oy)) - 0.38) * 4.5) ** 2)); |
| 527 | return { x: px, y: py, lit: mix(0.22, 0.82, glow), fade: mix(0.55, 0.92, glow) }; |
| 528 | } |
| 529 | case 'editing': { |
| 530 | const cyc = (t * 0.45) % 1; |
| 531 | if (part === 1) { |
| 532 | return { x: px + (cyc - 0.5) * 0.18, y: py + 0.014 * Math.sin(t * 9), lit: zone === 2 ? 0.92 : 0.38 + rim * 0.36, fade: 0.86 + rim * 0.14 }; |
| 533 | } |
| 534 | const normX = clamp((px + 0.44) / 1.06); |
| 535 | const drawn = smoothstep(normX - 0.08, normX + 0.02, 0.35 + cyc * 0.85); |
| 536 | return { x: px, y: py + 0.004 * Math.sin(t * 3), lit: 0.82, fade: 0.45 + 0.55 * drawn }; |
| 537 | } |
| 538 | case 'executing': { |
| 539 | if (part === 2) return { x: px, y: py + 0.004 * Math.sin(t * 3.1), lit: 0.94, fade: 0.98 }; |
| 540 | const cyc = (t * 0.62) % 1; |
| 541 | const ang = (cyc < 0.68 ? smoothstep(0, 0.68, cyc) * 20 - 10 : mix(10, -10, smoothstep(0.68, 1, cyc))) * DEG; |
| 542 | const dx = px - 0.28; |
| 543 | const dy = py + 0.28; |
| 544 | return { |
| 545 | x: 0.28 + dx * Math.cos(ang) - dy * Math.sin(ang), |
| 546 | y: -0.28 + dx * Math.sin(ang) + dy * Math.cos(ang), |
| 547 | lit: zone === 1 ? 0.65 : 0.35 + rim * 0.35, |
| 548 | fade: 0.80 + rim * 0.20, |
| 549 | }; |
| 550 | } |
| 551 | case 'testing': { |
| 552 | if (part === 2) { |
| 553 | const rise = (t * 0.65 + u * 2.1) % 1; |
| 554 | return { x: (px - 0.02) * 0.6 + 0.03 * Math.sin(rise * 8 + u * 5), y: mix(0.24, -0.78, rise), lit: 0.94, fade: 0.95 * (1 - smoothstep(0.82, 1, rise)) }; |
| 555 | } |
| 556 | const wave = part === 1 ? 0.018 * Math.sin(px * 7 + t * 3.4) : 0.004 * Math.sin(t * 2.2); |
| 557 | return { x: px, y: py + wave, lit: part === 1 ? 0.68 + rim * 0.22 : 0.36 + rim * 0.38, fade: 0.80 + rim * 0.20 }; |
| 558 | } |
| 559 | case 'browsing': { |
| 560 | if (part === 1 || part === 2) { |
| 561 | const R = Math.sqrt(Math.max(0.01, 0.64 * 0.64 - py * py)); |
| 562 | const lon = (part === 1 ? 0.50 : -0.50) + 0.20 * Math.sin(t * 1.45); |
| 563 | return { x: R * Math.sin(lon), y: py, lit: 0.65, fade: 0.88 }; |
| 564 | } |
| 565 | return { x: px, y: py + 0.004 * Math.sin(t * 1.8), lit: 0.45 + rim * 0.35, fade: 0.82 + rim * 0.18 }; |
| 566 | } |
| 567 | case 'computer': { |
| 568 | const cyc = (t * 0.55) % 1; |
| 569 | const press = cyc > 0.46 && cyc < 0.62 ? 0.03 : 0; |
| 570 | if (part === 2) { |
| 571 | const rp = clamp((cyc - 0.46) / 0.44); |
| 572 | const sc = 0.88 + rp * 0.58; |
| 573 | return { x: -0.24 + (px + 0.24) * sc, y: -0.48 + (py + 0.48) * sc, lit: 0.92, fade: cyc >= 0.46 ? (1 - rp * 0.75) * 0.95 : 0.30 }; |
| 574 | } |
| 575 | const glide = part === 1 ? 0.036 * Math.sin(cyc * TAU) : 0.004 * Math.sin(t * 2); |
| 576 | return { x: px + glide + press * 0.5, y: py + glide * 0.6 + press, lit: 0.52 + rim * 0.35, fade: 0.84 + rim * 0.16 }; |
| 577 | } |
| 578 | case 'memory': { |
| 579 | const layer = Math.max(0, part - 1); |
| 580 | const w = 0.5 + 0.5 * Math.sin(t * 2.4 - layer * 1.05); |
| 581 | return { x: px, y: py + 0.022 * Math.sin(t * 2.4 - layer * 0.9), lit: 0.35 + w * 0.42 + rim * 0.20, fade: 0.72 + w * 0.26 }; |
| 582 | } |
| 583 | case 'responding': { |
| 584 | if (part >= 1 && part <= 3) { |
| 585 | const k = part - 1; |
| 586 | return { x: px, y: py - 0.08 * Math.max(0, Math.sin(t * 4.0 - k * 0.95)), lit: 0.94, fade: 0.98 }; |
| 587 | } |
| 588 | return { x: px, y: py + 0.015 * Math.sin(t * 1.8), lit: 0.35 + rim * 0.38, fade: 0.76 + rim * 0.22 }; |
| 589 | } |
| 590 | case 'delegating': { |
| 591 | const wIdx = Math.floor((part - 1) / 2); |
| 592 | const fluke = (part - 1) % 2 === 1; |
| 593 | return { |
| 594 | x: px + 0.032 * Math.sin(t * 1.7 + wIdx * 1.9), |
| 595 | y: py + 0.022 * Math.cos(t * 2.1 + wIdx * 1.6) + (fluke ? 0.036 * Math.sin(t * 5.2 + wIdx * 1.8) : 0), |
| 596 | lit: zone >= 1 ? 0.86 : 0.40 + rim * 0.38, |
| 597 | fade: 0.82 + rim * 0.18, |
| 598 | }; |
| 599 | } |
| 600 | case 'waiting': { |
| 601 | if (part === 3) { |
| 602 | const sd = px < 0 ? -1 : 1; |
| 603 | const beat = 0.45 + 0.55 * Math.max(0, Math.sin(t * 5.2 * sd)); |
| 604 | return { x: px + sd * 0.026 * beat, y: py, lit: 0.92, fade: 0.55 + 0.45 * beat }; |
| 605 | } |
| 606 | const ang = (part === 2 ? 14 : 10) * DEG * Math.sin(t * 5.2 - (part === 2 ? 0.45 : 0)) * (0.65 + 0.35 * Math.sin(t * 1.3)); |
| 607 | const dy = py + 0.58; |
| 608 | return { |
| 609 | x: px * Math.cos(ang) - dy * Math.sin(ang), |
| 610 | y: -0.58 + px * Math.sin(ang) + dy * Math.cos(ang), |
| 611 | lit: part === 2 ? 0.95 : 0.46 + rim * 0.38, |
| 612 | fade: 0.84 + rim * 0.16, |
| 613 | }; |
| 614 | } |
| 615 | case 'done': { |
| 616 | const pop = 1 + 0.06 * Math.exp(-t * 2.2) * Math.cos(t * 11) + 0.008 * Math.sin(t * 5.5); |
| 617 | return { x: px * pop, y: py * pop, lit: 0.56 + rim * 0.36, fade: 0.88 + rim * 0.12 }; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 |