-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV3.1
More file actions
1181 lines (1075 loc) · 73 KB
/
V3.1
File metadata and controls
1181 lines (1075 loc) · 73 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Parametric Part Creator</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body { font-family: 'Inter', sans-serif; }
::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-track { background: #1f2937; }
::-webkit-scrollbar-thumb { background: #4b5563; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #6b7280; }
aside { scrollbar-width: thin; scrollbar-color: #4b5563 #1f2937; }
#renderer-container canvas { width: 100% !important; height: 100% !important; outline: none; }
.toggle-checkbox:checked { right: 0; border-color: #06b6d4; }
.toggle-checkbox:checked + .toggle-label { background-color: #06b6d4; }
.unit-toggle-label { cursor: pointer; transition: all 0.2s; }
.unit-toggle-input:checked + .unit-toggle-label { background-color: #06b6d4; color: white; font-weight: 600; }
input[type=range] { accent-color: #06b6d4; cursor: pointer; }
</style>
<link rel="stylesheet" href="https://rsms.me/inter/inter.css">
</head>
<body class="bg-gray-800 text-white overflow-hidden h-screen w-full">
<div class="flex flex-col md:flex-row h-full">
<!-- Sidebar Controls -->
<aside class="w-full md:w-80 lg:w-96 bg-gray-900 border-r border-gray-700 flex flex-col h-full z-10 shadow-xl">
<div class="p-6 overflow-y-auto flex-grow">
<h1 class="text-2xl font-bold text-cyan-400 mb-6 flex items-center gap-2">
<svg class="w-8 h-8" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19.428 15.428a2 2 0 00-1.022-.547l-2.384-.477a6 6 0 00-3.86.517l-.318.158a6 6 0 01-3.86.517L6.05 15.21a2 2 0 00-1.806.547M8 4h8l-1 1v5.172a2 2 0 00.586 1.414l5 5c1.26 1.26.367 3.414-1.415 3.414H4.828c-1.782 0-2.674-2.154-1.414-3.414l5-5A2 2 0 009 10.172V5L8 4z"></path></svg>
Part Creator
</h1>
<!-- Unit System -->
<div class="mb-6 bg-gray-800 p-3 rounded-lg border border-gray-700">
<label class="block text-xs font-semibold text-gray-400 uppercase tracking-wider mb-2">Unit System</label>
<div class="flex bg-gray-700 rounded-lg p-1">
<input type="radio" name="unit" id="unit-mm" value="mm" class="hidden unit-toggle-input" checked>
<label for="unit-mm" class="unit-toggle-label w-1/2 text-center py-1 rounded text-sm">Millimeters</label>
<input type="radio" name="unit" id="unit-in" value="in" class="hidden unit-toggle-input">
<label for="unit-in" class="unit-toggle-label w-1/2 text-center py-1 rounded text-sm">Inches</label>
</div>
</div>
<!-- Part Selector -->
<div class="mb-6">
<label for="part-selector" class="block text-sm font-medium text-gray-300 mb-2">Select Part Type</label>
<div class="relative">
<select id="part-selector" class="w-full bg-gray-800 border border-gray-600 rounded-lg py-2.5 px-3 focus:outline-none focus:ring-2 focus:ring-cyan-500 appearance-none text-white">
<option value="roller" selected>Roller & Pulley</option>
<option value="gears">Gears</option>
<option value="washer">Washer</option>
<option value="box">Box / Enclosure</option>
<option value="knob">Knob</option>
<option value="funnel">Funnel</option>
</select>
<div class="pointer-events-none absolute inset-y-0 right-0 flex items-center px-2 text-gray-400">
<svg class="h-4 w-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 9l-7 7-7-7"></path></svg>
</div>
</div>
</div>
<!-- Parameters Container -->
<div id="params-container">
<!-- Roller Parameters -->
<div id="roller-params" class="space-y-5">
<h2 class="text-sm font-bold text-cyan-400 uppercase tracking-wider border-b border-gray-700 pb-2">Roller Geometry</h2>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Outer Diameter</label>
<span class="text-xs text-gray-500 unit-label">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="roller-outer-diameter-slider" min="5" max="200" value="30" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="roller-outer-diameter-number" value="30" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm focus:ring-1 focus:ring-cyan-500 outline-none">
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Width</label>
<span class="text-xs text-gray-500 unit-label">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="roller-width-slider" min="2" max="100" value="10" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="roller-width-number" value="10" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm focus:ring-1 focus:ring-cyan-500 outline-none">
</div>
</div>
<!-- Shaft Options -->
<div class="bg-gray-800 p-3 rounded border border-gray-700 space-y-3">
<div>
<label class="block text-xs text-gray-400 mb-1">Shaft Shape</label>
<select id="roller-axle-type-selector" class="w-full bg-gray-700 border border-gray-600 rounded px-2 py-1 text-sm">
<option value="round">Round</option>
<option value="d-shaft">D-Shaft</option>
<option value="3">Triangle (3)</option>
<option value="4">Square (4)</option>
<option value="5">Pentagon (5)</option>
<option value="6">Hexagon (6)</option>
<option value="7">Heptagon (7)</option>
<option value="8">Octagon (8)</option>
</select>
</div>
<div id="roller-axle-size-params">
<div class="flex justify-between mb-1">
<label class="text-xs text-gray-300">Bore Size</label>
<span class="text-xs text-gray-500 unit-label">(mm)</span>
</div>
<div class="flex items-center space-x-2">
<input type="range" id="roller-axle-diameter-slider" min="1" max="50" value="5" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="roller-axle-diameter-number" value="5" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-center text-xs">
</div>
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Edge Chamfer</label>
<span class="text-xs text-gray-500 unit-label">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="roller-chamfer-slider" min="0" max="10" value="1" step="0.1" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="roller-chamfer-number" value="1" step="0.1" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm focus:ring-1 focus:ring-cyan-500 outline-none">
</div>
</div>
<div class="bg-gray-800 p-3 rounded border border-gray-700">
<div class="flex items-center justify-between">
<label for="roller-add-groove-toggle" class="text-sm font-medium">Add Groove (Pulley)</label>
<div class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in">
<input type="checkbox" id="roller-add-groove-toggle" class="toggle-checkbox absolute block w-6 h-6 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
<label for="roller-add-groove-toggle" class="toggle-label block overflow-hidden h-6 rounded-full bg-gray-600 cursor-pointer"></label>
</div>
</div>
<div id="roller-groove-params" class="hidden mt-4 space-y-4 border-t border-gray-700 pt-3">
<div>
<label class="text-xs text-gray-400">Depth <span class="unit-label">(mm)</span></label>
<div class="flex items-center space-x-2 mt-1">
<input type="range" id="roller-groove-depth-slider" min="0.5" max="20" value="3" step="0.1" class="w-full h-1 bg-gray-600 rounded-lg appearance-none">
<input type="number" id="roller-groove-depth-number" value="3" step="0.1" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-center text-xs">
</div>
</div>
<div>
<label class="text-xs text-gray-400">Angle (deg)</label>
<div class="flex items-center space-x-2 mt-1">
<input type="range" id="roller-groove-angle-slider" min="30" max="160" value="90" step="1" class="w-full h-1 bg-gray-600 rounded-lg appearance-none">
<input type="number" id="roller-groove-angle-number" value="90" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-center text-xs">
</div>
</div>
</div>
</div>
</div>
<!-- Gears Parameters -->
<div id="gears-params" class="hidden space-y-6">
<div class="bg-gray-800 p-3 rounded-lg border border-gray-700 space-y-3">
<div class="flex items-center justify-between">
<label for="gear-simulate-toggle" class="text-sm font-bold text-cyan-400">Simulate Rotation</label>
<div class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in">
<input type="checkbox" id="gear-simulate-toggle" class="toggle-checkbox absolute block w-6 h-6 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
<label for="gear-simulate-toggle" class="toggle-label block overflow-hidden h-6 rounded-full bg-gray-600 cursor-pointer"></label>
</div>
</div>
<div class="flex items-center justify-between border-t border-gray-700 pt-2">
<label class="text-xs text-gray-400">Active Gears</label>
<select id="num-gears-selector" class="bg-gray-700 border border-gray-600 rounded py-1 px-2 text-xs">
<option value="1">1</option><option value="2">2</option><option value="3">3</option>
<option value="4">4</option><option value="5">5</option><option value="6">6</option>
</select>
</div>
<div>
<label class="text-xs text-gray-400">Module <span class="unit-label">(mm)</span></label>
<div class="flex items-center space-x-2 mt-1">
<input type="range" id="gear-module-slider" min="0.5" max="10" value="2" step="0.1" class="w-full h-2 bg-gray-700 rounded-lg">
<input type="number" id="gear-module-number" value="2" step="0.1" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-xs text-center">
</div>
</div>
<div>
<label class="text-xs text-gray-400">Face Width <span class="unit-label">(mm)</span></label>
<div class="flex items-center space-x-2 mt-1">
<input type="range" id="gear-face-width-slider" min="2" max="100" value="10" class="w-full h-2 bg-gray-700 rounded-lg">
<input type="number" id="gear-face-width-number" value="10" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-xs text-center">
</div>
</div>
<div>
<label class="text-xs text-gray-400">Type</label>
<select id="gear-type-selector" class="w-full bg-gray-700 border border-gray-600 rounded px-2 py-1 text-xs mt-1">
<option value="spur">Spur</option>
<option value="helical">Helical</option>
<option value="herringbone">Herringbone</option>
</select>
</div>
<div id="gear-helix-params" class="hidden">
<label class="text-xs text-gray-400">Helix Angle</label>
<div class="flex items-center space-x-2 mt-1">
<input type="range" id="gear-helix-angle-slider" min="10" max="60" value="30" class="w-full h-2 bg-gray-700 rounded-lg">
<input type="number" id="gear-helix-angle-number" value="30" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-xs text-center">
</div>
</div>
</div>
<!-- Configs for Gears 1-6 -->
<div id="individual-gears-container">
<!-- Populated by JS -->
</div>
</div>
<!-- Washer Parameters -->
<div id="washer-params" class="space-y-5 hidden">
<h2 class="text-sm font-bold text-cyan-400 uppercase tracking-wider border-b border-gray-700 pb-2">Washer Geometry</h2>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Outer Diameter</label>
<span class="text-xs text-gray-500 unit-label">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="washer-outer-diameter-slider" min="5" max="100" value="30" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="washer-outer-diameter-number" value="30" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm focus:ring-1 focus:ring-cyan-500 outline-none">
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Inner Diameter</label>
<span class="text-xs text-gray-500 unit-label">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="washer-inner-diameter-slider" min="1" max="95" value="15" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="washer-inner-diameter-number" value="15" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm focus:ring-1 focus:ring-cyan-500 outline-none">
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Thickness</label>
<span class="text-xs text-gray-500 unit-label">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="washer-thickness-slider" min="0.5" max="20" value="2" step="0.1" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="washer-thickness-number" value="2" step="0.1" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm focus:ring-1 focus:ring-cyan-500 outline-none">
</div>
</div>
</div>
<!-- Box / Enclosure Parameters -->
<div id="box-params" class="space-y-5 hidden">
<h2 class="text-sm font-bold text-cyan-400 uppercase tracking-wider border-b border-gray-700 pb-2">Box Geometry</h2>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="text-xs text-gray-400">Length <span class="unit-label">(mm)</span></label>
<input type="number" id="box-length-number" value="50" class="w-full bg-gray-700 border border-gray-600 rounded px-2 py-1 text-sm mt-1">
<input type="range" id="box-length-slider" min="10" max="200" value="50" class="w-full h-1 bg-gray-600 rounded-lg mt-2">
</div>
<div>
<label class="text-xs text-gray-400">Width <span class="unit-label">(mm)</span></label>
<input type="number" id="box-width-number" value="50" class="w-full bg-gray-700 border border-gray-600 rounded px-2 py-1 text-sm mt-1">
<input type="range" id="box-width-slider" min="10" max="200" value="50" class="w-full h-1 bg-gray-600 rounded-lg mt-2">
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Height</label>
<span class="text-xs text-gray-500 unit-label">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="box-height-slider" min="10" max="200" value="30" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="box-height-number" value="30" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm">
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Wall Thickness</label>
<span class="text-xs text-gray-500 unit-label">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="box-wall-thickness-slider" min="1" max="10" value="2" step="0.5" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="box-wall-thickness-number" value="2" step="0.5" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm">
</div>
</div>
<div class="bg-gray-800 p-3 rounded border border-gray-700">
<div class="flex items-center justify-between">
<label for="box-generate-lid-toggle" class="text-sm font-medium">Generate Lid</label>
<div class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in">
<input type="checkbox" id="box-generate-lid-toggle" class="toggle-checkbox absolute block w-6 h-6 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
<label for="box-generate-lid-toggle" class="toggle-label block overflow-hidden h-6 rounded-full bg-gray-600 cursor-pointer"></label>
</div>
</div>
<div id="box-lid-params" class="hidden mt-4 space-y-4 border-t border-gray-700 pt-3">
<div>
<label class="text-xs text-gray-400">Lid Height <span class="unit-label">(mm)</span></label>
<div class="flex items-center space-x-2 mt-1">
<input type="range" id="box-lid-height-slider" min="2" max="20" value="4" step="0.5" class="w-full h-1 bg-gray-600 rounded-lg">
<input type="number" id="box-lid-height-number" value="4" step="0.5" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-center text-xs">
</div>
</div>
<div>
<label class="text-xs text-gray-400">Tolerance <span class="unit-label">(mm)</span></label>
<div class="flex items-center space-x-2 mt-1">
<input type="range" id="box-lid-tolerance-slider" min="0" max="1" value="0.2" step="0.05" class="w-full h-1 bg-gray-600 rounded-lg">
<input type="number" id="box-lid-tolerance-number" value="0.2" step="0.05" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-center text-xs">
</div>
</div>
<!-- Handle Toggle -->
<div class="flex items-center justify-between pt-2 border-t border-gray-700 mt-2">
<label for="box-generate-handle-toggle" class="text-sm font-medium">Add Handle</label>
<div class="relative inline-block w-10 mr-2 align-middle select-none transition duration-200 ease-in">
<input type="checkbox" id="box-generate-handle-toggle" class="toggle-checkbox absolute block w-6 h-6 rounded-full bg-white border-4 appearance-none cursor-pointer"/>
<label for="box-generate-handle-toggle" class="toggle-label block overflow-hidden h-6 rounded-full bg-gray-600 cursor-pointer"></label>
</div>
</div>
<div id="box-handle-params" class="hidden mt-2 space-y-3">
<div>
<label class="block text-xs text-gray-400 mb-1">Shape</label>
<select id="box-handle-shape-selector" class="w-full bg-gray-700 border border-gray-600 rounded px-2 py-1 text-sm">
<option value="rectangle">Rectangle</option>
<option value="cylinder">Cylinder</option>
</select>
</div>
<div id="box-handle-rect-params"><div class="grid grid-cols-2 gap-2"><div><label class="text-xs text-gray-400">Length <span class="unit-label">(mm)</span></label><input type="number" id="box-handle-length-number" value="20" class="w-full bg-gray-700 border border-gray-600 rounded px-1 py-1 text-sm mt-1"></div><div><label class="text-xs text-gray-400">Width <span class="unit-label">(mm)</span></label><input type="number" id="box-handle-width-number" value="10" class="w-full bg-gray-700 border border-gray-600 rounded px-1 py-1 text-sm mt-1"></div></div></div>
<div id="box-handle-cyl-params" class="hidden"><div><label class="text-xs text-gray-400">Diameter <span class="unit-label">(mm)</span></label><div class="flex items-center space-x-2 mt-1"><input type="range" id="box-handle-diameter-slider" min="5" max="50" value="15" class="w-full h-1 bg-gray-600 rounded-lg"><input type="number" id="box-handle-diameter-number" value="15" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-center text-xs"></div></div></div>
<div><label class="text-xs text-gray-400">Height <span class="unit-label">(mm)</span></label><div class="flex items-center space-x-2 mt-1"><input type="range" id="box-handle-height-slider" min="5" max="50" value="10" class="w-full h-1 bg-gray-600 rounded-lg"><input type="number" id="box-handle-height-number" value="10" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-center text-xs"></div></div>
</div>
</div>
</div>
</div>
<!-- Knob Parameters -->
<div id="knob-params" class="space-y-5 hidden">
<h2 class="text-sm font-bold text-cyan-400 uppercase tracking-wider border-b border-gray-700 pb-2">Knob Geometry</h2>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Outer Diameter</label>
<span class="unit-label text-xs text-gray-500">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="knob-outer-diameter-slider" min="10" max="100" value="30" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="knob-outer-diameter-number" value="30" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm">
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Height</label>
<span class="unit-label text-xs text-gray-500">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="knob-height-slider" min="5" max="50" value="20" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="knob-height-number" value="20" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm">
</div>
</div>
<div class="grid grid-cols-2 gap-4">
<div>
<label class="block text-xs text-gray-400 mb-1">Grip Style</label>
<select id="knob-grip-style-selector" class="w-full bg-gray-700 border border-gray-600 rounded px-2 py-1 text-sm">
<option value="smooth">Smooth</option>
<option value="knurled">Knurled</option>
</select>
</div>
<div>
<label class="block text-xs text-gray-400 mb-1">Shaft Shape</label>
<select id="knob-shaft-type-selector" class="w-full bg-gray-700 border border-gray-600 rounded px-2 py-1 text-sm">
<option value="round">Round</option>
<option value="d-shaft">D-Shaft</option>
<option value="3">Triangle (3)</option>
<option value="4">Square (4)</option>
<option value="5">Pentagon (5)</option>
<option value="6">Hexagon (6)</option>
<option value="7">Heptagon (7)</option>
<option value="8">Octagon (8)</option>
</select>
</div>
</div>
<div class="bg-gray-800 p-3 rounded border border-gray-700">
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Bore Size</label>
<span class="unit-label text-xs text-gray-500">(mm)</span>
</div>
<div class="flex items-center space-x-2">
<input type="range" id="knob-shaft-diameter-slider" min="2" max="20" value="6" step="0.1" class="w-full h-1 bg-gray-600 rounded-lg">
<input type="number" id="knob-shaft-diameter-number" min="1" max="20" value="6" step="0.1" class="w-16 bg-gray-700 border border-gray-600 rounded px-1 py-0.5 text-center text-xs">
</div>
</div>
</div>
<!-- Funnel Parameters -->
<div id="funnel-params" class="space-y-5 hidden">
<h2 class="text-sm font-bold text-cyan-400 uppercase tracking-wider border-b border-gray-700 pb-2">Funnel Geometry</h2>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Top Diameter</label>
<span class="unit-label text-xs text-gray-500">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="funnel-top-diameter-slider" min="20" max="200" value="80" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="funnel-top-diameter-number" value="80" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm">
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Bottom Diameter</label>
<span class="unit-label text-xs text-gray-500">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="funnel-bottom-diameter-slider" min="5" max="50" value="10" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="funnel-bottom-diameter-number" value="10" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm">
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Height</label>
<span class="unit-label text-xs text-gray-500">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="funnel-height-slider" min="10" max="200" value="50" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="funnel-height-number" value="50" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm">
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Spout Length</label>
<span class="unit-label text-xs text-gray-500">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="funnel-spout-length-slider" min="5" max="100" value="30" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="funnel-spout-length-number" value="30" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm">
</div>
</div>
<div>
<div class="flex justify-between mb-1">
<label class="text-sm text-gray-300">Wall Thickness</label>
<span class="unit-label text-xs text-gray-500">(mm)</span>
</div>
<div class="flex items-center space-x-3">
<input type="range" id="funnel-wall-thickness-slider" min="0.5" max="5" value="2" step="0.5" class="w-full h-2 bg-gray-700 rounded-lg appearance-none">
<input type="number" id="funnel-wall-thickness-number" value="2" class="w-20 bg-gray-700 border border-gray-600 rounded px-2 py-1 text-right text-sm">
</div>
</div>
</div>
</div>
</div>
<!-- Action Buttons -->
<div class="p-6 border-t border-gray-700 bg-gray-800">
<div class="mb-4 flex items-center justify-between">
<label for="part-color" class="text-sm text-gray-400">Appearance</label>
<div class="flex items-center gap-2">
<input type="color" id="part-color" value="#06b6d4" class="w-8 h-8 rounded border border-gray-600 cursor-pointer bg-transparent p-0">
<button id="toggle-grid" class="text-xs text-gray-400 hover:text-white border border-gray-600 rounded px-2 py-1">Toggle Grid</button>
</div>
</div>
<div class="grid grid-cols-2 gap-3">
<button id="reset-params" class="bg-gray-700 hover:bg-gray-600 text-gray-200 py-2.5 px-4 rounded-lg text-sm font-semibold transition duration-200 border border-gray-600">
Reset
</button>
<button id="export-stl" class="bg-cyan-600 hover:bg-cyan-500 text-white py-2.5 px-4 rounded-lg text-sm font-semibold transition duration-200 shadow-lg flex items-center justify-center gap-2">
<span>Export STL</span>
<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 16v1a3 3 0 003 3h10a3 3 0 003-3v-1m-4-4l-4 4m0 0l-4-4m4 4V4"></path></svg>
</button>
</div>
</div>
</aside>
<!-- 3D Viewer -->
<main class="flex-grow bg-gray-900 relative h-full overflow-hidden">
<div id="renderer-container" class="w-full h-full"></div>
<div id="loading-indicator" class="absolute inset-0 bg-gray-900 flex flex-col items-center justify-center z-50">
<div class="w-12 h-12 border-4 border-cyan-600 border-t-transparent rounded-full animate-spin"></div>
<p class="mt-4 text-cyan-500 font-medium">Initializing Engine...</p>
</div>
</main>
</div>
<script type="module">
import * as THREE from 'https://esm.sh/three@0.160.0';
import { OrbitControls } from 'https://esm.sh/three@0.160.0/examples/jsm/controls/OrbitControls.js';
import { STLExporter } from 'https://esm.sh/three@0.160.0/examples/jsm/exporters/STLExporter.js';
import * as BufferGeometryUtils from 'https://esm.sh/three@0.160.0/examples/jsm/utils/BufferGeometryUtils.js';
let scene, camera, renderer, controls, currentMesh, gridHelper, mainMaterial;
let currentUnit = 'mm';
let isAnimatingGears = false;
const MM_TO_IN = 1 / 25.4;
// Default Parameters
const defaultParamsMM = {
roller: {
'roller-outer-diameter': 30, 'roller-width': 10, 'roller-axle-diameter': 5, 'roller-chamfer': 1,
'roller-add-groove-toggle': false, 'roller-groove-depth': 3, 'roller-groove-angle': 90,
'roller-axle-type-selector': 'round'
},
washer: {
'washer-outer-diameter': 30, 'washer-inner-diameter': 15, 'washer-thickness': 2,
},
box: {
'box-length': 50, 'box-width': 50, 'box-height': 30, 'box-wall-thickness': 2,
'box-generate-lid-toggle': false, 'box-lid-height': 4, 'box-lid-tolerance': 0.2,
'box-generate-handle-toggle': false, 'box-handle-shape-selector': 'rectangle',
'box-handle-length': 20, 'box-handle-width': 10, 'box-handle-diameter': 15, 'box-handle-height': 10
},
knob: {
'knob-outer-diameter': 30, 'knob-height': 20, 'knob-grip-style-selector': 'smooth',
'knob-shaft-type-selector': 'round', 'knob-shaft-diameter': 6
},
funnel: {
'funnel-top-diameter': 80, 'funnel-bottom-diameter': 10, 'funnel-height': 50,
'funnel-spout-length': 30, 'funnel-wall-thickness': 2
},
gears: {
'num-gears-selector': 1, 'gear-module': 2, 'gear-pressure-angle': 20, 'gear-face-width': 10, 'gear-type': 'spur', 'gear-helix-angle': 30,
'gear1-teeth': 40, 'gear1-bore-diameter': 10,
'gear2-teeth': 20, 'gear2-bore-diameter': 8,
'gear3-teeth': 30, 'gear3-bore-diameter': 8,
'gear4-teeth': 25, 'gear4-bore-diameter': 8,
'gear5-teeth': 15, 'gear5-bore-diameter': 6,
'gear6-teeth': 35, 'gear6-bore-diameter': 8,
}
};
// --- Core 3D Scene Setup ---
function init3D() {
const container = document.getElementById('renderer-container');
scene = new THREE.Scene();
scene.background = new THREE.Color(0x111827);
scene.fog = new THREE.Fog(0x111827, 50, 300);
camera = new THREE.PerspectiveCamera(45, container.clientWidth / container.clientHeight, 0.1, 1000);
camera.position.set(50, 40, 60);
renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });
renderer.setSize(container.clientWidth, container.clientHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
container.appendChild(renderer.domElement);
// Lighting
const hemiLight = new THREE.HemisphereLight(0xffffff, 0x444444, 1.2);
hemiLight.position.set(0, 200, 0);
scene.add(hemiLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 2.0);
dirLight.position.set(50, 100, 50);
dirLight.castShadow = true;
dirLight.shadow.mapSize.width = 2048;
dirLight.shadow.mapSize.height = 2048;
scene.add(dirLight);
controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
gridHelper = new THREE.GridHelper(200, 20, 0x4b5563, 0x374151);
scene.add(gridHelper);
mainMaterial = new THREE.MeshStandardMaterial({
color: document.getElementById('part-color').value,
roughness: 0.4,
metalness: 0.2
});
onWindowResize();
window.addEventListener('resize', onWindowResize, false);
document.getElementById('loading-indicator').style.display = 'none';
animate();
}
function animate() {
requestAnimationFrame(animate);
controls.update();
// Gear Animation Logic
if (isAnimatingGears && currentMesh && currentMesh.isGroup) {
const speed = 0.02;
const gear1 = currentMesh.getObjectByName('gear1');
if(gear1) {
const teeth1 = gear1.userData.teeth;
gear1.rotation.y += speed;
currentMesh.children.forEach(child => {
if (child.name !== 'gear1' && child.name.startsWith('gear')) {
const teethN = child.userData.teeth;
child.rotation.y -= speed * (teeth1 / teethN);
}
});
}
}
renderer.render(scene, camera);
}
function onWindowResize() {
const container = document.getElementById('renderer-container');
if (container && camera && renderer) {
camera.aspect = container.clientWidth / container.clientHeight;
camera.updateProjectionMatrix();
renderer.setSize(container.clientWidth, container.clientHeight);
}
}
function getValue(id) {
const input = document.getElementById(`${id}-number`) || document.getElementById(id);
if (!input) return 0;
const stored = input.dataset.valueMm ? parseFloat(input.dataset.valueMm) : parseFloat(input.value);
return isNaN(stored) ? 0 : stored;
}
// --- Shared Generator: Bore Path ---
function createBorePath(type, size) {
const path = new THREE.Path();
// size behaves as diameter or width across flats
// For polygons, "size" = flat-to-flat distance (inscribed diameter)
// For round, "size" = diameter
const radius = size / 2;
if (type === 'round') {
path.absarc(0, 0, Math.max(0.1, radius), 0, Math.PI * 2, true);
} else if (type === 'd-shaft') {
const r = Math.max(0.1, radius);
const flatDepth = r * 0.8;
const angle = Math.acos(Math.min(Math.max(flatDepth/r, -1), 1));
path.absarc(0, 0, r, angle, -angle, false);
path.lineTo(flatDepth, 0);
} else {
// Polygon (3-8 sides)
const sides = parseInt(type);
if (!isNaN(sides) && sides >= 3) {
// Calculate circumradius from inscribed radius (size/2)
// r_in = r_circum * cos(PI/n)
// r_circum = r_in / cos(PI/n)
const r_circum = radius / Math.cos(Math.PI / sides);
// Rotate to align flats if even, or points if odd?
// Standard: Hex (6) has flats top/bottom if offset 90deg?
// Let's create it such that a flat is at the bottom (y = -radius)
// Vertex angles: offset + i * 2PI/n
// For Square (4): vertices at 45, 135... gives flats at 0, 90.
let offset = -Math.PI / 2; // Point down
if (sides % 2 === 0) offset = -Math.PI/sides - Math.PI/2; // Rotate half segment?
// Simple logic:
// Just generate vertices.
const angleStep = (Math.PI * 2) / sides;
// Align flat to bottom for even polygons
const startAngle = (sides % 2 === 0) ? angleStep / 2 : 0;
path.moveTo(r_circum * Math.cos(startAngle), r_circum * Math.sin(startAngle));
for (let i = 1; i <= sides; i++) {
const theta = startAngle + i * angleStep;
path.lineTo(r_circum * Math.cos(theta), r_circum * Math.sin(theta));
}
path.closePath();
} else {
// Fallback to round
path.absarc(0, 0, Math.max(0.1, radius), 0, Math.PI * 2, true);
}
}
return path;
}
// --- Standard Geometries ---
function createRoller() {
const outerD = getValue('roller-outer-diameter');
const width = getValue('roller-width');
const axleType = document.getElementById('roller-axle-type-selector').value;
const boreSize = getValue('roller-axle-diameter'); // Universal size input
let chamfer = getValue('roller-chamfer');
const addGroove = document.getElementById('roller-add-groove-toggle').checked;
const R = outerD / 2;
const halfW = width / 2;
// Determine Hub needs
let hubGeometry;
let startRadiusForLathe;
if (axleType === 'round') {
startRadiusForLathe = boreSize / 2;
} else {
// For non-round shafts, create Hub via Extrude
const shape = new THREE.Shape();
// Calculate safe hub outer radius
// Needs to be larger than the bore shape
let boreMaxRadius = boreSize / 2;
if (!isNaN(parseInt(axleType))) {
// For polygons, circumradius is larger than size/2
boreMaxRadius = (boreSize / 2) / Math.cos(Math.PI / parseInt(axleType));
}
const hubRadius = Math.min(R - 1, boreMaxRadius + 3);
shape.absarc(0, 0, hubRadius, 0, Math.PI * 2, false);
shape.holes.push(createBorePath(axleType, boreSize));
hubGeometry = new THREE.ExtrudeGeometry(shape, { depth: width, bevelEnabled: false, curveSegments: 32 });
hubGeometry.center();
startRadiusForLathe = hubRadius - 0.05; // Overlap
}
// Create Rim (Lathe)
chamfer = Math.min(chamfer, (R - startRadiusForLathe) / 2 - 0.1, halfW - 0.1);
if (chamfer < 0) chamfer = 0;
const shape = new THREE.Shape();
shape.moveTo(startRadiusForLathe, -halfW);
shape.lineTo(R - chamfer, -halfW);
if (chamfer > 0) shape.lineTo(R, -halfW + chamfer);
if (addGroove) {
const depth = Math.min(getValue('roller-groove-depth'), (R - startRadiusForLathe) - 1);
const angleDeg = parseFloat(document.getElementById('roller-groove-angle-number').value);
const grooveTopY = depth / Math.tan((angleDeg * Math.PI) / 360);
if (grooveTopY < halfW - chamfer) {
shape.lineTo(R, -grooveTopY); shape.lineTo(R - depth, 0); shape.lineTo(R, grooveTopY);
} else shape.lineTo(R, 0);
}
if (chamfer > 0) shape.lineTo(R, halfW - chamfer);
shape.lineTo(R - chamfer, halfW);
shape.lineTo(startRadiusForLathe, halfW);
shape.closePath();
const latheGeometry = new THREE.LatheGeometry(shape.getPoints(50), 64);
latheGeometry.rotateX(Math.PI / 2);
if (hubGeometry) {
const geo1 = latheGeometry.toNonIndexed();
const geo2 = hubGeometry.toNonIndexed();
return BufferGeometryUtils.mergeGeometries([geo1, geo2]);
}
return latheGeometry;
}
function createBox() {
const L = getValue('box-length'), W = getValue('box-width'), H = getValue('box-height');
let T = getValue('box-wall-thickness');
const generateLid = document.getElementById('box-generate-lid-toggle').checked;
T = Math.min(T, L/2 - 0.1, W/2 - 0.1, H - 0.1);
const parts = [];
const bottom = new THREE.BoxGeometry(L, T, W); bottom.translate(0, T/2, 0); parts.push(bottom);
const front = new THREE.BoxGeometry(L, H-T, T); front.translate(0, T + (H-T)/2, W/2 - T/2); parts.push(front);
const back = new THREE.BoxGeometry(L, H-T, T); back.translate(0, T + (H-T)/2, -W/2 + T/2); parts.push(back);
const left = new THREE.BoxGeometry(T, H-T, W - 2*T); left.translate(-L/2 + T/2, T + (H-T)/2, 0); parts.push(left);
const right = new THREE.BoxGeometry(T, H-T, W - 2*T); right.translate(L/2 - T/2, T + (H-T)/2, 0); parts.push(right);
if (generateLid) {
const lidH = getValue('box-lid-height'), tol = getValue('box-lid-tolerance');
const lidY = H + 10;
const lidTop = new THREE.BoxGeometry(L, T, W); lidTop.translate(0, lidY + lidH - T/2, 0); parts.push(lidTop);
const lipH = Math.max(0.1, lidH - T);
const lip = new THREE.BoxGeometry(L - 2*T - tol, lipH, W - 2*T - tol); lip.translate(0, lidY + lipH/2, 0); parts.push(lip);
if (document.getElementById('box-generate-handle-toggle').checked) {
const hShape = document.getElementById('box-handle-shape-selector').value;
const hH = getValue('box-handle-height');
let hGeo;
if (hShape === 'rectangle') hGeo = new THREE.BoxGeometry(getValue('box-handle-length'), hH, getValue('box-handle-width'));
else hGeo = new THREE.CylinderGeometry(getValue('box-handle-diameter')/2, getValue('box-handle-diameter')/2, hH, 32);
hGeo.translate(0, lidY + lidH + hH/2, 0); parts.push(hGeo);
}
}
return BufferGeometryUtils.mergeGeometries(parts);
}
function createWasher() {
const outerD = getValue('washer-outer-diameter');
let innerD = getValue('washer-inner-diameter');
const thickness = getValue('washer-thickness');
if (innerD >= outerD) innerD = outerD - 1;
const shape = new THREE.Shape();
shape.absarc(0, 0, outerD / 2, 0, Math.PI * 2, false);
shape.holes.push(createBorePath('round', innerD)); // Washer is always round for now? Or should it support hex?
// User requested "all parts with a bore". Washer technically has a bore.
// But washer UI doesn't have a selector. Let's keep it round for standard washers.
const geometry = new THREE.ExtrudeGeometry(shape, { depth: thickness, bevelEnabled: false, curveSegments: 64 });
geometry.rotateX(Math.PI / 2);
geometry.translate(0, thickness / 2, 0);
return geometry;
}
// --- Gear Geometry ---
function createSingleSpurGear(params) {
const { numTeeth, module, faceWidth, boreType, boreDiameter, pressureAngle, gearType, helixAngle } = params;
const pitchRadius = (numTeeth * module) / 2;
const baseRadius = pitchRadius * Math.cos(pressureAngle * Math.PI / 180);
const outerRadius = pitchRadius + module;
const rootRadius = pitchRadius - 1.25 * module;
const numPoints = 10;
const toothAngle = 2 * Math.PI / numTeeth;
const invAlpha = Math.tan(pressureAngle * Math.PI/180) - (pressureAngle * Math.PI/180);
const halfToothAngleBase = (Math.PI / (2 * numTeeth)) + invAlpha;
const finalPoints = [];
for (let i = 0; i < numTeeth; i++) {
const angle = i * toothAngle;
if (rootRadius < baseRadius) {
const theta = angle - halfToothAngleBase;
finalPoints.push(new THREE.Vector2(rootRadius * Math.cos(theta), rootRadius * Math.sin(theta)));
finalPoints.push(new THREE.Vector2(baseRadius * Math.cos(theta), baseRadius * Math.sin(theta)));
}
for(let j=0; j<=numPoints; j++) {
const r = baseRadius + (outerRadius - baseRadius) * (j/numPoints);
const alpha = Math.acos(baseRadius/r);
const inv = Math.tan(alpha) - alpha;
const theta = angle - (halfToothAngleBase - inv);
finalPoints.push(new THREE.Vector2(r * Math.cos(theta), r * Math.sin(theta)));
}
for(let j=numPoints; j>=0; j--) {
const r = baseRadius + (outerRadius - baseRadius) * (j/numPoints);
const alpha = Math.acos(baseRadius/r);
const inv = Math.tan(alpha) - alpha;
const theta = angle + (halfToothAngleBase - inv);
finalPoints.push(new THREE.Vector2(r * Math.cos(theta), r * Math.sin(theta)));
}
if (rootRadius < baseRadius) {
const theta = angle + halfToothAngleBase;
finalPoints.push(new THREE.Vector2(baseRadius * Math.cos(theta), baseRadius * Math.sin(theta)));
finalPoints.push(new THREE.Vector2(rootRadius * Math.cos(theta), rootRadius * Math.sin(theta)));
}
}
const shape = new THREE.Shape(finalPoints);
// Universal Bore
shape.holes.push(createBorePath(boreType, boreDiameter));
const isHelical = gearType !== 'spur';
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: faceWidth, bevelEnabled: true, bevelSize: module*0.05, bevelThickness: module*0.05, steps: isHelical ? 20 : 1
});
if (isHelical) {
const pos = geometry.attributes.position;
const vertex = new THREE.Vector3();
const beta = helixAngle * Math.PI / 180;
for (let i = 0; i < pos.count; i++) {
vertex.fromBufferAttribute(pos, i);
let twist = 0;
if(gearType === 'helical') twist = (vertex.z / faceWidth) * (faceWidth * Math.tan(beta) / pitchRadius);
else {
const half = faceWidth/2;
const maxTwist = (half * Math.tan(beta)) / pitchRadius;
twist = (vertex.z <= half) ? (vertex.z/half)*maxTwist : maxTwist - ((vertex.z-half)/half)*maxTwist;
}
const cos = Math.cos(twist), sin = Math.sin(twist);
const nx = vertex.x * cos - vertex.y * sin;
const ny = vertex.x * sin + vertex.y * cos;
pos.setXY(i, nx, ny);
}
geometry.computeVertexNormals();
}
geometry.rotateX(Math.PI / 2);
geometry.center();
geometry.translate(0, faceWidth/2, 0);
return geometry;
}
// --- Knob Generator ---
function createKnob() {
const outerD = getValue('knob-outer-diameter');
const height = getValue('knob-height');
const gripStyle = document.getElementById('knob-grip-style-selector').value;
const shaftType = document.getElementById('knob-shaft-type-selector').value;
const boreSize = getValue('knob-shaft-diameter');
const R = outerD / 2;
const shape = new THREE.Shape();
if (gripStyle === 'knurled') {
const knurls = 24;
const depth = R * 0.1;
for (let i = 0; i < knurls * 2; i++) {
const angle = (i / (knurls * 2)) * Math.PI * 2;
const r = i % 2 === 0 ? R : R - depth;
if (i === 0) shape.moveTo(Math.cos(angle)*r, Math.sin(angle)*r);
else shape.lineTo(Math.cos(angle)*r, Math.sin(angle)*r);
}
} else {
shape.absarc(0, 0, R, 0, Math.PI * 2, false);
}
shape.closePath();
// Universal Bore
shape.holes.push(createBorePath(shaftType, boreSize));
const geometry = new THREE.ExtrudeGeometry(shape, {
depth: height,
bevelEnabled: true,
bevelThickness: 1,
bevelSize: 1,
bevelSegments: 3,
curveSegments: 64
});
geometry.rotateX(Math.PI / 2);
geometry.translate(0, height/2, 0);
return geometry;
}
// --- Funnel Generator ---
function createFunnel() {
const topR = getValue('funnel-top-diameter') / 2;
const botR = getValue('funnel-bottom-diameter') / 2;
const H = getValue('funnel-height');
const spoutL = getValue('funnel-spout-length');
const thickness = getValue('funnel-wall-thickness');
const points = [];
points.push(new THREE.Vector2(botR, 0));
points.push(new THREE.Vector2(botR, spoutL));
points.push(new THREE.Vector2(topR, spoutL + H));
points.push(new THREE.Vector2(topR - thickness, spoutL + H));
points.push(new THREE.Vector2(botR - thickness, spoutL));
points.push(new THREE.Vector2(botR - thickness, 0));
points.push(new THREE.Vector2(botR, 0));
const geometry = new THREE.LatheGeometry(points, 64);
geometry.rotateX(Math.PI);
geometry.translate(0, spoutL + H, 0);
return geometry;
}
// --- Main Update Logic ---
function updateMesh() {
const partType = document.getElementById('part-selector').value;
let geometry;
if (currentMesh) {
scene.remove(currentMesh);
if(currentMesh.geometry) currentMesh.geometry.dispose();
if(currentMesh.isGroup) {
currentMesh.traverse(c => { if(c.isMesh) c.geometry.dispose(); });
}
}
if(partType !== 'gears') isAnimatingGears = false;
if (partType === 'gears') {
const group = new THREE.Group();
const numGears = parseInt(document.getElementById('num-gears-selector').value);
const module = getValue('gear-module');
const pressureAngle = getValue('gear-pressure-angle');
const faceWidth = getValue('gear-face-width');
const gearType = document.getElementById('gear-type-selector').value;
const helixAngle = getValue('gear-helix-angle');
const t1 = getValue('gear1-teeth');
const g1 = createSingleSpurGear({
numTeeth: t1, module, faceWidth, pressureAngle, gearType, helixAngle,
boreType: document.getElementById('gear1-bore-type-selector').value,
boreDiameter: getValue('gear1-bore-diameter')
});
const m1 = new THREE.Mesh(g1, mainMaterial);
m1.name = 'gear1'; m1.userData.teeth = t1;
m1.rotation.y = 0;
group.add(m1);
const r1 = (t1 * module) / 2;
for(let i=2; i<=numGears; i++) {
const tN = getValue(`gear${i}-teeth`);
const gN = createSingleSpurGear({
numTeeth: tN, module, faceWidth, pressureAngle, gearType, helixAngle: -helixAngle,
boreType: document.getElementById(`gear${i}-bore-type-selector`).value,
boreDiameter: getValue(`gear${i}-bore-diameter`)
});
const mN = new THREE.Mesh(gN, mainMaterial);
mN.name = `gear${i}`; mN.userData.teeth = tN;
const rN = (tN * module) / 2;
const dist = r1 + rN;
const placementAngle = ((i-2) * 2 * Math.PI) / (Math.max(1, numGears-1));
mN.position.set(Math.cos(placementAngle)*dist, 0, Math.sin(placementAngle)*dist);
mN.rotation.y = placementAngle + Math.PI + (Math.PI / tN);
group.add(mN);
}
const box = new THREE.Box3().setFromObject(group);
const center = new THREE.Vector3(); box.getCenter(center);
group.position.set(-center.x, 0, -center.z);
currentMesh = group;
isAnimatingGears = document.getElementById('gear-simulate-toggle').checked;
}
else {
try {
switch(partType) {
case 'roller': geometry = createRoller(); break;
case 'washer': geometry = createWasher(); break;
case 'box': geometry = createBox(); break;
case 'knob': geometry = createKnob(); break;
case 'funnel': geometry = createFunnel(); break;
}
} catch(e) { console.error(e); }
if (geometry) {