-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathvulkan_ocean.cpp
More file actions
2409 lines (2246 loc) · 126 KB
/
Copy pathvulkan_ocean.cpp
File metadata and controls
2409 lines (2246 loc) · 126 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
// Ocean — Vulkan PT demo of FFT-displaced water using DisplacedMesh.
//
// Single-cascade Phillips spectrum + GPU IFFT chain feeds vertex positions
// directly into the BLAS each frame; the path tracer's existing transmission
// BSDF handles refraction, Beer-Lambert absorption, and reflections. A simple
// sandy floor sits below the surface so caustics from the photon-mapping
// pass become visible as the sun moves.
//
// Phase 1 of the WebTide-style ocean integration. Multi-cascade + foam +
// procedural sky come later; for now a single 40 m tile + an HDRI sky is
// enough to validate the geometry pipeline and BLAS-rebuild-per-frame.
#include "threepp/audio/Audio.hpp"
#include "threepp/extras/curves/CatmullRomCurve3.hpp"
#include "threepp/extras/imgui/ImguiContext.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/loaders/GLTFLoader.hpp"
#include "threepp/loaders/RGBELoader.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/renderers/VulkanRenderer.hpp"
#include "threepp/textures/DataTexture.hpp"
#include "threepp/threepp.hpp"
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <execution>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <numeric>
#include <random>
#include <unordered_map>
#include <vector>
using namespace threepp;
namespace {
// Boat input state — captured by KeyListener, polled each frame.
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;
}
};
// Persistent boat state. Position is world, yaw is rotation around +Y
// (heading); pitch and roll are read each frame from wave-surface tilt
// and aren't integrated. Forward speed is along +heading; max ~14 kn
// (~7 m/s) for a research vessel of Gunnerus's size.
struct BoatState {
Vector3 position{0.f, 0.f, 0.f};
float yaw = 0.f; // radians
float forwardSpeed = 0.f; // m/s along +heading
float smoothPitch = 0.f; // radians, low-passed from wave tilt
float smoothRoll = 0.f; // radians
float y = 0.f; // metres, spring-damped toward wave height
float vY = 0.f; // m/s, heave velocity (state for the spring-damper)
};
}// namespace
namespace {
constexpr float kTileSize = 1000.0f; // metres — full mesh extent and cascade-0 tile
constexpr uint32_t kFftSize = 1024; // FFT resolution per cascade — drives wave detail, NOT mesh density.
constexpr float kPlaneEdge = kTileSize; // mesh extends one full FFT tile in X and Z
// Mesh density is decoupled from FFT size: the water_displace.comp samples
// the height texture via normalised UVs (u = i / (gridDim-1)) so the mesh
// can be any tessellation while the wave field stays at kFftSize². Halving
// the subdivision from kFftSize-1 to kFftSize/2-1 drops the vertex count
// from ~1 M to ~262 K — a 4× win on BLAS rebuild/refit and the per-vertex
// displace dispatch, while wave geometry stays crisp (vertex spacing ~2 m
// still resolves λ ≥ 4 m, and Phillips 1/k⁴ puts most energy above that).
constexpr int kSubdiv = static_cast<int>(kFftSize) / 2 - 1;
auto makeOceanMaterial() {
auto mat = MeshPhysicalMaterial::create();
// Pure water has no diffuse pigment — the blue comes from Beer-Lambert
// absorption through the medium, not albedo.
mat->color = Color::white;
// Small roughness simulates the sub-pixel chop the FFT can't resolve.
// 0.04 broadens the specular lobe just enough that each highlight
// covers multiple pixels — converges fast under TAA, avoids the
// salt-and-pepper sparkle that 0.01 + a tight-mip normal map gives
// on distant water.
mat->roughness = 0.04f;
mat->metalness = 0.0f;
mat->setIor(1.33f);
mat->transmission = 1.0f;
// doubleSided + thickness opts this surface into the path tracer's
// thin-shell transmission path: every transmission crossing applies
// Beer-Lambert for `thickness` metres of in-medium depth. The down-
// crossing (camera → water) tints the refracted ray; the up-crossing
// (sand → camera, after bounce) tints again. 2 m × 2 ≈ 4 m of
// effective tint — a tropical-ocean blue that still shows refracted
// sand under the brightest crests. Without doubleSided the BSDF
// would need to use the actual ray distance through the medium
// (~12 m here), which over-saturates to near-black.
mat->side = Side::Double;
mat->thickness = 2.0f;
// Opt this surface into the path tracer's thin-shell BSDF: a single
// FFT-displaced plane has no closed interior, so both faces should
// refract as entries and Beer-Lambert applies per-crossing using
// `thickness` as the in-medium proxy. Without this flag, the back-
// face hit (ray bouncing off sand) would refract using eta=ior with
// gl_HitTEXT = full water column → opaque deep blue, no see-through.
mat->thinWalled = true;
mat->attenuationColor = Color(0.10f, 0.45f, 0.55f);
mat->attenuationDistance = 3.0f;
mat->clearcoat = 0.1;
// Sub-mesh-resolution wave detail comes from the FFT fine cascade
// sampled directly in closest_hit (binding 32 → cascade-2 height,
// gated on `thinWalled`). That animates with the wave field for free
// and replaces the procedural normal map this example used to ship.
return mat;
}
auto makeSandMaterial() {
return MeshStandardMaterial::create(MeshStandardMaterial::Params{}.color(Color(0.02,0.02,0.02)).roughness(1.0f));
}
}// namespace
// ── Procedural enclosing archipelago ────────────────────────────────────────
// A ring of rocky islands around the play area (r ≈ 385–495 m) so the scene
// reads as a sheltered Norwegian skerry bay instead of bare open horizon.
// Deterministic value-noise FBM drives everything: an angular "mass plan"
// (periodic by construction — noise sampled on a circle) picks where islands
// rise and where passes stay open sea; a radial bump profile dives both
// shores below the sand floor so the rims bury cleanly; ridged FBM adds the
// rocky relief. Surface detail comes from three maps baked in the mesh's own
// polar parameterisation (the Vulkan PT has no vertex-colour path for
// meshes): an albedo map layering two granite tones, strata banding, scree,
// grass / heather / lichen niches and a wet waterline; a tangent-space
// normal map carrying creased-slab relief far below the vertex grid; and a
// roughness map that turns the wet band glossy — all derived from the same
// height field.
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 smoothstepf(float e0, float e1, float x) {
const float t = std::clamp((x - e0) / (e1 - e0), 0.f, 1.f);
return t * t * (3.f - 2.f * t);
}
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 = smoothstepf(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: creased-slab ridges (λ ≈ 11 m down to ~3.5 m) — real
// geometry now that the vertex grid resolves it; the baked normal map
// carries only the finer scales (≤ 3 m). Scaled by the mass plan so
// the submerged passes stay smooth sills.
const float slab = 1.f - std::abs(2.f * fbm(x * 0.09f, z * 0.09f, 3) - 1.f);
const float meso = 3.5f * (slab * slab - 0.45f);
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
// 1.2 m radius matches the ~1.4 m vertex grid so the meso slabs shade
// correctly instead of being averaged away.
Vector3 normalAt(float x, float z, float eps = 1.2f) {
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();
}
// High-frequency rock relief for the baked normal map — creased slabs
// (λ ≈ 3 m) plus value-noise grain (λ ≈ 1 m): only the scales below what
// the vertex grid carries (the λ ≥ 3.5 m slabs live in heightAt now).
float detailHeight(float x, float z) {
const float r2 = 1.f - std::abs(2.f * fbm(x * 0.31f + 53.f, z * 0.31f + 17.f, 3) - 1.f);
const float g = fbm(x * 0.9f + 9.f, z * 0.9f + 27.f, 2);
return 0.55f * r2 * r2 + 0.10f * g;
}
struct BakedMaps {
std::shared_ptr<DataTexture> albedo;
std::shared_ptr<DataTexture> normal;
std::shared_ptr<DataTexture> rough;
};
// Albedo + tangent-space normal + roughness in the mesh's polar
// parameterisation (u = angle, v = radius): one texel ≈ 0.7 m of
// coastline, 0.4 m radially. Each texel re-evaluates the height field —
// centre + 4 neighbours give the slope normal AND the Laplacian
// (crest/hollow) from the same five probes — so every mask tracks the
// actual geometry. Rows bake in parallel; the pass is a one-off at startup.
BakedMaps bakeMaps() {
const int W = 4096, H = 256;
std::vector<unsigned char> albPx(static_cast<size_t>(W) * H * 4);
std::vector<unsigned char> nrmPx(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);
std::for_each(std::execution::par, 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 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 = smoothstepf(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;
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) *
smoothstepf(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 = smoothstepf(0.60f, 0.78f, ny) * smoothstepf(1.2f, 2.6f, h) *
(1.f - smoothstepf(5.f, 13.f, h)) *
smoothstepf(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 = smoothstepf(0.45f, 0.62f, ny) * smoothstepf(1.5f, 3.5f, h) *
(1.f - smoothstepf(22.f, 34.f, h)) *
smoothstepf(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 = smoothstepf(0.60f, 0.80f, ny) * smoothstepf(0.8f, 2.6f, h) *
(1.f - smoothstepf(24.f, 38.f, h)) *
smoothstepf(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 = smoothstepf(8.f, 20.f, h) *
smoothstepf(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 * smoothstepf(28.f, 52.f, h);
cr *= alt;
cg *= alt;
cb *= alt;
// algae film straddling the waterline, then the dark wet band
const float algae = (1.f - smoothstepf(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 - smoothstepf(0.4f, 2.2f, h)) * 0.85f;
cr += (0.095f - cr) * wet;
cg += (0.095f - cg) * wet;
cb += (0.09f - cb) * wet;
// detail normal: world-plane gradient of the relief field,
// projected onto the polar tangent frame (T = +u = angular,
// B = +v = radial — matches the shader's derivative TBN).
// Damped under vegetation: soil and moss smooth micro-relief.
const float de = 0.5f;
const float damp = 0.8f * (1.f - 0.65f * std::max(grass, heather));
const float gx = (detailHeight(wx + de, wz) - detailHeight(wx - de, wz)) / (2.f * de) * damp;
const float gz = (detailHeight(wx, wz + de) - detailHeight(wx, wz - de)) / (2.f * de) * damp;
const float st = -gx * sa + gz * ca;// slope along +u (angular)
const float sb = gx * ca + gz * sa; // slope along +v (radial)
const float inv = 1.f / std::sqrt(st * st + sb * sb + 1.f);
// roughness (.g multiplies material roughness): matte dry
// granite, matte vegetation, water-slicked rock turns glossy
float rough = 0.86f + 0.10f * (mottle - 0.5f) - 0.06f * crest;
rough += (0.95f - rough) * std::max(grass, heather);
rough += (0.45f - rough) * wet;
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;
nrmPx[i + 0] = toByte(-st * inv * 0.5f + 0.5f);
nrmPx[i + 1] = toByte(-sb * inv * 0.5f + 0.5f);
nrmPx[i + 2] = toByte(inv * 0.5f + 0.5f);
nrmPx[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(nrmPx), false),
makeTex(std::move(rghPx), false)};
}
std::shared_ptr<Mesh> build() {
// 2048 angular columns ≈ 1.35 m spacing at mid-ring, 64 radial rows
// ≈ 1.7 m — fine enough to resolve the λ ≥ 3.5 m meso slabs in
// heightAt. The seam column is duplicated (u = 0 and u = 1) so UVs
// never wrap. ~133 K verts / ~262 K tris, static BLAS built once;
// rows fill in parallel (≈ 670 K height-field probes).
const int NA = 2048, NR = 64;
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);
std::for_each(std::execution::par, 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->normalMap = maps.normal;
mat->roughnessMap = maps.rough;
auto mesh = Mesh::create(geo, mat);
mesh->frustumCulled = false;// the ring surrounds the camera — always partly in view
return mesh;
}
}// namespace island
// ── Procedural looping audio (engine + ocean/wind ambience) ─────────────────
// Same temp-WAV approach as the Shooter example: the Audio API loads files,
// so the loops are synthesised once at startup and written to the temp dir.
// Seamless looping: every deterministic component (engine harmonics, swell /
// gust LFOs) is given an exact integer number of cycles over the loop length,
// then the synth renders an extra tail whose start is crossfaded back onto
// the head — periodic terms pass through the wrap unchanged while the noise
// and one-pole filter states blend across it.
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;
}
// 16-bit mono PCM WAV writer (verbatim from the Shooter example).
void writeWav(const std::filesystem::path& path, const std::vector<float>& samples, int sr = 44100) {
std::ofstream f(path, std::ios::binary);
auto u32 = [&](uint32_t v) { f.write(reinterpret_cast<char*>(&v), 4); };
auto u16 = [&](uint16_t v) { f.write(reinterpret_cast<char*>(&v), 2); };
const uint32_t dataBytes = static_cast<uint32_t>(samples.size()) * 2u;
f.write("RIFF", 4);
u32(36 + dataBytes);
f.write("WAVE", 4);
f.write("fmt ", 4);
u32(16);
u16(1);// PCM
u16(1);// mono
u32(sr);
u32(sr * 2);
u16(2);
u16(16);
f.write("data", 4);
u32(dataBytes);
for (float x : samples) {
const auto q = static_cast<int16_t>(std::lround(std::clamp(x, -1.f, 1.f) * 32767.f));
f.write(reinterpret_cast<const char*>(&q), 2);
}
}
// 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";
writeWav(enginePath, synthEngineLoop());
writeWav(wavesPath, synthOceanLoop());
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: throttle is open
// (autopilot under way, or W/S held) — 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) {
// ── Headless capture (dev iteration loop) ───────────────────────────────
// vulkan_ocean --shot <name.png> [--frames N] [--night] [--pt] [--vista]
// Fixed aerial camera, N warm-up frames (TAA/denoiser converge), one PNG
// into <project>/aaa_caps/, exit. --night starts in night mode; --pt
// captures the path-traced reference instead of the deferred default;
// --vista frames a high oblique overview (archipelago ring + lighthouse).
std::string shotPath;
int shotFrames = 240;
bool startNight = false;
bool shotPT = false;
bool shotVista = false;
bool shotClose = false;// near-surface grazing view — surface-artifact hunting
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], "--pt") == 0) shotPT = 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], "--toggle") == 0) toggleNightAt = 60;
}
const bool capturing = !shotPath.empty();
int shotFrame = 0;
Canvas canvas("Vulkan PT Ocean", {{"vsync", false}, {"size", WindowSize{1600, 900}}});
VulkanRenderer renderer(canvas);
renderer.setDenoise(true);
renderer.setRestirDIEnabled(true);
renderer.setFireflyClamp(6.0f);
renderer.setMaxBounces(2);
// Trace PT 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;
if (shotPT) renderer.setRenderMode(VulkanRenderer::RenderMode::ReferencePT);// --pt: capture the PT reference
RGBELoader rgbe;
auto env = rgbe.load(std::string(DATA_FOLDER) +
"/textures/env/autumn_field_puresky_2k.hdr");
Scene scene;
scene.background = env;
scene.environment = env;
// Sun-like directional light. The HDR env already contains a sun (env
// CDF + MIS will importance-sample it), so the directional is mostly
// here to drive the photon-mapping caustics pass — 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 surface. PlaneGeometry with kSubdiv segments → kFftSize²
// vertices. The DisplacedMesh detects the grid dimension at first-frame
// init and runs the FFT/displace pipeline against it.
auto oceanGeo = PlaneGeometry::create(kPlaneEdge, kPlaneEdge, kSubdiv, kSubdiv);
oceanGeo->rotateX(-math::PI / 2.f);
auto oceanMat = makeOceanMaterial();
auto ocean = DisplacedMesh::create(oceanGeo, oceanMat);
// Three-cascade FFT:
// tileSize0 = 1000 m → big swells, dominant macro shapes.
// tileSize1 = 100 m → mid-frequency waves filling each swell face.
// tileSize2 = 8 m → fine chop in the 4–8 m range (the rest aliases
// at 1.95 m mesh spacing, but Phillips 1/k⁴ puts
// most energy in the resolvable end).
// The band-pass scheme (PhillipsSpectrum.kMin/kMax in the renderer)
// keeps each cascade in its own k-range so they stack cleanly without
// double-counting wavelengths the adjacent band already covers.
ocean->params.tileSize0 = kTileSize;
ocean->params.tileSize1 = 100.0f;
ocean->params.tileSize2 = 8.0f;
ocean->params.windTheta = 0.6f; // wind slightly off the X axis
// windSpeed scales wave amplitude as V⁴ in Phillips, so it's the
// dominant lever for "how big is the sea": 20 m/s = gale (10 m mountain
// crests, dwarfs the boat), 8–10 = Beaufort 4–5 moderate sea (1–2 m
// waves, visible chop without overpowering geometry). waveScale should
// stay near 1 (physical) — keeping it at 0.1 just attenuates the entire
// multi-cascade detail and reads as a glassy lake.
ocean->params.windSpeed = shotClose ? 3.5f : 10.0f;// --close: calm glassy sea — the artifact-hunting state
ocean->params.waveScale = 1.0f;
ocean->params.choppiness = 0.55f; // sharper crests, more visible wave-folding
// Per-cascade FFT resolution. Cascade-0 (big swells, 1 km tile) needs the
// full kFftSize to keep macro-shape fidelity. Cascades 1 and 2 carry
// shorter wavelengths whose resolvable detail saturates well below the
// cascade-0 resolution — halving them cuts FFT cost ~2× with no visible
// loss.
ocean->params.textureSize0 = kFftSize;
ocean->params.textureSize1 = kFftSize / 2;
ocean->params.textureSize2 = kFftSize / 2;
scene.add(ocean);
// ── Lighthouse (scene centre) ───────────────────────────────────────────
// Rock base + tapered white tower + red gallery + emissive lamp room. The
// LAMP is the night-mode hero: an emissive mesh (area light for the PT /
// deferred emissive paths) + a rotating SpotLight whose beam the deferred
// volumetric march renders as the classic sweeping lighthouse fan.
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
{
auto rockMat = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(0.22f, 0.21f, 0.20f)).roughness(0.95f).metalness(0.f));
auto rock = Mesh::create(CylinderGeometry::create(7.f, 10.f, 6.f, 24), rockMat);
rock->position.set(0.f, -1.f, 0.f);
scene.add(rock);
auto towerMat = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(0.92f, 0.90f, 0.86f)).roughness(0.6f).metalness(0.f));
auto tower = Mesh::create(CylinderGeometry::create(1.9f, 2.8f, 16.f, 24), towerMat);
tower->position.set(0.f, 10.f, 0.f);
scene.add(tower);
auto bandMat = MeshStandardMaterial::create(MeshStandardMaterial::Params{}
.color(Color(0.75f, 0.12f, 0.10f)).roughness(0.6f).metalness(0.f));
auto gallery = Mesh::create(CylinderGeometry::create(2.4f, 2.4f, 1.2f, 24), bandMat);
gallery->position.set(0.f, 18.6f, 0.f);
scene.add(gallery);
auto lamp = Mesh::create(CylinderGeometry::create(1.4f, 1.4f, 2.2f, 16), lampMat);
lamp->position.set(0.f, 20.3f, 0.f);
scene.add(lamp);
auto roof = Mesh::create(CylinderGeometry::create(0.1f, 1.8f, 1.6f, 16), bandMat);
roof->position.set(0.f, 22.2f, 0.f);
scene.add(roof);
}
// 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);
// Dim blue moonlight — direction matches the procedural night env's moon
// disc so shadows and the bright sky spot agree.
auto moon = DirectionalLight::create(Color(0.65f, 0.75f, 1.0f), 0.f);
moon->position.set(-0.55f, 0.60f, 0.35f);
Object3D moonTarget;
moonTarget.position.set(0.f, 0.f, 0.f);
moon->setTarget(moonTarget);
scene.add(moon);
// ── Procedural night sky (equirect, RGBA float) ─────────────────────────
// Deep-blue elevation gradient + faint horizon glow + star field + a moon
// disc bright enough that the PT's env CDF importance-samples it (it acts
// as the night "sun"). Built once; the night toggle swaps scene.environment
// / background and the renderer re-runs PMREM + descriptor rewrites.
std::shared_ptr<Texture> nightEnv;
{
// 2048×1024: one texel ≈ 0.18° ≈ ~4 screen px at this FOV — stars stay
// point-like. At 512² a texel was ~19 px and bilinear magnification
// rendered every star as a big square tent.
const int W = 2048, H = 1024;
std::vector<float> data(static_cast<size_t>(W) * H * 4, 0.f);
// sampleEnvLod maps dir.y=+1 → v=1.0 (zenith = last row).
const Vector3 moonDir = Vector3(-0.55f, 0.60f, 0.35f).normalize();
for (int y = 0; y < H; ++y) {
const float v = (y + 0.5f) / H;
const float elev = (v - 0.5f) * math::PI;// >0 = above horizon
for (int x = 0; x < W; ++x) {
const float u = (x + 0.5f) / W;
const float az = (u - 0.5f) * 2.f * math::PI;
const Vector3 dir(std::cos(elev) * std::cos(az), std::sin(elev),
std::cos(elev) * std::sin(az));
// Sky gradient: near-black zenith → faint blue horizon band;
// below the horizon a dark sea-glow so reflections aren't void.
float r, g, b;
if (elev >= 0.f) {
const float horizon = std::exp(-elev * 4.5f);
r = 0.004f + 0.020f * horizon;
g = 0.006f + 0.028f * horizon;
b = 0.012f + 0.050f * horizon;
} else {
const float fade = std::exp(elev * 6.f);
r = 0.003f * fade; g = 0.004f * fade; b = 0.007f * fade;
}
// Moon disc (~1.7° radius) + soft glow halo.
const float cosToMoon = std::clamp(dir.dot(moonDir), -1.f, 1.f);
const float angTo = std::acos(cosToMoon);
if (angTo < 0.03f) {
r += 28.f; g += 32.f; b += 40.f;
} else {
const float glow = 0.35f * std::exp(-angTo * angTo * 90.f);
r += glow * 0.65f; g += glow * 0.75f; b += glow;
}
const size_t i = (static_cast<size_t>(y) * W + x) * 4;
data[i + 0] = r; data[i + 1] = g; data[i + 2] = b; data[i + 3] = 1.f;
}
}
// NO baked stars: a star is sub-texel at ANY practical env resolution,
// so after bilinear magnification (+ TAA upscale) every baked star
// renders as a ~14 px blob. Stars come from the renderer's procedural
// direction-space star field instead (setDeferredStarfield) — crisp
// points at every resolution/FOV. The env keeps what magnifies well:
// gradient, horizon glow, moon (which also feeds reflections + PT CDF).
Image img{std::move(data), static_cast<unsigned>(W), static_cast<unsigned>(H), 0};
nightEnv = Texture::create(img);
nightEnv->format = Format::RGBA;
nightEnv->type = Type::Float;
nightEnv->colorSpace = ColorSpace::Linear;
nightEnv->mapping = Mapping::EquirectangularReflection;
nightEnv->needsUpdate();
}
// ── Day/night toggle ────────────────────────────────────────────────────
bool night = startNight;
float beamSpeed = 0.45f;// rad/s — ~14 s revolution, classic lighthouse cadence
float beamAngle = 0.f;
float hazeDensity = 0.018f;// σ (1/m) for the deferred volumetric beams
auto applyMode = [&] {
if (night) {
scene.background = nightEnv;
scene.environment = nightEnv;
sun->intensity = 0.f;
moon->intensity = 0.30f;
beam->intensity = 150000.f;// inverse-square: bright enough to read at 300 m
lampMat->emissiveIntensity = 25.f;
renderer.setDeferredVolumetrics(hazeDensity, 0.6f);
renderer.setDeferredStarfield(1.0f);
renderer.toneMappingExposure = 1.15f;
} else {
scene.background = env;
scene.environment = env;
sun->intensity = 2.0f;
moon->intensity = 0.f;
beam->intensity = 0.f;
lampMat->emissiveIntensity = 0.f;
renderer.setDeferredVolumetrics(0.f, 0.6f);
renderer.setDeferredStarfield(0.f);
renderer.toneMappingExposure = 0.7f;
}
// Material PBR values live in a GPU MaterialDesc refreshed on version
// bump — emissiveIntensity is a plain field, so without this the
// day↔night toggle leaves the GPU-side lamp at the OLD emissive:
// no glow, no lamp area light, and (since strongly-emissive housings
// are what shadow rays skip) the housing blocks the beam entirely.
lampMat->needsUpdate();
};
applyMode();