@egen-civitas/esm-styleguide 2.1.1 → 2.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,23 +1,27 @@
1
- "use client";
2
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
1
+ 'use client';
2
+ import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
3
3
  import React, { useEffect, useRef, useState } from "react";
4
4
  import { cn } from "../page-background/page-background.utils.js";
5
5
  export const WHITE = {
6
- color: "#F4F4F5",
7
- eyeColor: "#09090B"
6
+ color: '#F4F4F5',
7
+ eyeColor: '#09090B'
8
8
  };
9
9
  export const BLACK = {
10
- color: "#18181B",
11
- eyeColor: "#F4F4F5"
10
+ color: '#18181B',
11
+ eyeColor: '#F4F4F5'
12
12
  };
13
13
  export const CUSTOM_DEFAULT = {
14
- color: "#7C5CFF",
15
- eyeColor: "#FAFAFA"
14
+ color: '#7C5CFF',
15
+ eyeColor: '#FAFAFA'
16
16
  };
17
17
  /** Violet / foam / dust-rose — stock unlit wash. */ export const WEBGL_COLORS = [
18
- "#7C6AF7",
19
- "#7DD3C7",
20
- "#E8B4D4"
18
+ '#7C6AF7',
19
+ '#7DD3C7',
20
+ '#E8B4D4'
21
+ ];
22
+ /** Neon ring of the ear-cups: green → yellow (measured on the EGEN mascot). */ export const EAR_COLORS = [
23
+ '#3DBA2E',
24
+ '#F2E62F'
21
25
  ];
22
26
  const VERT = `
23
27
  attribute vec2 a_position;
@@ -26,19 +30,54 @@ void main() {
26
30
  }
27
31
  `;
