Spaces:
Running

File size: 20,274 Bytes
3de135f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// Simplified 3D spacecraft models built from primitives (units: metres).
// Every builder returns { group, fit } where `fit` is the radius used to frame the main body.
// Meshes tagged with userData.inst = '<id>' belong to an instrument; their materials are
// cloned so the viewer can highlight them, and their centres become the hotspot markers.

import * as THREE from 'three';

/* ------------------------------------------------------------ textures */
function canvasTex(w, h, draw, repeat = [1, 1]) {
  const c = document.createElement('canvas'); c.width = w; c.height = h;
  draw(c.getContext('2d'), w, h);
  const t = new THREE.CanvasTexture(c);
  t.wrapS = t.wrapT = THREE.RepeatWrapping; t.repeat.set(...repeat);
  t.anisotropy = 4; t.colorSpace = THREE.SRGBColorSpace;
  return t;
}
let TEX = null;
function textures() {
  if (TEX) return TEX;
  const foil = (base, light, dark) => canvasTex(256, 256, (g, w, h) => {
    g.fillStyle = base; g.fillRect(0, 0, w, h);
    for (let i = 0; i < 420; i++) {
      g.fillStyle = Math.random() < 0.5 ? light : dark;
      g.globalAlpha = 0.08 + Math.random() * 0.22;
      g.beginPath();
      const x = Math.random() * w, y = Math.random() * h, s = 6 + Math.random() * 40;
      g.moveTo(x, y); g.lineTo(x + s, y + (Math.random() - .5) * s); g.lineTo(x + (Math.random() - .5) * s, y + s); g.fill();
    }
    g.globalAlpha = 1;
  });
  TEX = {
    gold: foil('#c8962e', '#ffe08a', '#6d4a10'),
    silver: foil('#b9bec4', '#ffffff', '#6b7076'),
    black: foil('#1b1c20', '#4a4c52', '#000000'),
    solar: canvasTex(256, 256, (g, w, h) => {
      g.fillStyle = '#9aa3ad'; g.fillRect(0, 0, w, h);
      const n = 8, s = w / n;
      for (let i = 0; i < n; i++) for (let j = 0; j < n; j++) {
        const l = 14 + Math.random() * 10;
        g.fillStyle = `hsl(222, 70%, ${l}%)`;
        g.fillRect(i * s + 2, j * s + 2, s - 4, s - 4);
        g.strokeStyle = 'rgba(200,210,230,0.25)'; g.lineWidth = 1;
        g.beginPath(); g.moveTo(i * s + s / 2, j * s + 2); g.lineTo(i * s + s / 2, j * s + s - 2); g.stroke();
      }
    })
  };
  return TEX;
}

let MAT = null;
function mats() {
  if (MAT) return MAT;
  const T = textures();
  MAT = {
    gold: new THREE.MeshStandardMaterial({ map: T.gold, color: 0xffe2a0, metalness: 0.7, roughness: 0.35 }),
    silver: new THREE.MeshStandardMaterial({ map: T.silver, metalness: 0.9, roughness: 0.32 }),
    alu: new THREE.MeshStandardMaterial({ color: 0xc9cdd2, metalness: 0.85, roughness: 0.3 }),
    white: new THREE.MeshStandardMaterial({ color: 0xe8e8e4, metalness: 0.05, roughness: 0.65, side: THREE.DoubleSide }),
    dark: new THREE.MeshStandardMaterial({ color: 0x2b2d32, metalness: 0.5, roughness: 0.55 }),
    black: new THREE.MeshStandardMaterial({ map: T.black, metalness: 0.6, roughness: 0.5 }),
    solar: new THREE.MeshStandardMaterial({ map: T.solar, metalness: 0.45, roughness: 0.3 }),
    titanium: new THREE.MeshStandardMaterial({ color: 0x8d9298, metalness: 0.9, roughness: 0.42 }),
    boom: new THREE.MeshStandardMaterial({ color: 0xd8d2c0, metalness: 0.3, roughness: 0.6 }),
    rtg: new THREE.MeshStandardMaterial({ color: 0x3b3b3e, metalness: 0.6, roughness: 0.45 }),
    copper: new THREE.MeshStandardMaterial({ color: 0xd9a441, metalness: 1, roughness: 0.18 }),
    mirror: new THREE.MeshStandardMaterial({ color: 0xd99a1c, emissive: 0x2a1600, metalness: 0.55, roughness: 0.2 }),
    shield: new THREE.MeshStandardMaterial({ color: 0xcfc2e8, metalness: 0.45, roughness: 0.35, side: THREE.DoubleSide }),
    mesh: new THREE.MeshStandardMaterial({ color: 0xe3c46a, metalness: 0.9, roughness: 0.3, transparent: true, opacity: 0.55, side: THREE.DoubleSide }),
    lens: new THREE.MeshStandardMaterial({ color: 0x101830, metalness: 1, roughness: 0.05 })
  };
  return MAT;
}

