File size: 28,294 Bytes
59c7fd7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
import * as THREE from 'three';
import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
import { redPaintMaps, brassMaps, woodMaps, rubberMaps, canvasMaps } from './textures.js';

export function createSubject() {
  const subject = new THREE.Group();
  subject.name = 'subject';

  // ---------------- materials ----------------
  const redMaps = redPaintMaps(2, 1.5);
  const red = new THREE.MeshPhysicalMaterial({
    ...redMaps, color: 0xffffff, metalness: 0.24, roughness: 0.47,
    bumpScale: 0.005, clearcoat: 0.32, clearcoatRoughness: 0.5, envMapIntensity: 0.95,
  });
  const redDark = new THREE.MeshPhysicalMaterial({
    ...redMaps, color: 0xb9b9b9, metalness: 0.22, roughness: 0.55,
    bumpScale: 0.004, clearcoat: 0.22, clearcoatRoughness: 0.55,
  });
  const brass = new THREE.MeshStandardMaterial({
    ...brassMaps(1, 1), metalness: 1.0, roughness: 0.3, envMapIntensity: 1.25,
  });
  const brassDark = new THREE.MeshStandardMaterial({
    color: 0x9a7630, metalness: 1.0, roughness: 0.42,
  });
  const wood = new THREE.MeshStandardMaterial({
    ...woodMaps(2, 1), metalness: 0.0, roughness: 0.62,
  });
  const woodDark = new THREE.MeshStandardMaterial({
    ...woodMaps(1, 1, 'dark'), metalness: 0.0, roughness: 0.72,
  });
  const rubber = new THREE.MeshStandardMaterial({
    ...rubberMaps(1, 1), metalness: 0.0, roughness: 0.88,
  });
  const hoseMat = new THREE.MeshStandardMaterial({
    ...canvasMaps(2, 2), color: 0xa5976f, metalness: 0.0, roughness: 0.92,
  });
  const steel = new THREE.MeshStandardMaterial({ color: 0x2c2d31, metalness: 0.9, roughness: 0.46 });
  const chrome = new THREE.MeshStandardMaterial({ color: 0xdfe4e8, metalness: 1.0, roughness: 0.15, envMapIntensity: 1.4 });
  const glass = new THREE.MeshPhysicalMaterial({
    color: 0x38525c, metalness: 0.45, roughness: 0.05, transparent: true,
    opacity: 0.5, envMapIntensity: 2.0, side: THREE.DoubleSide,
  });
  const interior = new THREE.MeshStandardMaterial({ color: 0x1b1612, roughness: 0.85 });
  const leather = new THREE.MeshStandardMaterial({ color: 0x3a2418, roughness: 0.7 });
  const lampLens = new THREE.MeshStandardMaterial({
    color: 0xffe9c0, emissive: 0xffcd80, emissiveIntensity: 1.1,
    metalness: 0.1, roughness: 0.15,
  });
  const redLens = new THREE.MeshStandardMaterial({
    color: 0x8e0f0f, emissive: 0x550303, emissiveIntensity: 0.7, roughness: 0.25,
  });

  function setup(mesh, cast = true, receive = true) {
    mesh.castShadow = cast;
    mesh.receiveShadow = receive;
    return mesh;
  }

  const box = (w, h, d, mat, r = 0.02, seg = 2) =>
    setup(new THREE.Mesh(new RoundedBoxGeometry(w, h, d, seg, r), mat));

  function add(parent, mesh, x = 0, y = 0, z = 0) {
    mesh.position.set(x, y, z);
    parent.add(mesh);
    return mesh;
  }

  // ---------------- chassis ----------------
  const chassis = new THREE.Group();
  subject.add(chassis);
  for (const z of [-0.48, 0.48]) {
    add(chassis, box(5.25, 0.14, 0.1, steel, 0.015), -0.12, 0.7, z);
  }
  add(chassis, box(5.0, 0.09, 1.05, steel, 0.02), -0.15, 0.77, 0);
  // front + rear cross members
  add(chassis, box(0.12, 0.1, 1.4, steel, 0.015), 2.25, 0.66, 0);
  add(chassis, box(0.12, 0.1, 1.5, steel, 0.015), -2.45, 0.66, 0);

  // ---------------- wheels ----------------
  function makeWheel(radius, width, spokes, accent) {
    const g = new THREE.Group();
    // tire
    const pr = [
      [0.40, -width * 0.5], [0.475, -width * 0.5], [0.525, -width * 0.38],
      [0.55, -width * 0.2], [0.558, 0], [0.55, width * 0.2],
      [0.525, width * 0.38], [0.475, width * 0.5], [0.40, width * 0.5],
      [0.40, -width * 0.5],
    ].map(([r, y]) => new THREE.Vector2(r * radius / 0.558, y));
    const tireGeo = new THREE.LatheGeometry(pr, 32);
    tireGeo.rotateX(Math.PI / 2);
    const tire = setup(new THREE.Mesh(tireGeo, rubber));
    g.add(tire);
    // tread blocks
    const tread = new THREE.InstancedMesh(new THREE.BoxGeometry(0.055, 0.045, width * 0.72), rubber, 20);
    const d = new THREE.Object3D();
    for (let i = 0; i < 20; i++) {
      const a = (i / 20) * Math.PI * 2;
      d.position.set(Math.cos(a) * radius * 0.985, Math.sin(a) * radius * 0.985, 0);
      d.rotation.set(0, 0, a + Math.PI / 2);
      d.updateMatrix();
      tread.setMatrixAt(i, d.matrix);
    }
    tread.castShadow = true;
    g.add(tread);
    // steel rim ring
    const rim = setup(new THREE.Mesh(new THREE.TorusGeometry(radius * 0.72, 0.024, 8, 36), steel));
    rim.position.z = width * 0.02;
    g.add(rim);
    // wooden artillery spokes
    const spokeGeo = new THREE.CylinderGeometry(0.023, 0.04, radius * 0.57, 6);
    const spokeMesh = new THREE.InstancedMesh(spokeGeo, accent, spokes);
    for (let i = 0; i < spokes; i++) {
      const a = (i / spokes) * Math.PI * 2;
      d.position.set(Math.sin(a) * radius * 0.43, Math.cos(a) * radius * 0.43, 0);
      d.rotation.set(0, 0, -a);
      d.updateMatrix();
      spokeMesh.setMatrixAt(i, d.matrix);
    }
    spokeMesh.castShadow = true;
    g.add(spokeMesh);
    // hub
    const hubGeo = new THREE.CylinderGeometry(radius * 0.2, radius * 0.24, width * 0.9, 16);
    hubGeo.rotateX(Math.PI / 2);
    const hub = setup(new THREE.Mesh(hubGeo, woodDark));
    g.add(hub);
    const capGeo = new THREE.CylinderGeometry(radius * 0.1, radius * 0.15, width * 0.4, 16);
    capGeo.rotateX(Math.PI / 2);
    const cap = setup(new THREE.Mesh(capGeo, brass));
    cap.position.z = width * 0.5;
    g.add(cap);
    // bolts
    const bolts = new THREE.InstancedMesh(new THREE.CylinderGeometry(0.012, 0.012, 0.02, 6), brass, 8);
    for (let i = 0; i < 8; i++) {
      const a = (i / 8) * Math.PI * 2;
      d.position.set(Math.cos(a) * radius * 0.18, Math.sin(a) * radius * 0.18, width * 0.46);
      d.rotation.set(Math.PI / 2, 0, 0);
      d.updateMatrix();
      bolts.setMatrixAt(i, d.matrix);
    }
    g.add(bolts);
    return g;
  }

  const wheels = [];
  const frontRadius = 0.54, rearRadius = 0.58;
  for (const z of [-0.82, 0.82]) {
    const w = makeWheel(frontRadius, 0.30, 12, woodDark);
    w.position.set(1.58, frontRadius, z);
    subject.add(w);
    wheels.push(w);
  }
  for (const z of [-0.86, 0.86]) {
    const w = makeWheel(rearRadius, 0.34, 12, woodDark);
    w.position.set(-1.55, rearRadius, z);
    subject.add(w);
    wheels.push(w);
  }

  // axles
  for (const [x, y, zs] of [[1.58, frontRadius, 1.75], [-1.55, rearRadius, 1.8]]) {
    const axleGeo = new THREE.CylinderGeometry(0.045, 0.045, zs, 12);
    axleGeo.rotateX(Math.PI / 2);
    add(subject, setup(new THREE.Mesh(axleGeo, steel)), x, y, 0);
  }
  // leaf springs
  for (const x of [1.5, -1.45]) {
    for (const z of [-0.5, 0.5]) {
      const spring = box(1.1, 0.05, 0.07, steel, 0.012);
      spring.position.set(x, 0.82, z);
      subject.add(spring);
      for (let i = 0; i < 3; i++) {
        const leaf = box(1.0 - i * 0.16, 0.02, 0.06, steel, 0.006);
        leaf.position.set(x, 0.85 + i * 0.018, z);
        subject.add(leaf);
      }
    }
  }

  // ---------------- fenders ----------------
  function archFender(radius, thick, width, start, end, mat) {
    const shape = new THREE.Shape();
    shape.absarc(0, 0, radius, start, end, false);
    shape.absarc(0, 0, radius - thick, end, start, true);
    const geo = new THREE.ExtrudeGeometry(shape, {
      depth: width, bevelEnabled: true, bevelSize: 0.012, bevelThickness: 0.012, bevelSegments: 2, curveSegments: 24,
    });
    geo.translate(0, 0, -width / 2);
    return setup(new THREE.Mesh(geo, mat));
  }
  for (const z of [-0.83, 0.83]) {
    const f = archFender(frontRadius + 0.17, 0.05, 0.40, 0.06, Math.PI - 0.06, redDark);
    f.position.set(1.58, frontRadius, z);
    subject.add(f);
  }
  for (const z of [-0.87, 0.87]) {
    const f = archFender(rearRadius + 0.17, 0.05, 0.48, 0.1, Math.PI - 0.02, redDark);
    f.position.set(-1.55, rearRadius, z);
    subject.add(f);
  }

  // running boards
  for (const z of [-0.98, 0.98]) {
    const rb = box(2.7, 0.07, 0.28, steel, 0.02);
    rb.position.set(-0.05, 0.52, z);
    subject.add(rb);
    // brass step plate
    const step = box(0.5, 0.025, 0.22, brassDark, 0.01);
    step.position.set(0.35, 0.565, z);
    subject.add(step);
    // support brackets
    for (const x of [-1.1, 0.0, 1.1]) {
      add(subject, box(0.05, 0.12, 0.22, steel, 0.01), x, 0.47, z);
    }
  }

  // ---------------- hood ----------------
  const hoodShape = new THREE.Shape();
  hoodShape.moveTo(0.30, 0.80);
  hoodShape.lineTo(1.48, 0.80);
  hoodShape.lineTo(1.60, 0.92);
  hoodShape.lineTo(1.60, 1.22);
  hoodShape.quadraticCurveTo(1.58, 1.38, 1.36, 1.41);
  hoodShape.lineTo(0.30, 1.43);
  hoodShape.closePath();
  const hoodGeo = new THREE.ExtrudeGeometry(hoodShape, {
    depth: 1.16, bevelEnabled: true, bevelSize: 0.045, bevelThickness: 0.045,
    bevelSegments: 4, curveSegments: 16,
  });
  hoodGeo.translate(0, 0, -0.58);
  const hood = setup(new THREE.Mesh(hoodGeo, red));
  subject.add(hood);

  // hood side louvers
  for (const z of [-0.6, 0.6]) {
    for (let i = 0; i < 10; i++) {
      const l = box(0.012, 0.02, 0.56, steel, 0.004);
      l.position.set(0.85 + i * 0.05, 1.0, z);
      subject.add(l);
    }
  }
  // hood centre seam
  const seam = box(1.1, 0.014, 0.018, brassDark, 0.004);
  seam.position.set(0.9, 1.445, 0);
  subject.add(seam);
  // cowl band
  add(subject, box(0.1, 0.64, 1.24, brassDark, 0.02), 0.33, 1.11, 0);

  // radiator
  const radiator = box(0.2, 0.60, 1.24, red, 0.03);
  radiator.position.set(1.66, 1.10, 0);
  subject.add(radiator);
  // chrome surround
  for (const [w, h, z] of [[0.03, 0.62, -0.615], [0.03, 0.62, 0.615]]) {
    add(subject, box(0.05, h, 0.055, chrome, 0.012), 1.77, 1.10, z);
  }
  add(subject, box(0.05, 0.055, 1.24, chrome, 0.012), 1.77, 1.40, 0);
  add(subject, box(0.05, 0.055, 1.24, chrome, 0.012), 1.77, 0.80, 0);
  // grille slats
  const slats = new THREE.InstancedMesh(new THREE.BoxGeometry(0.02, 0.53, 0.022), chrome, 15);
  const sd = new THREE.Object3D();
  for (let i = 0; i < 15; i++) {
    sd.position.set(1.775, 1.10, -0.55 + i * 0.079);
    sd.updateMatrix();
    slats.setMatrixAt(i, sd.matrix);
  }
  slats.castShadow = true;
  subject.add(slats);
  // radiator cap
  const capGeo = new THREE.CylinderGeometry(0.05, 0.06, 0.07, 12);
  add(subject, setup(new THREE.Mesh(capGeo, brass)), 1.55, 1.46, 0);
  const capTop = new THREE.SphereGeometry(0.05, 12, 8);
  add(subject, setup(new THREE.Mesh(capTop, brass)), 1.55, 1.5, 0);

  // headlights
  for (const z of [-0.67, 0.67]) {
    const g = new THREE.Group();
    const bucketPts = [
      [0.02, -0.14], [0.085, -0.14], [0.105, -0.07], [0.105, 0.045], [0.095, 0.075], [0.02, 0.075],
    ].map(([r, y]) => new THREE.Vector2(r, y));
    const bg = new THREE.LatheGeometry(bucketPts, 20);
    bg.rotateZ(-Math.PI / 2);
    const bucket = setup(new THREE.Mesh(bg, brass));
    g.add(bucket);
    const lensGeo = new THREE.CircleGeometry(0.092, 20);
    lensGeo.rotateY(Math.PI / 2);
    const lens = new THREE.Mesh(lensGeo, lampLens);
    lens.position.x = 0.077;
    g.add(lens);
    const ringGeo = new THREE.TorusGeometry(0.1, 0.013, 8, 24);
    ringGeo.rotateY(Math.PI / 2);
    const ring = setup(new THREE.Mesh(ringGeo, chrome));
    ring.position.x = 0.075;
    g.add(ring);
    // bracket
    const br = box(0.05, 0.24, 0.05, steel, 0.012);
    br.position.set(-0.05, -0.2, 0);
    g.add(br);
    g.position.set(1.81, 1.16, z);
    subject.add(g);
  }

  // ---------------- cab ----------------
  const cab = new THREE.Group();
  subject.add(cab);
  // lower body / doors
  const cabLower = box(1.34, 0.72, 1.86, red, 0.06);
  cabLower.position.set(-0.34, 1.15, 0);
  cab.add(cabLower);
  // interior volume
  const inner = box(1.12, 0.62, 1.62, interior, 0.03);
  inner.position.set(-0.34, 1.72, 0);
  cab.add(inner);
  // bench seat
  const seat = box(0.42, 0.16, 1.5, leather, 0.05);
  seat.position.set(-0.7, 1.52, 0);
  cab.add(seat);
  const seatBack = box(0.14, 0.5, 1.5, leather, 0.05);
  seatBack.position.set(-0.89, 1.75, 0);
  cab.add(seatBack);
  // steering wheel
  const sw = new THREE.Group();
  const swRim = new THREE.Mesh(new THREE.TorusGeometry(0.19, 0.018, 8, 24), woodDark);
  sw.add(swRim);
  for (let i = 0; i < 3; i++) {
    const sp = new THREE.Mesh(new THREE.CylinderGeometry(0.008, 0.008, 0.19, 5), steel);
    sp.position.set(Math.cos(i * Math.PI * 2 / 3) * 0.095, Math.sin(i * Math.PI * 2 / 3) * 0.095, 0);
    sp.rotation.z = i * Math.PI * 2 / 3 + Math.PI / 2;
    sw.add(sp);
  }
  sw.position.set(0.02, 1.66, -0.46);
  sw.rotation.set(0, 0, -0.55);
  cab.add(sw);
  // corner pillars
  for (const x of [-0.96, 0.31]) {
    for (const z of [-0.905, 0.905]) {
      const p = box(0.09, 0.6, 0.09, red, 0.02);
      p.position.set(x, 1.78, z);
      cab.add(p);
    }
  }
  // rear pillars / panel
  const rearPanel = box(0.09, 1.2, 1.82, red, 0.03);
  rearPanel.position.set(-0.98, 1.38, 0);
  cab.add(rearPanel);
  // roof
  const roof = box(1.5, 0.17, 1.98, red, 0.07);
  roof.position.set(-0.33, 2.06, 0);
  cab.add(roof);
  const roofCap = box(1.36, 0.05, 1.86, redDark, 0.025);
  roofCap.position.set(-0.33, 2.155, 0);
  cab.add(roofCap);
  // roof brass rail
  for (const z of [-0.92, 0.92]) {
    const rail = new THREE.Mesh(new THREE.CylinderGeometry(0.012, 0.012, 1.1, 8), brass);
    rail.rotation.z = Math.PI / 2;
    rail.position.set(-0.3, 2.19, z);
    cab.add(rail);
    for (const x of [-0.8, 0.2]) {
      add(cab, box(0.04, 0.07, 0.03, brass, 0.008), x, 2.16, z);
    }
  }
  // windshield glass
  const ws = box(0.02, 0.48, 1.56, glass, 0.008);
  ws.position.set(0.335, 1.73, 0);
  cab.add(ws);
  // windshield frame
  add(cab, box(0.04, 0.05, 1.62, brassDark, 0.01), 0.34, 1.97, 0);
  add(cab, box(0.04, 0.05, 1.62, brassDark, 0.01), 0.34, 1.49, 0);
  add(cab, box(0.04, 0.5, 0.05, brassDark, 0.01), 0.34, 1.73, 0);
  add(cab, box(0.04, 0.5, 0.04, brassDark, 0.01), 0.34, 1.73, -0.8);
  add(cab, box(0.04, 0.5, 0.04, brassDark, 0.01), 0.34, 1.73, 0.8);
  // side windows
  for (const z of [-0.925, 0.925]) {
    const sg = box(0.95, 0.46, 0.02, glass, 0.008);
    sg.position.set(-0.32, 1.73, z);
    cab.add(sg);
    add(cab, box(1.0, 0.04, 0.035, brassDark, 0.01), -0.32, 1.97, z);
    add(cab, box(1.0, 0.04, 0.035, brassDark, 0.01), -0.32, 1.49, z);
    add(cab, box(0.04, 0.5, 0.035, brassDark, 0.01), -0.79, 1.73, z);
    add(cab, box(0.04, 0.5, 0.035, brassDark, 0.01), 0.15, 1.73, z);
  }
  // door panels + handles
  for (const z of [-0.94, 0.94]) {
    const dp = box(0.98, 0.5, 0.025, redDark, 0.03);
    dp.position.set(-0.34, 1.14, z);
    cab.add(dp);
    const h = new THREE.Mesh(new THREE.CylinderGeometry(0.012, 0.012, 0.16, 8), brass);
    h.rotation.x = Math.PI / 2;
    h.position.set(-0.04, 1.14, z);
    cab.add(h);
    // hinges
    for (const y of [0.98, 1.32]) {
      add(cab, box(0.06, 0.1, 0.03, brass, 0.008), -0.76, y, z);
    }
  }
  // cowl trim / instrument panel
  add(cab, box(0.06, 0.52, 1.78, brassDark, 0.015), 0.3, 1.2, 0);

  // door number decals
  const numTex = makeNumberTexture('1');
  const decal = new THREE.MeshStandardMaterial({
    map: numTex, transparent: true, metalness: 0.85, roughness: 0.3,
    depthWrite: false, polygonOffset: true, polygonOffsetFactor: -2,
  });
  for (const z of [-0.955, 0.955]) {
    const p = new THREE.Mesh(new THREE.PlaneGeometry(0.34, 0.34), decal);
    p.position.set(-0.34, 1.16, z);
    p.rotation.y = z > 0 ? 0 : Math.PI;
    cab.add(p);
  }

  // ---------------- brass bell ----------------
  const bellGroup = new THREE.Group();
  {
    const pts = [
      [0.018, 0.20], [0.045, 0.17], [0.055, 0.12], [0.07, 0.06],
      [0.1, 0.015], [0.115, 0.0], [0.10, -0.005], [0.085, 0.02],
      [0.06, 0.06], [0.045, 0.11], [0.035, 0.16], [0.012, 0.19],
    ].map(([r, y]) => new THREE.Vector2(r, y));
    const bell = setup(new THREE.Mesh(new THREE.LatheGeometry(pts, 28), brass));
    bellGroup.add(bell);
    // crown knob
    const knob = new THREE.Mesh(new THREE.SphereGeometry(0.035, 12, 8), brass);
    knob.position.y = 0.21;
    bellGroup.add(knob);
    // yoke arch
    const yokeCurve = new THREE.CatmullRomCurve3([
      new THREE.Vector3(-0.135, 0.03, 0), new THREE.Vector3(-0.14, 0.24, 0),
      new THREE.Vector3(0, 0.32, 0), new THREE.Vector3(0.14, 0.24, 0),
      new THREE.Vector3(0.135, 0.03, 0),
    ]);
    const yoke = setup(new THREE.Mesh(new THREE.TubeGeometry(yokeCurve, 24, 0.014, 8), brass));
    bellGroup.add(yoke);
    // base plate
    const base = new THREE.Mesh(new THREE.CylinderGeometry(0.11, 0.13, 0.035, 20), brassDark);
    base.position.y = -0.04;
    bellGroup.add(base);
    // clapper
    const clap = new THREE.Mesh(new THREE.SphereGeometry(0.022, 8, 6), brassDark);
    clap.position.y = -0.01;
    bellGroup.add(clap);
    bellGroup.position.set(0.58, 1.50, 0);
    bellGroup.scale.setScalar(0.92);
    cab.add(bellGroup);
  }

  // horn
  {
    const hornCurve = new THREE.CatmullRomCurve3([
      new THREE.Vector3(1.3, 1.33, -0.45), new THREE.Vector3(1.42, 1.36, -0.5),
      new THREE.Vector3(1.52, 1.4, -0.55),
    ]);
    const h = setup(new THREE.Mesh(new THREE.TubeGeometry(hornCurve, 12, 0.035, 10), brass));
    subject.add(h);
    const cone = setup(new THREE.Mesh(new THREE.CylinderGeometry(0.075, 0.03, 0.12, 16), brass));
    cone.rotation.z = -Math.PI / 2;
    cone.position.set(1.57, 1.42, -0.56);
    subject.add(cone);
  }

  // ---------------- apparatus body ----------------
  const body = new THREE.Group();
  subject.add(body);
  const bodyBox = box(1.95, 0.76, 1.86, red, 0.05);
  bodyBox.position.set(-1.88, 1.17, 0);
  body.add(bodyBox);
  // rear panel and top cap
  const topCap = box(2.0, 0.08, 1.92, redDark, 0.03);
  topCap.position.set(-1.88, 1.57, 0);
  body.add(topCap);
  // side compartment doors
  for (const z of [-0.945, 0.945]) {
    for (let i = 0; i < 2; i++) {
      const d = box(0.82, 0.52, 0.03, redDark, 0.025);
      d.position.set(-2.28 + i * 0.02, 1.15, z);
      body.add(d);
      // panel inset frame
      const frame = box(0.7, 0.4, 0.012, red, 0.02);
      frame.position.set(-2.28, 1.15, z + Math.sign(z) * 0.02);
      body.add(frame);
      // hinges
      for (const y of [0.95, 1.35]) {
        add(body, box(0.06, 0.09, 0.025, brass, 0.006), -2.66, y, z + Math.sign(z) * 0.025);
      }
      // latch
      add(body, box(0.06, 0.04, 0.02, brass, 0.006), -1.93, 1.15, z + Math.sign(z) * 0.03);
      // small gold trim
      add(body, box(0.86, 0.02, 0.015, brassDark, 0.005), -2.28, 1.43, z + Math.sign(z) * 0.02);
    }
  }
  // rear doors
  for (const z of [-0.44, 0.44]) {
    const d = box(0.03, 0.55, 0.8, redDark, 0.02);
    d.position.set(-2.87, 1.15, z);
    body.add(d);
    for (const y of [0.95, 1.35]) {
      add(body, box(0.025, 0.09, 0.06, brass, 0.006), -2.89, y, z);
    }
  }
  // body top rail
  for (const z of [-0.94, 0.94]) {
    add(body, new THREE.Mesh(new THREE.CylinderGeometry(0.022, 0.022, 1.85, 8), brass), 0, 0, 0)
      .rotation.z = Math.PI / 2;
    const rail = body.children[body.children.length - 1];
    rail.position.set(-1.88, 1.61, z);
  }
  // rear step + bumper
  const stepBoard = box(1.4, 0.07, 0.34, woodDark, 0.02);
  stepBoard.position.set(-2.98, 0.56, 0);
  body.add(stepBoard);
  for (const z of [-0.6, 0.6]) {
    add(body, box(0.06, 0.34, 0.06, steel, 0.01), -2.9, 0.74, z);
  }
  // tow hitch
  add(body, box(0.28, 0.09, 0.09, steel, 0.02), -3.06, 0.62, 0);

  // spare wheel on rear? skip

  // ---------------- hose reels ----------------
  function makeReel(z) {
    const g = new THREE.Group();
    const drum = new THREE.Mesh(new THREE.CylinderGeometry(0.24, 0.24, 0.3, 24), woodDark);
    drum.rotation.x = Math.PI / 2;
    setup(drum);
    g.add(drum);
    for (const dz of [-0.145, 0.145]) {
      const fl = new THREE.Mesh(new THREE.CylinderGeometry(0.262, 0.262, 0.03, 24), brass);
      fl.rotation.x = Math.PI / 2;
      fl.position.z = dz;
      setup(fl);
      g.add(fl);
    }
    // coiled hose
    const pts = [];
    const turns = 4.5;
    for (let i = 0; i <= 180; i++) {
      const t = i / 180;
      const a = t * Math.PI * 2 * turns;
      const r = 0.298 + Math.sin(t * Math.PI) * 0.01;
      pts.push(new THREE.Vector3(Math.cos(a) * r, Math.sin(a) * r, -0.1 + t * 0.2));
    }
    const curve = new THREE.CatmullRomCurve3(pts);
    const coil = setup(new THREE.Mesh(new THREE.TubeGeometry(curve, 320, 0.028, 8), hoseMat));
    g.add(coil);
    // brass axle + crank
    const axle = new THREE.Mesh(new THREE.CylinderGeometry(0.03, 0.03, 0.46, 10), steel);
    axle.rotation.x = Math.PI / 2;
    g.add(axle);
    const crank = new THREE.Mesh(new THREE.TorusGeometry(0.1, 0.014, 6, 16), brass);
    crank.position.z = 0.25;
    g.add(crank);
    // dangling hose tail + brass nozzle
    const tail = new THREE.CatmullRomCurve3([
      new THREE.Vector3(0.24, 0.18, 0.12), new THREE.Vector3(0.34, 0.06, 0.14),
      new THREE.Vector3(0.36, -0.12, 0.15), new THREE.Vector3(0.33, -0.3, 0.14),
      new THREE.Vector3(0.28, -0.44, 0.1),
    ]);
    const tm = setup(new THREE.Mesh(new THREE.TubeGeometry(tail, 20, 0.026, 8), hoseMat));
    g.add(tm);
    const nozzle = setup(new THREE.Mesh(new THREE.CylinderGeometry(0.028, 0.045, 0.14, 10), brass));
    nozzle.position.set(0.272, -0.5, 0.09);
    nozzle.rotation.z = 0.16;
    g.add(nozzle);
    return g;
  }
  // mounting bracket
  for (const z of [-0.42, 0.42]) {
    const reel = makeReel(z);
    reel.position.set(-2.58, 1.86, z);
    body.add(reel);
    add(subject, box(0.07, 0.3, 0.07, brassDark, 0.012), -2.58, 1.74, z + Math.sign(z) * 0.22);
  }
  // hose bed canvas cover (folded hose) along rear top
  const bed = box(1.2, 0.18, 1.3, canvasMaps ? hoseMat : hoseMat, 0.05);
  bed.position.set(-2.05, 1.68, 0);
  bed.scale.y = 0.7;
  body.add(bed);
  for (let i = 0; i < 3; i++) {
    const strap = box(0.05, 0.2, 1.34, leather, 0.012);
    strap.position.set(-2.4 + i * 0.35, 1.68, 0);
    body.add(strap);
  }

  // ---------------- ladder ----------------
  const ladder = new THREE.Group();
  const ladderRailGeo = new RoundedBoxGeometry(4.9, 0.095, 0.075, 2, 0.02);
  for (const z of [-0.31, 0.31]) {
    const rail = new THREE.Mesh(ladderRailGeo, wood);
    rail.position.set(-0.55, 0, z);
    setup(rail);
    ladder.add(rail);
  }
  const rungGeo = new THREE.BoxGeometry(0.075, 0.06, 0.62);
  const rungs = new THREE.InstancedMesh(rungGeo, wood, 15);
  const rd = new THREE.Object3D();
  for (let i = 0; i < 15; i++) {
    rd.position.set(-2.9 + i * 0.335, 0, 0);
    rd.updateMatrix();
    rungs.setMatrixAt(i, rd.matrix);
  }
  rungs.castShadow = true;
  ladder.add(rungs);
  // metal rung ends
  const endGeo = new THREE.CylinderGeometry(0.011, 0.011, 0.66, 6);
  const ends = new THREE.InstancedMesh(endGeo, steel, 15);
  for (let i = 0; i < 15; i++) {
    rd.position.set(-2.9 + i * 0.335, 0, 0);
    rd.rotation.set(Math.PI / 2, 0, 0);
    rd.updateMatrix();
    ends.setMatrixAt(i, rd.matrix);
  }
  ladder.add(ends);
  ladder.position.set(0, 2.16, 0);
  ladder.rotation.z = -0.032;

  // extension section
  const ext = new THREE.Group();
  for (const z of [-0.235, 0.235]) {
    const rail = new THREE.Mesh(new RoundedBoxGeometry(3.95, 0.08, 0.06, 2, 0.018), woodDark);
    rail.position.set(-0.9, 0.075, z);
    setup(rail);
    ext.add(rail);
  }
  const extRungs = new THREE.InstancedMesh(new THREE.BoxGeometry(0.06, 0.05, 0.5), woodDark, 12);
  for (let i = 0; i < 12; i++) {
    rd.position.set(-2.65 + i * 0.335, 0.075, 0);
    rd.rotation.set(0, 0, 0);
    rd.updateMatrix();
    extRungs.setMatrixAt(i, rd.matrix);
  }
  extRungs.castShadow = true;
  ext.add(extRungs);
  ladder.add(ext);
  subject.add(ladder);

  // ladder support racks
  for (const [x, y0, y1] of [[0.98, 1.38, 2.13], [-2.6, 1.6, 2.25]]) {
    for (const z of [-0.31, 0.31]) {
      const post = box(0.055, y1 - y0, 0.055, brassDark, 0.012);
      post.position.set(x, (y0 + y1) / 2, z);
      subject.add(post);
      const pad = box(0.16, 0.06, 0.2, leather, 0.02);
      pad.position.set(x, y1, z);
      subject.add(pad);
      const foot = box(0.16, 0.04, 0.16, steel, 0.01);
      foot.position.set(x, y0, z);
      subject.add(foot);
    }
  }
  // diagonal braces to ladder rack
  for (const z of [-0.31, 0.31]) {
    const brace = box(0.045, 0.9, 0.045, steel, 0.01);
    brace.position.set(-2.2, 1.85, z * 1.15);
    brace.rotation.z = 0.55;
    subject.add(brace);
  }
  // ladder ropes
  for (const z of [-0.34, 0.34]) {
    const rope = new THREE.Mesh(new THREE.CylinderGeometry(0.008, 0.008, 4.3, 5), leather);
    rope.rotation.z = Math.PI / 2;
    rope.position.set(-0.55, 2.33, z);
    subject.add(rope);
  }
  // extension rope hanging at rear
  const ropeCurve = new THREE.CatmullRomCurve3([
    new THREE.Vector3(-2.9, 2.4, 0.3), new THREE.Vector3(-3.0, 2.1, 0.34),
    new THREE.Vector3(-3.05, 1.7, 0.4), new THREE.Vector3(-2.95, 1.45, 0.5),
    new THREE.Vector3(-2.85, 1.3, 0.55),
  ]);
  subject.add(setup(new THREE.Mesh(new THREE.TubeGeometry(ropeCurve, 24, 0.012, 6), leather)));

  // ---------------- small details ----------------
  // rear lantern
  for (const z of [-0.7, 0.7]) {
    const lantern = new THREE.Group();
    const bodyL = new THREE.Mesh(new THREE.CylinderGeometry(0.06, 0.075, 0.16, 8), brass);
    lantern.add(bodyL);
    const lens = new THREE.Mesh(new THREE.SphereGeometry(0.05, 10, 8), redLens);
    lens.position.y = -0.08;
    lantern.add(lens);
    const top = new THREE.Mesh(new THREE.ConeGeometry(0.06, 0.07, 8), brass);
    top.position.y = 0.11;
    lantern.add(top);
    lantern.position.set(-2.9, 1.38, z);
    subject.add(lantern);
  }
  // side coiled hose loops
  for (const z of [-0.99, 0.99]) {
    const coil = new THREE.Group();
    for (let i = 0; i < 3; i++) {
      const t = new THREE.Mesh(new THREE.TorusGeometry(0.16 - i * 0.022, 0.026, 6, 20), hoseMat);
      t.rotation.y = Math.PI / 2;
      t.position.x = -1.2 + i * 0.03;
      coil.add(t);
    }
    coil.position.set(0, 1.2, z);
    subject.add(coil);
    add(subject, box(0.1, 0.24, 0.08, brassDark, 0.01), -1.2, 1.2, z);
  }
  // fire axe on side
  {
    const handle = box(0.05, 0.6, 0.05, wood, 0.012);
    handle.position.set(-2.25, 1.0, -0.99);
    subject.add(handle);
    const head = new THREE.Mesh(new THREE.CylinderGeometry(0.02, 0.09, 0.16, 6), steel);
    head.rotation.z = Math.PI / 2;
    head.position.set(-2.25, 1.28, -0.99);
    subject.add(head);
  }
  // running board grab rails (against cab sides)
  for (const z of [-0.94, 0.94]) {
    for (const x of [-0.82, 0.08]) {
      const post = new THREE.Mesh(new THREE.CylinderGeometry(0.011, 0.011, 0.32, 6), brass);
      post.position.set(x, 1.0, z);
      subject.add(post);
    }
    const rail = new THREE.Mesh(new THREE.CylinderGeometry(0.012, 0.012, 0.9, 6), brass);
    rail.rotation.z = Math.PI / 2;
    rail.position.set(-0.37, 1.16, z);
    subject.add(rail);
  }

  // brass pinstripe along body / hood
  for (const z of [-0.945, 0.945]) {
    const stripe = box(3.2, 0.015, 0.008, brassDark, 0.003);
    stripe.position.set(-0.9, 1.47, z);
    subject.add(stripe);
  }

  subject.traverse((o) => {
    if (o.isMesh) {
      o.castShadow = true;
      o.receiveShadow = true;
      if (o.material === glass) o.castShadow = false;
    }
  });

  subject.userData.materials = { red, brass, wood, rubber, steel, chrome, hoseMat };
  return subject;
}

function makeNumberTexture(num) {
  const c = document.createElement('canvas');
  c.width = c.height = 256;
  const ctx = c.getContext('2d');
  ctx.clearRect(0, 0, 256, 256);
  // gold ring
  ctx.strokeStyle = '#e8c878';
  ctx.lineWidth = 10;
  ctx.beginPath();
  ctx.arc(128, 128, 106, 0, Math.PI * 2);
  ctx.stroke();
  ctx.strokeStyle = '#8c6a22';
  ctx.lineWidth = 2;
  ctx.beginPath();
  ctx.arc(128, 128, 98, 0, Math.PI * 2);
  ctx.stroke();
  // number
  ctx.fillStyle = '#f0d18a';
  ctx.font = 'bold 132px Georgia, serif';
  ctx.textAlign = 'center';
  ctx.textBaseline = 'middle';
  ctx.fillText(num, 128, 138);
  ctx.fillStyle = 'rgba(255,240,190,0.25)';
  ctx.font = 'bold 132px Georgia, serif';
  ctx.fillText(num, 126, 134);
  const t = new THREE.CanvasTexture(c);
  t.colorSpace = THREE.SRGBColorSpace;
  t.anisotropy = 8;
  return t;
}