-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathvulkan_ocean.cpp
More file actions
2602 lines (2420 loc) · 136 KB
/
Copy pathvulkan_ocean.cpp
File metadata and controls
2602 lines (2420 loc) · 136 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
// Vulkan deferred ocean demo — FFT-displaced water, ray-traced reflections and refraction, LIDAR and radar.
#include "threepp/audio/Audio.hpp"
#include "threepp/audio/WavFile.hpp"
#include "threepp/extras/curves/CatmullRomCurve3.hpp"
#include "threepp/extras/imgui/RendererSettings.hpp"
#include "threepp/extras/terrain/DetailTexture.hpp"
#include "threepp/geometries/PlaneGeometry.hpp"
#include "threepp/helpers/LidarWaveform.hpp"
#include "threepp/helpers/PathTracedLidarSensor.hpp"
#include "threepp/input/KeyListener.hpp"
#include "threepp/lights/AmbientLight.hpp"
#include "threepp/lights/DirectionalLight.hpp"
#include "threepp/geometries/LatheGeometry.hpp"
#include "threepp/geometries/TorusGeometry.hpp"
#include "threepp/loaders/GLTFLoader.hpp"
#include "threepp/loaders/RGBELoader.hpp"
#include "threepp/utils/BufferGeometryUtils.hpp"
#include "threepp/utils/Parallel.hpp"
#include "threepp/materials/MeshPhysicalMaterial.hpp"
#include "threepp/materials/MeshStandardMaterial.hpp"
#include "threepp/math/Box3.hpp"
#include "threepp/math/Matrix3.hpp"
#include "threepp/math/Matrix4.hpp"
#include "threepp/objects/DisplacedMesh.hpp"
#include "threepp/objects/Ocean.hpp"
#include "threepp/renderers/VulkanRenderer.hpp"
#include "threepp/textures/DataTexture.hpp"
#include "threepp/threepp.hpp"
#include "capture_util.hpp"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <iostream>
#include <memory>
#include <numeric>
#include <random>
#include <unordered_map>
#include <vector>
using namespace threepp;
namespace {
struct BoatInput : KeyListener {
bool W = false, A = false, S = false, D = false;
bool shotRequest = false;// F12: dump the next frame to aaa_caps/ (artifact reports)
void onKeyPressed(KeyEvent e) override {
if (e.key == Key::F12) shotRequest = true;
update(e.key, true);
}
void onKeyReleased(KeyEvent e) override { update(e.key, false); }
void update(Key k, bool down) {
if (k == Key::W || k == Key::UP) W = down;
if (k == Key::S || k == Key::DOWN) S = down;
if (k == Key::A || k == Key::LEFT) A = down;
if (k == Key::D || k == Key::RIGHT) D = down;
}
};
// 3-DOF maneuvering model (surge/sway/yaw) + wave-driven heave/pitch/roll.
struct BoatState {
Vector3 position{0.f, 0.f, 0.f};
float yaw = 0.f; // heading, radians
float yawRate = 0.f; // rad/s
float forwardSpeed = 0.f; // surge, m/s
float swaySpeed = 0.f; // sway, m/s
float rudder = 0.f; // radians
float throttle = 0.f; // ∈ [−1, 1]
bool aground = false;
float smoothPitch = 0.f; // radians, low-passed
float smoothRoll = 0.f; // radians, low-passed
float y = 0.f; // heave, metres
float vY = 0.f; // heave velocity, m/s
};
constexpr float kTileSize = 1000.0f; // metres
// FFT res drives wave detail; mesh density is independent (sampled via normalised UVs).
constexpr uint32_t kFftSize = 1024;
constexpr float kPlaneEdge = kTileSize;
constexpr uint32_t kOceanRes = kFftSize / 2; // 512 verts/side ≈ 2 m spacing over 1 km tile
auto makeSandMaterial() {
return MeshStandardMaterial::create(MeshStandardMaterial::Params{}.color(Color(0.02,0.02,0.02)).roughness(1.0f));
}
}// namespace
// Procedural archipelago ring (r 385–495 m): value-noise FBM height field with
// baked albedo + roughness maps. Sub-metre rock relief no longer lives in a
// unique baked normal map — it is carried by the framework's world-XZ-anchored
// tiled detail layer (MaterialWithDetailMap + terrain::makeDetailMaps), which
// the Vulkan deferred G-buffer renders with stochastic tiling, distance fade
// and gated triplanar on steep faces. The vertex grid + analytic normals still
// carry everything ≥ ~0.6 m.
namespace island {
constexpr float kInnerR = 385.f;// boat waypoints reach ~320 m — keep clear water
constexpr float kOuterR = 495.f;// stays inside the 1 km ocean/sand tile
constexpr float kPeakH = 55.f; // tallest summits (m)
constexpr float kSkirt = 7.f; // rim depth — below the sand floor (-5 m)
float hashf(int xi, int zi) {
uint32_t h = static_cast<uint32_t>(xi) * 374761393u + static_cast<uint32_t>(zi) * 668265263u;
h = (h ^ (h >> 13)) * 1274126177u;
h ^= h >> 16;
return static_cast<float>(h) * (1.f / 4294967296.f);
}
// Value noise with quintic fade, range [0,1].
float vnoise(float x, float z) {
const float fx = std::floor(x), fz = std::floor(z);
const int xi = static_cast<int>(fx), zi = static_cast<int>(fz);
float tx = x - fx, tz = z - fz;
tx = tx * tx * tx * (tx * (tx * 6.f - 15.f) + 10.f);
tz = tz * tz * tz * (tz * (tz * 6.f - 15.f) + 10.f);
const float a = hashf(xi, zi), b = hashf(xi + 1, zi);
const float c = hashf(xi, zi + 1), d = hashf(xi + 1, zi + 1);
return a + (b - a) * tx + (c - a) * tz + (a - b - c + d) * tx * tz;
}
float fbm(float x, float z, int octaves) {
float sum = 0.f, amp = 0.5f, norm = 0.f;
for (int o = 0; o < octaves; ++o) {
sum += amp * vnoise(x, z);
norm += amp;
amp *= 0.5f;
// irrational-ish lacunarity + offset decorrelates the octave lattices
const float nx = x * 1.93f + 19.7f, nz = z * 2.11f + 7.3f;
x = nx;
z = nz;
}
return sum / norm;
}
float heightAt(float x, float z) {
const float r = std::sqrt(x * x + z * z);
// ring coordinate, warped so the coastlines wander instead of circling
float w = (r - kInnerR) / (kOuterR - kInnerR);
w += 0.20f * (2.f * fbm(x * 0.011f, z * 0.011f, 2) - 1.f);
if (w <= 0.f || w >= 1.f) return -kSkirt;
const float prof = std::pow(std::sin(math::PI * w), 1.5f);
// mass plan: low-frequency noise on a circle, thresholded — sections
// below the band stay a submerged sill (the passes between islands)
const float a = std::atan2(z, x);
const float m0 = 0.65f * vnoise(7.3f + std::cos(a) * 2.9f, 3.1f + std::sin(a) * 2.9f) +
0.35f * vnoise(13.7f + std::cos(a) * 5.3f, 23.9f + std::sin(a) * 5.3f);
const float m = math::smoothstep(0.27f, 0.60f, m0);
// ridged FBM (fold-over of signed noise) = sharp rocky crests
const float ridge = 1.f - std::abs(2.f * fbm(x * 0.035f, z * 0.035f, 4) - 1.f);
// meso relief: layered creased-slab ridges carried as REAL geometry by
// the dense vertex grid — λ ≈ 11 m slabs, ≈ 4 m ledges, and sub-metre
// bumps (≈ 1.8 m and ≈ 0.9 m). Amplitudes are absolute (a ledge is a
// ledge, independent of the mountain's height) and stay gentler than
// their own wavelength. Scaled by the mass plan so the submerged passes
// stay smooth sills; the normal map now carries only the truly
// sub-vertex scales (≤ ~0.6 m).
const float slab = 1.f - std::abs(2.f * fbm(x * 0.09f, z * 0.09f, 3) - 1.f);
const float slabFn = 1.f - std::abs(2.f * fbm(x * 0.24f, z * 0.24f, 3) - 1.f);
const float bump = fbm(x * 0.55f + 41.f, z * 0.55f + 17.f, 2);
const float grainHi = fbm(x * 1.15f + 7.f, z * 1.15f + 91.f, 2);
const float meso = 3.5f * (slab * slab - 0.45f)
+ 1.8f * (slabFn * slabFn - 0.45f)
+ 0.7f * (bump - 0.5f)
+ 0.35f * (grainHi - 0.5f);
const float crest = (kPeakH * (0.45f + 0.55f * ridge * ridge) + meso) * m;
// prof=1 mid-ring: passes top out 2 m underwater, islands rise to crest
return -kSkirt + (crest + kSkirt - 2.f) * prof;
}
// Analytic finite-difference normal — seam-consistent (the height field is
// continuous in angle) and adds sub-vertex shading detail for free. The
// 0.6 m radius matches the denser ~0.4 m vertex grid so the sub-metre
// relief shades correctly instead of being averaged away.
Vector3 normalAt(float x, float z, float eps = 0.6f) {
const float dhdx = (heightAt(x + eps, z) - heightAt(x - eps, z)) / (2.f * eps);
const float dhdz = (heightAt(x, z + eps) - heightAt(x, z - eps)) / (2.f * eps);
Vector3 n(-dhdx, 1.f, -dhdz);
return n.normalize();
}
struct BakedMaps {
std::shared_ptr<DataTexture> albedo;
std::shared_ptr<DataTexture> rough;
};
// Bake albedo + roughness in polar UV (u = angle, v = radius); rows run in
// parallel. The normal map is gone — cm-scale rock relief is now the
// framework detail layer (see build()).
BakedMaps bakeMaps() {
const int W = 10240, H = 512;
std::vector<unsigned char> albPx(static_cast<size_t>(W) * H * 4);
std::vector<unsigned char> rghPx(static_cast<size_t>(W) * H * 4);
auto toByte = [](float v) {
return static_cast<unsigned char>(std::lround(std::clamp(v, 0.f, 1.f) * 255.f));
};
std::vector<int> rows(H);
std::iota(rows.begin(), rows.end(), 0);
threepp::parallelForEach(rows.begin(), rows.end(), [&](int y) {
const float r = kInnerR + (kOuterR - kInnerR) * ((y + 0.5f) / H);
for (int x = 0; x < W; ++x) {
const float a = 2.f * math::PI * ((x + 0.5f) / W);
const float ca = std::cos(a), sa = std::sin(a);
const float wx = ca * r, wz = sa * r;
const float eps = 2.5f;
const float h = heightAt(wx, wz);
const float hxp = heightAt(wx + eps, wz), hxm = heightAt(wx - eps, wz);
const float hzp = heightAt(wx, wz + eps), hzm = heightAt(wx, wz - eps);
const float dhdx = (hxp - hxm) / (2.f * eps);
const float dhdz = (hzp - hzm) / (2.f * eps);
const float ny = 1.f / std::sqrt(1.f + dhdx * dhdx + dhdz * dhdz);
const float lap = (hxp + hxm + hzp + hzm - 4.f * h) / (eps * eps);
// convexity: exposed crests bleach, concave seams collect dirt
const float crest = std::clamp(-lap * 0.8f, -1.f, 1.f);
const float tone = fbm(wx * 0.0045f, wz * 0.0045f, 2); // per-face rock tone, λ ≈ 220 m
const float mottle = fbm(wx * 0.05f, wz * 0.05f, 3); // λ ≈ 20 m
const float grain = fbm(wx * 0.7f + 5.f, wz * 0.7f + 13.f, 2);// λ ≈ 1.4 m
const float micro = fbm(wx * 1.6f + 211.f, wz * 1.6f + 97.f, 2);// λ ≈ 0.6 m
const float vegL = fbm(wx * 0.013f + 31.f, wz * 0.013f, 3); // veg patches, λ ≈ 75 m
const float vegS = fbm(wx * 0.11f + 7.f, wz * 0.11f + 3.f, 2);// ragged veg edges, λ ≈ 9 m
const float warp = fbm(wx * 0.03f + 71.f, wz * 0.03f + 11.f, 2);
// two-tone base: pale weathered granite vs darker gneiss,
// blended at island scale so each face reads as its own mass
const float t = math::smoothstep(0.35f, 0.65f, tone);
float cr = 0.26f + 0.20f * t;
float cg = 0.245f + 0.195f * t;
float cb = 0.235f + 0.175f * t;
const float speck = (mottle - 0.5f) * 0.14f + (grain - 0.5f) * 0.09f
+ (micro - 0.5f) * 0.05f;
cr += speck;
cg += speck;
cb += speck * 0.9f;
// wandering sub-horizontal strata bands on steep faces
const float strata = 1.f + 0.09f * std::sin(h * 0.75f + 6.f * warp) *
math::smoothstep(0.85f, 0.55f, ny);
// crest/hollow shading from the Laplacian
const float cav = 1.f + 0.14f * crest;
cr *= strata * cav;
cg *= strata * cav;
cb *= strata * cav;
// scree aprons: gentle low benches at the cliff feet collect debris
const float scree = math::smoothstep(0.60f, 0.78f, ny) * math::smoothstep(1.2f, 2.6f, h) *
(1.f - math::smoothstep(5.f, 13.f, h)) *
math::smoothstep(0.35f, 0.65f, vegS) * 0.55f;
cr += (0.41f - cr) * scree;
cg += (0.375f - cg) * scree;
cb += (0.315f - cb) * scree;
// heather/shrub on mid slopes, its own patch noise
const float hePatch = fbm(wx * 0.019f + 57.f, wz * 0.019f + 91.f, 2);
const float heather = math::smoothstep(0.45f, 0.62f, ny) * math::smoothstep(1.5f, 3.5f, h) *
(1.f - math::smoothstep(22.f, 34.f, h)) *
math::smoothstep(0.45f, 0.62f, hePatch);
cr += (0.205f + 0.05f * hePatch - cr) * heather;
cg += (0.17f + 0.05f * hePatch - cg) * heather;
cb += (0.105f - cb) * heather;
// grass/moss on flat low benches; hollows accumulate soil, so
// the Laplacian feeds the patch threshold
const float gPatch = 0.55f * vegL + 0.30f * vegS +
0.15f * std::clamp(lap * 1.5f, 0.f, 1.f);
const float grass = math::smoothstep(0.60f, 0.80f, ny) * math::smoothstep(0.8f, 2.6f, h) *
(1.f - math::smoothstep(24.f, 38.f, h)) *
math::smoothstep(0.42f, 0.58f, gPatch);
cr += (0.13f + 0.07f * vegS - cr) * grass;
cg += (0.27f + 0.08f * vegL - cg) * grass;
cb += (0.085f - cb) * grass;
// pale lichen crusts on exposed high rock
const float lich = math::smoothstep(8.f, 20.f, h) *
math::smoothstep(0.55f, 0.75f, fbm(wx * 0.15f + 13.f, wz * 0.15f + 29.f, 2)) *
std::clamp(0.5f + 0.5f * crest, 0.f, 1.f) * 0.30f;
cr += (0.50f - cr) * lich;
cg += (0.51f - cg) * lich;
cb += (0.46f - cb) * lich;
// summits bleach toward bare washed rock
const float alt = 1.f + 0.08f * math::smoothstep(28.f, 52.f, h);
cr *= alt;
cg *= alt;
cb *= alt;
// Conifer forest — the signature cover of these slopes. Climbs
// steep ground (unlike grass) but sheds off sheer cliff faces;
// a coarse stand field + a ragged canopy-edge noise carve
// clearings and break the treeline so it never reads as a
// contour line. Deep blue-green, varied stand to stand.
const float forestStand = fbm(wx * 0.020f + 13.f, wz * 0.020f + 47.f, 3);// λ ≈ 50 m
const float forestEdge = fbm(wx * 0.13f + 61.f, wz * 0.13f + 29.f, 2); // λ ≈ 8 m
const float forest = math::smoothstep(0.40f, 0.58f, ny) *
math::smoothstep(1.5f, 4.0f, h) *
(1.f - math::smoothstep(26.f, 36.f, h)) *
math::smoothstep(0.34f, 0.52f, 0.6f * forestStand + 0.4f * forestEdge);
cr += (0.045f + 0.030f * forestStand - cr) * forest;
cg += (0.135f + 0.060f * forestStand - cg) * forest;
cb += (0.050f + 0.020f * forestStand - cb) * forest;
// Snow — settles on low-angle ground above the snowline and
// pools in the concave couloirs (lap > 0) well below it, while
// shedding off anything approaching vertical. Faint cool tint
// on the ambient-shadowed fields; a patch field keeps the
// upper edge ragged instead of a hard ring. At this 55 m skerry
// scale snow reads as summit caps + gully streaks, not full
// alpine cover — matching a low Norwegian coastal massif.
const float snowPatch = fbm(wx * 0.05f + 91.f, wz * 0.05f + 5.f, 2);
const float snowSlope = math::smoothstep(0.42f, 0.72f, ny);
const float snowfield = math::smoothstep(26.f, 42.f, h);
const float couloir = std::clamp(lap * 1.4f, 0.f, 1.f) * math::smoothstep(16.f, 28.f, h);
float snow = snowSlope * std::clamp(std::max(snowfield, couloir), 0.f, 1.f);
snow *= 0.6f + 0.4f * math::smoothstep(0.35f, 0.65f, snowPatch);
cr += (0.92f - cr) * snow;
cg += (0.94f - cg) * snow;
cb += (0.98f - cb) * snow;
// algae film straddling the waterline, then the dark wet band
const float algae = (1.f - math::smoothstep(0.6f, 1.4f, std::abs(h - 0.3f))) * 0.5f;
cr += (0.10f - cr) * algae;
cg += (0.15f - cg) * algae;
cb += (0.10f - cb) * algae;
const float wet = (1.f - math::smoothstep(0.4f, 2.2f, h)) * 0.85f;
cr += (0.095f - cr) * wet;
cg += (0.095f - cg) * wet;
cb += (0.09f - cb) * wet;
// Snowmelt waterfalls — sparse thin ribbons down the steep
// faces. A slow azimuthal selector picks a few fall-lines and
// a high-frequency stripe makes each one narrow; both depend
// only on the angle, so a ribbon runs unbroken down the face
// as the radius row changes. Gated to steep rock between the
// splash zone and the snow source above. Bright and slightly
// blue; the glossy wet sheen is added in the roughness block.
const float fallRegion = math::smoothstep(0.60f, 0.82f, fbm(a * 2.5f + 11.f, 4.0f, 2));
const float fallStripe = std::pow(std::max(0.f, std::sin(a * 48.f + 6.f * fbm(a * 9.f, 2.f, 2))), 60.f);
const float fall = fallRegion * fallStripe *
math::smoothstep(0.30f, 0.55f, 1.f - ny) *
math::smoothstep(4.f, 9.f, h) *
(1.f - math::smoothstep(34.f, 44.f, h));
cr += (0.82f - cr) * fall;
cg += (0.86f - cg) * fall;
cb += (0.92f - cb) * fall;
// roughness (.g multiplies material roughness): matte dry
// granite, matte vegetation, matte conifer canopy, water-
// slicked rock turns glossy, bright soft snow, glossy falls
float rough = 0.86f + 0.10f * (mottle - 0.5f) - 0.06f * crest;
rough += (0.95f - rough) * std::max(grass, heather);
rough += (0.93f - rough) * forest;
rough += (0.45f - rough) * wet;
rough += (0.62f - rough) * snow;
rough += (0.38f - rough) * fall;
const size_t i = (static_cast<size_t>(y) * W + x) * 4;
albPx[i + 0] = toByte(cr);
albPx[i + 1] = toByte(cg);
albPx[i + 2] = toByte(cb);
albPx[i + 3] = 255;
rghPx[i + 0] = 255;
rghPx[i + 1] = toByte(rough);
rghPx[i + 2] = 0;
rghPx[i + 3] = 255;
}
});
auto makeTex = [&](std::vector<unsigned char>&& px, bool srgb) {
auto tex = DataTexture::create(ImageData{std::move(px)},
static_cast<unsigned>(W), static_cast<unsigned>(H));
if (srgb) tex->colorSpace = ColorSpace::sRGB;// normal/rough stay raw UNORM
tex->magFilter = Filter::Linear;
tex->minFilter = Filter::LinearMipmapLinear;
tex->generateMipmaps = true;
tex->needsUpdate();
return tex;
};
return {makeTex(std::move(albPx), true),
makeTex(std::move(rghPx), false)};
}
std::shared_ptr<Mesh> build() {
// 8192 angular × 256 radial (≈0.34/0.43 m spacing); seam column duplicated for clean UVs.
const int NA = 8192, NR = 256;
std::vector<float> pos(static_cast<size_t>(NA + 1) * (NR + 1) * 3);
std::vector<float> nrm(static_cast<size_t>(NA + 1) * (NR + 1) * 3);
std::vector<float> uv(static_cast<size_t>(NA + 1) * (NR + 1) * 2);
std::vector<int> rows(NR + 1);
std::iota(rows.begin(), rows.end(), 0);
threepp::parallelForEach(rows.begin(), rows.end(), [&](int j) {
const float r = kInnerR + (kOuterR - kInnerR) * (static_cast<float>(j) / NR);
for (int i = 0; i <= NA; ++i) {
const float a = 2.f * math::PI * (static_cast<float>(i) / NA);
const float x = std::cos(a) * r, z = std::sin(a) * r;
const Vector3 n = normalAt(x, z);
const size_t v = static_cast<size_t>(j) * (NA + 1) + i;
pos[v * 3 + 0] = x;
pos[v * 3 + 1] = heightAt(x, z);
pos[v * 3 + 2] = z;
nrm[v * 3 + 0] = n.x;
nrm[v * 3 + 1] = n.y;
nrm[v * 3 + 2] = n.z;
uv[v * 2 + 0] = static_cast<float>(i) / NA;
uv[v * 2 + 1] = static_cast<float>(j) / NR;
}
});
std::vector<unsigned int> idx;
idx.reserve(static_cast<size_t>(NA) * NR * 6);
for (int j = 0; j < NR; ++j)
for (int i = 0; i < NA; ++i) {
const unsigned a0 = j * (NA + 1) + i;// (i, j)
const unsigned b0 = a0 + 1; // (i+1, j)
const unsigned a1 = a0 + (NA + 1); // (i, j+1)
const unsigned b1 = a1 + 1; // (i+1, j+1)
idx.insert(idx.end(), {a0, b0, b1});
idx.insert(idx.end(), {a0, b1, a1});
}
auto geo = BufferGeometry::create();
geo->setIndex(idx);
geo->setAttribute("position", FloatBufferAttribute::create(pos, 3));
geo->setAttribute("normal", FloatBufferAttribute::create(nrm, 3));
geo->setAttribute("uv", FloatBufferAttribute::create(uv, 2));
geo->computeBoundingBox();
geo->computeBoundingSphere();
auto mat = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.roughness(1.f)// baked map carries the variation
.metalness(0.f));
const BakedMaps maps = bakeMaps();
mat->map = maps.albedo;
mat->roughnessMap = maps.rough;
// Sub-metre granite relief: the framework's world-XZ-anchored tiled
// detail layer replaces the old unique baked normal map. One shared
// heightfield feeds a coherent detail albedo + tangent-normal/roughness
// pair; the Vulkan deferred G-buffer tiles it stochastically, fades it
// with distance (no far shimmer) and switches to triplanar on the ring's
// steep faces so cliffs get unstretched grain automatically. Tuned to
// the old bake's creased-slab + grain granite character: a ~1.7 m repeat
// carries the slab/ledge scale, normalStrength the craggy relief.
// Note vs the old bake: this layer is UNIFORM — it lacks the bespoke
// per-texel canopy/snow damping that softened micro-relief under
// vegetation and snow. detailNormalScale is trimmed slightly to keep
// forested/snow patches from reading over-crisp.
{
terrain::DetailMapOptions dopt;
dopt.dim = 256;
dopt.seed = 1717u;
dopt.albedoContrast = 0.34f;
dopt.chroma = 0.05f;
dopt.normalStrength = 2.4f;// craggier than the terrain default — bare granite
dopt.roughContrast = 0.5f;
const terrain::DetailMaps dm = terrain::makeDetailMaps(dopt);
mat->detailMap = dm.albedo;
mat->detailNormalMap = dm.normalRough;
mat->detailRepeat = 0.6f;// one repeat per ≈1.67 m (slab/ledge scale)
mat->detailStrength = 0.35f;// gentle albedo breakup; the bake carries the colour
mat->detailNormalScale = 0.85f;// trimmed: no per-texel veg/snow damp anymore
mat->detailRoughStrength = 0.5f;
}
auto mesh = Mesh::create(geo, mat);
mesh->frustumCulled = false;// the ring surrounds the camera — always partly in view
return mesh;
}
}// namespace island
// Procedural audio: engine + ocean/wind ambience synthesised to temp WAV, seamlessly looped.
// Seamless loops: periodic terms wrap naturally; a tail crossfade blends filter state at the seam.
namespace {
struct OnePole {
float y = 0.f;
float operator()(float x, float a) {
y += a * (x - y);
return y;
}
};
float lpAlpha(float cutoffHz, int sr) {
return 1.f - std::exp(-2.f * math::PI * cutoffHz / static_cast<float>(sr));
}
std::vector<float> normalized(std::vector<float> s, float peak) {
float m = 0.f;
for (float x : s) m = std::max(m, std::abs(x));
if (m > 1e-6f)
for (float& x : s) x *= peak / m;
return s;
}
// Fold the `extra`-sample overhang back onto the head (linear crossfade).
// out[0] == s[n] so the n-1 → 0 junction is the continuation of the tail;
// by i == extra the signal is back on the head verbatim.
std::vector<float> loopable(const std::vector<float>& s, int n, int extra) {
std::vector<float> out(s.begin(), s.begin() + n);
for (int i = 0; i < extra; ++i) {
const float w = static_cast<float>(i) / static_cast<float>(extra);
out[i] = s[n + i] * (1.f - w) + s[i] * w;
}
return out;
}
// Marine diesel at mid RPM, 2 s loop. Firing rate f0 = 27 Hz (54 exact
// cycles): harmonic stack for the tonal drone, a |sin|³ "chug" envelope
// gating low-passed exhaust noise, and a faint band-passed mechanical
// clatter. Played at rate 0.7 (idle) … 1.6 (full ahead) by the updater.
std::vector<float> synthEngineLoop(int sr = 44100) {
const float dur = 2.0f;
const int n = static_cast<int>(sr * dur);
const int extra = sr / 4;
std::mt19937 r(7);
auto rn = [&] { return std::uniform_real_distribution<float>(-1.f, 1.f)(r); };
const float f0 = 27.f;
OnePole lpExhaust, lpClatHi, lpClatLo;
const float aExhaust = lpAlpha(170.f, sr);
const float aClatHi = lpAlpha(1300.f, sr);
const float aClatLo = lpAlpha(450.f, sr);
std::vector<float> s(n + extra);
for (int i = 0; i < n + extra; ++i) {
const float t = static_cast<float>(i) / sr;
const float chug = std::pow(0.55f + 0.45f * std::abs(std::sin(math::PI * f0 * t)), 3.f);
float tone = 0.f;
tone += std::sin(2.f * math::PI * f0 * t) * 0.55f;
tone += std::sin(2.f * math::PI * 2.f * f0 * t) * 0.30f;
tone += std::sin(2.f * math::PI * 3.f * f0 * t) * 0.16f;
tone += std::sin(2.f * math::PI * 4.f * f0 * t) * 0.09f;
const float w = rn();
const float exhaust = lpExhaust(w, aExhaust) * chug * 1.7f;
const float clatter = (lpClatHi(w, aClatHi) - lpClatLo(w, aClatLo)) * (0.4f + 0.6f * chug) * 0.45f;
s[i] = tone * (0.7f + 0.3f * chug) + exhaust + clatter;
}
return normalized(loopable(s, n, extra), 0.7f);
}
// Rolling sea, 8 s loop: deep low-passed noise swelling on three loop-
// exact LFOs (k/8 Hz), plus a brighter band-passed "wash" that peaks on
// its own sharper envelope — the crest-breaking hiss over the rumble.
std::vector<float> synthOceanLoop(int sr = 44100) {
const float dur = 8.0f;
const int n = static_cast<int>(sr * dur);
const int extra = sr;
std::mt19937 r(11);
auto rn = [&] { return std::uniform_real_distribution<float>(-1.f, 1.f)(r); };
OnePole lpDeep, lpWashHi, lpWashLo;
const float aDeep = lpAlpha(240.f, sr);
const float aWashHi = lpAlpha(1500.f, sr);
const float aWashLo = lpAlpha(500.f, sr);
std::vector<float> s(n + extra);
for (int i = 0; i < n + extra; ++i) {
const float t = static_cast<float>(i) / sr;
float swell = 0.6f * std::sin(2.f * math::PI * 0.125f * t)
+ 0.3f * std::sin(2.f * math::PI * 0.375f * t + 1.7f)
+ 0.1f * std::sin(2.f * math::PI * 0.625f * t + 4.1f);
swell = 0.55f + 0.45f * swell;
const float washEnv = std::pow(0.5f + 0.5f * std::sin(2.f * math::PI * 0.25f * t + 2.6f), 3.f);
const float w = rn();
const float deep = lpDeep(w, aDeep) * swell * 1.0f;
const float wash = (lpWashHi(w, aWashHi) - lpWashLo(w, aWashLo)) * washEnv * 0.55f;
s[i] = deep + wash;
}
return normalized(loopable(s, n, extra), 0.6f);
}
// Wind, 8 s loop. NOT a flat noise band — that reads as TV static. The
// "whoosh" character comes from (a) a NARROW low band whose cutoff SWEEPS
// upward with the gust envelope (the rising pitch of a building gust),
// (b) 12 dB/oct edges — cascaded one-poles; a single pole leaks so much
// above cutoff that the leak IS the white-noise hiss — and (c) a hard
// lull↔gust amplitude swing (gust², near-silent lulls) so it reads as
// weather, not a constant carrier. A faint flutter band rides only the
// gust peaks (gust⁴). Gust LFOs are loop-exact (k/8 Hz).
std::vector<float> synthWindLoop(int sr = 44100) {
const float dur = 8.0f;
const int n = static_cast<int>(sr * dur);
const int extra = sr;
std::mt19937 r(13);
auto rn = [&] { return std::uniform_real_distribution<float>(-1.f, 1.f)(r); };
OnePole hi1, hi2, lo1, lo2, fl1, fl2;
const float aFlHi = lpAlpha(1000.f, sr);
const float aFlLo = lpAlpha(450.f, sr);
std::vector<float> s(n + extra);
for (int i = 0; i < n + extra; ++i) {
const float t = static_cast<float>(i) / sr;
float gust = 0.55f * std::sin(2.f * math::PI * 0.25f * t)
+ 0.30f * std::sin(2.f * math::PI * 0.5f * t + 1.3f)
+ 0.15f * std::sin(2.f * math::PI * 0.875f * t + 4.0f);
gust = std::clamp(0.5f + 0.5f * gust, 0.f, 1.f);
// Swept band: lulls murmur at ~60–180 Hz, full gusts open to
// ~140–620 Hz. The per-sample alpha is driven by the loop-exact
// LFOs, so the sweep itself wraps seamlessly too.
const float aHi = lpAlpha(180.f + 440.f * gust, sr);
const float aLo = lpAlpha(60.f + 80.f * gust, sr);
const float w = rn();
const float band = hi2(hi1(w, aHi), aHi) - lo2(lo1(w, aLo), aLo);
const float whoosh = band * (0.10f + 0.90f * gust * gust);
const float flutter = (fl1(w, aFlHi) - fl2(w, aFlLo)) * gust * gust * gust * gust * 0.18f;
s[i] = whoosh + flutter;
}
return normalized(loopable(s, n, extra), 0.5f);
}
// Engine (spatialised at the stern) + ocean/wind ambience loops, with the
// listener following the camera. Degrades to a no-op when no audio device
// is available; never constructed in headless --shot capture runs.
struct OceanSounds {
std::unique_ptr<AudioListener> listener;
std::unique_ptr<PositionalAudio> engine;
std::unique_ptr<Audio> waves, wind;
bool ok = false;
float rpm_ = 0.f;// smoothed RPM proxy ∈ [0,1] — the engine spools, it doesn't snap
void init() {
try {
const auto dir = std::filesystem::temp_directory_path() / "threepp_ocean_sounds";
std::filesystem::create_directories(dir);
const auto enginePath = dir / "engine_loop.wav";
const auto wavesPath = dir / "waves_loop.wav";
const auto windPath = dir / "wind_loop.wav";
audio::writeWav(enginePath, synthEngineLoop());
audio::writeWav(wavesPath, synthOceanLoop());
audio::writeWav(windPath, synthWindLoop());
listener = std::make_unique<AudioListener>();
// Engine: full volume within ~10 m (the side/deck camera),
// shallow inverse falloff so the chase cam still hears it and
// the far buoy cam gets only a faint distant throb.
engine = std::make_unique<PositionalAudio>(*listener, enginePath);
engine->setDistanceModel(PositionalAudio::DistanceModel::Inverse);
engine->setMinDistance(10.f);
engine->setRolloffFactor(0.5f);
engine->setLooping(true);
engine->setVolume(0.f);
engine->play();
waves = std::make_unique<Audio>(*listener, wavesPath);
waves->setLooping(true);
waves->setVolume(0.f);
waves->play();
wind = std::make_unique<Audio>(*listener, windPath);
wind->setLooping(true);
wind->setVolume(0.f);
wind->play();
ok = true;
} catch (const std::exception& e) {
std::cerr << "[audio] disabled: " << e.what() << "\n";
}
}
// sternWorld: engine mount position. thrusting: the telegraph is
// open — bumps the RPM floor so the engine revs as thrust is
// applied, before boat speed builds.
// uw: smoothed submersion ∈ [0,1] — above-surface sound ducks under
// water (wind almost fully, waves partially, engine least: hull noise
// carries through the water).
void update(float dt, const Vector3& sternWorld, float forwardSpeed,
bool thrusting, float uw, const PerspectiveCamera& cam,
float masterVolume) {
if (!ok) return;
listener->setMasterVolume(masterVolume);
const float speedNorm = std::clamp(std::abs(forwardSpeed) / 8.f, 0.f, 1.f);
float target = 0.18f + 0.82f * speedNorm;
if (thrusting) target = std::max(target, 0.45f);
// Spool up faster than the wind-down coast (turbo lag vs. inertia).
const float tau = target > rpm_ ? 0.9f : 1.8f;
rpm_ += (target - rpm_) * (1.f - std::exp(-dt / tau));
engine->setPlaybackRate((0.7f + 0.9f * rpm_) * (1.f - 0.10f * uw));
engine->setVolume((0.25f + 0.65f * rpm_) * (1.f - 0.35f * uw));
engine->position.copy(sternWorld);
engine->updateMatrixWorld(true);// push the source position to the audio engine
// Slight speed bump on the ambience = apparent wind over the deck.
waves->setVolume((0.45f + 0.10f * speedNorm) * (1.f - 0.55f * uw));
wind->setVolume((0.15f + 0.08f * speedNorm) * (1.f - 0.85f * uw));
listener->position.copy(cam.position);
listener->quaternion.copy(cam.quaternion);
listener->updateMatrixWorld(true);
}
};
}// namespace
int main(int argc, char** argv) {
// --shot <name.png> [--frames N] [--night] [--pt] [--vista] [--close] [--island] [--toggle]
std::string shotPath;
int shotFrames = 240;
bool startNight = false;
bool shotVista = false;
bool shotClose = false;// near-surface grazing view — surface-artifact hunting
bool shotIsland = false;// low close-up of the −X archipelago island (terrain-detail capture)
int toggleNightAt = 0;// --toggle: start in day, flip to night mid-run (exercises the runtime toggle path)
for (int i = 1; i < argc; ++i) {
if (std::strcmp(argv[i], "--shot") == 0 && i + 1 < argc) shotPath = argv[++i];
else if (std::strcmp(argv[i], "--frames") == 0 && i + 1 < argc) shotFrames = std::atoi(argv[++i]);
else if (std::strcmp(argv[i], "--night") == 0) startNight = true;
else if (std::strcmp(argv[i], "--vista") == 0) shotVista = true;
else if (std::strcmp(argv[i], "--close") == 0) shotClose = true;
else if (std::strcmp(argv[i], "--island") == 0) shotIsland = true;
else if (std::strcmp(argv[i], "--toggle") == 0) toggleNightAt = 60;
}
const bool capturing = !shotPath.empty();
// Shared capture machinery (capture_util.hpp): --cam/--look override the
// capture framing below with NO rebuild; --profile dumps per-pass timings.
const capture::Args capArgs = capture::parseArgs(argc, argv);
int shotFrame = 0;
Canvas canvas("Vulkan Ocean", {{"vsync", false}, {"size", WindowSize{1600, 900}}});
auto renderer = VulkanRenderer(canvas);
renderer.setDenoise(true);
renderer.setRestirDIEnabled(true);
renderer.setFireflyClamp(6.0f);
// Render at lower resolution; TAA upsamples to full swapchain by
// accumulating jittered low-res samples into the full-res history.
renderer.setRenderScale(0.9f);
renderer.toneMapping = ToneMapping::ACESFilmic;
renderer.toneMappingExposure = 0.7f;
RGBELoader rgbe;
auto env = rgbe.load(std::string(DATA_FOLDER) +
"/textures/env/autumn_field_puresky_2k.hdr");
Scene scene;
if (env) {
scene.background = env;
scene.environment = env;
}
// Sun-like directional light. The HDR env already contains a sun (the
// renderer's one-sun policy lets a scene DirectionalLight claim it), so
// the directional is mostly here to drive crisp RT sun shadows and the
// volumetric god rays — kept gentle so it doesn't double up with the
// env's own sun on the surface.
auto sun = DirectionalLight::create(Color(1.0f, 0.95f, 0.85f), 2.0f);
sun->position.set(2.f, 1.f, 2.f);
Object3D sunTarget;
sunTarget.position.set(0.f, 0.f, 0.f);
sun->setTarget(sunTarget);
scene.add(sun);
// Sand floor sits directly under the ocean tile and matches its extent:
// making the floor larger leaves a visible sand frame around the water
// when viewed from above (the open-ocean illusion breaks). At the
// edges, rays going past the water plane just hit the env sky, which
// sells "horizon" better than visible beach.
auto floor = Mesh::create(PlaneGeometry::create(kPlaneEdge, kPlaneEdge),
makeSandMaterial());
floor->rotation.x = -math::PI / 2.f;
floor->position.y = -5.f;
scene.add(floor);
// Enclosing archipelago ring — see namespace island above. Static mesh,
// one BLAS build; the lighthouse beam grazes its cliffs at night.
scene.add(island::build());
Ocean::Options oceanOpts;
oceanOpts.size = kTileSize;
oceanOpts.resolution = kOceanRes;
oceanOpts.fftSize = kFftSize;
// windSpeed scales wave amplitude as V⁴ in Phillips, so it's the dominant
// lever for "how big is the sea": 20 m/s = gale, 8–10 = Beaufort 4–5 (1–2 m
// waves, visible chop without overpowering geometry). --close drops to a
// calm glassy sea — the surface-artifact-hunting state.
oceanOpts.windSpeed = shotClose ? 3.5f : 10.0f;
auto ocean = Ocean::create(oceanOpts);
scene.add(ocean);
// ── Lighthouse (scene centre) ───────────────────────────────────────────
// Procedural Norwegian-coast station: skerry → lathe tower → glazed lantern.
// Rotating lamp hood gives the emissive area light the same directionality as the SpotLight.
auto lampMat = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(1.f, 0.95f, 0.8f)).roughness(0.4f).metalness(0.f));
lampMat->emissive = Color(1.f, 0.85f, 0.55f);
lampMat->emissiveIntensity = 0.f;// day: off — night toggle raises it
std::shared_ptr<Mesh> lensHood; // rotated with beamAngle in the render loop
std::shared_ptr<MeshStandardMaterial> hoodMat;// soft lens glow — night toggle drives it
{
auto whitePaint = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(0.92f, 0.90f, 0.86f)).roughness(0.55f).metalness(0.f));
auto redPaint = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(0.70f, 0.10f, 0.08f)).roughness(0.5f).metalness(0.f));
auto ironwork = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(0.07f, 0.075f, 0.08f)).roughness(0.45f).metalness(0.85f));
auto concrete = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(0.55f, 0.54f, 0.52f)).roughness(0.9f).metalness(0.f));
// Granite skerry: flattened sphere with vertices pushed in/out by the
// island FBM — same granite character as the surrounding ring. The
// noise mixes a y-dependent term so the relief isn't vertical ridges.
auto rockMat = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(0.14f, 0.135f, 0.13f)).roughness(0.95f).metalness(0.f));
auto rockGeo = SphereGeometry::create(8.5f, 56, 28);
{
auto* p = rockGeo->getAttribute<float>("position");
for (unsigned i = 0; i < p->count(); ++i) {
const float x = p->getX(i), y = p->getY(i), z = p->getZ(i);
// Two scales: a broad mass term shifts whole flanks in/out, a
// ridged term (fold-over, like the islands') carves crevices
// and sharp spurs so the skerry reads as fractured granite
// rather than a sanded dome.
const float broad = island::fbm(x * 0.16f + 11.f, z * 0.16f + 5.f, 3)
+ 0.5f * island::fbm(y * 0.45f + 3.f, (x + z) * 0.22f + 17.f, 2);
const float ridge = 1.f - std::abs(2.f * island::fbm(x * 0.45f + 31.f, z * 0.45f + 13.f, 3) - 1.f);
const float k = 1.f + 0.55f * (broad / 1.5f - 0.5f) + 0.30f * (ridge * ridge - 0.45f);
p->setXYZ(i, x * k, y * 0.5f * k, z * k);
}
rockGeo->computeVertexNormals();
rockGeo->computeBoundingBox();
rockGeo->computeBoundingSphere();
}
auto rock = Mesh::create(rockGeo, rockMat);
rock->position.set(0.f, -1.8f, 0.f);
scene.add(rock);
// Concrete foundation pads — generous height absorbs the skerry's
// noise variance so the tower and hut always sit on solid footing.
auto pad = Mesh::create(CylinderGeometry::create(3.4f, 3.7f, 1.2f, 32), concrete);
pad->position.set(0.f, 2.0f, 0.f);
scene.add(pad);
auto hutPad = Mesh::create(CylinderGeometry::create(2.5f, 2.9f, 1.6f, 24), concrete);
hutPad->position.set(5.6f, 0.6f, 0.f);
scene.add(hutPad);
// Tower: lathe profile, base at world y = 2.6 (pad top). Plinth with a
// chamfer, concave-tapered shaft, corbel flare carrying the gallery.
constexpr float kTowerBase = 2.6f;
{
const std::vector<Vector2> prof = {
{0.05f, 0.00f}, {2.85f, 0.00f}, {2.85f, 0.85f}, {2.45f, 1.05f},
{2.29f, 3.50f}, {2.04f, 6.50f}, {1.83f, 9.50f},
{1.66f, 12.50f}, {1.52f, 15.00f},
{1.55f, 15.15f}, {1.95f, 15.75f}, {1.95f, 15.90f}};
auto tower = Mesh::create(LatheGeometry::create(prof, 48), whitePaint);
tower->position.y = kTowerBase;
scene.add(tower);
}
// Gallery deck + red watch room below the lantern.
auto deck = Mesh::create(CylinderGeometry::create(2.8f, 2.8f, 0.22f, 32), redPaint);
deck->position.set(0.f, 18.6f, 0.f);
scene.add(deck);
auto watchRoom = Mesh::create(CylinderGeometry::create(1.8f, 1.8f, 0.95f, 24), redPaint);
watchRoom->position.set(0.f, 19.18f, 0.f);
scene.add(watchRoom);
// Lantern glazing: an open thin-walled transmission cylinder — the
// lamp shines through real glass. Radius stays inside the 1.7 m
// offset the render loop pushes the SpotLight origin out to, so the
// spot never starts inside the pane.
auto glassMat = MeshPhysicalMaterial::create();
glassMat->color = Color::white;
glassMat->roughness = 0.03f;
glassMat->metalness = 0.f;
glassMat->transmission = 1.f;
glassMat->setIor(1.52f);
glassMat->thickness = 0.02f;
glassMat->thinWalled = true;
glassMat->side = Side::Double;
auto glazing = Mesh::create(
CylinderGeometry::create(1.45f, 1.45f, 1.4f, 24, 1, true), glassMat);
glazing->position.set(0.f, 20.3f, 0.f);
scene.add(glazing);
// Lantern frame: 8 vertical mullions + top/bottom astragal rings,
// baked into one static geometry (one BLAS instance).
{
std::vector<std::shared_ptr<BufferGeometry>> parts;
for (int i = 0; i < 8; ++i) {
const float a = 2.f * math::PI * (static_cast<float>(i) / 8.f);
auto m = BoxGeometry::create(0.09f, 1.4f, 0.09f);
m->rotateY(a);
m->translate(1.45f * std::sin(a), 20.3f, 1.45f * std::cos(a));
parts.push_back(m);
}
for (const float y : {19.62f, 20.98f}) {
auto ring = TorusGeometry::create(1.45f, 0.06f, 10, 48);
ring->rotateX(math::PI / 2.f);
ring->translate(0.f, y, 0.f);
parts.push_back(ring);
}
scene.add(Mesh::create(mergeBufferGeometries(parts), redPaint));
}
// Lamp + rotating lens assembly. The "hood" is a partial cylinder with
// a ~109° window the render loop yaws to track the beam direction —
// the full lamp blazes through the opening while the shell itself
// glows softly at night (a Fresnel optic leaks light everywhere, it
// doesn't black out the lantern), so the beacon reads from any bearing.
auto lamp = Mesh::create(CylinderGeometry::create(0.5f, 0.5f, 1.0f, 16), lampMat);
lamp->position.set(0.f, 20.3f, 0.f);
scene.add(lamp);
hoodMat = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(0.30f, 0.27f, 0.22f)).roughness(0.4f).metalness(0.2f));
hoodMat->emissive = Color(1.f, 0.85f, 0.55f);
hoodMat->emissiveIntensity = 0.f;// day: off — night toggle raises it
hoodMat->side = Side::Double;
lensHood = Mesh::create(
CylinderGeometry::create(0.8f, 0.8f, 1.25f, 24, 1, true,
0.95f, math::TWO_PI - 1.9f), hoodMat);
lensHood->position.set(0.f, 20.3f, 0.f);
scene.add(lensHood);
// Domed roof + ventilator ball and lightning rod.
{
const std::vector<Vector2> domeProf = {
{1.92f, 0.00f}, {1.78f, 0.42f}, {1.45f, 0.80f},
{0.95f, 1.10f}, {0.42f, 1.32f}, {0.10f, 1.42f}};
auto dome = Mesh::create(LatheGeometry::create(domeProf, 32), redPaint);
dome->position.y = 21.0f;
scene.add(dome);
}
// Gallery railing (18 stanchions + two hoop rails) and the roof
// finial, merged into a single ironwork instance.
{
std::vector<std::shared_ptr<BufferGeometry>> parts;
for (int i = 0; i < 18; ++i) {
const float a = 2.f * math::PI * (static_cast<float>(i) / 18.f);
auto post = CylinderGeometry::create(0.035f, 0.035f, 1.0f, 6);
post->translate(2.62f * std::sin(a), 19.21f, 2.62f * std::cos(a));
parts.push_back(post);
}
for (const float y : {19.40f, 19.70f}) {
auto rail = TorusGeometry::create(2.62f, 0.045f, 8, 48);
rail->rotateX(math::PI / 2.f);
rail->translate(0.f, y, 0.f);
parts.push_back(rail);
}
auto ball = SphereGeometry::create(0.17f, 12, 8);
ball->translate(0.f, 22.55f, 0.f);
parts.push_back(ball);
auto rod = CylinderGeometry::create(0.02f, 0.02f, 0.6f, 6);
rod->translate(0.f, 22.95f, 0.f);
parts.push_back(rod);
scene.add(Mesh::create(mergeBufferGeometries(parts), ironwork));
}
// Door at the plinth and three shaft windows, all facing the hut
// (+X). Window radius follows the shaft taper so each frame sits
// proud of the wall by ~10 cm.
{
auto shaftR = [&](float yWorld) {
const float yl = yWorld - kTowerBase;
return 2.45f - 0.95f * (yl - 1.05f) / 13.95f;
};
std::vector<std::shared_ptr<BufferGeometry>> parts;
auto door = BoxGeometry::create(0.3f, 2.0f, 0.95f);
door->translate(2.45f, 3.6f, 0.f);
parts.push_back(door);
for (const float y : {7.0f, 10.5f, 14.0f}) {
auto win = BoxGeometry::create(0.24f, 0.7f, 0.5f);
win->translate(shaftR(y), y, 0.f);
parts.push_back(win);
}
auto joinery = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(0.05f, 0.10f, 0.08f)).roughness(0.6f).metalness(0.1f));
scene.add(Mesh::create(mergeBufferGeometries(parts), joinery));
}
// Service hut: white walls, red gabled roof (45°-rotated box reads as
// a gable from any playable distance), on its own pad. Yawed a touch
// so it doesn't sit axis-aligned with the door/window azimuth.
const float hutYaw = 0.35f;
auto hut = Mesh::create(BoxGeometry::create(3.0f, 2.2f, 2.4f), whitePaint);
hut->position.set(5.6f, 2.5f, 0.f);
hut->rotation.y = hutYaw;
scene.add(hut);
auto hutRoof = Mesh::create(BoxGeometry::create(1.75f, 1.75f, 3.0f), redPaint);
// Euler XYZ applies Z first: the 45° gable tilt, then the yaw.
hutRoof->rotation.set(0.f, hutYaw, math::PI / 4.f);
hutRoof->position.set(5.6f, 3.55f, 0.f);
scene.add(hutRoof);
}
// Rotating beam — narrow long-throw spot, aimed slightly below horizontal
// so the far end grazes the swells. decay 2 = physical inverse-square; a
// real lighthouse lamp is O(10⁵–10⁶ cd), which is what it takes to light
// water hundreds of metres out. Off by day (intensity 0).
auto beam = SpotLight::create(Color(1.f, 0.92f, 0.72f), 0.f,
/*distance=*/600.f, /*angle=*/math::PI / 40.f,
/*penumbra=*/0.45f, /*decay=*/2.f);
beam->position.set(0.f, 20.3f, 0.f);
Object3D beamTarget;
beamTarget.position.set(300.f, -4.f, 0.f);
beam->setTarget(beamTarget);
scene.add(beam);