28
32
  const FRAG = `
33
+ #ifdef GL_FRAGMENT_PRECISION_HIGH
29
34
  precision highp float;
35
+ #else
36
+ precision mediump float;
37
+ #endif
30
38
 
31
39
  uniform vec2 u_resolution;
32
40
  uniform float u_time;
33
41
  uniform float u_speed;
34
- uniform vec2 u_look;
42
+ uniform mat3 u_rot;
35
43
  uniform float u_blink;
36
44
  uniform float u_mode;
45
+ uniform float u_ears;
37
46
  uniform vec3 u_body;
38
47
  uniform vec3 u_eye;
39
48
  uniform vec3 u_c1;
40
49
  uniform vec3 u_c2;
41
50
  uniform vec3 u_c3;
51
+ uniform vec3 u_ear1;
52
+ uniform vec3 u_ear2;
53
+
54
+ // ---- Camera ---------------------------------------------------------------
55
+ const float CAM_DIST = 6.0;
56
+ const float HALF_VIEW = 1.5;
57
+
58
+ // ---- Head (local space, +z = face) ----------------------------------------
59
+ const vec3 HEAD_R = vec3(1.0, 0.74, 0.86);
60
+
61
+ // ---- Face (decals projected on the head surface) --------------------------
62
+ const float EYE_X = 0.455;
63
+ const float EYE_Y = 0.075;
64
+ const float EYE_RX = 0.118;
65
+ const float EYE_RY = 0.128;
66
+ const float LINE_HALF = 0.021;
67
+
68
+ // ---- Ear-cups (headphones) -------------------------------------------------
69
+ const vec3 EAR_C = vec3(1.02, -0.10, 0.02);
70
+ const float EAR_R = 0.385;
71
+ const float EAR_HT = 0.072;
72
+ const float EAR_ROUND = 0.045;
73
+ const float RECESS_R = 0.335;
74
+ const float RECESS_D = 0.020;
75
+ const float RING_R = 0.272;
76
+ const float RING_T = 0.034;
77
+ const float RING_X = 0.062;
78
+ // cos / sin of the toe-in (cup faces slightly forward)
79
+ const float EC = 0.9287;
80
+ const float ES = 0.3709;
42
81
 
43
82
  float hash(vec2 p) {
44
83
  return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453123);
@@ -67,27 +106,6 @@ float fbm(vec2 p) {
67
106
  return v;
68
107
  }
69
108
 
70
- vec3 orient(vec3 p, vec2 look) {
71
- float yaw = look.x * 0.92;
72
- float pitch = -look.y * 0.78;
73
- float cy = cos(yaw);
74
- float sy = sin(yaw);
75
- float cp = cos(pitch);
76
- float sp = sin(pitch);
77
- vec3 q = vec3(p.x, p.y * cp - p.z * sp, p.y * sp + p.z * cp);
78
- return vec3(q.x * cy + q.z * sy, q.y, -q.x * sy + q.z * cy);
79
- }
80
-
81
- float eyeMask(vec3 n, vec3 e, vec3 right, vec3 up, float halfH, float rad) {
82
- float facing = dot(n, e);
83
- float x = dot(n, right) - dot(e, right);
84
- float y = dot(n, up) - dot(e, up);
85
- y -= clamp(y, -halfH, halfH);
86
- float d = length(vec2(x, y)) - rad;
87
- float fill = 1.0 - smoothstep(-0.01, 0.01, d);
88
- return fill * smoothstep(0.12, 0.32, facing);
89
- }
90
-
91
109
  vec3 wash(vec3 n, float t) {
92
110
  vec2 q = n.xy * 2.2 + n.z * 0.85;
93
111
  vec2 w1 = vec2(
@@ -101,51 +119,248 @@ vec3 wash(vec3 n, float t) {
101
119
  return mix(col, u_c3, pow(smoothstep(0.42, 0.9, g), 1.4));
102
120
  }
103
121
 
122
+ float smin(float a, float b, float k) {
123
+ float h = max(k - abs(a - b), 0.0) / k;
124
+ return min(a, b) - h * h * k * 0.25;
125
+ }
126
+
127
+ float smax(float a, float b, float k) {
128
+ return -smin(-a, -b, k);
129
+ }
130
+
131
+ float sdEllipsoid(vec3 p, vec3 r) {
132
+ float k0 = length(p / r);
133
+ float k1 = length(p / (r * r));
134
+ return k0 * (k0 - 1.0) / max(k1, 1e-4);
135
+ }
136
+
137
+ // Ear frame: x = cup axis (outward, toed-in), (y, z) = radial plane. Mirrored.
138
+ vec3 earFrame(vec3 p) {
139
+ vec3 q = vec3(abs(p.x), p.y, p.z) - EAR_C;
140
+ return vec3(EC * q.x + ES * q.z, q.y, -ES * q.x + EC * q.z);
141
+ }
142
+
143
+ float sdCup(vec3 e) {
144
+ float rr = EAR_ROUND;
145
+ float rad = length(e.yz);
146
+ vec2 w = vec2(rad - EAR_R + rr, abs(e.x) - EAR_HT + rr);
147
+ float d = min(max(w.x, w.y), 0.0) + length(max(w, 0.0)) - rr;
148
+ // shallow recess in the outer face: the rim stays proud around it
149
+ float rec = max(rad - RECESS_R, (EAR_HT - RECESS_D) - e.x);
150
+ return smax(d, -rec, 0.012);
151
+ }
152
+
153
+ float sdRing(vec3 e) {
154
+ vec2 t = vec2(length(e.yz) - RING_R, e.x - RING_X);
155
+ return length(t) - RING_T;
156
+ }
157
+
158
+ // x = distance to the whole shape, y = distance to the neon ring
159
+ vec2 map(vec3 p) {
160
+ float d = sdEllipsoid(p, HEAD_R);
161
+ float dr = 1e3;
162
+ if (u_ears > 0.5) {
163
+ vec3 e = earFrame(p);
164
+ d = smin(d, sdCup(e), 0.045);
165
+ dr = sdRing(e);
166
+ d = min(d, dr);
167
+ }
168
+ return vec2(d, dr);
169
+ }
170
+
171
+ vec3 calcNormal(vec3 p) {
172
+ const vec2 k = vec2(1.0, -1.0);
173
+ const float h = 0.0016;
174
+ return normalize(
175
+ k.xyy * map(p + k.xyy * h).x +
176
+ k.yyx * map(p + k.yyx * h).x +
177
+ k.yxy * map(p + k.yxy * h).x +
178
+ k.xxx * map(p + k.xxx * h).x
179
+ );
180
+ }
181
+
182
+ float calcAO(vec3 p, vec3 n) {
183
+ float occ = 0.0;
184
+ float s = 1.0;
185
+ for (int i = 1; i <= 4; i++) {
186
+ float h = 0.045 * float(i);
187
+ occ += (h - map(p + n * h).x) * s;
188
+ s *= 0.72;
189
+ }
190
+ return clamp(1.0 - 2.4 * occ, 0.0, 1.0);
191
+ }
192
+
193
+ float softShadow(vec3 ro, vec3 rd) {
194
+ float res = 1.0;
195
+ float t = 0.05;
196
+ for (int i = 0; i < 22; i++) {
197
+ float h = map(ro + rd * t).x;
198
+ res = min(res, 9.0 * h / t);
199
+ t += clamp(h, 0.02, 0.22);
200
+ if (res < 0.002 || t > 2.6) break;
201
+ }
202
+ return clamp(res, 0.0, 1.0);
203
+ }
204
+
104
205
  void main() {
105
206
  vec2 uv = (gl_FragCoord.xy / u_resolution.xy) * 2.0 - 1.0;
106
207
  float aspect = u_resolution.x / max(u_resolution.y, 1.0);
107
208
  uv.x *= aspect;
108
209
 
109
- float radius = 0.94;
110
- vec2 p = uv / radius;
111
- float r2 = dot(p, p);
112
- float edge = 1.0 - smoothstep(0.985, 1.012, sqrt(max(r2, 0.0)));
113
- if (edge <= 0.001) {
210
+ float kView = HALF_VIEW / CAM_DIST;
211
+ float pixAng = 2.0 * kView / max(u_resolution.y, 1.0);
212
+
213
+ vec3 rdW = normalize(vec3(uv * kView, -1.0));
214
+ vec3 roW = vec3(0.0, 0.0, CAM_DIST);
215
+ // world -> head space (u_rot is head -> world; v * M == transpose(M) * v)
216
+ vec3 ro = roW * u_rot;
217
+ vec3 rd = rdW * u_rot;
218
+
219
+ // bounding sphere
220
+ float bnd = 1.55;
221
+ float b = dot(ro, rd);
222
+ float c = dot(ro, ro) - bnd * bnd;
223
+ float disc = b * b - c;
224
+ if (disc < 0.0) {
114
225
  gl_FragColor = vec4(0.0);
115
226
  return;
116
227
  }
228
+ float sq = sqrt(disc);
229
+ float tEnd = -b + sq;
230
+ float t = max(-b - sq, 0.0);
117
231
 
118
- float z = sqrt(max(1.0 - r2, 0.0));
119
- vec3 n = normalize(vec3(p, z));
232
+ float minD = 1e3;
233
+ float tMin = t;
234
+ float glow = 0.0;
235
+ bool hit = false;
236
+ vec2 m = vec2(1e3);
120
237
 
121
- vec2 look = u_look;
122
- float lm = length(look);
123
- if (lm > 1.0) look /= lm;
238
+ for (int i = 0; i < 80; i++) {
239
+ vec3 p = ro + rd * t;
240
+ m = map(p);
241
+ float eps = max(0.0007, 0.35 * t * pixAng);
242
+ if (m.x < eps) {
243
+ hit = true;
244
+ break;
245
+ }
246
+ if (m.x < minD) {
247
+ minD = m.x;
248
+ tMin = t;
249
+ }
250
+ float stp = m.x * 0.85;
251
+ glow += exp(-m.y * m.y * 150.0) * stp;
252
+ t += stp;
253
+ if (t > tEnd) break;
254
+ }
124
255
 
125
- vec3 right = orient(vec3(1.0, 0.0, 0.0), look);
126
- vec3 up = orient(vec3(0.0, 1.0, 0.0), look);
127
- vec3 eL = orient(normalize(vec3(-0.32, 0.08, 1.0)), look);
128
- vec3 eR = orient(normalize(vec3(0.32, 0.08, 1.0)), look);
256
+ vec3 glowCol = mix(u_ear1, u_ear2, 0.5);
257
+ float ga = (u_ears > 0.5) ? (1.0 - exp(-glow * 3.0)) * 0.8 : 0.0;
129
258
 
130
- float halfH = mix(0.128, 0.012, u_blink);
131
- float rad = mix(0.054, 0.062, u_blink);
132
- float eyes = max(
133
- eyeMask(n, eL, right, up, halfH, rad),
134
- eyeMask(n, eR, right, up, halfH, rad)
135
- );
259
+ float cov = 0.0;
260
+ vec3 col = vec3(0.0);
261
+
262
+ if (hit) {
263
+ cov = 1.0;
264
+ vec3 pl = ro + rd * t;
265
+ vec3 nl = calcNormal(pl);
266
+ vec3 nW = normalize(u_rot * nl);
267
+ vec3 v = -rdW;
268
+
269
+ // lights are fixed in the world: turning the head re-shades it
270
+ vec3 keyW = normalize(vec3(-0.5, 0.72, 0.78));
271
+ vec3 fillW = normalize(vec3(0.85, 0.05, 0.45));
272
+ vec3 keyL = keyW * u_rot;
273
+ float sh = softShadow(pl + nl * 0.012, keyL);
274
+ float ao = calcAO(pl, nl);
136
275
 
137
- vec3 body = u_mode > 0.5 ? wash(n, u_time * u_speed) : u_body;
138
- vec3 col = mix(body, u_eye, clamp(eyes, 0.0, 1.0));
276
+ float ndl = dot(nW, keyW);
277
+ float wrap = clamp((ndl + 0.35) / 1.35, 0.0, 1.0);
278
+ float fill = clamp((dot(nW, fillW) + 0.3) / 1.3, 0.0, 1.0);
279
+ float sky = nW.y * 0.5 + 0.5;
280
+ vec3 amb = mix(vec3(0.60, 0.61, 0.64), vec3(0.80, 0.81, 0.84), sky);
281
+ vec3 lightSum = amb * ao
282
+ + vec3(1.0, 0.975, 0.94) * 0.62 * wrap * mix(0.55, 1.0, sh) * (0.55 + 0.45 * ao)
283
+ + vec3(0.82, 0.86, 1.0) * 0.12 * fill * ao;
139
284
 
140
- gl_FragColor = vec4(clamp(col, 0.0, 1.0), edge);
285
+ vec3 hv = normalize(keyW + v);
286
+ float ndh = max(dot(nW, hv), 0.0);
287
+ float specK = (pow(ndh, 64.0) * 0.55 + pow(ndh, 14.0) * 0.10) * sh;
288
+ float ndv = max(dot(nW, v), 0.0);
289
+ float fres = 0.04 + 0.96 * pow(1.0 - ndv, 5.0);
290
+ vec3 refl = reflect(-v, nW);
291
+ vec3 env = mix(vec3(0.30, 0.31, 0.34), vec3(0.96, 0.97, 0.99), smoothstep(-0.25, 0.95, refl.y));
292
+
293
+ float px = t * pixAng;
294
+ vec3 body = (u_mode > 0.5) ? wash(nl, u_time * u_speed) : u_body;
295
+
296
+ // ---- ear materials ------------------------------------------------------
297
+ float plate = 0.0;
298
+ float ringMask = 0.0;
299
+ vec3 ringCol = vec3(0.0);
300
+ float spill = 0.0;
301
+ if (u_ears > 0.5) {
302
+ vec3 e = earFrame(pl);
303
+ float rad = length(e.yz);
304
+ float outward = smoothstep(0.02, 0.05, e.x);
305
+ plate = (1.0 - smoothstep(RECESS_R - 0.006 - px, RECESS_R + 0.004 + px, rad)) * outward;
306
+ float dRing = sdRing(e);
307
+ ringMask = 1.0 - smoothstep(0.0, 0.01 + px, dRing);
308
+ float ang = atan(e.z, e.y);
309
+ float gradT = 0.5 + 0.5 * sin(ang + (pl.x > 0.0 ? 0.6 : 2.5));
310
+ vec3 neon = mix(u_ear1, u_ear2, gradT);
311
+ float core = exp(-pow(dRing / RING_T + 1.0, 2.0) * 1.1);
312
+ ringCol = mix(neon, vec3(1.0), 0.40 * core) * (1.05 + 0.08 * sin(u_time * 1.7));
313
+ spill = exp(-max(dRing, 0.0) * 14.0) * outward;
314
+ }
315
+ vec3 plateCol = mix(vec3(0.16, 0.17, 0.20), u_body * 0.22 + vec3(0.07), 0.5);
316
+ body = mix(body, plateCol, plate);
317
+
318
+ // ---- face decals: eyes + connecting line --------------------------------
319
+ float onFace = smoothstep(0.18, 0.42, pl.z);
320
+ float lid = mix(1.0, 0.10, u_blink);
321
+ float aa = max(px * 1.1, 0.0035);
322
+ vec2 pe = pl.xy - vec2(0.0, EYE_Y);
323
+ vec2 dl = vec2(abs(pe.x) - EYE_X, pe.y) / vec2(EYE_RX, EYE_RY * lid);
324
+ float eyeM = 1.0 - smoothstep(1.0 - aa / EYE_RX, 1.0 + aa / EYE_RX, length(dl));
325
+ float lw = max(LINE_HALF, 0.6 * px);
326
+ float lineM =
327
+ (1.0 - smoothstep(lw - aa, lw + aa, abs(pe.y))) *
328
+ (1.0 - smoothstep(EYE_X - aa, EYE_X + aa, abs(pe.x)));
329
+ float face = clamp(max(eyeM, lineM), 0.0, 1.0) * onFace;
330
+
331
+ // ---- shade ----------------------------------------------------------------
332
+ vec3 lit = body * lightSum;
333
+ lit += vec3(1.0) * specK * (1.0 - plate * 0.6);
334
+ lit = mix(lit, env * mix(vec3(1.0), body, 0.35), fres * 0.42);
335
+
336
+ vec3 eyeLit = u_eye * (0.84 + 0.16 * wrap) + vec3(1.0) * specK * 0.42 + env * fres * 0.16;
337
+ col = mix(lit, eyeLit, face);
338
+
339
+ // neon spills light on the cup face and on the head next to it
340
+ col += mix(u_ear1, u_ear2, 0.5) * spill * 0.34 * (1.0 - face);
341
+ // the ring itself emits: no lighting
342
+ col = mix(col, ringCol, ringMask);
343
+ } else if (minD < 1e2) {
344
+ // anti-aliased silhouette: how close did the ray pass to the surface?
345
+ float r = minD / max(tMin * pixAng, 1e-5);
346
+ cov = 1.0 - smoothstep(0.0, 1.15, r);
347
+ // soft rim colour for the faded edge: reuse the body tone
348
+ vec3 body = (u_mode > 0.5) ? wash(normalize(ro + rd * tMin), u_time * u_speed) : u_body;
349
+ col = body * 0.9;
350
+ }
351
+
352
+ // neon halo behind / around the ring
353
+ vec3 premul = col * cov + glowCol * ga * (1.0 - cov);
354
+ float a = cov + ga * (1.0 - cov);
355
+ gl_FragColor = vec4(clamp(premul / max(a, 1e-4), 0.0, 1.0), clamp(a, 0.0, 1.0));
141
356
  }
142
357
  `;