/* ------------------------------------------------------------- helpers */
const V = (x, y, z) => new THREE.Vector3(x, y, z);
function tag(mesh, inst) { if (inst) { mesh.material = mesh.material.clone(); mesh.userData.inst = inst; } return mesh; }
function add(parent, mesh, pos, inst) { if (pos) mesh.position.copy(pos); parent.add(tag(mesh, inst)); return mesh; }
function cyl(r, h, mat, seg = 24, rTop = r) { return new THREE.Mesh(new THREE.CylinderGeometry(rTop, r, h, seg), mat); }
function box(w, h, d, mat) { return new THREE.Mesh(new THREE.BoxGeometry(w, h, d), mat); }
function sphere(r, mat) { return new THREE.Mesh(new THREE.SphereGeometry(r, 20, 14), mat); }
function dish(R, depth, mat, open = 'up') {
  const pts = [];
  for (let i = 0; i <= 24; i++) { const r = R * i / 24; pts.push(new THREE.Vector2(Math.max(r, 0.001), depth * (r / R) ** 2)); }
  const m = new THREE.Mesh(new THREE.LatheGeometry(pts, 48), mat);
  if (open === 'down') m.rotation.x = Math.PI;
  return m;
}
function rod(a, b, r, mat) {
  const d = b.clone().sub(a), len = d.length();
  const m = new THREE.Mesh(new THREE.CylinderGeometry(r, r, len, 8), mat);
  m.position.copy(a).addScaledVector(d, 0.5);
  m.quaternion.setFromUnitVectors(V(0, 1, 0), d.normalize());
  return m;
}
function lattice(a, b, w, mat) {
  // triangular truss boom
  const g = new THREE.Group();
  const d = b.clone().sub(a).normalize();
  const u = Math.abs(d.y) < 0.9 ? V(0, 1, 0) : V(1, 0, 0);
  const p1 = u.clone().cross(d).normalize(), p2 = d.clone().cross(p1);
  const offs = [0, 1, 2].map(k => p1.clone().multiplyScalar(Math.cos(k * 2.094) * w).addScaledVector(p2, Math.sin(k * 2.094) * w));
  offs.forEach(o => g.add(rod(a.clone().add(o), b.clone().add(o), w * 0.12, mat)));
  const len = b.distanceTo(a), n = Math.max(2, Math.round(len / (w * 4)));
  for (let i = 0; i <= n; i++) {
    const c = a.clone().lerp(b, i / n);
    for (let k = 0; k < 3; k++) g.add(rod(c.clone().add(offs[k]), c.clone().add(offs[(k + 1) % 3]), w * 0.08, mat));
  }
  return g;
}
function rtg(len, r) {
  const g = new THREE.Group(), M = mats();
  g.add(cyl(r, len, M.rtg, 16));
  for (let i = 0; i < 6; i++) {
    const f = box(r * 1.1, len * 0.9, r * 0.08, M.rtg);
    const a = i * Math.PI / 3; f.position.set(Math.cos(a) * r * 1.3, 0, Math.sin(a) * r * 1.3); f.rotation.y = -a;
    g.add(f);
  }
  return g;
}
function wing(n, w, h, gap, start, M) {
  // row of n solar panels along +x, lying in the xz plane
  const g = new THREE.Group();
  for (let i = 0; i < n; i++) {
    const p = box(w, 0.05, h, M.solar);
    p.position.x = start + gap + i * (w + gap) + w / 2;
    g.add(p);
  }
  g.add(rod(V(0, 0, 0), V(start + gap, 0, 0), 0.06, M.alu));
  return g;
}
function sensor(size, mat) { return box(size, size * 0.7, size, mat); }

