desky-agent 0.3.0 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/ui/clawd3d.js CHANGED
@@ -1,375 +1,377 @@
1
- // The 3D Desky of the app: the print STLs assembled on the joint axes of cad/check.py, the face drawn as the firmware
2
- // draws it, the agent states as firmware/src/play.cpp plays them. Ported from landing/index.html.
3
- // One renderer and one canvas; app.html moves the canvas into whichever place shows him (overview, moves, wizard).
4
- //
5
- // window.Clawd3D: mount(el), shot(name), state(name), pose([y, p, r, l]), loop(A, B), face(name), glow(joint | null)
6
- // "clawd3d" fires on window when it is ready, "clawd3d-fail" when WebGL or the models are not there.
7
- import * as THREE from 'three';
8
- import { STLLoader } from './vendor/STLLoader.js';
9
- import { OrbitControls } from './vendor/OrbitControls.js';
10
- import { RoomEnvironment } from './vendor/RoomEnvironment.js';
11
-
12
- const REDUCE = matchMedia('(prefers-reduced-motion: reduce)').matches;
13
- const rnd = (a, b) => a + Math.random() * (b - a);
14
-
15
- // ---------------------------------------------------------------- face: shapes and morphs from firmware/src/play.cpp
16
- let OR = '#D9774B', EYE = '#000'; // his body colour and eyes: colors() changes them
17
- const SH = {
18
- EYES: ['R', 40, 72, 0, 0], BLINK: ['R', 52, 8, 0, 0], SLEEP: ['R', 52, 8, 0, 10], FOCUS: ['R', 48, 28, 0, 16],
19
- LOOK_L: ['R', 40, 72, -30, 0], LOOK_R: ['R', 40, 72, 30, 0], READ_L: ['R', 36, 40, -18, 18], READ_R: ['R', 36, 40, 18, 18],
20
- HAPPY: ['C', 52, 26, 0, 0], ASK: ['O', 64, 64, 0, 4], OOPS: ['X', 44, 44, 0, 0],
21
- };
22
- const BLINKS = new Set(['EYES', 'LOOK_L', 'LOOK_R', 'FOCUS', 'READ_L', 'READ_R', 'ASK']);
23
- const EX = [95, 225], EY = 120;
24
-
25
- class Face {
26
- constructor() {
27
- this.cv = document.createElement('canvas'); this.cv.width = 320; this.cv.height = 240;
28
- this.ctx = this.cv.getContext('2d');
29
- this.tex = new THREE.CanvasTexture(this.cv);
30
- this.tex.colorSpace = THREE.SRGBColorSpace; this.tex.magFilter = THREE.NearestFilter; this.tex.minFilter = THREE.LinearFilter;
31
- this.face = this.from = 'EYES'; this.busy = false; this.blink = 0; this.back = 'EYES';
32
- this.nextBlink = performance.now() + 2000;
33
- this.render('EYES', 'EYES', 1);
34
- }
35
- go(f, ms, t) { this.from = this.face; this.face = f; this.t0 = t; this.ms = ms || 1; this.busy = true; }
36
- set(f, ms = 200) {
37
- const t = performance.now();
38
- if (this.blink) { this.blink = 0; this.nextBlink = t + rnd(1800, 4000); }
39
- if (f !== this.face) this.go(f, ms, t);
40
- }
41
- poly(pts) { const c = this.ctx; c.beginPath(); c.moveTo(pts[0], pts[1]); for (let i = 2; i < pts.length; i += 2) c.lineTo(pts[i], pts[i + 1]); c.closePath(); c.fill(); }
42
- eye(e, s, left, k) {
43
- const [kind, ew, eh, dx, dy] = e, c = this.ctx, cx = EX[k] + dx, cy = EY + dy;
44
- c.fillStyle = EYE;
45
- if (kind === 'R') { const w = 52 + (ew - 52) * s, h = 8 + (eh - 8) * s; c.fillRect(cx - w / 2, cy - h / 2, w, h); return; }
46
- const w = ew, h = Math.max(8, eh * s), hw = w / 2, hh = h / 2;
47
- if (kind === 'C') return this.poly([cx - hw, cy + hh, cx, cy - hh + 8, cx + hw, cy + hh, cx + hw, cy + hh - 8, cx, cy - hh, cx - hw, cy + hh - 8]);
48
- if (kind === 'X') {
49
- const tip = left ? cx + hw : cx - hw, back = left ? cx - hw : cx + hw;
50
- return this.poly([back - 4, cy - hh, back + 4, cy - hh, tip + 4, cy, back + 4, cy + hh, back - 4, cy + hh, tip - 4, cy]);
51
- }
52
- c.beginPath(); c.roundRect(cx - hw, cy - hh, w, h, Math.min(hw, hh)); c.fill();
53
- if (s > 0.7) {
54
- c.fillStyle = OR; c.fillRect(cx - hw / 2, cy - hh / 2, 10, 10); c.fillStyle = EYE;
55
- const outer = left ? cx - hw : cx + hw, inner = left ? cx + hw / 3 : cx - hw / 3;
56
- this.poly([outer, cy - hh - 6, inner, cy - hh - 18, inner, cy - hh - 12, outer, cy - hh]);
57
- }
58
- }
59
- render(a, b, u) {
60
- const c = this.ctx, ea = SH[a], eb = SH[b];
61
- c.fillStyle = OR; c.fillRect(0, 0, 320, 240);
62
- for (let k = 0; k < 2; k++) {
63
- if (ea[0] === 'R' && eb[0] === 'R') this.eye(ea.map((v, i) => i ? v + (eb[i] - v) * u : v), 1, k === 0, k);
64
- else if (u < .5) this.eye(ea, 1 - 2 * u, k === 0, k);
65
- else this.eye(eb, 2 * u - 1, k === 0, k);
66
- }
67
- this.tex.needsUpdate = true;
68
- }
69
- z(t) { // the floating "z" above his right eye, as in the firmware
70
- const c = this.ctx; c.fillStyle = EYE;
71
- for (let i = 0; i < 2; i++) {
72
- const p = (t / 2400 + i / 2) % 1, s = 14 + i * 6, x = 258 + i * 18, y = 46 - p * 40;
73
- c.globalAlpha = Math.sin(p * Math.PI);
74
- this.poly([x, y, x + s, y, x + s, y + s * .25, x + s * .4, y + s * .75, x + s, y + s * .75, x + s, y + s, x, y + s, x, y + s * .75, x + s * .6, y + s * .25, x, y + s * .25]);
75
- }
76
- c.globalAlpha = 1;
77
- }
78
- tick(t) {
79
- if (this.busy) {
80
- const u = Math.min(1, (t - this.t0) / this.ms);
81
- this.render(this.from, this.face, u);
82
- if (u >= 1) {
83
- this.busy = false;
84
- if (this.blink === 1) { this.blink = 2; this.go(this.back, 110, t); }
85
- else if (this.blink === 2) { this.blink = 0; this.nextBlink = t + rnd(2200, 5400); }
86
- }
87
- } else if (BLINKS.has(this.face) && t > this.nextBlink) { this.back = this.face; this.blink = 1; this.go('BLINK', 70, t); }
88
- else if (this.face === 'SLEEP') { this.render('SLEEP', 'SLEEP', 1); this.z(t); }
89
- }
90
- }
91
-
92
- // ---------------------------------------------------------------- behaviour: the agent states of firmware/src/play.cpp
93
- // pose = [J1 yaw, J2 pitch, J3 arm, J4 arm], degrees. Yaw and pitch jump (the servo's own speed), arms glide at dps.
94
- const mv = (to, dps) => ({ mv: to, dps }), wt = ms => ({ wt: ms }), fc = (f, ms = 200) => ({ fc: f, ms });
95
- const HOME = [0, 0, 65, 65], AT_YOU = [0, -5, 65, 65];
96
- function* typingIn(b) { yield mv([0, Math.min(b.pose[1], 0), 90, 90], 90); yield mv([0, 25, 90, 90], 60); }
97
- function* typingOut(b) { yield mv([0, 0, b.pose[2], b.pose[3]], 60); yield mv(HOME, 60); }
98
- const SCRIPTS = {
99
- wake: function* () { yield fc('EYES', 700); yield mv(AT_YOU, 40); },
100
- listen: function* () {
101
- yield fc('EYES'); yield mv(AT_YOU, 60);
102
- for (let i = 0; i < 2; i++) { yield mv([0, 12, 65, 65], 90); yield mv([0, -3, 65, 65], 90); }
103
- yield mv([0, 10, 55, 55], 60);
104
- },
105
- work: function* (b) {
106
- yield fc('FOCUS'); yield* typingIn(b);
107
- for (;;) {
108
- const r = Math.random() < .5;
109
- yield mv([0, 25, r ? 84 : 100, r ? 100 : 84], 150); yield wt(rnd(60, 200));
110
- if (Math.random() < .1) { yield mv([0, 22, 90, 90], 60); yield wt(500); }
111
- }
112
- },
113
- read: function* (b) {
114
- yield fc('READ_L'); yield mv([0, 15, 50, 50], 50);
115
- for (;;) {
116
- yield fc(b.face.face === 'READ_L' ? 'READ_R' : 'READ_L', 350); yield wt(rnd(450, 950));
117
- if (Math.random() < .2) { yield fc('READ_L', 120); yield wt(300); }
118
- }
119
- },
120
- ask: function* () {
121
- yield fc('ASK', 250); yield mv([0, -5, 70, 70], 60);
122
- for (;;) { yield wt(rnd(2500, 5000)); const y = Math.random() < .5 ? 6 : -6; yield mv([y, -5, 70, 70], 40); yield wt(900); yield mv([0, -5, 70, 70], 40); }
123
- },
124
- error: function* () {
125
- yield fc('OOPS', 150); yield mv(HOME, 60);
126
- for (let i = 0; i < 2; i++) { yield mv([-12, 0, 65, 65], 90); yield wt(140); yield mv([12, 0, 65, 65], 90); yield wt(140); }
127
- yield mv(HOME, 90);
128
- },
129
- done: function* () {
130
- yield fc('HAPPY');
131
- for (let i = 0; i < 3; i++) { yield mv([0, 8, 140, 140], 160); yield mv([0, 0, 100, 100], 160); }
132
- yield mv(AT_YOU, 90); yield fc('EYES', 300);
133
- },
134
- idle: function* (b) {
135
- yield fc('EYES'); yield mv(HOME, 40);
136
- for (;;) {
137
- yield wt(rnd(2500, 5000));
138
- const y = (Math.random() < .5 ? 1 : -1) * rnd(10, 25);
139
- yield fc(y < 0 ? 'LOOK_L' : 'LOOK_R'); yield mv([y, 0, b.pose[2], b.pose[3]], 30); yield wt(rnd(1000, 2000));
140
- yield fc('EYES'); yield mv([0, 0, b.pose[2], b.pose[3]], 30);
141
- }
142
- },
143
- sleep: function* () { yield fc('SLEEP', 600); yield mv([0, -10, 5, 5], 25); },
144
- wave: function* () { // the app's "wave", for the wizard
145
- yield fc('HAPPY'); yield mv([0, 0, 65, 150], 120);
146
- for (let i = 0; i < 3; i++) { yield mv([0, 0, 65, 120], 160); yield mv([0, 0, 65, 150], 160); }
147
- yield mv(HOME, 90); yield fc('EYES', 300);
148
- },
149
- };
150
- class Bot {
151
- constructor(face) { this.face = face; this.pose = [...HOME]; this.cmd = [...HOME]; this.state = 'idle'; this.set('idle'); }
152
- run(gen) { this.manual = null; this.step = null; this.glide = null; this.gen = gen; }
153
- set(s) {
154
- const leaving = this.state === 'work' && s !== 'work', me = this, script = SCRIPTS[s] || SCRIPTS.idle;
155
- this.state = s;
156
- this.run((function* () { if (leaving) yield* typingOut(me); yield* script(me); for (;;) yield wt(1000); })());
157
- }
158
- setManual(p) { this.state = 'manual'; this.gen = null; this.step = null; this.glide = null; this.manual = p.slice(); }
159
- // to B and back, again and again: what the wizard is about to ask the real one to do
160
- loop(A, B) {
161
- this.state = 'loop';
162
- this.run((function* () { yield mv(A, 120); for (;;) { yield wt(700); yield mv(B, 70); yield wt(1100); yield mv(A, 70); } })());
163
- }
164
- begin(st, t) {
165
- if (st.fc) this.face.set(st.fc, st.ms);
166
- else if (st.wt) st.end = t + st.wt;
167
- else {
168
- this.cmd[0] = st.mv[0]; this.cmd[1] = st.mv[1];
169
- const a = this.pose.slice(2), ms = Math.max(Math.abs(st.mv[2] - a[0]), Math.abs(st.mv[3] - a[1])) / st.dps * 1000;
170
- this.glide = { a, b: st.mv.slice(2), t0: t, ms: Math.max(ms, 1) };
171
- }
172
- }
173
- over(st, t) {
174
- if (st.fc) return true;
175
- if (st.wt) return t >= st.end;
176
- return !this.glide && Math.abs(this.pose[0] - this.cmd[0]) < .5 && Math.abs(this.pose[1] - this.cmd[1]) < .5;
177
- }
178
- tick(t, dt) {
179
- if (this.manual) {
180
- this.cmd[0] = this.manual[0]; this.cmd[1] = this.manual[1];
181
- for (const j of [2, 3]) { const d = this.manual[j] - this.pose[j]; this.pose[j] += Math.sign(d) * Math.min(Math.abs(d), 240 * dt); }
182
- }
183
- for (const j of [0, 1]) { // MG90S: roughly 300 deg/s under load; slower here, to be seen
184
- const d = this.cmd[j] - this.pose[j]; this.pose[j] += Math.sign(d) * Math.min(Math.abs(d), (this.state === 'loop' ? 70 : 300) * dt);
185
- }
186
- if (this.glide) {
187
- const g = this.glide, u = Math.min(1, (t - g.t0) / g.ms), e = u * u * (3 - 2 * u);
188
- this.pose[2] = g.a[0] + (g.b[0] - g.a[0]) * e; this.pose[3] = g.a[1] + (g.b[1] - g.a[1]) * e;
189
- if (u >= 1) this.glide = null;
190
- }
191
- for (let guard = 0; this.gen && guard < 20; guard++) {
192
- if (!this.step) { this.step = this.gen.next().value; this.begin(this.step, t); }
193
- if (this.over(this.step, t)) this.step = null; else break;
194
- }
195
- this.face.tick(t);
196
- }
197
- }
198
-
199
- // ---------------------------------------------------------------- scene from the STLs (cad/build.py export, assembled)
200
- function build() {
201
- const canvas = document.createElement('canvas');
202
- canvas.className = 'c3d';
203
- const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: true, powerPreference: 'low-power' });
204
- renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
205
- renderer.toneMapping = THREE.NeutralToneMapping;
206
- renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap;
207
- const scene = new THREE.Scene();
208
- const pmrem = new THREE.PMREMGenerator(renderer);
209
- scene.environment = pmrem.fromScene(new RoomEnvironment(), .04).texture;
210
- scene.environmentIntensity = .55;
211
- const camera = new THREE.PerspectiveCamera(28, 1, 2, 4000);
212
- const cad = (x, y, z) => new THREE.Vector3(x, z, -y); // CAD: Z up, Y toward the laptop
213
- const key = new THREE.DirectionalLight(0xffffff, 2.4);
214
- key.position.copy(cad(-160, 220, 320)); key.castShadow = true;
215
- key.shadow.mapSize.set(1024, 1024); key.shadow.bias = -.0004; key.shadow.normalBias = .4;
216
- Object.assign(key.shadow.camera, { left: -150, right: 150, top: 150, bottom: -150, near: 50, far: 900 });
217
- scene.add(key, key.target);
218
- const rim = new THREE.DirectionalLight(0xffe6d6, .9); rim.position.copy(cad(200, -220, 200)); scene.add(rim);
219
- scene.add(new THREE.HemisphereLight(0xffffff, 0xbdb6ad, .5));
220
- const ground = new THREE.Mesh(new THREE.PlaneGeometry(3000, 3000), new THREE.ShadowMaterial({ opacity: .16 }));
221
- ground.rotation.x = -Math.PI / 2; ground.position.y = -8; ground.receiveShadow = true; scene.add(ground);
222
- // joint hierarchy, axes and groups exactly as cad/check.py
223
- const root = new THREE.Group(); root.rotation.x = -Math.PI / 2; scene.add(root);
224
- const G = { ground: new THREE.Group(), yaw: new THREE.Group() };
225
- const pitchPivot = new THREE.Group(); pitchPivot.position.set(0, 0, 46.5);
226
- G.pitch = new THREE.Group(); G.pitch.position.set(0, 0, -46.5); pitchPivot.add(G.pitch);
227
- root.add(G.ground, G.yaw); G.yaw.add(pitchPivot);
228
- const armPivot = {};
229
- for (const j of ['J3', 'J4']) {
230
- const p = new THREE.Group(); p.position.set(0, -2.2, 90);
231
- G[j] = new THREE.Group(); G[j].position.set(0, 2.2, -90); p.add(G[j]); G.pitch.add(p); armPivot[j] = p;
232
- }
233
- // J3 is his right arm: R1 sits at +x, which is your left when you face him
234
- const GROUPS = {
235
- ground: ['B0_base', 'C1_column', 'C2_column_lid', 'D1_desk_frame', 'D2_desk_top', 'L1_laptop', 'P1_mug', 'P2_plant'],
236
- yaw: ['S1_seat', 'S2_spacer', 'A04_cap_J2'],
237
- pitch: ['B1_body', 'B2_face_plate', 'B3_side_left', 'B4_side_right', 'A04_cap_J3', 'A04_cap_J4'],
238
- J3: ['R1_arm_left'],
239
- J4: ['R2_arm_right'],
240
- };
241
- const mat = (color, rough = .62) => new THREE.MeshStandardMaterial({ color, roughness: rough, metalness: 0 });
242
- const M = { orange: mat(0xd9774b, .55), gray: mat(0x58554f), desk: mat(0x8c8781), dark: mat(0x2f2e2c, .5) };
243
- const matOf = n => /^(B1|B2|B3|B4|R1|R2)_/.test(n) ? M.orange : n === 'L1_laptop' ? M.dark : /^(D1|D2|P1|P2)_/.test(n) ? M.desk : M.gray;
244
- const face = new Face();
245
- function quad(x0, x1, z0, z1, y, material) {
246
- const g = new THREE.BufferGeometry();
247
- g.setAttribute('position', new THREE.Float32BufferAttribute([x0, y, z1, x1, y, z1, x0, y, z0, x1, y, z0], 3));
248
- g.setAttribute('uv', new THREE.Float32BufferAttribute([0, 1, 1, 1, 0, 0, 1, 0], 2));
249
- g.setIndex([0, 2, 1, 1, 2, 3]);
250
- const m = new THREE.Mesh(g, material); G.pitch.add(m); return m;
251
- }
252
- quad(25.1, -22.3, 73.8, 110.2, 16.05, new THREE.MeshBasicMaterial({ color: 0x0c0c0c, side: THREE.DoubleSide }));
253
- quad(21.8, -19.0, 76.7, 107.3, 16.2, new THREE.MeshBasicMaterial({ map: face.tex, side: THREE.DoubleSide, toneMapped: false, polygonOffset: true, polygonOffsetFactor: -2 }));
254
- const bot = new Bot(face);
255
- const meshes = { yaw: [], pitch: [], J3: [], J4: [] }, body = [];
256
- let bodyHex = 0xd9774b;
257
- const loader = new STLLoader();
258
- const loaded = Promise.all(Object.entries(GROUPS).flatMap(([g, ns]) => ns.map(async n => {
259
- const geo = await loader.loadAsync(`models/${n}.stl`);
260
- const mesh = new THREE.Mesh(geo, matOf(n).clone());
261
- mesh.castShadow = mesh.receiveShadow = true; mesh.name = n;
262
- G[g].add(mesh);
263
- if (meshes[g]) meshes[g].push(mesh);
264
- if (/^(B1|B2|B3|B4|R1|R2)_/.test(n)) { mesh.material.color.setHex(bodyHex); body.push(mesh); }
265
- })));
266
-
267
- // ---- camera: direction to look from, the point looked at, the radius in mm to fit
268
- const controls = new OrbitControls(camera, canvas);
269
- controls.enableZoom = false; controls.enablePan = false; controls.enableDamping = true; controls.dampingFactor = .08;
270
- controls.minPolarAngle = .2; controls.maxPolarAngle = Math.PI / 2 - .02; controls.rotateSpeed = .6;
271
- const SHOTS = {
272
- hero: [[-175, 235, 150], [0, 22, 52], 118],
273
- front: [[0, 250, 190], [0, 12, 70], 98],
274
- face: [[0, 250, 140], [0, 16, 88], 52],
275
- yaw: [[-50, 190, 330], [0, 8, 62], 108],
276
- side: [[260, 40, 110], [0, 14, 62], 108],
277
- armR: [[190, 220, 150], [0, 6, 72], 100],
278
- armL: [[-190, 220, 150], [0, 6, 72], 100],
279
- moves: [[-200, 210, 150], [0, 20, 55], 116],
280
- };
281
- let tween = null, current = 'hero';
282
- function dist(r) {
283
- const vf = THREE.MathUtils.degToRad(camera.fov);
284
- const h = 2 * Math.atan(Math.tan(vf / 2) * camera.aspect);
285
- return r / Math.sin(Math.min(vf, h) / 2);
286
- }
287
- function shot(name, instant) {
288
- current = name;
289
- const [p, t, r] = SHOTS[name] || SHOTS.hero, tgt = cad(...t), pos = cad(...p).sub(tgt).setLength(dist(r)).add(tgt);
290
- if (instant || REDUCE) { camera.position.copy(pos); controls.target.copy(tgt); tween = null; return; }
291
- tween = { p0: camera.position.clone(), t0: controls.target.clone(), p1: pos, t1: tgt, s: performance.now(), d: 1100 };
292
- }
293
- controls.addEventListener('start', () => { tween = null; });
294
-
295
- // ---- a joint lit up: the part that is about to move glows
296
- let glowOn = null;
297
- const glowSet = j => {
298
- for (const k of Object.keys(meshes)) for (const m of meshes[k]) m.material.emissive.setHex(0x000000);
299
- glowOn = j == null ? null : ['yaw', 'pitch', 'J3', 'J4'][j];
300
- };
301
-
302
- // ---- where he is shown: the canvas moves between hosts; it renders only while it sits in a visible one
303
- let host = null;
304
- function size() {
305
- if (!host) return;
306
- const w = host.clientWidth, h = host.clientHeight;
307
- if (!w || !h) return;
308
- renderer.setSize(w, h, false);
309
- camera.aspect = w / h; camera.updateProjectionMatrix();
310
- shot(current, true);
311
- }
312
- const ro = new ResizeObserver(size);
313
- function mount(el, name) {
314
- if (host === el) { if (name) shot(name); return; }
315
- if (host) ro.unobserve(host);
316
- host = el;
317
- if (!el) { canvas.remove(); return; }
318
- el.appendChild(canvas); ro.observe(el);
319
- current = name || current; size();
320
- }
321
- const v = THREE.MathUtils.degToRad;
322
- let last = performance.now(), dragging = false;
323
- controls.addEventListener('start', () => { dragging = true; });
324
- controls.addEventListener('end', () => { dragging = false; });
325
- renderer.setAnimationLoop(t => {
326
- if (!host || !host.offsetParent) return; // hidden page: no frames
327
- if (!dragging && !tween && t - last < 30) return; // about 30 fps is plenty for a servo; full rate while dragged
328
- const dt = Math.min(.05, Math.max(0, (t - last) / 1000)); last = t;
329
- bot.tick(t, dt);
330
- const [yaw, pitch, a3, a4] = bot.pose;
331
- G.yaw.rotation.z = v(yaw); pitchPivot.rotation.x = v(-pitch);
332
- armPivot.J3.rotation.x = v(a3); armPivot.J4.rotation.x = v(a4);
333
- if (glowOn) {
334
- const k = .35 + .3 * Math.sin(t / 260);
335
- const set = glowOn === 'yaw' ? [...meshes.yaw, ...meshes.pitch] : meshes[glowOn];
336
- for (const m of set) m.material.emissive.setRGB(.9 * k, .32 * k, .06 * k);
337
- }
338
- if (tween) {
339
- const u = Math.min(1, (t - tween.s) / tween.d), e = u < .5 ? 4 * u * u * u : 1 - Math.pow(-2 * u + 2, 3) / 2;
340
- camera.position.lerpVectors(tween.p0, tween.p1, e); controls.target.lerpVectors(tween.t0, tween.t1, e);
341
- if (u >= 1) tween = null;
342
- }
343
- controls.update();
344
- renderer.render(scene, camera);
345
- });
346
- shot('hero', true);
347
-
348
- return {
349
- loaded, mount,
350
- shot: name => shot(name),
351
- state: (s, force) => { if (force || bot.state !== s) bot.set(s); },
352
- pose: p => bot.setManual(p),
353
- loop: (A, B) => bot.loop(A, B),
354
- face: f => face.set(f, 250),
355
- glow: glowSet,
356
- // Desky's colour: the printed parts and the screen behind his eyes, as the board draws them
357
- colors: (bodyCss, eyeCss) => {
358
- OR = bodyCss; EYE = eyeCss; bodyHex = new THREE.Color(bodyCss).getHex();
359
- for (const m of body) m.material.color.setHex(bodyHex);
360
- face.render(face.face, face.face, 1);
361
- },
362
- get poseNow() { return bot.pose.slice(); },
363
- };
364
- }
365
-
366
- try {
367
- const probe = document.createElement('canvas');
368
- if (!(probe.getContext('webgl2') || probe.getContext('webgl'))) throw new Error('no WebGL');
369
- const api = build();
370
- api.loaded.then(() => { window.Clawd3D = api; dispatchEvent(new Event('clawd3d')); })
371
- .catch(e => { console.warn('Clawd3D:', e); dispatchEvent(new Event('clawd3d-fail')); });
372
- } catch (e) {
373
- console.warn('Clawd3D:', e);
374
- dispatchEvent(new Event('clawd3d-fail'));
375
- }
1
+ // The 3D Desky of the app: the print STLs assembled on the joint axes of cad/check.py, the face drawn as the firmware
2
+ // draws it, the agent states as firmware/src/play.cpp plays them. Ported from landing/index.html.
3
+ // One renderer and one canvas; app.html moves the canvas into whichever place shows him (overview, moves, wizard).
4
+ //
5
+ // window.Clawd3D: mount(el), shot(name), state(name), pose([y, p, r, l]), loop(A, B), face(name), glow(joint | null)
6
+ // "clawd3d" fires on window when it is ready, "clawd3d-fail" when WebGL or the models are not there.
7
+ import * as THREE from 'three';
8
+ import { STLLoader } from './vendor/STLLoader.js';
9
+ import { OrbitControls } from './vendor/OrbitControls.js';
10
+ import { RoomEnvironment } from './vendor/RoomEnvironment.js';
11
+
12
+ const REDUCE = matchMedia('(prefers-reduced-motion: reduce)').matches;
13
+ const rnd = (a, b) => a + Math.random() * (b - a);
14
+
15
+ // ---------------------------------------------------------------- face: shapes and morphs from firmware/src/play.cpp
16
+ let OR = '#D9774B', EYE = '#000'; // his body colour and eyes: colors() changes them
17
+ const SH = {
18
+ EYES: ['R', 40, 72, 0, 0], BLINK: ['R', 52, 8, 0, 0], SLEEP: ['R', 52, 8, 0, 10], FOCUS: ['R', 48, 28, 0, 16],
19
+ LOOK_L: ['R', 40, 72, -30, 0], LOOK_R: ['R', 40, 72, 30, 0], READ_L: ['R', 36, 40, -18, 18], READ_R: ['R', 36, 40, 18, 18],
20
+ HAPPY: ['C', 52, 26, 0, 0], ASK: ['O', 64, 64, 0, 4], OOPS: ['X', 44, 44, 0, 0],
21
+ };
22
+ const BLINKS = new Set(['EYES', 'LOOK_L', 'LOOK_R', 'FOCUS', 'READ_L', 'READ_R', 'ASK']);
23
+ const EX = [95, 225], EY = 120;
24
+
25
+ class Face {
26
+ constructor() {
27
+ this.cv = document.createElement('canvas'); this.cv.width = 320; this.cv.height = 240;
28
+ this.ctx = this.cv.getContext('2d');
29
+ this.tex = new THREE.CanvasTexture(this.cv);
30
+ this.tex.colorSpace = THREE.SRGBColorSpace; this.tex.magFilter = THREE.NearestFilter; this.tex.minFilter = THREE.LinearFilter;
31
+ this.face = this.from = 'EYES'; this.busy = false; this.blink = 0; this.back = 'EYES';
32
+ this.nextBlink = performance.now() + 2000;
33
+ this.render('EYES', 'EYES', 1);
34
+ }
35
+ go(f, ms, t) { this.from = this.face; this.face = f; this.t0 = t; this.ms = ms || 1; this.busy = true; }
36
+ set(f, ms = 200) {
37
+ const t = performance.now();
38
+ if (this.blink) { this.blink = 0; this.nextBlink = t + rnd(1800, 4000); }
39
+ if (f !== this.face) this.go(f, ms, t);
40
+ }
41
+ poly(pts) { const c = this.ctx; c.beginPath(); c.moveTo(pts[0], pts[1]); for (let i = 2; i < pts.length; i += 2) c.lineTo(pts[i], pts[i + 1]); c.closePath(); c.fill(); }
42
+ eye(e, s, left, k) {
43
+ const [kind, ew, eh, dx, dy] = e, c = this.ctx, cx = EX[k] + dx, cy = EY + dy;
44
+ c.fillStyle = EYE;
45
+ if (kind === 'R') { const w = 52 + (ew - 52) * s, h = 8 + (eh - 8) * s; c.fillRect(cx - w / 2, cy - h / 2, w, h); return; }
46
+ const w = ew, h = Math.max(8, eh * s), hw = w / 2, hh = h / 2;
47
+ if (kind === 'C') return this.poly([cx - hw, cy + hh, cx, cy - hh + 8, cx + hw, cy + hh, cx + hw, cy + hh - 8, cx, cy - hh, cx - hw, cy + hh - 8]);
48
+ if (kind === 'X') {
49
+ const tip = left ? cx + hw : cx - hw, back = left ? cx - hw : cx + hw;
50
+ return this.poly([back - 4, cy - hh, back + 4, cy - hh, tip + 4, cy, back + 4, cy + hh, back - 4, cy + hh, tip - 4, cy]);
51
+ }
52
+ c.beginPath(); c.roundRect(cx - hw, cy - hh, w, h, Math.min(hw, hh)); c.fill();
53
+ if (s > 0.7) {
54
+ c.fillStyle = OR; c.fillRect(cx - hw / 2, cy - hh / 2, 10, 10); c.fillStyle = EYE;
55
+ const outer = left ? cx - hw : cx + hw, inner = left ? cx + hw / 3 : cx - hw / 3;
56
+ this.poly([outer, cy - hh - 6, inner, cy - hh - 18, inner, cy - hh - 12, outer, cy - hh]);
57
+ }
58
+ }
59
+ render(a, b, u) {
60
+ const c = this.ctx, ea = SH[a], eb = SH[b];
61
+ c.fillStyle = OR; c.fillRect(0, 0, 320, 240);
62
+ for (let k = 0; k < 2; k++) {
63
+ if (ea[0] === 'R' && eb[0] === 'R') this.eye(ea.map((v, i) => i ? v + (eb[i] - v) * u : v), 1, k === 0, k);
64
+ else if (u < .5) this.eye(ea, 1 - 2 * u, k === 0, k);
65
+ else this.eye(eb, 2 * u - 1, k === 0, k);
66
+ }
67
+ this.tex.needsUpdate = true;
68
+ }
69
+ z(t) { // the floating "z" above his right eye, as in the firmware
70
+ const c = this.ctx; c.fillStyle = EYE;
71
+ for (let i = 0; i < 2; i++) {
72
+ const p = (t / 2400 + i / 2) % 1, s = 14 + i * 6, x = 258 + i * 18, y = 46 - p * 40;
73
+ c.globalAlpha = Math.sin(p * Math.PI);
74
+ this.poly([x, y, x + s, y, x + s, y + s * .25, x + s * .4, y + s * .75, x + s, y + s * .75, x + s, y + s, x, y + s, x, y + s * .75, x + s * .6, y + s * .25, x, y + s * .25]);
75
+ }
76
+ c.globalAlpha = 1;
77
+ }
78
+ tick(t) {
79
+ if (this.busy) {
80
+ const u = Math.min(1, (t - this.t0) / this.ms);
81
+ this.render(this.from, this.face, u);
82
+ if (u >= 1) {
83
+ this.busy = false;
84
+ if (this.blink === 1) { this.blink = 2; this.go(this.back, 110, t); }
85
+ else if (this.blink === 2) { this.blink = 0; this.nextBlink = t + rnd(2200, 5400); }
86
+ }
87
+ } else if (BLINKS.has(this.face) && t > this.nextBlink) { this.back = this.face; this.blink = 1; this.go('BLINK', 70, t); }
88
+ else if (this.face === 'SLEEP') { this.render('SLEEP', 'SLEEP', 1); this.z(t); }
89
+ }
90
+ }
91
+
92
+ // ---------------------------------------------------------------- behaviour: the agent states of firmware/src/play.cpp
93
+ // pose = [J1 yaw, J2 pitch, J3 arm, J4 arm], degrees. Yaw and pitch jump (the servo's own speed), arms glide at dps.
94
+ const mv = (to, dps) => ({ mv: to, dps }), wt = ms => ({ wt: ms }), fc = (f, ms = 200) => ({ fc: f, ms });
95
+ const HOME = [0, 0, 65, 65], AT_YOU = [0, -5, 65, 65];
96
+ function* typingIn(b) { yield mv([0, Math.min(b.pose[1], 0), 90, 90], 90); yield mv([0, 25, 90, 90], 60); }
97
+ function* typingOut(b) { yield mv([0, 0, b.pose[2], b.pose[3]], 60); yield mv(HOME, 60); }
98
+ const SCRIPTS = {
99
+ wake: function* () { yield fc('EYES', 700); yield mv(AT_YOU, 40); },
100
+ listen: function* () {
101
+ yield fc('EYES'); yield mv(AT_YOU, 60);
102
+ for (let i = 0; i < 2; i++) { yield mv([0, 12, 65, 65], 90); yield mv([0, -3, 65, 65], 90); }
103
+ yield mv([0, 10, 55, 55], 60);
104
+ },
105
+ work: function* (b) {
106
+ yield fc('FOCUS'); yield* typingIn(b);
107
+ for (;;) {
108
+ const r = Math.random() < .5;
109
+ yield mv([0, 25, r ? 84 : 100, r ? 100 : 84], 150); yield wt(rnd(60, 200));
110
+ if (Math.random() < .1) { yield mv([0, 22, 90, 90], 60); yield wt(500); }
111
+ }
112
+ },
113
+ read: function* (b) {
114
+ yield fc('READ_L'); yield mv([0, 15, 50, 50], 50);
115
+ for (;;) {
116
+ yield fc(b.face.face === 'READ_L' ? 'READ_R' : 'READ_L', 350); yield wt(rnd(450, 950));
117
+ if (Math.random() < .2) { yield fc('READ_L', 120); yield wt(300); }
118
+ }
119
+ },
120
+ ask: function* () {
121
+ yield fc('ASK', 250); yield mv([0, -5, 70, 70], 60);
122
+ for (;;) { yield wt(rnd(2500, 5000)); const y = Math.random() < .5 ? 6 : -6; yield mv([y, -5, 70, 70], 40); yield wt(900); yield mv([0, -5, 70, 70], 40); }
123
+ },
124
+ error: function* () {
125
+ yield fc('OOPS', 150); yield mv(HOME, 60);
126
+ for (let i = 0; i < 2; i++) { yield mv([-12, 0, 65, 65], 90); yield wt(140); yield mv([12, 0, 65, 65], 90); yield wt(140); }
127
+ yield mv(HOME, 90);
128
+ },
129
+ done: function* () {
130
+ yield fc('HAPPY');
131
+ for (let i = 0; i < 3; i++) { yield mv([0, 8, 140, 140], 160); yield mv([0, 0, 100, 100], 160); }
132
+ yield mv(AT_YOU, 90); yield fc('EYES', 300);
133
+ },
134
+ idle: function* (b) {
135
+ yield fc('EYES'); yield mv(HOME, 40);
136
+ for (;;) {
137
+ yield wt(rnd(2500, 5000));
138
+ const y = (Math.random() < .5 ? 1 : -1) * rnd(10, 25);
139
+ yield fc(y < 0 ? 'LOOK_L' : 'LOOK_R'); yield mv([y, 0, b.pose[2], b.pose[3]], 30); yield wt(rnd(1000, 2000));
140
+ yield fc('EYES'); yield mv([0, 0, b.pose[2], b.pose[3]], 30);
141
+ }
142
+ },
143
+ sleep: function* () { yield fc('SLEEP', 600); yield mv([0, -10, 5, 5], 25); },
144
+ wave: function* () { // the app's "wave", for the wizard
145
+ yield fc('HAPPY'); yield mv([0, 0, 65, 150], 120);
146
+ for (let i = 0; i < 3; i++) { yield mv([0, 0, 65, 120], 160); yield mv([0, 0, 65, 150], 160); }
147
+ yield mv(HOME, 90); yield fc('EYES', 300);
148
+ },
149
+ };
150
+ class Bot {
151
+ constructor(face) { this.face = face; this.pose = [...HOME]; this.cmd = [...HOME]; this.state = 'idle'; this.set('idle'); }
152
+ run(gen) { this.manual = null; this.step = null; this.glide = null; this.gen = gen; }
153
+ set(s) {
154
+ const leaving = this.state === 'work' && s !== 'work', me = this, script = SCRIPTS[s] || SCRIPTS.idle;
155
+ this.state = s;
156
+ this.run((function* () { if (leaving) yield* typingOut(me); yield* script(me); for (;;) yield wt(1000); })());
157
+ }
158
+ setManual(p) { this.state = 'manual'; this.gen = null; this.step = null; this.glide = null; this.manual = p.slice(); }
159
+ // to B and back, again and again: what the wizard is about to ask the real one to do
160
+ loop(A, B) {
161
+ this.state = 'loop';
162
+ this.run((function* () { yield mv(A, 120); for (;;) { yield wt(700); yield mv(B, 70); yield wt(1100); yield mv(A, 70); } })());
163
+ }
164
+ begin(st, t) {
165
+ if (st.fc) this.face.set(st.fc, st.ms);
166
+ else if (st.wt) st.end = t + st.wt;
167
+ else {
168
+ this.cmd[0] = st.mv[0]; this.cmd[1] = st.mv[1];
169
+ const a = this.pose.slice(2), ms = Math.max(Math.abs(st.mv[2] - a[0]), Math.abs(st.mv[3] - a[1])) / st.dps * 1000;
170
+ this.glide = { a, b: st.mv.slice(2), t0: t, ms: Math.max(ms, 1) };
171
+ }
172
+ }
173
+ over(st, t) {
174
+ if (st.fc) return true;
175
+ if (st.wt) return t >= st.end;
176
+ return !this.glide && Math.abs(this.pose[0] - this.cmd[0]) < .5 && Math.abs(this.pose[1] - this.cmd[1]) < .5;
177
+ }
178
+ tick(t, dt) {
179
+ if (this.manual) {
180
+ this.cmd[0] = this.manual[0]; this.cmd[1] = this.manual[1];
181
+ for (const j of [2, 3]) { const d = this.manual[j] - this.pose[j]; this.pose[j] += Math.sign(d) * Math.min(Math.abs(d), 240 * dt); }
182
+ }
183
+ for (const j of [0, 1]) { // MG90S: roughly 300 deg/s under load; slower here, to be seen
184
+ const d = this.cmd[j] - this.pose[j]; this.pose[j] += Math.sign(d) * Math.min(Math.abs(d), (this.state === 'loop' ? 70 : 300) * dt);
185
+ }
186
+ if (this.glide) {
187
+ const g = this.glide, u = Math.min(1, (t - g.t0) / g.ms), e = u * u * (3 - 2 * u);
188
+ this.pose[2] = g.a[0] + (g.b[0] - g.a[0]) * e; this.pose[3] = g.a[1] + (g.b[1] - g.a[1]) * e;
189
+ if (u >= 1) this.glide = null;
190
+ }
191
+ for (let guard = 0; this.gen && guard < 20; guard++) {
192
+ if (!this.step) { this.step = this.gen.next().value; this.begin(this.step, t); }
193
+ if (this.over(this.step, t)) this.step = null; else break;
194
+ }
195
+ this.face.tick(t);
196
+ }
197
+ }
198
+
199
+ // ---------------------------------------------------------------- scene from the STLs (cad/build.py export, assembled)
200
+ function build() {
201
+ const canvas = document.createElement('canvas');
202
+ canvas.className = 'c3d';
203
+ const renderer = new THREE.WebGLRenderer({ canvas, antialias: true, alpha: true, powerPreference: 'low-power' });
204
+ renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
205
+ renderer.toneMapping = THREE.NeutralToneMapping;
206
+ renderer.shadowMap.enabled = true; renderer.shadowMap.type = THREE.PCFSoftShadowMap;
207
+ const scene = new THREE.Scene();
208
+ const pmrem = new THREE.PMREMGenerator(renderer);
209
+ scene.environment = pmrem.fromScene(new RoomEnvironment(), .04).texture;
210
+ scene.environmentIntensity = .8; // black PLA needs the room's reflections to show its edges
211
+ const camera = new THREE.PerspectiveCamera(28, 1, 2, 4000);
212
+ const cad = (x, y, z) => new THREE.Vector3(x, z, -y); // CAD: Z up, Y toward the laptop
213
+ const key = new THREE.DirectionalLight(0xffffff, 2.4);
214
+ key.position.copy(cad(-160, 220, 320)); key.castShadow = true;
215
+ key.shadow.mapSize.set(1024, 1024); key.shadow.bias = -.0004; key.shadow.normalBias = .4;
216
+ Object.assign(key.shadow.camera, { left: -150, right: 150, top: 150, bottom: -150, near: 50, far: 900 });
217
+ scene.add(key, key.target);
218
+ const rim = new THREE.DirectionalLight(0xffe6d6, 1.4); rim.position.copy(cad(200, -220, 200)); scene.add(rim);
219
+ scene.add(new THREE.HemisphereLight(0xffffff, 0xbdb6ad, .5));
220
+ const ground = new THREE.Mesh(new THREE.PlaneGeometry(3000, 3000), new THREE.ShadowMaterial({ opacity: .16 }));
221
+ ground.rotation.x = -Math.PI / 2; ground.position.y = -8; ground.receiveShadow = true; scene.add(ground);
222
+ // joint hierarchy, axes and groups exactly as cad/check.py
223
+ const root = new THREE.Group(); root.rotation.x = -Math.PI / 2; scene.add(root);
224
+ const G = { ground: new THREE.Group(), yaw: new THREE.Group() };
225
+ const pitchPivot = new THREE.Group(); pitchPivot.position.set(0, 0, 46.5);
226
+ G.pitch = new THREE.Group(); G.pitch.position.set(0, 0, -46.5); pitchPivot.add(G.pitch);
227
+ root.add(G.ground, G.yaw); G.yaw.add(pitchPivot);
228
+ const armPivot = {};
229
+ for (const j of ['J3', 'J4']) {
230
+ const p = new THREE.Group(); p.position.set(0, -2.2, 90);
231
+ G[j] = new THREE.Group(); G[j].position.set(0, 2.2, -90); p.add(G[j]); G.pitch.add(p); armPivot[j] = p;
232
+ }
233
+ // J3 is his right arm: R1 sits at +x, which is your left when you face him
234
+ const GROUPS = {
235
+ ground: ['B0_base', 'C1_column', 'C2_column_lid', 'D1_desk_frame', 'D2_desk_top', 'L1_laptop', 'P1_mug', 'P2_plant'],
236
+ yaw: ['S1_seat', 'S2_spacer', 'A04_cap_J2'],
237
+ pitch: ['B1_body', 'B2_face_plate', 'B3_side_left', 'B4_side_right', 'A04_cap_J3', 'A04_cap_J4'],
238
+ J3: ['R1_arm_left'],
239
+ J4: ['R2_arm_right'],
240
+ };
241
+ const mat = (color, rough = .62) => new THREE.MeshStandardMaterial({ color, roughness: rough, metalness: 0 });
242
+ // everything but Desky himself is printed black: the desk, what stands on it, the base, the column and the seat.
243
+ // A few shades apart, so the parts still read against each other
244
+ const M = { orange: mat(0xd9774b, .55), gray: mat(0x1e1d1c, .5), desk: mat(0x252422, .55), dark: mat(0x2c2b29, .42) };
245
+ const matOf = n => /^(B1|B2|B3|B4|R1|R2)_/.test(n) ? M.orange : n === 'L1_laptop' ? M.dark : /^(D1|D2|P1|P2)_/.test(n) ? M.desk : M.gray;
246
+ const face = new Face();
247
+ function quad(x0, x1, z0, z1, y, material) {
248
+ const g = new THREE.BufferGeometry();
249
+ g.setAttribute('position', new THREE.Float32BufferAttribute([x0, y, z1, x1, y, z1, x0, y, z0, x1, y, z0], 3));
250
+ g.setAttribute('uv', new THREE.Float32BufferAttribute([0, 1, 1, 1, 0, 0, 1, 0], 2));
251
+ g.setIndex([0, 2, 1, 1, 2, 3]);
252
+ const m = new THREE.Mesh(g, material); G.pitch.add(m); return m;
253
+ }
254
+ quad(25.1, -22.3, 73.8, 110.2, 16.05, new THREE.MeshBasicMaterial({ color: 0x0c0c0c, side: THREE.DoubleSide }));
255
+ quad(21.8, -19.0, 76.7, 107.3, 16.2, new THREE.MeshBasicMaterial({ map: face.tex, side: THREE.DoubleSide, toneMapped: false, polygonOffset: true, polygonOffsetFactor: -2 }));
256
+ const bot = new Bot(face);
257
+ const meshes = { yaw: [], pitch: [], J3: [], J4: [] }, body = [];
258
+ let bodyHex = 0xd9774b;
259
+ const loader = new STLLoader();
260
+ const loaded = Promise.all(Object.entries(GROUPS).flatMap(([g, ns]) => ns.map(async n => {
261
+ const geo = await loader.loadAsync(`models/${n}.stl`);
262
+ const mesh = new THREE.Mesh(geo, matOf(n).clone());
263
+ mesh.castShadow = mesh.receiveShadow = true; mesh.name = n;
264
+ G[g].add(mesh);
265
+ if (meshes[g]) meshes[g].push(mesh);
266
+ if (/^(B1|B2|B3|B4|R1|R2)_/.test(n)) { mesh.material.color.setHex(bodyHex); body.push(mesh); }
267
+ })));
268
+
269
+ // ---- camera: direction to look from, the point looked at, the radius in mm to fit
270
+ const controls = new OrbitControls(camera, canvas);
271
+ controls.enableZoom = false; controls.enablePan = false; controls.enableDamping = true; controls.dampingFactor = .08;
272
+ controls.minPolarAngle = .2; controls.maxPolarAngle = Math.PI / 2 - .02; controls.rotateSpeed = .6;
273
+ const SHOTS = {
274
+ hero: [[-175, 235, 150], [0, 22, 52], 118],
275
+ front: [[0, 250, 190], [0, 12, 70], 98],
276
+ face: [[0, 250, 140], [0, 16, 88], 52],
277
+ yaw: [[-50, 190, 330], [0, 8, 62], 108],
278
+ side: [[260, 40, 110], [0, 14, 62], 108],
279
+ armR: [[190, 220, 150], [0, 6, 72], 100],
280
+ armL: [[-190, 220, 150], [0, 6, 72], 100],
281
+ moves: [[-200, 210, 150], [0, 20, 55], 116],
282
+ };
283
+ let tween = null, current = 'hero';
284
+ function dist(r) {
285
+ const vf = THREE.MathUtils.degToRad(camera.fov);
286
+ const h = 2 * Math.atan(Math.tan(vf / 2) * camera.aspect);
287
+ return r / Math.sin(Math.min(vf, h) / 2);
288
+ }
289
+ function shot(name, instant) {
290
+ current = name;
291
+ const [p, t, r] = SHOTS[name] || SHOTS.hero, tgt = cad(...t), pos = cad(...p).sub(tgt).setLength(dist(r)).add(tgt);
292
+ if (instant || REDUCE) { camera.position.copy(pos); controls.target.copy(tgt); tween = null; return; }
293
+ tween = { p0: camera.position.clone(), t0: controls.target.clone(), p1: pos, t1: tgt, s: performance.now(), d: 1100 };
294
+ }
295
+ controls.addEventListener('start', () => { tween = null; });
296
+
297
+ // ---- a joint lit up: the part that is about to move glows
298
+ let glowOn = null;
299
+ const glowSet = j => {
300
+ for (const k of Object.keys(meshes)) for (const m of meshes[k]) m.material.emissive.setHex(0x000000);
301
+ glowOn = j == null ? null : ['yaw', 'pitch', 'J3', 'J4'][j];
302
+ };
303
+
304
+ // ---- where he is shown: the canvas moves between hosts; it renders only while it sits in a visible one
305
+ let host = null;
306
+ function size() {
307
+ if (!host) return;
308
+ const w = host.clientWidth, h = host.clientHeight;
309
+ if (!w || !h) return;
310
+ renderer.setSize(w, h, false);
311
+ camera.aspect = w / h; camera.updateProjectionMatrix();
312
+ shot(current, true);
313
+ }
314
+ const ro = new ResizeObserver(size);
315
+ function mount(el, name) {
316
+ if (host === el) { if (name) shot(name); return; }
317
+ if (host) ro.unobserve(host);
318
+ host = el;
319
+ if (!el) { canvas.remove(); return; }
320
+ el.appendChild(canvas); ro.observe(el);
321
+ current = name || current; size();
322
+ }
323
+ const v = THREE.MathUtils.degToRad;
324
+ let last = performance.now(), dragging = false;
325
+ controls.addEventListener('start', () => { dragging = true; });
326
+ controls.addEventListener('end', () => { dragging = false; });
327
+ renderer.setAnimationLoop(t => {
328
+ if (!host || !host.offsetParent) return; // hidden page: no frames
329
+ if (!dragging && !tween && t - last < 30) return; // about 30 fps is plenty for a servo; full rate while dragged
330
+ const dt = Math.min(.05, Math.max(0, (t - last) / 1000)); last = t;
331
+ bot.tick(t, dt);
332
+ const [yaw, pitch, a3, a4] = bot.pose;
333
+ G.yaw.rotation.z = v(yaw); pitchPivot.rotation.x = v(-pitch);
334
+ armPivot.J3.rotation.x = v(a3); armPivot.J4.rotation.x = v(a4);
335
+ if (glowOn) {
336
+ const k = .35 + .3 * Math.sin(t / 260);
337
+ const set = glowOn === 'yaw' ? [...meshes.yaw, ...meshes.pitch] : meshes[glowOn];
338
+ for (const m of set) m.material.emissive.setRGB(.9 * k, .32 * k, .06 * k);
339
+ }
340
+ if (tween) {
341
+ const u = Math.min(1, (t - tween.s) / tween.d), e = u < .5 ? 4 * u * u * u : 1 - Math.pow(-2 * u + 2, 3) / 2;
342
+ camera.position.lerpVectors(tween.p0, tween.p1, e); controls.target.lerpVectors(tween.t0, tween.t1, e);
343
+ if (u >= 1) tween = null;
344
+ }
345
+ controls.update();
346
+ renderer.render(scene, camera);
347
+ });
348
+ shot('hero', true);
349
+
350
+ return {
351
+ loaded, mount,
352
+ shot: name => shot(name),
353
+ state: (s, force) => { if (force || bot.state !== s) bot.set(s); },
354
+ pose: p => bot.setManual(p),
355
+ loop: (A, B) => bot.loop(A, B),
356
+ face: f => face.set(f, 250),
357
+ glow: glowSet,
358
+ // Desky's colour: the printed parts and the screen behind his eyes, as the board draws them
359
+ colors: (bodyCss, eyeCss) => {
360
+ OR = bodyCss; EYE = eyeCss; bodyHex = new THREE.Color(bodyCss).getHex();
361
+ for (const m of body) m.material.color.setHex(bodyHex);
362
+ face.render(face.face, face.face, 1);
363
+ },
364
+ get poseNow() { return bot.pose.slice(); },
365
+ };
366
+ }
367
+
368
+ try {
369
+ const probe = document.createElement('canvas');
370
+ if (!(probe.getContext('webgl2') || probe.getContext('webgl'))) throw new Error('no WebGL');
371
+ const api = build();
372
+ api.loaded.then(() => { window.Clawd3D = api; dispatchEvent(new Event('clawd3d')); })
373
+ .catch(e => { console.warn('Clawd3D:', e); dispatchEvent(new Event('clawd3d-fail')); });
374
+ } catch (e) {
375
+ console.warn('Clawd3D:', e);
376
+ dispatchEvent(new Event('clawd3d-fail'));
377
+ }