143
358
  function isDev() {
144
- return typeof process !== "undefined" && process.env?.NODE_ENV !== "production";
359
+ return typeof process !== 'undefined' && process.env?.NODE_ENV !== 'production';
145
360
  }
146
361
  function hexToRgb(hex) {
147
- const h = hex.replace("#", "").trim();
148
- const full = h.length === 3 ? h.split("").map((c)=>c + c).join("") : h.padEnd(6, "0").slice(0, 6);
362
+ const h = hex.replace('#', '').trim();
363
+ const full = h.length === 3 ? h.split('').map((c)=>c + c).join('') : h.padEnd(6, '0').slice(0, 6);
149
364
  const n = Number.parseInt(full, 16);
150
365
  if (Number.isNaN(n)) return [
151
366
  0.96,
@@ -165,35 +380,48 @@ function compile(gl, type, source) {
165
380
  gl.compileShader(shader);
166
381
  if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
167
382
  if (isDev()) {
168
- console.warn("LiveOrb: shader failed to compile\n", gl.getShaderInfoLog(shader));
383
+ console.warn('LiveOrb: shader failed to compile\n', gl.getShaderInfoLog(shader));
169
384
  }
170
385
  gl.deleteShader(shader);
171
386
  return null;
172
387
  }
173
388
  return shader;
174
389
  }
175
- export function resolveVariant(variant, color, eyeColor, colors) {
390
+ export function resolveVariant(variant, color, eyeColor, colors, earColors) {
176
391
  let body = WHITE.color;
177
392
  let eye = WHITE.eyeColor;
178
393
  let mode = 0;
179
394
  let palette = WEBGL_COLORS;
180
- if (variant === "black") {
395
+ let ear = EAR_COLORS;
396
+ if (variant === 'black') {
181
397
  body = BLACK.color;
182
398
  eye = BLACK.eyeColor;
183
- } else if (variant === "webgl") {
399
+ } else if (variant === 'webgl') {
184
400
  palette = colors && colors.length > 0 ? colors : WEBGL_COLORS;
185
401
  body = palette[1] ?? WEBGL_COLORS[1];
186
- eye = "#0C0C10";
402
+ eye = '#0C0C10';
187
403
  mode = 1;
188
- } else if (variant === "custom") {
404
+ // the neon ring borrows the wash so the whole head stays one palette
405
+ ear = [
406
+ palette[0] ?? WEBGL_COLORS[0],
407
+ palette[2] ?? WEBGL_COLORS[2]
408
+ ];
409
+ } else if (variant === 'custom') {
189
410
  body = color ?? CUSTOM_DEFAULT.color;
190
411
  eye = eyeColor ?? CUSTOM_DEFAULT.eyeColor;
191
412
  }
413
+ if (earColors && earColors.length > 0) {
414
+ ear = [
415
+ earColors[0],
416
+ earColors[1] ?? earColors[0]
417
+ ];
418
+ }
192
419
  return {
193
420
  body,
194
421
  eye,
195
422
  mode,
196
- palette
423
+ palette,
424
+ ear
197
425
  };
198
426
  }
199
427
  export function fallbackFaceStyle(resolved) {
@@ -209,32 +437,73 @@ export function fallbackFaceStyle(resolved) {
209
437
  backgroundColor: resolved.body
210
438
  };
211
439
  }
440
+ /** Head limits (radians) reached when the pointer is far away. */ const MAX_YAW = 0.62;
441
+ const MAX_PITCH = 0.4;
442
+ /** Resting gaze: a hair above the horizon, like a head held up. */ const NEUTRAL_Y = 0.06;
443
+ /**
444
+ * Head → world rotation, column-major (what `uniformMatrix3fv` expects).
445
+ * Order: roll about the face axis, then nod (pitch), then turn (yaw) —
446
+ * so the head turns about the vertical axis, like a neck would.
447
+ */ export function headRotation(yaw, pitch, roll) {
448
+ const cy = Math.cos(yaw);
449
+ const sy = Math.sin(yaw);
450
+ const cp = Math.cos(pitch);
451
+ const sp = Math.sin(pitch);
452
+ const cr = Math.cos(roll);
453
+ const sr = Math.sin(roll);
454
+ // M = Ry(yaw) · Rx(pitch) · Rz(roll)
455
+ const r00 = cy * cr + sy * sp * sr;
456
+ const r01 = -cy * sr + sy * sp * cr;
457
+ const r02 = sy * cp;
458
+ const r10 = cp * sr;
459
+ const r11 = cp * cr;
460
+ const r12 = -sp;
461
+ const r20 = -sy * cr + cy * sp * sr;
462
+ const r21 = sy * sr + cy * sp * cr;
463
+ const r22 = cy * cp;
464
+ return new Float32Array([
465
+ r00,
466
+ r10,
467
+ r20,
468
+ r01,
469
+ r11,
470
+ r21,
471
+ r02,
472
+ r12,
473
+ r22
474
+ ]);
475
+ }
212
476
  /**
213
- * Evenly lit sphere with two capsule eyes that follow the pointer.
214
- * The orb stays put — only the gaze moves.
477
+ * Evenly lit 3D head (Baymax-style): wide rounded skull, two oval eyes joined
478
+ * by a thin line, glowing headphone ear-cups. The head turns toward the
479
+ * pointer; eyes, line and ears are rigid with it, so they follow the surface.
215
480
  */ export function createLiveOrb(canvas, initial = {}) {
216
481
  let options = {
217
- variant: "white",
482
+ variant: 'white',
218
483
  interactive: true,
219
484
  blink: true,
485
+ ears: true,
220
486
  ...initial
221
487
  };
488
+ // `look` is the head pose in [-1, 1]² (x: turn right, y: look up).
222
489
  const look = {
223
490
  x: 0,
224
- y: 0.08
491
+ y: NEUTRAL_Y,
492
+ vx: 0,
493
+ vy: 0
225
494
  };
226
495
  const targetLook = {
227
496
  x: 0,
228
- y: 0.08
497
+ y: NEUTRAL_Y
229
498
  };
230
499
  let reduce = false;
231
- const gl = canvas.getContext("webgl", {
500
+ const gl = canvas.getContext('webgl', {
232
501
  alpha: true,
233
502
  antialias: false,
234
503
  depth: false,
235
504
  stencil: false,
236
505
  premultipliedAlpha: false,
237
- powerPreference: "high-performance"
506
+ powerPreference: 'high-performance'
238
507
  });
239
508
  if (!gl) return null;
240
509
  const vs = compile(gl, gl.VERTEX_SHADER, VERT);
@@ -247,7 +516,7 @@ export function fallbackFaceStyle(resolved) {
247
516
  gl.linkProgram(program);
248
517
  if (!gl.getProgramParameter(program, gl.LINK_STATUS)) {
249
518
  if (isDev()) {
250
- console.warn("LiveOrb: program failed to link\n", gl.getProgramInfoLog(program));
519
+ console.warn('LiveOrb: program failed to link\n', gl.getProgramInfoLog(program));
251
520
  }
252
521
  return null;
253
522
  }
@@ -269,25 +538,31 @@ export function fallbackFaceStyle(resolved) {
269
538
  1,
270
539
  1
271
540
  ]), gl.STATIC_DRAW);
272
- const loc = gl.getAttribLocation(program, "a_position");
541
+ const loc = gl.getAttribLocation(program, 'a_position');
273
542
  gl.enableVertexAttribArray(loc);
274
543
  gl.vertexAttribPointer(loc, 2, gl.FLOAT, false, 0, 0);
275
- const uResolution = gl.getUniformLocation(program, "u_resolution");
276
- const uTime = gl.getUniformLocation(program, "u_time");
277
- const uSpeed = gl.getUniformLocation(program, "u_speed");
278
- const uLook = gl.getUniformLocation(program, "u_look");
279
- const uBlink = gl.getUniformLocation(program, "u_blink");
280
- const uMode = gl.getUniformLocation(program, "u_mode");
281
- const uBody = gl.getUniformLocation(program, "u_body");
282
- const uEye = gl.getUniformLocation(program, "u_eye");
283
- const uC1 = gl.getUniformLocation(program, "u_c1");
284
- const uC2 = gl.getUniformLocation(program, "u_c2");
285
- const uC3 = gl.getUniformLocation(program, "u_c3");
286
- gl.enable(gl.BLEND);
287
- gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
544
+ const u = (name)=>gl.getUniformLocation(program, name);
545
+ const uResolution = u('u_resolution');
546
+ const uTime = u('u_time');
547
+ const uSpeed = u('u_speed');
548
+ const uRot = u('u_rot');
549
+ const uBlink = u('u_blink');
550
+ const uMode = u('u_mode');
551
+ const uEars = u('u_ears');
552
+ const uBody = u('u_body');
553
+ const uEye = u('u_eye');
554
+ const uC1 = u('u_c1');
555
+ const uC2 = u('u_c2');
556
+ const uC3 = u('u_c3');
557
+ const uEar1 = u('u_ear1');
558
+ const uEar2 = u('u_ear2');
559
+ // The fragment shader writes final straight-alpha colour: no blending.
560
+ gl.disable(gl.BLEND);
288
561
  let raf = 0;
289
562
  let running = true;
563
+ let visible = true;
290
564
  const start = performance.now();
565
+ let last = start;
291
566
  let nextBlink = start + 1800 + Math.random() * 2400;
292
567
  let blinkAt = -10000;
293
568
  const resize = ()=>{
@@ -306,37 +581,72 @@ export function fallbackFaceStyle(resolved) {
306
581
  resize();
307
582
  const ro = new ResizeObserver(resize);
308
583
  if (canvas.parentElement) ro.observe(canvas.parentElement);
309
- const mqReduce = window.matchMedia("(prefers-reduced-motion: reduce)");
584
+ // No need to burn GPU while the head is scrolled away / in a hidden tab.
585
+ const io = typeof IntersectionObserver !== 'undefined' ? new IntersectionObserver(([entry])=>{
586
+ visible = entry ? entry.isIntersecting : true;
587
+ }) : null;
588
+ io?.observe(canvas);
589
+ const mqReduce = window.matchMedia('(prefers-reduced-motion: reduce)');
310
590
  const onReduce = ()=>{
311
591
  reduce = mqReduce.matches;
312
592
  };
313
593
  onReduce();
314
- mqReduce.addEventListener("change", onReduce);
594
+ mqReduce.addEventListener('change', onReduce);
315
595
  const onMove = (e)=>{
316
596
  if (!options.interactive) return;
317
597
  const parent = canvas.parentElement;
318
598
  if (!parent) return;
319
599
  const rect = parent.getBoundingClientRect();
320
600
  if (rect.width <= 0 || rect.height <= 0) return;
321
- const dx = (e.clientX - (rect.left + rect.width / 2)) / (rect.width / 2);
322
- const dy = (rect.top + rect.height / 2 - e.clientY) / (rect.height / 2);
323
- targetLook.x = Math.min(1, Math.max(-1, dx));
324
- targetLook.y = Math.min(1, Math.max(-1, dy));
601
+ // Angle-like mapping: a small avatar still turns smoothly over a long
602
+ // distance instead of snapping to the limit a few pixels from its centre.
603
+ const reach = Math.max(rect.width, rect.height) * 3 + 120;
604
+ const dx = e.clientX - (rect.left + rect.width / 2);
605
+ const dy = rect.top + rect.height / 2 - e.clientY;
606
+ targetLook.x = Math.tanh(dx / reach * 2.2);
607
+ targetLook.y = Math.tanh(dy / reach * 2.2);
325
608
  };
326
- window.addEventListener("pointermove", onMove, {
609
+ const onLeave = (e)=>{
610
+ if (e.relatedTarget === null) {
611
+ targetLook.x = 0;
612
+ targetLook.y = NEUTRAL_Y;
613
+ }
614
+ };
615
+ window.addEventListener('pointermove', onMove, {
327
616
  passive: true
328
617
  });
618
+ document.addEventListener('mouseout', onLeave);
619
+ const onLost = (e)=>{
620
+ e.preventDefault();
621
+ running = false;
622
+ cancelAnimationFrame(raf);
623
+ options.onHasGl?.(false);
624
+ };
625
+ canvas.addEventListener('webglcontextlost', onLost);
329
626
  const tick = (now)=>{
330
627
  if (!running) return;
331
- const resolved = resolveVariant(options.variant, options.color, options.eyeColor, options.colors);
628
+ raf = requestAnimationFrame(tick);
629
+ const dt = Math.min(0.05, Math.max(0.001, (now - last) / 1000));
630
+ last = now;
631
+ if (!visible || document.hidden) return;
632
+ const resolved = resolveVariant(options.variant, options.color, options.eyeColor, options.colors, options.earColors);
332
633
  const time = (now - start) / 1000;
333
- if (options.interactive) {
334
- look.x += (targetLook.x - look.x) * 0.16;
335
- look.y += (targetLook.y - look.y) * 0.16;
336
- } else {
337
- look.x += (0 - look.x) * 0.12;
338
- look.y += (0.08 - look.y) * 0.12;
339
- }
634
+ // Spring-damped head pose: a slight overshoot makes it feel like a neck.
635
+ const tx = options.interactive ? targetLook.x : 0;
636
+ const ty = options.interactive ? targetLook.y : NEUTRAL_Y;
637
+ const K = 130;
638
+ const C = 17;
639
+ look.vx += ((tx - look.x) * K - look.vx * C) * dt;
640
+ look.vy += ((ty - look.y) * K - look.vy * C) * dt;
641
+ look.x += look.vx * dt;
642
+ look.y += look.vy * dt;
643
+ // Idle life: a barely-there sway so the 3D form reads even at rest.
644
+ const sway = reduce ? 0 : 1;
645
+ const lx = Math.max(-1, Math.min(1, look.x)) + Math.sin(time * 0.9) * 0.03 * sway;
646
+ const ly = Math.max(-1, Math.min(1, look.y)) + Math.sin(time * 0.67 + 1.3) * 0.022 * sway;
647
+ const yaw = lx * MAX_YAW;
648
+ const pitch = -ly * MAX_PITCH;
649
+ const roll = -lx * 0.09;
340
650
  if (!reduce && options.blink && now >= nextBlink) {
341
651
  blinkAt = now;
342
652
  nextBlink = now + 2200 + Math.random() * 3800;
@@ -355,21 +665,25 @@ export function fallbackFaceStyle(resolved) {
355
665
  1,
356
666
  2
357
667
  ].map((i)=>hexToRgb(resolved.palette[i % resolved.palette.length] ?? WEBGL_COLORS[i]));
668
+ const ear1 = hexToRgb(resolved.ear[0] ?? EAR_COLORS[0]);
669
+ const ear2 = hexToRgb(resolved.ear[1] ?? EAR_COLORS[1]);
358
670
  gl.clearColor(0, 0, 0, 0);
359
671
  gl.clear(gl.COLOR_BUFFER_BIT);
360
672
  gl.uniform2f(uResolution, canvas.width, canvas.height);
361
673
  gl.uniform1f(uTime, reduce ? 0 : time);
362
674
  gl.uniform1f(uSpeed, reduce ? 0 : 0.55);
363
- gl.uniform2f(uLook, look.x, look.y);
675
+ gl.uniformMatrix3fv(uRot, false, headRotation(yaw, pitch, roll));
364
676
  gl.uniform1f(uBlink, b);
365
677
  gl.uniform1f(uMode, resolved.mode);
678
+ gl.uniform1f(uEars, options.ears ? 1 : 0);
366
679
  gl.uniform3f(uBody, body[0], body[1], body[2]);
367
680
  gl.uniform3f(uEye, eye[0], eye[1], eye[2]);
368
681
  gl.uniform3f(uC1, palette[0][0], palette[0][1], palette[0][2]);
369
682
  gl.uniform3f(uC2, palette[1][0], palette[1][1], palette[1][2]);
370
683
  gl.uniform3f(uC3, palette[2][0], palette[2][1], palette[2][2]);
684
+ gl.uniform3f(uEar1, ear1[0], ear1[1], ear1[2]);
685
+ gl.uniform3f(uEar2, ear2[0], ear2[1], ear2[2]);
371
686
  gl.drawArrays(gl.TRIANGLES, 0, 6);
372
- raf = requestAnimationFrame(tick);
373
687
  };
374
688
  raf = requestAnimationFrame(tick);
375
689
  return {
@@ -383,8 +697,11 @@ export function fallbackFaceStyle(resolved) {
383
697
  running = false;
384
698
  cancelAnimationFrame(raf);
385
699
  ro.disconnect();
386
- mqReduce.removeEventListener("change", onReduce);
387
- window.removeEventListener("pointermove", onMove);
700
+ io?.disconnect();
701
+ mqReduce.removeEventListener('change', onReduce);
702
+ window.removeEventListener('pointermove', onMove);
703
+ document.removeEventListener('mouseout', onLeave);
704
+ canvas.removeEventListener('webglcontextlost', onLost);
388
705
  options.onHasGl?.(false);
389
706
  gl.deleteProgram(program);
390
707
  gl.deleteShader(vs);
@@ -394,9 +711,151 @@ export function fallbackFaceStyle(resolved) {
394
711
  };
395
712
  }
396
713
  /**
397
- * Evenly lit sphere with two capsule eyes that follow the pointer.
398
- * The orb stays put — only the gaze moves.
399
- */ export function LiveOrb({ className, size = 280, variant = "white", color, eyeColor, colors, interactive = true, blink = true }) {
714
+ * CSS-only head used when WebGL is unavailable. Same design as the 3D head —
715
+ * wide skull, two oval eyes joined by a line, glowing ear-cups — and a light
716
+ * parallax toward the pointer so it still feels alive without a GPU.
717
+ * Geometry is expressed in % of the box (same units as the shader: the view
718
+ * spans 2.84 head-units, so 1 unit ≈ 35.2 %).
719
+ */ function FallbackHead({ resolved, ears, interactive }) {
720
+ const faceRef = useRef(null);
721
+ const earsRef = useRef(null);
722
+ useEffect(()=>{
723
+ if (!interactive) return undefined;
724
+ const face = faceRef.current;
725
+ const earsEl = earsRef.current;
726
+ const root = face?.parentElement;
727
+ if (!face || !root) return undefined;
728
+ const onMove = (e)=>{
729
+ const rect = root.getBoundingClientRect();
730
+ if (rect.width <= 0 || rect.height <= 0) return;
731
+ const reach = Math.max(rect.width, rect.height) * 3 + 120;
732
+ const x = Math.tanh((e.clientX - (rect.left + rect.width / 2)) / reach * 2.2);
733
+ const y = Math.tanh((rect.top + rect.height / 2 - e.clientY) / reach * 2.2);
734
+ // face features sit on the front: they slide the most
735
+ face.style.transform = `translate(${(x * 5.5).toFixed(2)}%, ${(-y * 3.4).toFixed(2)}%)`;
736
+ // ear-cups are at the back of the sides: they slide the other way, less
737
+ if (earsEl) {
738
+ earsEl.style.transform = `translate(${(-x * 1.8).toFixed(2)}%, ${(-y * 1.2).toFixed(2)}%)`;
739
+ }
740
+ };
741
+ window.addEventListener('pointermove', onMove, {
742
+ passive: true
743
+ });
744
+ return ()=>window.removeEventListener('pointermove', onMove);
745
+ }, [
746
+ interactive
747
+ ]);
748
+ const [c1, c2] = resolved.ear;
749
+ const slide = 'transform 140ms ease-out';
750
+ // Inline on purpose: the fallback must not depend on any stylesheet.
751
+ const abs = {
752
+ position: 'absolute'
753
+ };
754
+ const eye = {
755
+ ...abs,
756
+ backgroundColor: resolved.eye,
757
+ width: '8.3%',
758
+ height: '9%',
759
+ top: '42.9%',
760
+ borderRadius: '50%'
761
+ };
762
+ return /*#__PURE__*/ _jsxs(_Fragment, {
763
+ children: [
764
+ ears ? /*#__PURE__*/ _jsx("div", {
765
+ ref: earsRef,
766
+ "data-part": "ears",
767
+ "aria-hidden": true,
768
+ style: {
769
+ ...abs,
770
+ inset: 0,
771
+ transition: slide
772
+ },
773
+ children: [
774
+ 0,
775
+ 1
776
+ ].map((i)=>/*#__PURE__*/ _jsx("span", {
777
+ "data-part": "ear",
778
+ style: {
779
+ ...abs,
780
+ borderRadius: '50%',
781
+ left: i === 0 ? '9%' : '79%',
782
+ top: '37%',
783
+ width: '12%',
784
+ height: '30%',
785
+ background: `linear-gradient(${i === 0 ? 135 : 225}deg, ${c1}, ${c2})`,
786
+ boxShadow: `0 0 6px ${c2}66, inset 0 0 0 2px #0000001f`
787
+ },
788
+ children: /*#__PURE__*/ _jsx("span", {
789
+ style: {
790
+ ...abs,
791
+ borderRadius: '50%',
792
+ inset: '16% 22%',
793
+ background: 'radial-gradient(circle at 40% 30%, #3b3e45, #15171b)'
794
+ }
795
+ })
796
+ }, i))
797
+ }) : null,
798
+ /*#__PURE__*/ _jsx("div", {
799
+ "aria-hidden": true,
800
+ "data-part": "head",
801
+ style: {
802
+ ...abs,
803
+ overflow: 'hidden',
804
+ left: '15%',
805
+ top: '24%',
806
+ width: '70%',
807
+ height: '52%',
808
+ borderRadius: '50%',
809
+ ...fallbackFaceStyle(resolved),
810
+ boxShadow: 'inset -6px -8px 14px #0000002e, inset 5px 6px 12px #ffffff66'
811
+ }
812
+ }),
813
+ /*#__PURE__*/ _jsxs("div", {
814
+ ref: faceRef,
815
+ "aria-hidden": true,
816
+ "data-part": "face",
817
+ style: {
818
+ ...abs,
819
+ inset: 0,
820
+ transition: slide
821
+ },
822
+ children: [
823
+ /*#__PURE__*/ _jsx("span", {
824
+ "data-part": "eye",
825
+ style: {
826
+ ...eye,
827
+ left: '29.9%'
828
+ }
829
+ }),
830
+ /*#__PURE__*/ _jsx("span", {
831
+ "data-part": "eye",
832
+ style: {
833
+ ...eye,
834
+ left: '61.8%'
835
+ }
836
+ }),
837
+ /*#__PURE__*/ _jsx("span", {
838
+ "data-part": "line",
839
+ style: {
840
+ ...abs,
841
+ backgroundColor: resolved.eye,
842
+ left: '34%',
843
+ width: '32%',
844
+ height: '1.6%',
845
+ top: '46.6%',
846
+ borderRadius: '999px'
847
+ }
848
+ })
849
+ ]
850
+ })
851
+ ]
852
+ });
853
+ }
854
+ /**
855
+ * Evenly lit 3D head (Baymax-style): wide rounded skull, two oval eyes joined
856
+ * by a thin line, glowing headphone ear-cups. The head turns to follow the
857
+ * pointer. Falls back to a CSS head when WebGL is unavailable.
858
+ */ export function LiveOrb({ className, size = 280, label = 'Assistant avatar', variant = 'white', color, eyeColor, colors, interactive = true, blink = true, ears = true, earColors }) {
400
859
  const canvasRef = useRef(null);
401
860
  const instanceRef = useRef(null);
402
861
  const [hasGl, setHasGl] = useState(false);
@@ -410,6 +869,8 @@ export function fallbackFaceStyle(resolved) {
410
869
  colors,
411
870
  interactive,
412
871
  blink,
872
+ ears,
873
+ earColors,
413
874
  onHasGl: setHasGl
414
875
  });
415
876
  return ()=>{
@@ -427,6 +888,8 @@ export function fallbackFaceStyle(resolved) {
427
888
  colors,
428
889
  interactive,
429
890
  blink,
891
+ ears,
892
+ earColors,
430
893
  onHasGl: setHasGl
431
894
  });
432
895
  }, [
@@ -435,45 +898,26 @@ export function fallbackFaceStyle(resolved) {
435
898
  eyeColor,
436
899
  colors,
437
900
  interactive,
438
- blink
901
+ blink,
902
+ ears,
903
+ earColors
439
904
  ]);
440
- const resolved = resolveVariant(variant, color, eyeColor, colors);
905
+ const resolved = resolveVariant(variant, color, eyeColor, colors, earColors);
441
906
  return /*#__PURE__*/ _jsxs("div", {
442
907
  "data-slot": "live-orb",
443
908
  role: "img",
444
- "aria-label": "Orb character",
445
- className: cn("relative shrink-0", className),
909
+ "aria-label": label,
910
+ className: cn('relative shrink-0', className),
446
911
  style: {
912
+ position: 'relative',
447
913
  width: size,
448
914
  height: size
449
915
  },
450
916
  children: [
451
- !hasGl ? /*#__PURE__*/ _jsxs("div", {
452
- "aria-hidden": true,
453
- className: "absolute inset-[2%] overflow-hidden rounded-full",
454
- style: fallbackFaceStyle(resolved),
455
- children: [
456
- /*#__PURE__*/ _jsx("span", {
457
- className: "absolute rounded-full",
458
- style: {
459
- backgroundColor: resolved.eye,
460
- width: "13%",
461
- height: "28%",
462
- left: "31%",
463
- top: "34%"
464
- }
465
- }),
466
- /*#__PURE__*/ _jsx("span", {
467
- className: "absolute rounded-full",
468
- style: {
469
- backgroundColor: resolved.eye,
470
- width: "13%",
471
- height: "28%",
472
- left: "56%",
473
- top: "34%"
474
- }
475
- })
476
- ]
917
+ !hasGl ? /*#__PURE__*/ _jsx(FallbackHead, {
918
+ resolved: resolved,
919
+ ears: ears,
920
+ interactive: interactive
477
921
  }) : null,
478
922
  /*#__PURE__*/ _jsx("canvas", {
479
923
  ref: canvasRef,