/* ------------------------------------------------------------- builders */
const BUILDERS = {
  juno() {
    const M = mats(), g = new THREE.Group();
    add(g, cyl(1.8, 2.3, M.gold, 6));
    add(g, cyl(1.82, 0.08, M.dark, 6), V(0, 1.18, 0));
    add(g, box(0.9, 0.55, 0.9, M.titanium), V(0, 1.5, 0), 'vault');
    add(g, cyl(0.25, 0.8, M.alu, 12), V(0, 1.6, 0));
    add(g, dish(1.25, 0.32, M.white), V(0, 2.0, 0));
    add(g, cyl(0.06, 0.25, M.dark, 8, 0.12), V(0, 2.75, 0));
    [0, 1, 2].forEach(k => add(g, rod(V(Math.cos(k * 2.09) * 1.0, 2.25, Math.sin(k * 2.09) * 1.0), V(0, 2.7, 0), 0.02, M.alu)));
    add(g, cyl(0.45, 0.6, M.dark, 16, 0.2), V(0, -1.45, 0));
    // three solar wings at 120°, the third shorter and carrying the magnetometer boom
    [0, 1, 2].forEach(k => {
      const n = k === 2 ? 3 : 4;
      const wg = wing(n, 2.2, 2.65, 0.08, 1.5, M);
      wg.rotation.y = k * 2 * Math.PI / 3;
      g.add(wg);
      if (k === 2) {
        const end = 1.58 + n * 2.28;
        const b = new THREE.Group(); b.rotation.y = wg.rotation.y; g.add(b);
        b.add(lattice(V(end, 0, 0), V(end + 3.8, 0, 0), 0.18, M.boom));
        add(b, sensor(0.32, M.white), V(end + 2.2, 0, 0), 'mag');
        add(b, sensor(0.32, M.white), V(end + 3.8, 0, 0), 'mag');
      }
    });
    // particle detectors, aurora cameras, MWR patch antennas, Waves dipole
    add(g, box(0.5, 0.45, 0.4, M.silver), V(1.55, 0.4, 0.9), 'particles');
    add(g, box(0.4, 0.35, 0.4, M.silver), V(-1.6, -0.3, -0.6), 'particles');
    add(g, box(0.45, 0.5, 0.35, M.dark), V(-0.2, 0.5, 1.75), 'uvs');
    add(g, cyl(0.12, 0.3, M.lens, 12), V(-0.2, 0.5, 2.0), 'uvs').rotation.x = Math.PI / 2;
    for (let i = 0; i < 6; i++) {
      const a = i * Math.PI / 3 + Math.PI / 6;
      const p = box(1.3, 1.3, 0.05, M.dark);
      p.position.set(Math.cos(a) * 1.58, -0.3, Math.sin(a) * 1.58); p.rotation.y = -a + Math.PI / 2;
      if (i % 2 === 0) add(g, p, null, 'mwr');
    }
    add(g, rod(V(0.5, -1.0, -1.2), V(2.4, -2.6, -2.6), 0.03, M.alu), null, 'waves');
    add(g, rod(V(0.5, -1.0, -1.2), V(-0.9, -2.6, -3.2), 0.03, M.alu), null, 'waves');
    return { group: g, fit: 7 };
  },

  cassini() {
    const M = mats(), g = new THREE.Group();
    add(g, cyl(1.9, 4.2, M.gold, 24));
    add(g, cyl(1.95, 1.2, M.silver, 12), V(0, 2.7, 0));
    add(g, dish(2.0, 0.55, M.white), V(0, 3.32, 0));
    add(g, cyl(0.1, 0.5, M.dark, 8), V(0, 4.1, 0));
    [0, 1, 2].forEach(k => add(g, rod(V(Math.cos(k * 2.09) * 1.4, 3.6, Math.sin(k * 2.09) * 1.4), V(0, 4.3, 0), 0.03, M.alu)));
    add(g, cyl(0.55, 0.9, M.dark, 16, 0.25), V(0.6, -2.55, 0));
    add(g, cyl(0.55, 0.9, M.dark, 16, 0.25), V(-0.6, -2.55, 0));
    [0, 2.2, 4.1].forEach(a => {
      const r = rtg(1.6, 0.22); r.position.set(Math.cos(a) * 2.3, -1.7, Math.sin(a) * 2.3);
      r.rotation.z = Math.PI / 2; r.rotation.y = -a; g.add(r);
    });
    add(g, rtg(1.6, 0.22), null);
    add(g, cyl(1.35, 0.65, M.gold, 32, 0.25), V(2.45, 0.6, 0), 'huygens').rotation.z = -Math.PI / 2;
    g.add(lattice(V(-1.9, 2.7, 0), V(-12.9, 2.9, 0), 0.16, M.boom));
    add(g, sensor(0.35, M.white), V(-7.5, 2.8, 0), 'mag');
    add(g, sensor(0.35, M.white), V(-12.9, 2.9, 0), 'mag');
    add(g, box(1.3, 1.0, 0.8, M.silver), V(0.4, 2.0, 2.15), 'mimi');
    add(g, cyl(0.25, 0.5, M.alu, 12), V(1.2, 2.2, 2.3), 'inms').rotation.z = Math.PI / 2;
    add(g, box(1.7, 1.2, 1.0, M.black), V(0, 1.5, -2.2), 'uvis');
    [-0.5, 0, 0.5].forEach(x => add(g, cyl(0.16, 0.5, M.lens, 12), V(x, 1.5, -2.8), 'uvis').rotation.x = Math.PI / 2);
    [[1, 0.25, 0.6], [-0.4, 0.25, 1], [-0.6, 0.25, -0.8]].forEach(d =>
      add(g, rod(V(0, 3.0, 0), V(0, 3.0, 0).addScaledVector(V(...d).normalize(), 10), 0.025, M.alu), null, 'rpws'));
    return { group: g, fit: 7.5 };
  },

  voyager() {
    const M = mats(), g = new THREE.Group();
    add(g, cyl(0.95, 0.47, M.black, 10));
    add(g, dish(1.83, 0.42, M.white), V(0, 0.3, 0));
    add(g, cyl(0.25, 0.08, M.white, 16), V(0, 1.6, 0));
    [0, 1, 2, 3].forEach(k => add(g, rod(V(Math.cos(k * 1.57) * 1.2, 0.55, Math.sin(k * 1.57) * 1.2), V(0, 1.58, 0), 0.02, M.alu)));
    const rec = add(g, cyl(0.16, 0.02, M.copper, 32), V(0.85, -0.05, 0.3), 'record');
    rec.rotation.z = Math.PI / 2; rec.rotation.y = -0.34;
    g.add(rod(V(-0.9, 0, 0), V(-3.4, -0.25, 0), 0.05, M.boom));
    [-1.9, -2.55, -3.2].forEach(x => { const r = rtg(0.55, 0.2); r.position.set(x, -0.15, 0); r.rotation.z = Math.PI / 2; r.traverse(o => { if (o.isMesh) tag(o, 'rtg'); }); g.add(r); });
    g.add(lattice(V(0.9, 0, 0), V(3.0, 0.05, 0), 0.12, M.boom));
    add(g, box(0.4, 0.4, 0.4, M.silver), V(1.7, 0.25, 0.1), 'pls');
    add(g, cyl(0.16, 0.45, M.alu, 12), V(2.3, 0.3, -0.1), 'pls');
    add(g, box(0.65, 0.55, 0.6, M.black), V(3.25, 0.05, 0), 'scan');
    [-0.15, 0.15].forEach(z => add(g, cyl(0.1, 0.5, M.lens, 12), V(3.6, 0.15, z), 'scan').rotation.z = Math.PI / 2);
    g.add(lattice(V(0, -0.1, -0.9), V(0, -1.2, -13.8), 0.09, M.boom));
    add(g, sensor(0.25, M.white), V(0, -0.65, -7.4), 'mag');
    add(g, sensor(0.25, M.white), V(0, -1.2, -13.8), 'mag');
    add(g, rod(V(0.3, -0.2, 0.7), V(4.5, -3.5, 8.6), 0.02, M.alu), null, 'radio');
    add(g, rod(V(-0.3, -0.2, 0.7), V(-4.5, -3.5, 8.6), 0.02, M.alu), null, 'radio');
    return { group: g, fit: 5.5 };
  },

  galileo() {
    const M = mats(), g = new THREE.Group();
    add(g, cyl(1.2, 1.4, M.black, 10));
    add(g, cyl(0.3, 0.8, M.alu, 12), V(0, 1.1, 0));
    // umbrella antenna: 18 ribs, three stuck half-open (as really happened)
    const hub = V(0, 1.5, 0);
    for (let i = 0; i < 18; i++) {
      const a = i * Math.PI / 9, stuck = i === 3 || i === 9 || i === 14;
      const r = stuck ? 1.0 : 2.4, h = stuck ? 1.4 : 0.75;
      add(g, rod(hub, V(Math.cos(a) * r, 1.5 + h, Math.sin(a) * r), 0.025, M.alu), null, 'hga');
    }
    add(g, dish(2.35, 0.75, M.mesh), V(0, 1.5, 0), 'hga');
    [1, -1].forEach(s => {
      g.add(rod(V(s * 1.1, 0.2, 0), V(s * 3.3, 0.5, 0.6 * s), 0.05, M.boom));
      const r = rtg(1.1, 0.22); r.position.set(s * 3.6, 0.55, 0.7 * s); r.rotation.z = Math.PI / 2; g.add(r);
    });
    g.add(lattice(V(0, 0.4, 1.2), V(0, 0.9, 11.8), 0.12, M.boom));
    add(g, sensor(0.3, M.white), V(0, 0.65, 7), 'mag');
    add(g, sensor(0.3, M.white), V(0, 0.9, 11.8), 'mag');
    add(g, box(0.45, 0.4, 0.45, M.silver), V(1.15, 0.2, -0.5), 'epd');
    add(g, box(0.4, 0.4, 0.4, M.silver), V(-1.1, 0.1, -0.6), 'epd');
    add(g, cyl(0.85, 1.1, M.gold, 16), V(0, -1.25, 0));
    g.add(rod(V(0.7, -1.4, 0), V(1.9, -1.9, 0), 0.06, M.alu));
    add(g, box(0.7, 0.55, 0.55, M.black), V(2.15, -1.95, 0), 'scan');
    add(g, cyl(0.12, 0.5, M.lens, 12), V(2.15, -2.3, 0), 'scan');
    add(g, cyl(0.62, 0.55, M.gold, 32, 0.2), V(0, -2.1, 0), 'probe').rotation.x = Math.PI;
    return { group: g, fit: 5.5 };
  },

  hubble() {
    const M = mats(), g = new THREE.Group();
    const along = m => { m.rotation.z = Math.PI / 2; return m; };
    add(g, along(cyl(2.15, 6.5, M.silver, 32)), V(-2.6, 0, 0));
    add(g, along(cyl(2.0, 6.2, M.silver, 32)), V(3.6, 0, 0));
    add(g, along(cyl(1.6, 0.2, M.lens, 32)), V(0.9, 0, 0), 'mirror');
    // aperture door, hinged at the top edge and swung open
    const door = new THREE.Group(); door.position.set(6.75, 2.0, 0); door.rotation.z = 100 * Math.PI / 180; g.add(door);
    add(door, along(cyl(2.0, 0.12, M.silver, 32)), V(0, -2.0, 0));
    [1, -1].forEach(s => {
      g.add(rod(V(-0.4, 0, s * 2.1), V(-0.4, 0, s * 3.0), 0.08, M.alu));
      const p = box(2.45, 0.04, 7.1, M.solar); p.position.set(-0.4, 0, s * 6.55); p.rotation.x = 0.35; add(g, p, null, 'arrays');
      g.add(rod(V(1.5, s * 2.1, 0), V(1.5, s * 3.6, 0), 0.05, M.alu));
      const a = dish(0.6, 0.15, M.white); a.position.set(1.5, s * 3.7, 0); a.rotation.z = s > 0 ? -Math.PI / 2 : Math.PI / 2; g.add(a);
    });
    add(g, box(2.2, 0.9, 1.0, M.dark), V(-4.0, 1.75, 0), 'stis');
    add(g, box(2.2, 1.0, 0.9, M.dark), V(-4.0, 0, 1.75), 'acs');
    return { group: g, fit: 8 };
  },

  jwst() {
    const M = mats(), g = new THREE.Group();
    const shape = new THREE.Shape();
    [[10.5, 0], [6.5, 6], [-6.5, 6], [-10.5, 0], [-6.5, -6], [6.5, -6]].forEach(([x, y], i) => i ? shape.lineTo(x, y) : shape.moveTo(x, y));
    for (let i = 0; i < 5; i++) {
      const s = new THREE.Mesh(new THREE.ShapeGeometry(shape), M.shield);
      s.rotation.x = -Math.PI / 2; s.position.y = i * 0.18; const k = 1 - i * 0.025; s.scale.set(k, k, 1);
      add(g, s, null, 'sunshield');
    }
    add(g, box(2.2, 1.0, 2.2, M.black), V(0, -0.8, 0));
    const sp = box(2.4, 0.04, 6, M.solar); sp.position.set(0, -1.3, 3.8); sp.rotation.x = 0.3; g.add(sp);
    add(g, cyl(0.25, 1.6, M.alu, 12), V(0, 1.6, 0));
    // primary mirror: 18 hexagonal gold segments facing +z, standing above the sunshield
    const mirror = new THREE.Group(); mirror.position.set(0, 5.0, 0.6); g.add(mirror);
    const s = 1.32, r = s / Math.sqrt(3);
    const axial = [];
    for (let q = -2; q <= 2; q++) for (let p = -2; p <= 2; p++) { const rr = Math.max(Math.abs(q), Math.abs(p), Math.abs(-q - p)); if (rr >= 1 && rr <= 2) axial.push([q, p]); }
    axial.forEach(([q, p]) => {
      const x = s * (q + p / 2), y = s * (p * Math.sqrt(3) / 2);
      const seg = new THREE.Mesh(new THREE.CylinderGeometry(r * 0.97, r * 0.97, 0.08, 6), M.mirror);
      seg.rotation.x = Math.PI / 2; seg.rotation.y = Math.PI / 6;
      seg.position.set(x, y, (x * x + y * y) / 40);
      add(mirror, seg, null, 'mirror');
    });
    add(mirror, box(3.4, 3.4, 0.5, M.black), V(0, 0, -0.4));
    add(mirror, box(2.2, 2.0, 1.6, M.black), V(0, -0.6, -1.4), 'nircam');
    add(mirror, box(1.2, 1.0, 1.0, M.dark), V(1.3, -1.2, -1.2), 'miri');
    add(mirror, cyl(0.38, 0.1, M.mirror, 6), V(0, 0, 7.2)).rotation.x = Math.PI / 2;
    [[0, 3.1], [-2.7, -1.6], [2.7, -1.6]].forEach(([x, y]) => mirror.add(rod(V(x, y, 0.3), V(0, 0, 7.15), 0.04, M.alu)));
    return { group: g, fit: 9 };
  },

  juice() {
    const M = mats(), g = new THREE.Group();
    add(g, box(2.5, 4.0, 2.6, M.gold));
    add(g, dish(1.25, 0.32, M.white), V(0, 2.05, 0));
    add(g, cyl(0.06, 0.3, M.dark, 8), V(0, 2.75, 0));
    [1, -1].forEach(side => {
      // each wing: 3 panels in a row plus two side panels on the middle one (a cross)
      const wgrp = new THREE.Group(); wgrp.rotation.y = side > 0 ? -Math.PI / 2 : Math.PI / 2; g.add(wgrp);
      const wg = wing(3, 2.6, 3.4, 0.08, 1.3, M); wgrp.add(wg);
      [1, -1].forEach(z => { const p = box(2.6, 0.05, 3.4, M.solar); p.position.set(1.38 + 2.68 + 1.3, 0, z * 3.5); wgrp.add(p); });
    });
    g.add(lattice(V(-1.25, 1.0, 0), V(-11.8, 1.4, 0), 0.14, M.boom));
    add(g, sensor(0.3, M.white), V(-6.5, 1.2, 0), 'jmag');
    add(g, sensor(0.3, M.white), V(-11.8, 1.4, 0), 'jmag');
    add(g, box(16, 0.05, 0.35, M.alu), V(9.3, 1.7, 0.9), 'rime');
    [[1, 1], [1, -1], [-1, 1], [-1, -1]].forEach(([x, z]) => {
      const a = V(x * 1.25, -1.8, z * 1.3), b = a.clone().add(V(x * 2, -1.0, z * 2));
      add(g, rod(a, b, 0.025, M.alu), null, 'rpwi'); add(g, sphere(0.1, M.alu), b, 'rpwi');
    });
    add(g, box(0.5, 0.5, 0.4, M.silver), V(1.0, -0.6, 1.5), 'pep');
    add(g, box(0.45, 0.4, 0.4, M.silver), V(-0.9, 0.6, -1.5), 'pep');
    [-0.6, 0, 0.6].forEach(y => add(g, cyl(0.17, 0.45, M.lens, 12), V(1.45, y - 1.0, -0.6), 'optical').rotation.z = Math.PI / 2);
    add(g, box(0.4, 1.8, 1.0, M.black), V(1.35, -1.0, -0.6), 'optical');
    return { group: g, fit: 8 };
  },

  clipper() {
    const M = mats(), g = new THREE.Group();
    add(g, cyl(0.8, 5.0, M.white, 24));
    add(g, box(1.8, 1.1, 1.8, M.alu), V(0, 0.9, 0), 'vault');
    add(g, dish(1.5, 0.42, M.white), V(0, 2.55, 0));
    add(g, cyl(0.07, 0.35, M.dark, 8), V(0, 3.4, 0));
    add(g, cyl(0.5, 0.6, M.dark, 16, 0.25), V(0, -2.8, 0));
    [1, -1].forEach(side => {
      const wg = wing(5, 2.7, 4.1, 0.1, 0.9, M); wg.position.y = -0.8; if (side < 0) wg.rotation.y = Math.PI; g.add(wg);
      [2.15, -2.15].forEach(z => add(g, box(17.6, 0.06, 0.12, M.alu), V(side * 9.6, -0.7, z), 'reason'));
    });
    g.add(lattice(V(0, -2.4, 0.6), V(0, -5.6, 8.4), 0.16, M.boom));
    [0.45, 0.75, 1].forEach(f => add(g, sensor(0.28, M.white), V(0, -2.4, 0.6).lerp(V(0, -5.6, 8.4), f), 'ecm'));
    add(g, cyl(0.18, 0.2, M.alu, 16), V(0.85, 1.6, 0.3), 'pims').rotation.z = -Math.PI / 2;
    add(g, cyl(0.18, 0.2, M.alu, 16), V(-0.85, 1.6, -0.3), 'pims').rotation.z = Math.PI / 2;
    add(g, box(1.4, 0.9, 0.7, M.black), V(0, -1.6, 1.05), 'optical');
    [-0.4, 0, 0.4].forEach(x => add(g, cyl(0.13, 0.3, M.lens, 12), V(x, -1.6, 1.5), 'optical').rotation.x = Math.PI / 2);
    add(g, box(0.6, 0.6, 0.5, M.silver), V(-0.9, -0.3, 0.6), 'insitu');
    add(g, cyl(0.25, 0.3, M.alu, 16), V(0.95, -0.3, 0.5), 'insitu');
    return { group: g, fit: 9 };
  }
};

export const CRAFT_KEYS = Object.keys(BUILDERS);

export function buildCraft(key) {
  const out = BUILDERS[key]();
  out.group.userData.craft = key;
  return out;
}

// Centre of every tagged instrument in the group's local space.
export function hotspots(group) {
  const boxes = new Map();
  group.updateWorldMatrix(true, true);
  const inv = group.matrixWorld.clone().invert();
  group.traverse(o => {
    if (!o.isMesh || !o.userData.inst) return;
    o.geometry.computeBoundingBox();
    const b = o.geometry.boundingBox.clone().applyMatrix4(o.matrixWorld).applyMatrix4(inv);
    const id = o.userData.inst;
    if (!boxes.has(id)) boxes.set(id, { box: b, meshes: [o] });
    else { boxes.get(id).box.union(b); boxes.get(id).meshes.push(o); }
  });
  const res = new Map();
  boxes.forEach((v, id) => res.set(id, { pos: v.box.getCenter(new THREE.Vector3()), meshes: v.meshes }));
  return res;
}