-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_pages_3_d_lens_model.html
More file actions
1148 lines (1027 loc) · 38.6 KB
/
Copy pathgithub_pages_3_d_lens_model.html
File metadata and controls
1148 lines (1027 loc) · 38.6 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="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>3D 圆镜片模型|近视 / 远视 / 散光矫治镜片可视化</title>
<style>
:root {
color-scheme: dark;
--bg: #070a12;
--panel: rgba(16, 23, 42, 0.78);
--panel2: rgba(30, 41, 59, 0.72);
--line: rgba(148, 163, 184, 0.25);
--text: #e5e7eb;
--muted: #94a3b8;
--accent: #38bdf8;
--accent2: #a78bfa;
--good: #34d399;
--warn: #fbbf24;
--bad: #fb7185;
--shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
--radius: 18px;
font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
html, body {
margin: 0;
height: 100%;
overflow: hidden;
background:
radial-gradient(circle at 20% 10%, rgba(56, 189, 248, 0.14), transparent 34%),
radial-gradient(circle at 80% 4%, rgba(167, 139, 250, 0.16), transparent 32%),
linear-gradient(145deg, #05070d 0%, #0b1020 48%, #070a12 100%);
color: var(--text);
}
button, input, select {
font: inherit;
}
.app {
height: 100%;
display: grid;
grid-template-columns: minmax(0, 1fr) 390px;
gap: 14px;
padding: 14px;
}
.stage {
position: relative;
min-width: 0;
border: 1px solid var(--line);
border-radius: var(--radius);
overflow: hidden;
background: rgba(2, 6, 23, 0.46);
box-shadow: var(--shadow);
}
#viewport {
width: 100%;
height: 100%;
min-height: 360px;
}
.title-card {
position: absolute;
left: 16px;
top: 16px;
max-width: min(560px, calc(100% - 32px));
padding: 13px 14px;
border-radius: 16px;
background: rgba(15, 23, 42, 0.70);
border: 1px solid rgba(148, 163, 184, 0.23);
backdrop-filter: blur(16px);
pointer-events: none;
}
.title-card h1 {
margin: 0 0 7px;
font-size: 18px;
letter-spacing: 0.02em;
}
.title-card p {
margin: 0;
color: var(--muted);
font-size: 13px;
line-height: 1.55;
}
.legend {
position: absolute;
left: 16px;
bottom: 16px;
display: flex;
flex-wrap: wrap;
gap: 8px;
pointer-events: none;
}
.pill {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 7px 9px;
border-radius: 999px;
background: rgba(15, 23, 42, 0.72);
border: 1px solid rgba(148, 163, 184, 0.22);
color: var(--muted);
font-size: 12px;
backdrop-filter: blur(14px);
}
.dot {
width: 9px;
height: 9px;
border-radius: 50%;
display: inline-block;
background: var(--accent);
}
.dot.axis { background: #fb7185; }
.dot.perp { background: #fbbf24; }
.dot.ray { background: #38bdf8; }
.panel {
min-width: 0;
height: 100%;
overflow: hidden;
display: flex;
flex-direction: column;
border: 1px solid var(--line);
border-radius: var(--radius);
background: var(--panel);
box-shadow: var(--shadow);
backdrop-filter: blur(18px);
}
.panel-header {
padding: 16px 16px 12px;
border-bottom: 1px solid rgba(148, 163, 184, 0.18);
}
.panel-header h2 {
margin: 0 0 8px;
font-size: 16px;
}
.panel-header .sub {
margin: 0;
color: var(--muted);
font-size: 12.5px;
line-height: 1.5;
}
.controls {
padding: 14px 14px 18px;
overflow: auto;
}
.group {
margin-bottom: 13px;
padding: 12px;
border: 1px solid rgba(148, 163, 184, 0.18);
border-radius: 16px;
background: rgba(15, 23, 42, 0.45);
}
.group h3 {
margin: 0 0 12px;
font-size: 13px;
color: #dbeafe;
letter-spacing: 0.01em;
display: flex;
align-items: center;
justify-content: space-between;
gap: 8px;
}
.row {
display: grid;
grid-template-columns: 118px minmax(0, 1fr) 76px;
gap: 10px;
align-items: center;
margin: 10px 0;
}
.row label {
color: var(--muted);
font-size: 12.5px;
line-height: 1.25;
}
input[type="range"] {
width: 100%;
accent-color: var(--accent);
}
input[type="number"], select {
width: 100%;
border: 1px solid rgba(148, 163, 184, 0.24);
background: rgba(2, 6, 23, 0.55);
color: var(--text);
border-radius: 10px;
padding: 7px 8px;
outline: none;
}
input[type="number"]:focus, select:focus {
border-color: rgba(56, 189, 248, 0.75);
box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.12);
}
.select-row {
display: grid;
grid-template-columns: 118px minmax(0, 1fr);
gap: 10px;
align-items: center;
margin: 8px 0 12px;
}
.check-row {
display: flex;
align-items: center;
gap: 8px;
margin: 9px 0;
color: var(--muted);
font-size: 12.5px;
user-select: none;
}
input[type="checkbox"] {
accent-color: var(--accent);
width: 16px;
height: 16px;
}
.mini-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
margin-top: 8px;
}
.btn {
border: 1px solid rgba(148, 163, 184, 0.24);
background: rgba(2, 6, 23, 0.45);
color: var(--text);
border-radius: 12px;
padding: 9px 10px;
cursor: pointer;
transition: transform 120ms ease, border-color 120ms ease, background 120ms ease;
}
.btn:hover {
transform: translateY(-1px);
border-color: rgba(56, 189, 248, 0.55);
background: rgba(14, 165, 233, 0.12);
}
.stats {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 8px;
}
.stat {
padding: 10px;
border-radius: 13px;
background: rgba(2, 6, 23, 0.48);
border: 1px solid rgba(148, 163, 184, 0.14);
}
.stat .k {
display: block;
color: var(--muted);
font-size: 11.5px;
margin-bottom: 5px;
}
.stat .v {
font-size: 14px;
color: var(--text);
font-variant-numeric: tabular-nums;
}
.rx {
padding: 10px 11px;
margin-top: 10px;
border-radius: 13px;
background: linear-gradient(135deg, rgba(56, 189, 248, 0.12), rgba(167, 139, 250, 0.10));
border: 1px solid rgba(56, 189, 248, 0.18);
color: #e0f2fe;
font-size: 12.5px;
line-height: 1.55;
}
.formula {
margin-top: 10px;
padding: 10px 11px;
border-radius: 13px;
background: rgba(2, 6, 23, 0.42);
border: 1px dashed rgba(148, 163, 184, 0.28);
color: var(--muted);
font-size: 12.3px;
line-height: 1.55;
}
code {
color: #bae6fd;
background: rgba(56, 189, 248, 0.10);
padding: 1px 5px;
border-radius: 6px;
}
.toast {
position: absolute;
right: 16px;
bottom: 16px;
opacity: 0;
transform: translateY(8px);
transition: all 180ms ease;
padding: 10px 12px;
border-radius: 12px;
background: rgba(15, 23, 42, 0.80);
border: 1px solid rgba(148, 163, 184, 0.25);
color: #dbeafe;
font-size: 12.5px;
pointer-events: none;
backdrop-filter: blur(14px);
}
.toast.show {
opacity: 1;
transform: translateY(0);
}
.warning {
color: var(--warn);
}
@media (max-width: 980px) {
html, body { overflow: auto; }
.app {
height: auto;
min-height: 100%;
grid-template-columns: 1fr;
}
.stage { height: 62vh; min-height: 460px; }
.panel { height: auto; overflow: visible; }
.controls { overflow: visible; }
}
@media (max-width: 560px) {
.app { padding: 9px; gap: 9px; }
.stage { min-height: 430px; }
.title-card { left: 10px; top: 10px; }
.legend { left: 10px; bottom: 10px; }
.row { grid-template-columns: 96px minmax(0, 1fr) 64px; gap: 7px; }
.select-row { grid-template-columns: 96px minmax(0, 1fr); }
.stats { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<main class="app">
<section class="stage" aria-label="3D 镜片视图">
<div id="viewport"></div>
<div class="title-card">
<h1>3D 圆镜片模型</h1>
<p>近视用负球镜:边缘更厚;远视用正球镜:中心更厚;散光用柱镜:不同方向曲率不同。拖拽旋转,滚轮缩放,右键平移。</p>
</div>
<div class="legend">
<span class="pill"><span class="dot axis"></span>红线:柱镜轴位</span>
<span class="pill"><span class="dot perp"></span>黄线:柱镜最大作用方向</span>
<span class="pill"><span class="dot ray"></span>蓝线:光线示意</span>
</div>
<div class="toast" id="toast">已复制</div>
</section>
<aside class="panel">
<div class="panel-header">
<h2>参数控制</h2>
<p class="sub">这是可视化模型,不等同于真实镜片加工数据;横向缩放和曲率夸张用于让形变更容易观察。</p>
</div>
<div class="controls">
<section class="group">
<h3>处方与镜片类型</h3>
<div class="select-row">
<label for="preset">快速预设</label>
<select id="preset">
<option value="custom">自定义</option>
<option value="myopia">近视矫正:-3.00D 球镜</option>
<option value="hyperopia">远视矫正:+3.00D 球镜</option>
<option value="astigMinus">近视散光:-2.00D / -1.50D × 180</option>
<option value="astigPlus">远视散光:+2.00D / +1.50D × 90</option>
<option value="mixed">混合散光:+1.00D / -3.00D × 45</option>
</select>
</div>
<div class="row">
<label for="sphere">球镜 S / D</label>
<input id="sphereRange" type="range" min="-12" max="12" step="0.25" value="-3" />
<input id="sphere" type="number" min="-12" max="12" step="0.25" value="-3" />
</div>
<div class="row">
<label for="cylinder">柱镜 C / D</label>
<input id="cylinderRange" type="range" min="-8" max="8" step="0.25" value="0" />
<input id="cylinder" type="number" min="-8" max="8" step="0.25" value="0" />
</div>
<div class="row">
<label for="axis">轴位 Axis / °</label>
<input id="axisRange" type="range" min="0" max="180" step="1" value="180" />
<input id="axis" type="number" min="0" max="180" step="1" value="180" />
</div>
</section>
<section class="group">
<h3>几何参数</h3>
<div class="row">
<label for="diameter">镜片直径 / mm</label>
<input id="diameterRange" type="range" min="35" max="85" step="1" value="65" />
<input id="diameter" type="number" min="35" max="85" step="1" value="65" />
</div>
<div class="row">
<label for="baseThickness">基础厚度 / mm</label>
<input id="baseThicknessRange" type="range" min="0.5" max="10" step="0.1" value="2.2" />
<input id="baseThickness" type="number" min="0.5" max="10" step="0.1" value="2.2" />
</div>
<div class="row">
<label for="minThickness">最小厚度 / mm</label>
<input id="minThicknessRange" type="range" min="0.2" max="5" step="0.1" value="0.8" />
<input id="minThickness" type="number" min="0.2" max="5" step="0.1" value="0.8" />
</div>
<div class="row">
<label for="refractiveIndex">折射率 n</label>
<input id="refractiveIndexRange" type="range" min="1.49" max="1.74" step="0.01" value="1.60" />
<input id="refractiveIndex" type="number" min="1.49" max="1.74" step="0.01" value="1.60" />
</div>
<label class="check-row">
<input id="autoMin" type="checkbox" checked /> 自动平移厚度,让最薄处等于“最小厚度”
</label>
</section>
<section class="group">
<h3>可视化增强</h3>
<div class="row">
<label for="xScale">横向缩放 ×</label>
<input id="xScaleRange" type="range" min="0.5" max="6" step="0.05" value="1.00" />
<input id="xScale" type="number" min="0.5" max="6" step="0.05" value="1.00" />
</div>
<div class="row">
<label for="curveGain">曲率夸张 ×</label>
<input id="curveGainRange" type="range" min="0.1" max="8" step="0.05" value="1.60" />
<input id="curveGain" type="number" min="0.1" max="8" step="0.05" value="1.60" />
</div>
<div class="row">
<label for="opacity">透明度</label>
<input id="opacityRange" type="range" min="0.18" max="1" step="0.01" value="0.64" />
<input id="opacity" type="number" min="0.18" max="1" step="0.01" value="0.64" />
</div>
<div class="row">
<label for="resolution">网格精度</label>
<input id="resolutionRange" type="range" min="24" max="110" step="2" value="46" />
<input id="resolution" type="number" min="24" max="110" step="2" value="46" />
</div>
<label class="check-row">
<input id="xBoost" type="checkbox" checked /> 横向缩放同时增强形变,使参数变化更明显
</label>
<label class="check-row">
<input id="showWire" type="checkbox" /> 显示网格线
</label>
<label class="check-row">
<input id="showRays" type="checkbox" checked /> 显示光线示意
</label>
<label class="check-row">
<input id="showProfiles" type="checkbox" checked /> 显示水平/垂直截面轮廓
</label>
<label class="check-row">
<input id="showAstigTexture" type="checkbox" checked /> 显示散光曲率纹理,让轴位变化更明显
</label>
</section>
<section class="group">
<h3>模型读数</h3>
<div class="stats">
<div class="stat"><span class="k">最薄处</span><span class="v" id="minOut">-</span></div>
<div class="stat"><span class="k">最厚处</span><span class="v" id="maxOut">-</span></div>
<div class="stat"><span class="k">厚度差</span><span class="v" id="diffOut">-</span></div>
<div class="stat"><span class="k">估算焦距</span><span class="v" id="focusOut">-</span></div>
</div>
<div class="rx" id="rxOut">-</div>
<div class="formula">
形变核心:<code>厚度轮廓 ∝ -(S · r² + C · p⊥²)</code>。其中 <code>S</code> 为球镜,<code>C</code> 为柱镜,<code>p⊥</code> 是垂直于轴位方向的坐标。负度数让边缘更厚,正度数让中心更厚。
</div>
<div class="mini-grid">
<button class="btn" id="resetView">重置视角</button>
<button class="btn" id="copyLink">复制参数链接</button>
</div>
</section>
</div>
</aside>
</main>
<script type="importmap">
{
"imports": {
"three": "https://cdn.jsdelivr.net/npm/three@0.160.0/build/three.module.js",
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.160.0/examples/jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const $ = (id) => document.getElementById(id);
const paired = [
'sphere', 'cylinder', 'axis', 'diameter', 'baseThickness', 'minThickness',
'refractiveIndex', 'xScale', 'curveGain', 'opacity', 'resolution'
];
const UNIT_PER_MM_AT_DEFAULT = 2.2 / 32.5;
const DEG = Math.PI / 180;
const viewport = $('viewport');
const scene = new THREE.Scene();
scene.background = new THREE.Color(0x070a12);
const camera = new THREE.PerspectiveCamera(42, 1, 0.05, 100);
camera.position.set(4.8, -6.4, 3.2);
const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
renderer.setPixelRatio(Math.min(window.devicePixelRatio || 1, 2));
renderer.outputColorSpace = THREE.SRGBColorSpace;
viewport.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.07;
controls.target.set(0, 0, 0);
camera.lookAt(controls.target);
controls.update();
controls.minDistance = 2.2;
controls.maxDistance = 18;
const ambient = new THREE.HemisphereLight(0xdbeafe, 0x0f172a, 2.2);
scene.add(ambient);
const keyLight = new THREE.DirectionalLight(0xffffff, 2.2);
keyLight.position.set(4, -5, 7);
scene.add(keyLight);
const rimLight = new THREE.DirectionalLight(0x7dd3fc, 1.25);
rimLight.position.set(-5, 4, 3);
scene.add(rimLight);
const grid = new THREE.GridHelper(9, 18, 0x334155, 0x1e293b);
grid.rotation.x = Math.PI / 2;
grid.position.z = -1.15;
grid.material.opacity = 0.28;
grid.material.transparent = true;
scene.add(grid);
let lensGroup = null;
let currentStats = null;
function clamp(v, min, max) {
return Math.min(max, Math.max(min, v));
}
function signed(v, digits = 2) {
const n = Number(v);
return `${n >= 0 ? '+' : ''}${n.toFixed(digits)}`;
}
function formatD(v) {
return `${signed(v, 2)}D`;
}
function getParams() {
const p = {};
for (const key of paired) p[key] = Number($(key).value);
p.autoMin = $('autoMin').checked;
p.xBoost = $('xBoost').checked;
p.showWire = $('showWire').checked;
p.showRays = $('showRays').checked;
p.showProfiles = $('showProfiles').checked;
p.showAstigTexture = $('showAstigTexture').checked;
p.resolution = Math.round(p.resolution / 2) * 2;
p.axis = clamp(p.axis, 0, 180);
return p;
}
function syncPair(key, source) {
const num = $(key);
const range = $(`${key}Range`);
if (source === 'range') num.value = range.value;
if (source === 'number') range.value = num.value;
}
function bindInputs() {
for (const key of paired) {
const num = $(key);
const range = $(`${key}Range`);
range.addEventListener('input', () => {
syncPair(key, 'range');
$('preset').value = 'custom';
scheduleRebuild();
});
num.addEventListener('input', () => {
const min = Number(num.min);
const max = Number(num.max);
if (Number.isFinite(min) && Number.isFinite(max)) {
num.value = clamp(Number(num.value), min, max);
}
syncPair(key, 'number');
$('preset').value = 'custom';
scheduleRebuild();
});
}
for (const id of ['autoMin', 'xBoost', 'showWire', 'showRays', 'showProfiles', 'showAstigTexture']) {
$(id).addEventListener('change', rebuild);
}
$('preset').addEventListener('change', () => {
applyPreset($('preset').value);
scheduleRebuild();
});
$('resetView').addEventListener('click', () => {
camera.position.set(4.8, -6.4, 3.2);
controls.target.set(0, 0, 0);
camera.lookAt(controls.target);
controls.update();
});
$('copyLink').addEventListener('click', async () => {
const p = getParams();
const qs = new URLSearchParams({
s: p.sphere,
c: p.cylinder,
a: p.axis,
d: p.diameter,
bt: p.baseThickness,
mt: p.minThickness,
n: p.refractiveIndex,
xs: p.xScale,
cg: p.curveGain,
op: p.opacity,
res: p.resolution,
xb: p.xBoost ? 1 : 0,
wire: p.showWire ? 1 : 0,
rays: p.showRays ? 1 : 0,
prof: p.showProfiles ? 1 : 0,
astig: p.showAstigTexture ? 1 : 0
});
const url = `${location.origin}${location.pathname}?${qs.toString()}`;
try {
await navigator.clipboard.writeText(url);
showToast('已复制当前参数链接');
} catch {
showToast('浏览器禁止复制,可手动复制地址栏参数');
history.replaceState(null, '', `?${qs.toString()}`);
}
});
}
function applyPreset(value) {
const presets = {
myopia: { sphere: -3.00, cylinder: 0, axis: 180, curveGain: 1.6, xScale: 1 },
hyperopia: { sphere: 3.00, cylinder: 0, axis: 180, curveGain: 1.6, xScale: 1 },
astigMinus: { sphere: -2.00, cylinder: -1.50, axis: 180, curveGain: 2.0, xScale: 1.35 },
astigPlus: { sphere: 2.00, cylinder: 1.50, axis: 90, curveGain: 2.0, xScale: 1.35 },
mixed: { sphere: 1.00, cylinder: -3.00, axis: 45, curveGain: 2.2, xScale: 1.5 }
};
const preset = presets[value];
if (!preset) return;
for (const [key, val] of Object.entries(preset)) {
$(key).value = String(val);
$(`${key}Range`).value = String(val);
}
}
function loadFromQuery() {
const qs = new URLSearchParams(location.search);
const map = {
s: 'sphere',
c: 'cylinder',
a: 'axis',
d: 'diameter',
bt: 'baseThickness',
mt: 'minThickness',
n: 'refractiveIndex',
xs: 'xScale',
cg: 'curveGain',
op: 'opacity',
res: 'resolution'
};
for (const [shortKey, key] of Object.entries(map)) {
if (!qs.has(shortKey)) continue;
const val = Number(qs.get(shortKey));
if (!Number.isFinite(val)) continue;
$(key).value = String(val);
$(`${key}Range`).value = String(val);
}
if (qs.has('xb')) $('xBoost').checked = qs.get('xb') !== '0';
if (qs.has('wire')) $('showWire').checked = qs.get('wire') !== '0';
if (qs.has('rays')) $('showRays').checked = qs.get('rays') !== '0';
if (qs.has('prof')) $('showProfiles').checked = qs.get('prof') !== '0';
if (qs.has('astig')) $('showAstigTexture').checked = qs.get('astig') !== '0';
}
function makeLensGeometry(params) {
const rings = clamp(params.resolution, 24, 150);
const sectors = Math.max(72, Math.round(rings * 2));
const pointCount = (rings + 1) * sectors;
const radiusScene = 2.2 * (params.diameter / 65);
const baseScene = params.baseThickness * UNIT_PER_MM_AT_DEFAULT;
const minScene = params.minThickness * UNIT_PER_MM_AT_DEFAULT;
const diameterScale = params.diameter / 65;
const xBoostGain = params.xBoost ? Math.sqrt(params.xScale) : 1;
const powerIndexCorrection = 1.6 / params.refractiveIndex;
const gain = 0.044 * params.curveGain * xBoostGain * diameterScale * diameterScale * powerIndexCorrection;
const axisRad = params.axis * DEG;
const sinA = Math.sin(axisRad);
const cosA = Math.cos(axisRad);
const samples = [];
let minRaw = Infinity;
let maxRaw = -Infinity;
for (let i = 0; i <= rings; i++) {
const r = i / rings;
for (let j = 0; j < sectors; j++) {
const theta = (j / sectors) * Math.PI * 2;
const x = r * Math.cos(theta);
const y = r * Math.sin(theta);
const perp = -sinA * x + cosA * y;
const r2 = x * x + y * y;
const powerTerm = params.sphere * r2 + params.cylinder * perp * perp;
const profile = -powerTerm;
const rawThickness = baseScene + gain * profile;
minRaw = Math.min(minRaw, rawThickness);
maxRaw = Math.max(maxRaw, rawThickness);
samples.push({ x, y, r, theta, thickness: rawThickness, powerTerm, perp });
}
}
const shift = params.autoMin ? (minScene - minRaw) : Math.max(0, minScene - minRaw);
const positions = new Float32Array(pointCount * 2 * 3);
const colors = new Float32Array(pointCount * 2 * 3);
let minT = Infinity;
let maxT = -Infinity;
let maxZ = 0;
for (let i = 0; i < samples.length; i++) {
const s = samples[i];
const t = Math.max(minScene * 0.35, s.thickness + shift);
minT = Math.min(minT, t);
maxT = Math.max(maxT, t);
maxZ = Math.max(maxZ, t / 2);
const px = s.x * radiusScene * params.xScale;
const py = s.y * radiusScene;
const frontIndex = i * 3;
const backIndex = (pointCount + i) * 3;
positions[frontIndex] = px;
positions[frontIndex + 1] = py;
positions[frontIndex + 2] = t / 2;
positions[backIndex] = px;
positions[backIndex + 1] = py;
positions[backIndex + 2] = -t / 2;
}
const diffT = Math.max(1e-6, maxT - minT);
for (let i = 0; i < samples.length; i++) {
const t = Math.max(minScene * 0.35, samples[i].thickness + shift);
const k = clamp((t - minT) / diffT, 0, 1);
const color = new THREE.Color();
color.setHSL(0.56 - 0.12 * k, 0.84, 0.64 - 0.12 * k);
const frontIndex = i * 3;
const backIndex = (pointCount + i) * 3;
colors[frontIndex] = color.r;
colors[frontIndex + 1] = color.g;
colors[frontIndex + 2] = color.b;
colors[backIndex] = color.r;
colors[backIndex + 1] = color.g;
colors[backIndex + 2] = color.b;
}
const indices = [];
const idx = (i, j) => i * sectors + ((j % sectors + sectors) % sectors);
for (let i = 0; i < rings; i++) {
for (let j = 0; j < sectors; j++) {
const a = idx(i, j);
const b = idx(i + 1, j);
const c = idx(i + 1, j + 1);
const d = idx(i, j + 1);
indices.push(a, b, c, a, c, d);
const ab = pointCount + a;
const bb = pointCount + b;
const cb = pointCount + c;
const db = pointCount + d;
indices.push(ab, cb, bb, ab, db, cb);
}
}
for (let j = 0; j < sectors; j++) {
const f1 = idx(rings, j);
const f2 = idx(rings, j + 1);
const b1 = pointCount + f1;
const b2 = pointCount + f2;
indices.push(f1, f2, b2, f1, b2, b1);
}
const geometry = new THREE.BufferGeometry();
geometry.setAttribute('position', new THREE.BufferAttribute(positions, 3));
geometry.setAttribute('color', new THREE.BufferAttribute(colors, 3));
geometry.setIndex(indices);
geometry.computeVertexNormals();
return {
geometry,
rings,
sectors,
radiusScene,
minT,
maxT,
maxZ,
samples,
pointCount,
axisRad,
shift,
gain,
baseScene,
minScene,
sinA,
cosA
};
}
function lensThicknessAtUnit(params, lensData, x, y) {
const r2 = x * x + y * y;
const perp = -lensData.sinA * x + lensData.cosA * y;
return Math.max(
lensData.minScene * 0.35,
lensData.baseScene + lensData.gain * (-(params.sphere * r2 + params.cylinder * perp * perp)) + lensData.shift
);
}
function makeRimLines(params, lensData) {
const group = new THREE.Group();
const pos = lensData.geometry.getAttribute('position');
const front = [];
const back = [];
const outerStart = lensData.rings * lensData.sectors;
for (let j = 0; j <= lensData.sectors; j++) {
const i = outerStart + (j % lensData.sectors);
front.push(new THREE.Vector3(pos.getX(i), pos.getY(i), pos.getZ(i) + 0.012));
const bi = lensData.pointCount + i;
back.push(new THREE.Vector3(pos.getX(bi), pos.getY(bi), pos.getZ(bi) - 0.012));
}
const rimMat = new THREE.LineBasicMaterial({ color: 0xe0f2fe, transparent: true, opacity: 0.72 });
const sideMat = new THREE.LineBasicMaterial({ color: 0x93c5fd, transparent: true, opacity: 0.28 });
group.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints(front), rimMat));
group.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints(back), rimMat));
const side = [];
const stride = Math.max(6, Math.round(lensData.sectors / 24));
for (let j = 0; j < lensData.sectors; j += stride) {
const i = outerStart + j;
const bi = lensData.pointCount + i;
side.push(new THREE.Vector3(pos.getX(i), pos.getY(i), pos.getZ(i)));
side.push(new THREE.Vector3(pos.getX(bi), pos.getY(bi), pos.getZ(bi)));
}
const sideGeom = new THREE.BufferGeometry().setFromPoints(side);
group.add(new THREE.LineSegments(sideGeom, sideMat));
return group;
}
function makeAxisLines(params, lensData) {
const group = new THREE.Group();
const r = lensData.radiusScene * 1.05;
const z = lensData.maxZ + 0.035;
const axis = params.axis * DEG;
const axisPts = [
new THREE.Vector3(-Math.cos(axis) * r * params.xScale, -Math.sin(axis) * r, z),
new THREE.Vector3(Math.cos(axis) * r * params.xScale, Math.sin(axis) * r, z)
];
const perp = axis + Math.PI / 2;
const perpPts = [
new THREE.Vector3(-Math.cos(perp) * r * params.xScale, -Math.sin(perp) * r, z + 0.012),
new THREE.Vector3(Math.cos(perp) * r * params.xScale, Math.sin(perp) * r, z + 0.012)
];
const axisMat = new THREE.LineBasicMaterial({ color: 0xfb7185, transparent: true, opacity: 0.95 });
const perpMat = new THREE.LineBasicMaterial({ color: 0xfbbf24, transparent: true, opacity: 0.90 });
group.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints(axisPts), axisMat));
group.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints(perpPts), perpMat));
return group;
}
function makeProfiles(params, lensData) {
const group = new THREE.Group();
const matXFront = new THREE.LineBasicMaterial({ color: 0x93c5fd, transparent: true, opacity: 0.95 });
const matXBack = new THREE.LineBasicMaterial({ color: 0x60a5fa, transparent: true, opacity: 0.55 });
const matYFront = new THREE.LineBasicMaterial({ color: 0xc4b5fd, transparent: true, opacity: 0.95 });
const matYBack = new THREE.LineBasicMaterial({ color: 0xa78bfa, transparent: true, opacity: 0.55 });
const radiusScene = lensData.radiusScene;
function addLine(axisName, front, mat) {
const pts = [];
const steps = 120;
for (let i = 0; i <= steps; i++) {
const u = -1 + (2 * i) / steps;
const x = axisName === 'x' ? u : 0;
const y = axisName === 'y' ? u : 0;
const t = lensThicknessAtUnit(params, lensData, x, y);
pts.push(new THREE.Vector3(x * radiusScene * params.xScale, y * radiusScene, front ? t / 2 + 0.018 : -t / 2 - 0.018));
}
group.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints(pts), mat));
}
addLine('x', true, matXFront);
addLine('x', false, matXBack);
addLine('y', true, matYFront);
addLine('y', false, matYBack);
return group;
}
function makeAstigTexture(params, lensData) {
const group = new THREE.Group();
if (Math.abs(params.cylinder) < 0.125) return group;
const radiusScene = lensData.radiusScene;
const axis = params.axis * DEG;
const cosA = Math.cos(axis);
const sinA = Math.sin(axis);
const mat = new THREE.LineBasicMaterial({
color: params.cylinder < 0 ? 0xf472b6 : 0x2dd4bf,
transparent: true,
opacity: 0.48
});
for (let k = -4; k <= 4; k++) {
const u = k / 5;
const half = Math.sqrt(Math.max(0, 1 - u * u));
const pts = [];
const steps = 80;
for (let i = 0; i <= steps; i++) {
const v = -half + (2 * half * i) / steps;
const x = cosA * v - sinA * u;
const y = sinA * v + cosA * u;
const t = lensThicknessAtUnit(params, lensData, x, y);
pts.push(new THREE.Vector3(x * radiusScene * params.xScale, y * radiusScene, t / 2 + 0.028));
}
group.add(new THREE.Line(new THREE.BufferGeometry().setFromPoints(pts), mat));
}
return group;
}
function makeRays(params, lensData) {
const group = new THREE.Group();
const incoming = [];
const outgoing = [];
const axisRad = params.axis * DEG;
const sinA = Math.sin(axisRad);
const cosA = Math.cos(axisRad);
const rScene = lensData.radiusScene;
const zIn = -4.8;
const zBefore = -lensData.maxZ - 0.28;
const zAfter = lensData.maxZ + 0.28;
const zOut = 5.2;
const samples = [-0.72, -0.36, 0, 0.36, 0.72];
for (const ux of samples) {
for (const uy of samples) {
if (ux * ux + uy * uy > 0.84 * 0.84) continue;
if (Math.abs(ux) < 0.001 && Math.abs(uy) < 0.001) continue;
const x = ux * rScene * params.xScale;
const y = uy * rScene;
incoming.push(x, y, zIn, x, y, zBefore);
const r2 = ux * ux + uy * uy;
const perp = -sinA * ux + cosA * uy;
const meridianMix = r2 < 1e-6 ? 0 : (perp * perp) / r2;
const pEff = params.sphere + params.cylinder * meridianMix;
let ex = x;
let ey = y;
if (Math.abs(pEff) > 0.12) {
const focus = clamp(14 / Math.abs(pEff), 2.2, 16);