-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
1950 lines (1711 loc) · 80.7 KB
/
main.js
File metadata and controls
1950 lines (1711 loc) · 80.7 KB
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
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
import { EffectComposer } from 'three/addons/postprocessing/EffectComposer.js';
import { RenderPass } from 'three/addons/postprocessing/RenderPass.js';
import { SAOPass } from 'three/addons/postprocessing/SAOPass.js';
import { OutputPass } from 'three/addons/postprocessing/OutputPass.js';
// ─────────────────────────────────────────────────────────────────────────────
// ZF 8HP DATA
// ─────────────────────────────────────────────────────────────────────────────
const GEAR_DATA = {
'R': { engaged: ['A','B','D'], ratio: -3.297, name: 'Rev' },
'1': { engaged: ['A','B','C'], ratio: 4.696, name: '1st' },
'2': { engaged: ['A','B','E'], ratio: 3.130, name: '2nd' },
'3': { engaged: ['B','C','E'], ratio: 2.104, name: '3rd' },
'4': { engaged: ['B','C','D'], ratio: 1.667, name: '4th' },
'5': { engaged: ['C','D','E'], ratio: 1.285, name: '5th' },
'6': { engaged: ['B','D','E'], ratio: 1.000, name: '6th' },
'7': { engaged: ['A','D','E'], ratio: 0.839, name: '7th' },
'8': { engaged: ['A','C','D'], ratio: 0.667, name: '8th' },
};
const GS_SPEC = [
{ sun: 48, ring: 96 }, // GS1
{ sun: 48, ring: 96 }, // GS2
{ sun: 69, ring: 111 }, // GS3
{ sun: 23, ring: 85 }, // GS4
];
// ─────────────────────────────────────────────────────────────────────────────
// PALETTE — warm grays, muted metal, terracotta accent
// ─────────────────────────────────────────────────────────────────────────────
const PAL = {
bg: 0xf0efed,
housing: 0xc8c5be,
sun: 0xb8a870,
ring: 0x8a8a90,
planet: 0xa0a0a8,
carrier: 0x909098,
inputShaft: 0xc4a835,
outputShaft: 0x6aaa45,
connShaft: 0x9898a0,
clutchSteel: 0xb0b0b0,
clutchFric: 0x9a6e40,
engaged: 0xc44b1a,
engagedEmit: 0x441500,
disengaged: 0xa0a0a0,
drumOn: 0xc44b1a,
drumOff: 0x909090,
};
// ─────────────────────────────────────────────────────────────────────────────
// RENDERER
// ─────────────────────────────────────────────────────────────────────────────
const scene = new THREE.Scene();
scene.background = new THREE.Color(PAL.bg);
const camera = new THREE.PerspectiveCamera(38, innerWidth / innerHeight, 0.05, 300);
camera.position.set(3, 5, 12);
const renderer = new THREE.WebGLRenderer({ antialias: true, powerPreference: 'high-performance' });
renderer.setSize(innerWidth, innerHeight);
renderer.setPixelRatio(Math.min(devicePixelRatio, 2));
renderer.shadowMap.enabled = false;
renderer.toneMapping = THREE.ACESFilmicToneMapping;
renderer.toneMappingExposure = 1.3;
renderer.sortObjects = true; // ensure transparent objects are sorted by distance
document.getElementById('canvas-container').appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.06;
controls.target.set(0, 0, 0);
controls.minDistance = 3;
controls.maxDistance = 80;
controls.enableRotate = false; // rotation handled by modelPivot drag
controls.enablePan = true;
controls.zoomToCursor = true;
// Arcball-style drag — rotate modelPivot around world axes, floor stays fixed
{
let dragging = false, prevX = 0, prevY = 0;
const canvas = renderer.domElement;
const _qY = new THREE.Quaternion();
const _qX = new THREE.Quaternion();
const _axisY = new THREE.Vector3(0, 1, 0);
const _axisX = new THREE.Vector3();
canvas.addEventListener('pointerdown', e => {
if (e.button !== 0) return;
if (e.target !== canvas) return;
dragging = true;
prevX = e.clientX;
prevY = e.clientY;
});
window.addEventListener('pointermove', e => {
if (!dragging) return;
const dx = e.clientX - prevX;
const dy = e.clientY - prevY;
prevX = e.clientX;
prevY = e.clientY;
// Horizontal drag → rotate around world Y
_qY.setFromAxisAngle(_axisY, dx * 0.005);
// Vertical drag → rotate around camera's right vector (world space)
_axisX.set(1, 0, 0).applyQuaternion(camera.quaternion);
_qX.setFromAxisAngle(_axisX, dy * 0.005);
modelPivot.quaternion.premultiply(_qY).premultiply(_qX);
});
window.addEventListener('pointerup', () => { dragging = false; });
}
// ─────────────────────────────────────────────────────────────────────────────
// LIGHTING — soft, technical, from all angles
// ─────────────────────────────────────────────────────────────────────────────
scene.add(new THREE.HemisphereLight(0xdddcda, 0x88857e, 0.7));
const key = new THREE.DirectionalLight(0xfffaf0, 1.6);
key.position.set(10, 18, 14);
scene.add(key);
const fill = new THREE.DirectionalLight(0xd0d8e8, 0.5);
fill.position.set(-10, 6, -8);
scene.add(fill);
const back = new THREE.DirectionalLight(0xffe8d0, 0.3);
back.position.set(-4, -8, 12);
scene.add(back);
// ── Floor + shadow ───────────────────────────────────────────────────────────
// Large visible floor plane beneath the gearbox so it looks like it sits on a surface.
const FLOOR_Y = -5;
const floorGeo = new THREE.PlaneGeometry(200, 200);
const floorMat = new THREE.MeshBasicMaterial({ color: 0xf0efed });
const floor = new THREE.Mesh(floorGeo, floorMat);
floor.rotation.x = -Math.PI / 2;
floor.position.y = FLOOR_Y;
floor.renderOrder = -2;
scene.add(floor);
// Dark concentrated shadow on the floor
{
const sz = 512;
const c = document.createElement('canvas');
c.width = sz; c.height = sz;
const ctx = c.getContext('2d');
const g = ctx.createRadialGradient(sz/2, sz/2, 0, sz/2, sz/2, sz * 0.42);
g.addColorStop(0, 'rgba(0,0,0,0.80)');
g.addColorStop(0.2, 'rgba(0,0,0,0.55)');
g.addColorStop(0.45, 'rgba(0,0,0,0.30)');
g.addColorStop(0.7, 'rgba(0,0,0,0.10)');
g.addColorStop(1, 'rgba(0,0,0,0)');
ctx.fillStyle = g;
ctx.fillRect(0, 0, sz, sz);
const tex = new THREE.CanvasTexture(c);
const blobGeo = new THREE.PlaneGeometry(24, 12);
const blobMat = new THREE.MeshBasicMaterial({
map: tex, transparent: true, depthWrite: false,
polygonOffset: true, polygonOffsetFactor: -1, polygonOffsetUnits: -1,
});
const blob = new THREE.Mesh(blobGeo, blobMat);
blob.rotation.x = -Math.PI / 2;
blob.position.y = FLOOR_Y + 0.01;
blob.renderOrder = -1;
scene.add(blob);
}
// ─────────────────────────────────────────────────────────────────────────────
// POST-PROCESSING — SAO for ambient occlusion
// ─────────────────────────────────────────────────────────────────────────────
const composer = new EffectComposer(renderer);
composer.addPass(new RenderPass(scene, camera));
const sao = new SAOPass(scene, camera);
sao.params.saoBias = 0.5;
sao.params.saoIntensity = 0.008;
sao.params.saoScale = 3;
sao.params.saoKernelRadius = 50;
sao.params.saoBlur = true;
sao.params.saoBlurRadius = 6;
sao.params.saoBlurStdDev = 4;
sao.params.saoBlurDepthCutoff = 0.01;
composer.addPass(sao);
composer.addPass(new OutputPass());
// ─────────────────────────────────────────────────────────────────────────────
// INVOLUTE GEAR GEOMETRY
// ─────────────────────────────────────────────────────────────────────────────
/** Generate an involute curve point for angle t on base circle */
function involutePoint(rb, t) {
return [
rb * (Math.cos(t) + t * Math.sin(t)),
rb * (Math.sin(t) - t * Math.cos(t)),
];
}
/**
* Create a detailed external spur gear profile using correct involute math.
* Returns an ExtrudeGeometry along X axis.
*/
function makeExternalGear(module, teeth, faceWidth, boreR) {
const m = module;
const z = teeth;
const rp = (m * z) / 2; // pitch radius
const ra = rp + 0.7 * m; // addendum (tip) — reduced for clearance
const rd = rp - 1.25 * m; // dedendum (root)
const phi = 20 * Math.PI / 180; // pressure angle
const rb = rp * Math.cos(phi); // base circle
// Involute function: inv(α) = tan(α) - α
function inv(alpha) { return Math.tan(alpha) - alpha; }
// Angular half-tooth-thickness at any radius r — reduced by backlash factor
const backlash = 0.25; // fraction of tooth pitch for clearance
const halfThickPitch = Math.PI / (2 * z) * (1 - backlash);
const invPhi = inv(phi);
function halfThickAt(r) {
if (r <= rb) return halfThickPitch + invPhi;
const alpha = Math.acos(rb / r);
return halfThickPitch + invPhi - inv(alpha);
}
const shape = new THREE.Shape();
const ptsPerFlank = 14;
const toothAngle = (2 * Math.PI) / z;
for (let i = 0; i < z; i++) {
const tc = i * toothAngle; // tooth center angle
// Left flank: root → tip
for (let j = 0; j <= ptsPerFlank; j++) {
const frac = j / ptsPerFlank;
const r = rd + (ra - rd) * frac;
const angle = tc - halfThickAt(r);
const y = Math.cos(angle) * r;
const zz = Math.sin(angle) * r;
if (i === 0 && j === 0) shape.moveTo(y, zz);
else shape.lineTo(y, zz);
}
// Tip arc
const htTip = halfThickAt(ra);
const tipSteps = 3;
for (let s = 1; s <= tipSteps; s++) {
const a = tc - htTip + 2 * htTip * (s / tipSteps);
shape.lineTo(Math.cos(a) * ra, Math.sin(a) * ra);
}
// Right flank: tip → root
for (let j = ptsPerFlank; j >= 0; j--) {
const frac = j / ptsPerFlank;
const r = rd + (ra - rd) * frac;
const angle = tc + halfThickAt(r);
shape.lineTo(Math.cos(angle) * r, Math.sin(angle) * r);
}
// Root arc to next tooth
const rootEnd = tc + halfThickAt(rd);
if (i < z - 1) {
const nextRootStart = (i + 1) * toothAngle - halfThickAt(rd);
const steps = 4;
for (let s = 1; s <= steps; s++) {
const a = rootEnd + (nextRootStart - rootEnd) * (s / steps);
shape.lineTo(Math.cos(a) * rd, Math.sin(a) * rd);
}
}
}
shape.closePath();
// Bore hole
if (boreR > 0.01) {
const hole = new THREE.Path();
const seg = 36;
for (let i = 0; i <= seg; i++) {
const a = (i / seg) * Math.PI * 2;
if (i === 0) hole.moveTo(Math.cos(a) * boreR, Math.sin(a) * boreR);
else hole.lineTo(Math.cos(a) * boreR, Math.sin(a) * boreR);
}
shape.holes.push(hole);
}
const geo = new THREE.ExtrudeGeometry(shape, { depth: faceWidth, bevelEnabled: false });
geo.rotateY(Math.PI / 2);
geo.translate(-faceWidth / 2, 0, 0);
geo.computeVertexNormals();
return geo;
}
/**
* Internal (ring) gear: teeth on inside, smooth outside.
* Uses correct involute math (internal gear formula).
*/
function makeInternalGear(module, teeth, faceWidth, outerR) {
const m = module;
const z = teeth;
const rp = (m * z) / 2;
const ra = rp - 0.7 * m; // addendum (tip, inward) — reduced for clearance
const rd = rp + 0.8 * m; // dedendum (root, outward)
const phi = 20 * Math.PI / 180;
const rb = rp * Math.cos(phi);
function inv(alpha) { return Math.tan(alpha) - alpha; }
const invPhi = inv(phi);
// Internal gear tooth half-thickness — reduced by backlash factor
const backlash = 0.25;
const halfThickBase = Math.PI / (2 * z) * (1 - backlash);
function halfThickAt(r) {
if (r <= rb) return halfThickBase - invPhi;
const alpha = Math.acos(rb / r);
return halfThickBase - invPhi + inv(alpha);
}
const shape = new THREE.Shape();
// Outer circle
const seg = 64;
for (let i = 0; i <= seg; i++) {
const a = (i / seg) * Math.PI * 2;
if (i === 0) shape.moveTo(Math.cos(a) * outerR, Math.sin(a) * outerR);
else shape.lineTo(Math.cos(a) * outerR, Math.sin(a) * outerR);
}
shape.closePath();
// Inner hole with gear teeth
const hole = new THREE.Path();
const toothAngle = (2 * Math.PI) / z;
const ptsPerFlank = 10;
for (let i = 0; i < z; i++) {
const tc = i * toothAngle;
// Left flank: root (large r) → tip (small r)
for (let j = 0; j <= ptsPerFlank; j++) {
const frac = j / ptsPerFlank;
const r = rd + (ra - rd) * frac;
const angle = tc - halfThickAt(r);
const y = Math.cos(angle) * r;
const zz = Math.sin(angle) * r;
if (i === 0 && j === 0) hole.moveTo(y, zz);
else hole.lineTo(y, zz);
}
// Tip arc
const htTip = halfThickAt(ra);
const tipSteps = 3;
for (let s = 1; s <= tipSteps; s++) {
const a = tc - htTip + 2 * htTip * (s / tipSteps);
hole.lineTo(Math.cos(a) * ra, Math.sin(a) * ra);
}
// Right flank: tip → root
for (let j = ptsPerFlank; j >= 0; j--) {
const frac = j / ptsPerFlank;
const r = rd + (ra - rd) * frac;
const angle = tc + halfThickAt(r);
hole.lineTo(Math.cos(angle) * r, Math.sin(angle) * r);
}
// Root arc to next tooth
if (i < z - 1) {
const rootEnd = tc + halfThickAt(rd);
const nextRootStart = (i + 1) * toothAngle - halfThickAt(rd);
const steps = 3;
for (let s = 1; s <= steps; s++) {
const a = rootEnd + (nextRootStart - rootEnd) * (s / steps);
hole.lineTo(Math.cos(a) * rd, Math.sin(a) * rd);
}
}
}
shape.holes.push(hole);
const geo = new THREE.ExtrudeGeometry(shape, { depth: faceWidth, bevelEnabled: false });
geo.rotateY(Math.PI / 2);
geo.translate(-faceWidth / 2, 0, 0);
geo.computeVertexNormals();
return geo;
}
/** Hollow cylinder (shaft / drum) along X */
function makeTube(ir, or, length, segs = 32) {
const pts = [
new THREE.Vector2(ir, -length / 2),
new THREE.Vector2(or, -length / 2),
new THREE.Vector2(or, length / 2),
new THREE.Vector2(ir, length / 2),
];
const geo = new THREE.LatheGeometry(pts, segs);
geo.rotateZ(Math.PI / 2);
geo.computeVertexNormals();
return geo;
}
/** Shaft with a canvas texture showing bold stripes — rotation unmissable */
function makeVisibleShaft(radius, length, nStripes = 4, baseColor = 0xbbbbbb, stripeColor = 0x333333) {
const g = new THREE.Group();
// Create a striped texture via canvas
const canvas = document.createElement('canvas');
canvas.width = nStripes * 2 * 4; // small, repeating
canvas.height = 64;
const ctx = canvas.getContext('2d');
const segW = canvas.width / (nStripes * 2);
for (let i = 0; i < nStripes * 2; i++) {
ctx.fillStyle = i % 2 === 0 ? '#' + baseColor.toString(16).padStart(6, '0') : '#' + stripeColor.toString(16).padStart(6, '0');
ctx.fillRect(i * segW, 0, segW, canvas.height);
}
const tex = new THREE.CanvasTexture(canvas);
tex.wrapS = THREE.RepeatWrapping;
tex.wrapT = THREE.RepeatWrapping;
// Repeat once around circumference, stretch along length
tex.repeat.set(1, 1);
const cylGeo = new THREE.CylinderGeometry(radius, radius, length, 32, 1, false);
cylGeo.rotateZ(Math.PI / 2); // align along X
const cylMat = new THREE.MeshStandardMaterial({
map: tex,
metalness: 0.05,
roughness: 0.9,
});
const cyl = new THREE.Mesh(cylGeo, cylMat);
cyl.castShadow = true;
g.add(cyl);
return g;
}
/** Planet carrier with narrow ring plates + pin bosses + arms
* openSide: 'engine' = no outer ring/arms on -X side,
* 'tail' = no outer ring/arms on +X side,
* null = both sides */
function makeCarrier(ir, or, length, nArms, planetOrbitR, planetR, openSide) {
const g = new THREE.Group();
const mat = new THREE.MeshStandardMaterial({ color: PAL.carrier, metalness: 0.05, roughness: 0.9, side: THREE.DoubleSide });
// Narrow hub ring (center, around shaft)
const hubOr = ir + 0.08;
const hubGeo = new THREE.RingGeometry(ir, hubOr, 48);
// Outer ring (around planet orbit, narrow band) — same material as arms
const outerIr = planetOrbitR + planetR * 0.6;
const outerGeo = new THREE.RingGeometry(outerIr, or, 48);
// sides: [+X (tail), -X (engine)]
const sides = [{x: length / 2, side: 'tail'}, {x: -length / 2, side: 'engine'}];
sides.forEach(({x, side}) => {
// Hub disc on both sides
const hub = new THREE.Mesh(hubGeo, mat);
hub.rotation.y = Math.PI / 2;
hub.position.x = x;
g.add(hub);
// Outer ring only on non-open side
if (side !== openSide) {
const outer = new THREE.Mesh(outerGeo, mat);
outer.rotation.y = Math.PI / 2;
outer.position.x = x;
g.add(outer);
}
});
// Radial arms connecting hub to outer ring
const armRadius = 0.022;
const armLen = outerIr - hubOr;
const armGeo = new THREE.CylinderGeometry(armRadius, armRadius, armLen, 8);
const armOffsets = [];
if (openSide !== 'tail') armOffsets.push(length * 0.45);
if (openSide !== 'engine') armOffsets.push(-length * 0.45);
for (let i = 0; i < nArms; i++) {
const a = (i / nArms) * Math.PI * 2;
const midR = (hubOr + outerIr) / 2;
for (const xOff of armOffsets) {
const arm = new THREE.Mesh(armGeo, mat);
arm.position.set(xOff, Math.cos(a) * midR, Math.sin(a) * midR);
arm.rotation.x = a;
arm.castShadow = true;
g.add(arm);
}
}
return g;
}
/** Clutch pack: solid opaque cylinder with bee-stripe axial bands */
function makeClutchPack(ir, or, length, nDiscs) {
const g = new THREE.Group();
const lightMat = new THREE.MeshStandardMaterial({
color: PAL.clutchSteel, metalness: 0.1, roughness: 0.8,
});
const darkMat = new THREE.MeshStandardMaterial({
color: 0x222222, metalness: 0, roughness: 1,
});
// End caps
const capGeo = new THREE.RingGeometry(ir, or, 48);
for (const side of [-1, 1]) {
const cap = new THREE.Mesh(capGeo, lightMat);
cap.rotation.y = Math.PI / 2;
cap.position.x = side * length / 2;
g.add(cap);
}
// Alternating light/dark bands that together form the full cylinder wall
const nBands = nDiscs * 2 + 1;
const bandWidth = length / nBands;
for (let i = 0; i < nBands; i++) {
const isDark = i % 2 === 1;
const bGeo = makeTube(ir, or, bandWidth);
const band = new THREE.Mesh(bGeo, isDark ? darkMat : lightMat);
band.position.x = -length / 2 + bandWidth * (i + 0.5);
g.add(band);
}
return g;
}
function mat(color, opts = {}) {
const trans = opts.transparent ?? false;
return new THREE.MeshStandardMaterial({
color,
metalness: opts.metalness ?? 0.1,
roughness: opts.roughness ?? 0.85,
transparent: trans,
opacity: opts.opacity ?? 1,
side: trans ? THREE.DoubleSide : THREE.FrontSide,
depthWrite: !trans,
flatShading: false,
});
}
// ─────────────────────────────────────────────────────────────────────────────
// BUILD TRANSMISSION — axis along X, engine side = -X, output = +X
// ─────────────────────────────────────────────────────────────────────────────
const modelPivot = new THREE.Group();
scene.add(modelPivot);
const root = new THREE.Group();
modelPivot.add(root);
const housingGrp = new THREE.Group();
housingGrp.visible = false;
const shaftGrp = new THREE.Group();
const gearGrp = new THREE.Group();
const clutchGrp = new THREE.Group();
root.add(housingGrp, shaftGrp, gearGrp, clutchGrp);
// ── Real ZF 8HP dimensions (scaled: 1 unit ≈ 50mm) ──────────────────────────
// Overall transmission: ~710mm long, ~244mm case bore diameter
// Reference: ZF 8HP70 SAE paper + GEARS Magazine 845RE teardown
const SCALE = 1 / 50; // mm to model units
// Real diameters (mm) → model radii
const DIMS = {
caseBore: 244 * SCALE / 2, // 2.44 → r=1.22
tcOD: 268 * SCALE / 2, // torque converter radius
tcDepth: 174 * SCALE / 2, // TC depth (half — we model one side)
s1OD: 73.7 * SCALE / 2, // P1/P2 sun OD (48T, shared sun)
s2OD: 73.7 * SCALE / 2, // same as s1OD — one physical part
s3OD: 89.5 * SCALE / 2, // P3 sun OD (69T, 8HP70)
s4OD: 35 * SCALE / 2, // P4 sun OD (23T, estimated)
p3RingOD: 160.4 * SCALE / 2, // P3 ring gear OD
brakeA_OD: 146 * SCALE / 2, // Brake A friction OD
brakeB_OD: 198 * SCALE / 2, // Brake B friction OD
clutchC_OD: 170 * SCALE / 2, // Clutch C friction OD
clutchD_OD: 172 * SCALE / 2, // Clutch D friction OD (hub 162mm)
clutchE_OD: 170 * SCALE / 2, // Clutch E friction OD
drumC_OD: 117 * SCALE / 2, // Drum C inner OD
drumE_OD: 117 * SCALE / 2, // Drum E inner OD
hubD_OD: 162 * SCALE / 2, // Hub D OD
hubD_H: 30 * SCALE, // Hub D clutch pack thickness (4 plates ~17mm + clearance)
inputShaftR: 16 * SCALE / 2, // input shaft radius (estimated)
outputShaftR: 35 * SCALE / 2, // output shaft spline OD
};
// Gear module derived from P1 sun: module = OD / teeth (OD = 2*radius)
// All meshing gears within a set share the same module
const M = (DIMS.s1OD * 2) / 48; // ≈ 0.0307 per tooth
// Per-gear-set module (different sets can have different modules)
const M_GS = [
(DIMS.s1OD * 2) / 48, // GS1: from P1 sun
(DIMS.s2OD * 2) / 48, // GS2: from P2 sun
(DIMS.s3OD * 2) / 69, // GS3: from P3 sun
(DIMS.s4OD * 2) / 23, // GS4: from P4 sun
];
// Axial layout (mm from front face, engine side = 0)
// Real order: TC | Brake A | P1 + Brake B | P2 | P3 | C | D | E | P4 | Output
// Total gear train length ≈ 520mm
const AX = {
brakeA: 50 * SCALE, // Brake A, front
P1: 100 * SCALE, // first planetary (Brake B wraps around it)
P2: 170 * SCALE, // second planetary
P3: 240 * SCALE, // third planetary
clutchC: 310 * SCALE, // Clutch C
clutchD: 350 * SCALE, // Clutch D
clutchE: 390 * SCALE, // Clutch E
P4: 450 * SCALE, // fourth planetary (output)
};
const axCenter = 260 * SCALE; // center point for model origin
// Face widths (realistic: gear face ~22mm, clutch packs 17-26mm)
const FW = 22 * SCALE; // gear face width
const FW_CLUTCH = 25 * SCALE; // clutch pack width
const GAP = 20 * SCALE; // gap between components
// X positions of each gear set center (shifted so model is centered at origin)
const gsX = [
AX.P1 - axCenter,
AX.P2 - axCenter,
AX.P3 - axCenter,
AX.P4 - axCenter,
];
const totalLen = (AX.P4 - AX.P1) + FW * 2;
const parts = {
suns: [], rings: [], carriers: [], planets: [],
clutches: {},
gsGroups: [], // per-gear-set visibility groups
inputShaft: null, outputShaft: null,
};
let inactiveOpacity = 0.15; // controlled by UI slider
// ── Gear Sets ────────────────────────────────────────────────────────────────
// Real sun ODs per gear set
const SUN_RADII = [DIMS.s1OD, DIMS.s2OD, DIMS.s3OD, DIMS.s4OD];
// Shared GS1+GS2 sun gear — one long gear spanning both planetary sets
const sharedSunM = M_GS[0];
const sharedSunLen = Math.abs(gsX[1] - gsX[0]) + FW; // spans GS1 to GS2
const sharedSunGeo = makeExternalGear(sharedSunM, GS_SPEC[0].sun, sharedSunLen, 0.18);
const sharedSunMesh = new THREE.Mesh(sharedSunGeo, mat(PAL.sun, { metalness: 0.05, roughness: 0.85 }));
sharedSunMesh.position.x = (gsX[0] + gsX[1]) / 2; // centered between GS1 and GS2
sharedSunMesh.castShadow = true;
sharedSunMesh.receiveShadow = true;
gearGrp.add(sharedSunMesh);
// Register shared sun once under GS1 (gs1_sun = gs2_sun always)
parts.suns.push({ mesh: sharedSunMesh, idx: 0, teeth: GS_SPEC[0].sun });
GS_SPEC.forEach((spec, idx) => {
const x = gsX[idx];
const m = M_GS[idx]; // per-set module
const sunPitchR = (m * spec.sun) / 2;
const ringPitchR = (m * spec.ring) / 2;
const planetTeeth = (spec.ring - spec.sun) / 2;
const planetPitchR = (m * planetTeeth) / 2;
const planetOrbitR = sunPitchR + planetPitchR;
// Per-gear-set group for visibility toggling
const gsGroup = new THREE.Group();
gsGroup.name = `GS${idx + 1}`;
gearGrp.add(gsGroup);
parts.gsGroups.push(gsGroup);
// Sun — GS1 and GS2 share the long sun gear above; GS3/GS4 get their own
let sunMesh;
if (idx <= 1) {
sunMesh = sharedSunMesh;
} else {
const sunGeo = makeExternalGear(m, spec.sun, FW * 0.82, 0.18);
sunMesh = new THREE.Mesh(sunGeo, mat(PAL.sun, { metalness: 0.05, roughness: 0.85 }));
sunMesh.position.x = x;
sunMesh.castShadow = true;
sunMesh.receiveShadow = true;
gsGroup.add(sunMesh);
parts.suns.push({ mesh: sunMesh, idx, teeth: spec.sun });
}
// Ring
const ringOuterR = ringPitchR + m * 3.5;
const ringGeo = makeInternalGear(m, spec.ring, FW * 0.88, ringOuterR);
const ringMesh = new THREE.Mesh(ringGeo, mat(PAL.ring, {
metalness: 0.05, roughness: 0.85, transparent: true, opacity: 0.78,
polygonOffset: true, polygonOffsetFactor: 1, polygonOffsetUnits: 1,
}));
ringMesh.position.x = x;
ringMesh.castShadow = true;
ringMesh.receiveShadow = true;
gsGroup.add(ringMesh);
parts.rings.push({ mesh: ringMesh, idx, teeth: spec.ring });
// GS1 open on engine side (input), GS3 open on engine side (tapered drum)
const carrierOpenSide = (idx === 0 || idx === 2) ? 'engine' : null;
const carrier = makeCarrier(0.2, planetOrbitR + planetPitchR * 0.45, FW * 0.9, 4, planetOrbitR, planetPitchR + m, carrierOpenSide);
carrier.position.x = x;
gsGroup.add(carrier);
parts.carriers.push({ mesh: carrier, idx });
// Planets (4 per set)
const nP = 4;
// Rotate sun and ring by half-tooth so gaps face the first planet (at angle 0)
sunMesh.rotation.x = Math.PI / spec.sun;
ringMesh.rotation.x = Math.PI / spec.ring;
for (let p = 0; p < nP; p++) {
const a = (p / nP) * Math.PI * 2;
const pGeo = makeExternalGear(m, planetTeeth, FW * 0.75, 0.06);
const pMesh = new THREE.Mesh(pGeo, mat(PAL.planet, { metalness: 0.05, roughness: 0.85 }));
pMesh.position.set(x, Math.cos(a) * planetOrbitR, Math.sin(a) * planetOrbitR);
// Rolling constraint: planet rotates -a * Zs/Zp as it orbits by a
// Tooth at angle 0 (outward) for first planet; gap at π (inward) faces sun
pMesh.rotation.x = -a * spec.sun / planetTeeth;
pMesh.castShadow = true;
gsGroup.add(pMesh);
parts.planets.push({ mesh: pMesh, idx, angle: a, orbitR: planetOrbitR, baseX: x, pTeeth: planetTeeth });
}
});
// ── Torque Drums — cylindrical shells that carry rotation between gear sets ──
// In a real transmission, these drums wrap around gear elements and are what
// the clutches physically grab onto. They conduct torque between gear sets.
parts.drums = [];
// Drum colors per connection (so user can visually trace which drum goes where)
const DRUM_STYLES = [
{ color: 0xc09940, label: 'Sun shaft drum (GS1↔GS2 sun)' }, // gold
{ color: 0x5588aa, label: 'GS1 carrier ↔ GS4 ring drum' }, // blue
{ color: 0x7a6699, label: 'GS2 ring ↔ GS3 sun drum' }, // purple
{ color: 0x558866, label: 'GS3 ring ↔ GS4 sun drum' }, // teal
{ color: 0xaa6644, label: 'GS4 carrier → output drum' }, // brown
];
function addDrum(innerR, outerR, xStart, xEnd, color, labelText, endInnerR, endOuterR, noFlanges) {
const len = Math.abs(xEnd - xStart);
const xMid = (xStart + xEnd) / 2;
// Support tapered drums: different radii at start vs end
const ir0 = innerR, or0 = outerR;
const ir1 = endInnerR ?? innerR, or1 = endOuterR ?? outerR;
const grp = new THREE.Group();
grp.position.x = xMid;
const drumMat = new THREE.MeshStandardMaterial({
color: color,
metalness: 0.05,
roughness: 0.85,
transparent: true,
opacity: 0.4,
side: THREE.DoubleSide,
depthWrite: false,
});
// Cage bars — straight lines from start radius to end radius
const nBars = 5;
const barThick = Math.max(or0 - ir0, or1 - ir1);
for (let i = 0; i < nBars; i++) {
const a = (i / nBars) * Math.PI * 2;
const r0 = (ir0 + or0) / 2;
const r1 = (ir1 + or1) / 2;
// Build tapered bar from two triangles using BufferGeometry
const hw = 0.01; // half-width of bar
const positions = new Float32Array([
// start face (x = -len/2)
-len/2, Math.cos(a)*r0 - Math.sin(a)*barThick/2, Math.sin(a)*r0 + Math.cos(a)*barThick/2,
-len/2, Math.cos(a)*r0 + Math.sin(a)*barThick/2, Math.sin(a)*r0 - Math.cos(a)*barThick/2,
// end face (x = +len/2)
len/2, Math.cos(a)*r1 + Math.sin(a)*barThick/2, Math.sin(a)*r1 - Math.cos(a)*barThick/2,
len/2, Math.cos(a)*r1 - Math.sin(a)*barThick/2, Math.sin(a)*r1 + Math.cos(a)*barThick/2,
]);
// Extrude into a thin box by duplicating with offset
const cos_a = Math.cos(a), sin_a = Math.sin(a);
const ny = -sin_a * hw, nz = cos_a * hw;
const verts = new Float32Array(24); // 8 vertices
for (let v = 0; v < 4; v++) {
verts[v*3] = positions[v*3];
verts[v*3+1] = positions[v*3+1] + ny;
verts[v*3+2] = positions[v*3+2] + nz;
}
for (let v = 0; v < 4; v++) {
verts[12+v*3] = positions[v*3];
verts[12+v*3+1] = positions[v*3+1] - ny;
verts[12+v*3+2] = positions[v*3+2] - nz;
}
const idx = [0,1,2, 0,2,3, 4,6,5, 4,7,6, 0,4,5, 0,5,1, 2,6,7, 2,7,3, 0,3,7, 0,7,4, 1,5,6, 1,6,2];
const geo = new THREE.BufferGeometry();
geo.setAttribute('position', new THREE.BufferAttribute(verts, 3));
geo.setIndex(idx);
geo.computeVertexNormals();
const bar = new THREE.Mesh(geo, drumMat);
bar.renderOrder = 1;
grp.add(bar);
}
// End flanges at actual radii (skip if noFlanges)
if (!noFlanges) {
const flangeGeo0 = new THREE.RingGeometry(ir0, or0, 48);
const flange0 = new THREE.Mesh(flangeGeo0, drumMat);
flange0.rotation.y = Math.PI / 2;
flange0.position.x = -len / 2;
flange0.renderOrder = 1;
grp.add(flange0);
const flangeGeo1 = new THREE.RingGeometry(ir1, or1, 48);
const flange1 = new THREE.Mesh(flangeGeo1, drumMat);
flange1.rotation.y = Math.PI / 2;
flange1.position.x = len / 2;
flange1.renderOrder = 1;
grp.add(flange1);
}
gearGrp.add(grp);
return { group: grp, drumMat, innerR, outerR, xMid, len };
}
// ── Shaft dimensions (needed by drums AND shafts, defined early) ─────────────
const inpShaftLen = 521 * SCALE;
const outShaftLen = 180 * SCALE;
const sunShaftR = DIMS.s1OD + 0.02;
const c1ir = sunShaftR + 0.03; // GS1 carrier ↔ GS4 ring inner
const c1or = c1ir + 0.04;
const c2ir = c1or + 0.02; // GS2 ring ↔ GS3 sun inner
const c2or = c2ir + 0.04;
const c3ir = c2or + 0.02; // GS3 ring ↔ GS4 sun inner
const c3or = c3ir + 0.04;
// Sun shaft drum removed — the shared long sun gear itself is the visual connection
// GS1 carrier → GS4 ring drum (concentric tube, GS1 output face to GS4 input face)
{
const xStart = gsX[0] + FW * 0.5 + 0.02; // just inside GS1 tail face
const xEnd = gsX[3] - FW * 0.5 - 0.02; // just inside GS4 engine face
const grp = new THREE.Group();
const xMid = (xStart + xEnd) / 2;
const len = xEnd - xStart;
grp.position.x = xMid;
const tubeIr = DIMS.inputShaftR + 0.005;
const tubeOr = tubeIr + 0.01;
const drumMat = new THREE.MeshStandardMaterial({
color: DRUM_STYLES[1].color,
metalness: 0.05, roughness: 0.85,
transparent: true, opacity: 0.4,
side: THREE.DoubleSide, depthWrite: false,
});
const tubeGeo = makeTube(tubeIr, tubeOr, len);
const tube = new THREE.Mesh(tubeGeo, drumMat);
tube.renderOrder = 1;
grp.add(tube);
gearGrp.add(grp);
parts.drums.push({ group: grp, speedKey: 'gs1_carrier', type: 'drum', drumMat });
}
// GS2 ring → GS3 sun drum — tapered, connecting adjacent faces
const gs2ringR = (M_GS[1] * GS_SPEC[1].ring) / 2 + M_GS[1] * 3.5;
const gs3sunR = (M_GS[2] * GS_SPEC[2].sun) / 2;
const drum_gs2r_gs3s = addDrum(gs2ringR - 0.04, gs2ringR,
gsX[1] + FW * 0.5, gsX[2] - FW * 0.5, DRUM_STYLES[2].color, null,
gs3sunR - 0.02, gs3sunR + 0.02, true);
parts.drums.push({ group: drum_gs2r_gs3s.group, speedKey: 'gs2_ring', type: 'drum', drumMat: drum_gs2r_gs3s.drumMat });
// GS3 ring → GS4 sun drum — polyline rods routed above clutches C/D/E
const gs3ringR = (M_GS[2] * GS_SPEC[2].ring) / 2 + M_GS[2] * 3.5;
const gs4sunR = (M_GS[3] * GS_SPEC[3].sun) / 2;
const clutchClearR = Math.max(DIMS.clutchC_OD, DIMS.clutchE_OD + 0.08) + 0.06; // above clutch OD
{
const grp = new THREE.Group();
const drumMat = new THREE.MeshStandardMaterial({
color: DRUM_STYLES[3].color,
metalness: 0.05, roughness: 0.85,
transparent: true, opacity: 0.4,
side: THREE.DoubleSide, depthWrite: false,
});
// Polyline waypoints (in world X, local R):
// 1) GS4 sun surface → short stub toward output (+X)
// 2) radial outward to clearance radius
// 3) axial toward GS3 ring (-X)
// 4) radial inward to GS3 ring surface
const x4 = gsX[3] + FW * 0.5; // GS4 tail face
const x4out = x4 + FW; // ~1 inch past GS4 toward output
const x3 = gsX[2] + FW * 0.5; // GS3 tail face (facing clutches)
const xMid = (x3 + x4out) / 2;
grp.position.x = xMid;
const nBars = 5;
const barR = 0.015; // rod radius
for (let i = 0; i < nBars; i++) {
const a = (i / nBars) * Math.PI * 2;
const cos_a = Math.cos(a), sin_a = Math.sin(a);
// 4 segments per rod, each a thin cylinder
const segments = [
// seg 1: axial stub at GS4 sun radius
{ from: [x4, gs4sunR], to: [x4out, gs4sunR] },
// seg 2: radial outward at x4out
{ from: [x4out, gs4sunR], to: [x4out, clutchClearR] },
// seg 3: axial run above clutches
{ from: [x4out, clutchClearR], to: [x3, clutchClearR] },
// seg 4: radial inward to GS3 ring
{ from: [x3, clutchClearR], to: [x3, gs3ringR] },
];
segments.forEach(({ from, to }) => {
const [fx, fr] = from;
const [tx, tr] = to;
const dx = tx - fx, dr = tr - fr;
const segLen = Math.sqrt(dx * dx + dr * dr);
const mx = (fx + tx) / 2 - xMid; // local X
const mr = (fr + tr) / 2;
const geo = new THREE.CylinderGeometry(barR, barR, segLen, 6);
const rod = new THREE.Mesh(geo, drumMat);
// CylinderGeometry axis is Y; we need to orient it along the segment direction
// Segment direction in local frame: (dx, dr*cos_a, dr*sin_a)
// For axial segments (dr=0): rotate 90° around Z
// For radial segments (dx=0): rotate around X by angle a
if (Math.abs(dr) < 0.001) {
// Axial segment
rod.rotation.z = Math.PI / 2;
rod.position.set(mx, cos_a * mr, sin_a * mr);
} else if (Math.abs(dx) < 0.001) {
// Radial segment
rod.rotation.x = a;
rod.position.set(mx, cos_a * mr, sin_a * mr);
}
rod.renderOrder = 1;
grp.add(rod);
});
}
gearGrp.add(grp);
parts.drums.push({ group: grp, speedKey: 'gs3_ring', type: 'drum', drumMat });
}
// GS4 carrier → output drum
const gs4cR = (DIMS.s4OD + SUN_RADII[3] * GS_SPEC[3].ring / GS_SPEC[3].sun) / 2;
const drum_output = addDrum(gs4cR, gs4cR + 0.04,
gsX[3] - FW, gsX[3] + FW + outShaftLen * 0.3, DRUM_STYLES[4].color);
parts.drums.push({ group: drum_output.group, speedKey: 'gs4_carrier', type: 'drum', drumMat: drum_output.drumMat });
// ── Shafts ───────────────────────────────────────────────────────────────────
// Input shaft — runs from TC through to clutch area (real: ~521mm, 32 splines)
const inpShaft = makeVisibleShaft(DIMS.inputShaftR, inpShaftLen, 4, 0xd4a830, 0x665520);
inpShaft.position.x = (gsX[0] + gsX[1]) / 2 - 1;
shaftGrp.add(inpShaft);
parts.inputShaft = inpShaft;
// Output shaft — from P4 carrier out the back (real: ~180mm)
const outShaft = makeVisibleShaft(DIMS.outputShaftR, outShaftLen, 4, 0x6aaa45, 0x2d5520);
outShaft.position.x = gsX[3] + outShaftLen / 2 + FW;
shaftGrp.add(outShaft);
parts.outputShaft = outShaft;
// ── Clutches & Brakes ────────────────────────────────────────────────────────
// Clutch/brake specs with real dimensions and positions
// Real order: Brake A | P1 + Brake B | P2 | P3 | C | D | E | P4
const brakeAX = AX.brakeA - axCenter;
const brakeBX = AX.P1 - axCenter; // Brake B wraps around P1
const clutchCX = AX.clutchC - axCenter;
const clutchDX = AX.clutchD - axCenter;
const clutchEX = AX.clutchE - axCenter;
const cSpecs = {
A: { ir: DIMS.inputShaftR + 0.02, or: DIMS.brakeA_OD, len: FW_CLUTCH, nDiscs: 5, x: brakeAX },
B: { ir: DIMS.brakeB_OD - 0.15, or: DIMS.brakeB_OD, len: FW_CLUTCH, nDiscs: 5, x: brakeBX },
C: { ir: DIMS.drumC_OD - 0.05, or: DIMS.clutchC_OD, len: FW_CLUTCH, nDiscs: 6, x: clutchCX },
D: { ir: DIMS.drumC_OD - 0.05, or: DIMS.clutchC_OD, len: FW_CLUTCH, nDiscs: 4, x: clutchDX },
E: { ir: DIMS.drumE_OD - 0.05, or: DIMS.clutchE_OD + 0.08, len: FW_CLUTCH, nDiscs: 5, x: clutchEX },
};
// Speed key for each clutch — what member speed drives it
const CLUTCH_SPEED_KEYS = {
A: 'gs1_sun', // Brake A: GS1/GS2 sun → case
B: 'gs1_ring', // Brake B: GS1 ring → case
C: 'input', // Clutch C: input → GS4 sun
D: 'gs3_carrier', // Clutch D: GS3 carrier → output
E: 'gs3_sun', // Clutch E: GS3 sun ↔ GS3 ring
};
Object.entries(cSpecs).forEach(([name, s]) => {
const pack = makeClutchPack(s.ir, s.or, s.len, s.nDiscs);
pack.position.x = s.x;
clutchGrp.add(pack);
parts.clutches[name] = pack;
});
// ── Clutch labels — show what each clutch connects in the 3D view
const CLUTCH_LABELS = {
A: 'A: Sun↔Case',
B: 'B: Ring↔Case',
C: 'C: Input↔GS4',
D: 'D: GS3↔Output',
E: 'E: GS3s↔GS3r',
};
Object.entries(CLUTCH_LABELS).forEach(([name, text]) => {
const s = cSpecs[name];
const y = s.or + 0.35;
clutchGrp.add(makeLabel(text, new THREE.Vector3(s.x, y, 0), { fontSize: 18, color: '#c44b1a' }));
});