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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
pub mod camera;
pub mod debug;
pub mod figure;
pub mod lod;
pub mod math;
pub mod particle;
pub mod simple;
pub mod smoke_cycle;
pub mod terrain;
pub mod tether;
pub mod trail;

pub use self::{
    camera::{Camera, CameraMode},
    debug::{Debug, DebugShape, DebugShapeId},
    figure::FigureMgr,
    lod::Lod,
    particle::ParticleMgr,
    terrain::{SpriteRenderContextLazy, Terrain},
    tether::TetherMgr,
    trail::TrailMgr,
};
use crate::{
    audio::{ambience, ambience::AmbienceMgr, music::MusicMgr, sfx::SfxMgr, AudioFrontend},
    render::{
        create_skybox_mesh, CloudsLocals, Consts, CullingMode, Drawer, GlobalModel, Globals,
        GlobalsBindGroup, Light, Model, PointLightMatrix, PostProcessLocals, RainOcclusionLocals,
        Renderer, Shadow, ShadowLocals, SkyboxVertex,
    },
    session::PlayerDebugLines,
    settings::Settings,
    window::{AnalogGameInput, Event},
};
use client::Client;
use common::{
    calendar::Calendar,
    comp::{
        self, item::ItemDesc, ship::figuredata::VOXEL_COLLIDER_MANIFEST, slot::EquipSlot,
        tool::ToolKind,
    },
    outcome::Outcome,
    resources::{DeltaTime, TimeOfDay, TimeScale},
    terrain::{BlockKind, TerrainChunk, TerrainGrid},
    vol::ReadVol,
    weather::WeatherGrid,
};
use common_base::{prof_span, span};
use common_state::State;
use comp::item::Reagent;
use hashbrown::HashMap;
use num::traits::{Float, FloatConst};
use specs::{Entity as EcsEntity, Join, LendJoin, WorldExt};
use vek::*;

const ZOOM_CAP_PLAYER: f32 = 1000.0;
const ZOOM_CAP_ADMIN: f32 = 100000.0;

// TODO: Don't hard-code this.
const CURSOR_PAN_SCALE: f32 = 0.005;

pub(crate) const MAX_LIGHT_COUNT: usize = 20; // 31 (total shadow_mats is limited to 128 with default max_uniform_buffer_binding_size)
pub(crate) const MAX_SHADOW_COUNT: usize = 24;
pub(crate) const MAX_POINT_LIGHT_MATRICES_COUNT: usize = MAX_LIGHT_COUNT * 6 + 6;
const NUM_DIRECTED_LIGHTS: usize = 1;
const LIGHT_DIST_RADIUS: f32 = 64.0; // The distance beyond which lights may not emit light from their origin
const SHADOW_DIST_RADIUS: f32 = 8.0;
const SHADOW_MAX_DIST: f32 = 96.0; // The distance beyond which shadows may not be visible
/// The minimum sin γ we will use before switching to uniform mapping.
const EPSILON_UPSILON: f64 = -1.0;

const SHADOW_NEAR: f32 = 0.25; // Near plane for shadow map point light rendering.
const SHADOW_FAR: f32 = 128.0; // Far plane for shadow map point light rendering.

/// Above this speed is considered running
/// Used for first person camera effects
const RUNNING_THRESHOLD: f32 = 0.7;

/// The threashold for starting calculations with rain.
const RAIN_THRESHOLD: f32 = 0.0;

/// is_daylight, array of active lights.
pub type LightData<'a> = (bool, &'a [Light]);

struct EventLight {
    light: Light,
    timeout: f32,
    fadeout: fn(f32) -> f32,
}

struct Skybox {
    model: Model<SkyboxVertex>,
}

pub struct Scene {
    data: GlobalModel,
    globals_bind_group: GlobalsBindGroup,
    camera: Camera,
    camera_input_state: Vec2<f32>,
    event_lights: Vec<EventLight>,

    skybox: Skybox,
    terrain: Terrain<TerrainChunk>,
    pub debug: Debug,
    pub lod: Lod,
    loaded_distance: f32,
    /// x coordinate is sea level (minimum height for any land chunk), and y
    /// coordinate is the maximum height above the mnimimum for any land
    /// chunk.
    map_bounds: Vec2<f32>,
    select_pos: Option<Vec3<i32>>,
    light_data: Vec<Light>,

    particle_mgr: ParticleMgr,
    trail_mgr: TrailMgr,
    figure_mgr: FigureMgr,
    tether_mgr: TetherMgr,
    pub sfx_mgr: SfxMgr,
    pub music_mgr: MusicMgr,
    ambience_mgr: AmbienceMgr,

    integrated_rain_vel: f32,
    wind_vel: Vec2<f32>,
    pub interpolated_time_of_day: Option<f64>,
    last_lightning: Option<(Vec3<f32>, f64)>,
    local_time: f64,

    pub debug_vectors_enabled: bool,
}

pub struct SceneData<'a> {
    pub client: &'a Client,
    pub state: &'a State,
    pub viewpoint_entity: specs::Entity,
    pub mutable_viewpoint: bool,
    pub target_entity: Option<specs::Entity>,
    pub loaded_distance: f32,
    pub terrain_view_distance: u32, // not used currently
    pub entity_view_distance: u32,
    pub tick: u64,
    pub gamma: f32,
    pub exposure: f32,
    pub ambiance: f32,
    pub mouse_smoothing: bool,
    pub sprite_render_distance: f32,
    pub particles_enabled: bool,
    pub weapon_trails_enabled: bool,
    pub flashing_lights_enabled: bool,
    pub figure_lod_render_distance: f32,
    pub is_aiming: bool,
    pub interpolated_time_of_day: Option<f64>,
}

impl<'a> SceneData<'a> {
    pub fn get_sun_dir(&self) -> Vec3<f32> {
        TimeOfDay::new(self.interpolated_time_of_day.unwrap_or(0.0)).get_sun_dir()
    }

    pub fn get_moon_dir(&self) -> Vec3<f32> {
        TimeOfDay::new(self.interpolated_time_of_day.unwrap_or(0.0)).get_moon_dir()
    }
}

/// Approximate a scalar field of view angle using the parameterization from
/// section 4.3 of Lloyd's thesis:
///
/// W_e = 2 n_e tan θ
///
/// where
///
/// W_e = 2 is the width of the image plane (for our projections, since they go
/// from -1 to 1) n_e = near_plane is the near plane for the view frustum
/// θ = (fov / 2) is the half-angle of the FOV (the one passed to
/// Mat4::projection_rh_zo).
///
/// Although the widths for the x and y image planes are the same, they are
/// different in this framework due to the introduction of an aspect ratio:
///
/// y'(p) = 1.0 / tan(fov / 2) * p.y / -p.z
/// x'(p) = 1.0 / (aspect * tan(fov / 2)) * p.x / -p.z
///
/// i.e.
///
/// y'(x, y, -near, w) = 1 / tan(fov / 2) p.y / near
/// x'(x, y, -near, w) = 1 / (aspect * tan(fov / 2)) p.x / near
///
/// W_e,y = 2 * near_plane * tan(fov / 2)
/// W_e,x = 2 * near_plane * aspect * W_e,y
///
/// Θ_x = atan(W_e_y / 2 / near_plane) = atanfov / t()
///
/// i.e. we have an "effective" W_e_x of
///
/// 2 = 2 * near_plane * tan Θ
///
/// atan(1 / near_plane) = θ
///
/// y'
/// x(-near)
/// W_e = 2 * near_plane *
///
/// W_e_y / n_e = tan (fov / 2)
/// W_e_x = 2 n
fn compute_scalar_fov<F: Float>(_near_plane: F, fov: F, aspect: F) -> F {
    let two = F::one() + F::one();
    let theta_y = fov / two;
    let theta_x = (aspect * theta_y.tan()).atan();
    theta_x.min(theta_y)
}

/// Compute a near-optimal warping parameter that helps minimize error in a
/// shadow map.
///
/// See section 5.2 of Brandon Lloyd's thesis:
///
/// [http://gamma.cs.unc.edu/papers/documents/dissertations/lloyd07.pdf](Logarithmic Perspective Shadow Maps).
///
/// η =
///     0                                                         γ < γ_a
///     -1 + (η_b + 1)(1 + cos(90 (γ - γ_a)/(γ_b - γ_a)))   γ_a ≤ γ < γ_b
///     η_b + (η_c - η_b)  sin(90 (γ - γ_b)/(γ_c - γ_b))    γ_b ≤ γ < γ_c
///     η_c                                                 γ_c ≤ γ
///
/// NOTE: Equation's described behavior is *wrong!*  I have pieced together a
/// slightly different function that seems to more closely satisfy the author's
/// intent:
///
/// η =
///     -1                                                        γ < γ_a
///     -1 + (η_b + 1)            (γ - γ_a)/(γ_b - γ_a)     γ_a ≤ γ < γ_b
///     η_b + (η_c - η_b)  sin(90 (γ - γ_b)/(γ_c - γ_b))    γ_b ≤ γ < γ_c
///     η_c                                                 γ_c ≤ γ
///
/// There are other alternatives that may have more desirable properties, such
/// as:
///
/// η =
///     -1                                                        γ < γ_a
///     -1 + (η_b + 1)(1 - cos(90 (γ - γ_a)/(γ_b - γ_a)))   γ_a ≤ γ < γ_b
///     η_b + (η_c - η_b)  sin(90 (γ - γ_b)/(γ_c - γ_b))    γ_b ≤ γ < γ_c
///     η_c                                                 γ_c ≤ γ
fn compute_warping_parameter<F: Float + FloatConst>(
    gamma: F,
    (gamma_a, gamma_b, gamma_c): (F, F, F),
    (eta_b, eta_c): (F, F),
) -> F {
    if gamma < gamma_a {
        -F::one()
        /* F::zero() */
    } else if gamma_a <= gamma && gamma < gamma_b {
        /* -F::one() + (eta_b + F::one()) * (F::one() + (F::FRAC_PI_2() * (gamma - gamma_a) / (gamma_b - gamma_a)).cos()) */
        -F::one() + (eta_b + F::one()) * (F::one() - (F::FRAC_PI_2() * (gamma - gamma_a) / (gamma_b - gamma_a)).cos())
        // -F::one() + (eta_b + F::one()) * ((gamma - gamma_a) / (gamma_b - gamma_a))
    } else if gamma_b <= gamma && gamma < gamma_c {
        eta_b + (eta_c - eta_b) * (F::FRAC_PI_2() * (gamma - gamma_b) / (gamma_c - gamma_b)).sin()
    } else {
        eta_c
    }
    // NOTE: Just in case we go out of range due to floating point imprecision.
    .max(-F::one()).min(F::one())
}

/// Compute a near-optimal warping parameter that falls off quickly enough
/// when the warp angle goes past the minimum field of view angle, for
/// perspective projections.
///
/// For F_p (perspective warping) and view fov angle θ,the parameters are:
///
/// γ_a = θ / 3
/// γ_b = θ
/// γ_c = θ + 0.3(90 - θ)
///
/// η_b = -0.2
/// η_c = 0
///
/// See compute_warping_parameter.
fn compute_warping_parameter_perspective<F: Float + FloatConst>(
    gamma: F,
    near_plane: F,
    fov: F,
    aspect: F,
) -> F {
    let theta = compute_scalar_fov(near_plane, fov, aspect);
    let two = F::one() + F::one();
    let three = two + F::one();
    let ten = three + three + three + F::one();
    compute_warping_parameter(
        gamma,
        (
            theta / three,
            theta,
            theta + (three / ten) * (F::FRAC_PI_2() - theta),
        ),
        (-two / ten, F::zero()),
    )
}

impl Scene {
    /// Create a new `Scene` with default parameters.
    pub fn new(
        renderer: &mut Renderer,
        lazy_init: &mut SpriteRenderContextLazy,
        client: &Client,
        settings: &Settings,
    ) -> Self {
        let resolution = renderer.resolution().map(|e| e as f32);
        let sprite_render_context = lazy_init(renderer);

        let data = GlobalModel {
            globals: renderer.create_consts(&[Globals::default()]),
            lights: renderer.create_consts(&[Light::default(); MAX_LIGHT_COUNT]),
            shadows: renderer.create_consts(&[Shadow::default(); MAX_SHADOW_COUNT]),
            shadow_mats: renderer.create_shadow_bound_locals(&[ShadowLocals::default()]),
            rain_occlusion_mats: renderer
                .create_rain_occlusion_bound_locals(&[RainOcclusionLocals::default()]),
            point_light_matrices: Box::new(
                [PointLightMatrix::default(); MAX_POINT_LIGHT_MATRICES_COUNT],
            ),
        };

        let lod = Lod::new(renderer, client, settings);

        let globals_bind_group = renderer.bind_globals(&data, lod.get_data());

        let terrain = Terrain::new(renderer, &data, lod.get_data(), sprite_render_context);

        let camera_mode = match client.presence() {
            Some(comp::PresenceKind::Spectator) => CameraMode::Freefly,
            _ => CameraMode::ThirdPerson,
        };

        let calendar = client.state().ecs().read_resource::<Calendar>();

        Self {
            data,
            globals_bind_group,
            camera: Camera::new(resolution.x / resolution.y, camera_mode),
            camera_input_state: Vec2::zero(),
            event_lights: Vec::new(),

            skybox: Skybox {
                model: renderer.create_model(&create_skybox_mesh()).unwrap(),
            },
            terrain,
            debug: Debug::new(),
            lod,
            loaded_distance: 0.0,
            map_bounds: Vec2::new(
                client.world_data().min_chunk_alt(),
                client.world_data().max_chunk_alt(),
            ),
            select_pos: None,
            light_data: Vec::new(),
            particle_mgr: ParticleMgr::new(renderer),
            trail_mgr: TrailMgr::default(),
            figure_mgr: FigureMgr::new(renderer),
            tether_mgr: TetherMgr::new(renderer),
            sfx_mgr: SfxMgr::default(),
            music_mgr: MusicMgr::new(&calendar),
            ambience_mgr: AmbienceMgr {
                ambience: ambience::load_ambience_items(),
            },
            integrated_rain_vel: 0.0,
            wind_vel: Vec2::zero(),
            interpolated_time_of_day: None,
            last_lightning: None,
            local_time: 0.0,
            debug_vectors_enabled: false,
        }
    }

    /// Get a reference to the scene's globals.
    pub fn globals(&self) -> &Consts<Globals> { &self.data.globals }

    /// Get a reference to the scene's camera.
    pub fn camera(&self) -> &Camera { &self.camera }

    /// Get a reference to the scene's terrain.
    pub fn terrain(&self) -> &Terrain<TerrainChunk> { &self.terrain }

    /// Get a reference to the scene's lights.
    pub fn lights(&self) -> &Vec<Light> { &self.light_data }

    /// Get a reference to the scene's particle manager.
    pub fn particle_mgr(&self) -> &ParticleMgr { &self.particle_mgr }

    /// Get a reference to the scene's trail manager.
    pub fn trail_mgr(&self) -> &TrailMgr { &self.trail_mgr }

    /// Get a reference to the scene's figure manager.
    pub fn figure_mgr(&self) -> &FigureMgr { &self.figure_mgr }

    pub fn music_mgr(&self) -> &MusicMgr { &self.music_mgr }

    /// Get a mutable reference to the scene's camera.
    pub fn camera_mut(&mut self) -> &mut Camera { &mut self.camera }

    /// Set the block position that the player is interacting with
    pub fn set_select_pos(&mut self, pos: Option<Vec3<i32>>) { self.select_pos = pos; }

    pub fn select_pos(&self) -> Option<Vec3<i32>> { self.select_pos }

    /// Handle an incoming user input event (e.g.: cursor moved, key pressed,
    /// window closed).
    ///
    /// If the event is handled, return true.
    pub fn handle_input_event(&mut self, event: Event, client: &Client) -> bool {
        match event {
            // When the window is resized, change the camera's aspect ratio
            Event::Resize(dims) => {
                self.camera.set_aspect_ratio(dims.x as f32 / dims.y as f32);
                true
            },
            // Panning the cursor makes the camera rotate
            Event::CursorPan(delta) => {
                self.camera.rotate_by(Vec3::from(delta) * CURSOR_PAN_SCALE);
                true
            },
            // Zoom the camera when a zoom event occurs
            Event::Zoom(delta) => {
                let cap = if client.is_moderator() {
                    ZOOM_CAP_ADMIN
                } else {
                    ZOOM_CAP_PLAYER
                };
                // when zooming in the distance the camera travelles should be based on the
                // final distance. This is to make sure the camera travelles the
                // same distance when zooming in and out
                let player_scale = client
                    .state()
                    .read_component_copied::<comp::Scale>(client.entity())
                    .map_or(1.0, |s| s.0);
                if delta < 0.0 {
                    self.camera.zoom_switch(
                        // Thank you Imbris for doing the math
                        delta * (0.05 + self.camera.get_distance() * 0.01) / (1.0 - delta * 0.01),
                        cap,
                        player_scale,
                    );
                } else {
                    self.camera.zoom_switch(
                        delta * (0.05 + self.camera.get_distance() * 0.01),
                        cap,
                        player_scale,
                    );
                }
                true
            },
            Event::AnalogGameInput(input) => match input {
                AnalogGameInput::CameraX(d) => {
                    self.camera_input_state.x = d;
                    true
                },
                AnalogGameInput::CameraY(d) => {
                    self.camera_input_state.y = d;
                    true
                },
                _ => false,
            },
            // All other events are unhandled
            _ => false,
        }
    }

    pub fn handle_outcome(
        &mut self,
        outcome: &Outcome,
        scene_data: &SceneData,
        audio: &mut AudioFrontend,
    ) {
        span!(_guard, "handle_outcome", "Scene::handle_outcome");
        self.particle_mgr
            .handle_outcome(outcome, scene_data, &self.figure_mgr);
        self.sfx_mgr
            .handle_outcome(outcome, audio, scene_data.client);

        match outcome {
            Outcome::Lightning { pos } => {
                self.last_lightning = Some((*pos, scene_data.state.get_time()));
            },
            Outcome::Explosion {
                pos,
                power,
                is_attack,
                reagent,
                ..
            } => self.event_lights.push(EventLight {
                light: Light::new(
                    *pos,
                    match reagent {
                        Some(Reagent::Blue) => Rgb::new(0.15, 0.4, 1.0),
                        Some(Reagent::Green) => Rgb::new(0.0, 1.0, 0.0),
                        Some(Reagent::Purple) => Rgb::new(0.7, 0.0, 1.0),
                        Some(Reagent::Red) => {
                            if *is_attack {
                                Rgb::new(1.0, 0.5, 0.0)
                            } else {
                                Rgb::new(1.0, 0.0, 0.0)
                            }
                        },
                        Some(Reagent::Phoenix) => Rgb::new(1.0, 0.8, 0.3),
                        Some(Reagent::White) => Rgb::new(1.0, 1.0, 1.0),
                        Some(Reagent::Yellow) => Rgb::new(1.0, 1.0, 0.0),
                        None => Rgb::new(1.0, 0.5, 0.0),
                    },
                    power
                        * if *is_attack || reagent.is_none() {
                            2.5
                        } else {
                            5.0
                        },
                ),
                timeout: match reagent {
                    Some(_) => 1.0,
                    None => 0.5,
                },
                fadeout: |timeout| timeout * 2.0,
            }),
            Outcome::ProjectileShot { .. } => {},
            _ => {},
        }
    }

    /// Maintain data such as GPU constant buffers, models, etc. To be called
    /// once per tick.
    pub fn maintain(
        &mut self,
        renderer: &mut Renderer,
        audio: &mut AudioFrontend,
        scene_data: &SceneData,
        client: &Client,
        settings: &Settings,
    ) {
        span!(_guard, "maintain", "Scene::maintain");
        // Get player position.
        let ecs = scene_data.state.ecs();

        let dt = ecs.fetch::<DeltaTime>().0;

        self.local_time += dt as f64 * ecs.fetch::<TimeScale>().0;

        let positions = ecs.read_storage::<comp::Pos>();

        let viewpoint_pos = if let Some(viewpoint_pos) =
            positions.get(scene_data.viewpoint_entity).map(|pos| pos.0)
        {
            let viewpoint_ori = ecs
                .read_storage::<comp::Ori>()
                .get(scene_data.viewpoint_entity)
                .map_or(Quaternion::identity(), |ori| ori.to_quat());

            let viewpoint_look_ori = ecs
                .read_storage::<comp::CharacterActivity>()
                .get(scene_data.viewpoint_entity)
                .and_then(|activity| activity.look_dir)
                .map(|dir| {
                    let d = dir.to_vec();

                    let pitch = (-d.z).asin();
                    let yaw = d.x.atan2(d.y);

                    Vec3::new(yaw, pitch, 0.0)
                })
                .unwrap_or_else(|| {
                    let q = viewpoint_ori;
                    let sinr_cosp = 2.0 * (q.w * q.x + q.y * q.z);
                    let cosr_cosp = 1.0 - 2.0 * (q.x * q.x + q.y * q.y);
                    let pitch = sinr_cosp.atan2(cosr_cosp);

                    let siny_cosp = 2.0 * (q.w * q.z + q.x * q.y);
                    let cosy_cosp = 1.0 - 2.0 * (q.y * q.y + q.z * q.z);
                    let yaw = siny_cosp.atan2(cosy_cosp);

                    Vec3::new(-yaw, -pitch, 0.0)
                });

            let viewpoint_scale = ecs
                .read_storage::<comp::Scale>()
                .get(scene_data.viewpoint_entity)
                .map_or(1.0, |scale| scale.0);

            let (is_humanoid, viewpoint_height, viewpoint_eye_height) = ecs
                .read_storage::<comp::Body>()
                .get(scene_data.viewpoint_entity)
                .map_or((false, 1.0, 0.0), |b| {
                    (
                        matches!(b, comp::Body::Humanoid(_)),
                        b.height(),
                        b.eye_height(1.0), // Scale is applied later
                    )
                });

            if scene_data.mutable_viewpoint || matches!(self.camera.get_mode(), CameraMode::Freefly)
            {
                // Add the analog input to camera if it's a mutable viewpoint
                self.camera
                    .rotate_by(Vec3::from([self.camera_input_state.x, 0.0, 0.0]));
                self.camera
                    .rotate_by(Vec3::from([0.0, self.camera_input_state.y, 0.0]));
            } else {
                // Otherwise set the cameras rotation to the viewpoints
                self.camera.set_orientation(viewpoint_look_ori);
            }

            let viewpoint_offset = if is_humanoid {
                let viewpoint_rolling = ecs
                    .read_storage::<comp::CharacterState>()
                    .get(scene_data.viewpoint_entity)
                    .map_or(false, |cs| cs.is_dodge());

                let is_running = ecs
                    .read_storage::<comp::Vel>()
                    .get(scene_data.viewpoint_entity)
                    .zip(
                        ecs.read_storage::<comp::PhysicsState>()
                            .get(scene_data.viewpoint_entity),
                    )
                    .map(|(v, ps)| {
                        (v.0 - ps.ground_vel).magnitude_squared() > RUNNING_THRESHOLD.powi(2)
                    })
                    .unwrap_or(false);

                let on_ground = ecs
                    .read_storage::<comp::PhysicsState>()
                    .get(scene_data.viewpoint_entity)
                    .map(|p| p.on_ground.is_some());

                let player_entity = client.entity();
                let holding_ranged = client
                    .inventories()
                    .get(player_entity)
                    .and_then(|inv| inv.equipped(EquipSlot::ActiveMainhand))
                    .and_then(|item| item.tool_info())
                    .is_some_and(|tool_kind| {
                        matches!(
                            tool_kind,
                            ToolKind::Bow | ToolKind::Staff | ToolKind::Sceptre
                        )
                    });

                let up = match self.camera.get_mode() {
                    CameraMode::FirstPerson => {
                        if viewpoint_rolling {
                            viewpoint_height * 0.42
                        } else if is_running && on_ground.unwrap_or(false) {
                            viewpoint_eye_height
                                + (scene_data.state.get_time() as f32 * 17.0).sin() * 0.05
                        } else {
                            viewpoint_eye_height
                        }
                    },
                    CameraMode::ThirdPerson if scene_data.is_aiming && holding_ranged => {
                        viewpoint_height * 1.16 + settings.gameplay.aim_offset_y
                    },
                    CameraMode::ThirdPerson if scene_data.is_aiming => viewpoint_height * 1.16,
                    CameraMode::ThirdPerson => viewpoint_eye_height,
                    CameraMode::Freefly => 0.0,
                };

                let right = match self.camera.get_mode() {
                    CameraMode::FirstPerson => 0.0,
                    CameraMode::ThirdPerson if scene_data.is_aiming && holding_ranged => {
                        settings.gameplay.aim_offset_x
                    },
                    CameraMode::ThirdPerson => 0.0,
                    CameraMode::Freefly => 0.0,
                };

                // Alter camera position to match player.
                let tilt = self.camera.get_orientation().y;
                let dist = self.camera.get_distance();

                Vec3::unit_z() * (up * viewpoint_scale - tilt.min(0.0).sin() * dist * 0.6)
                    + self.camera.right() * (right * viewpoint_scale)
            } else {
                self.figure_mgr
                    .viewpoint_offset(scene_data, scene_data.viewpoint_entity)
            };

            match self.camera.get_mode() {
                CameraMode::FirstPerson | CameraMode::ThirdPerson => {
                    self.camera.set_focus_pos(viewpoint_pos + viewpoint_offset);
                },
                CameraMode::Freefly => {},
            };

            // Tick camera for interpolation.
            self.camera
                .update(scene_data.state.get_time(), dt, scene_data.mouse_smoothing);
            viewpoint_pos
        } else {
            Vec3::zero()
        };

        // Compute camera matrices.
        self.camera.compute_dependents(&scene_data.state.terrain());
        let camera::Dependents {
            view_mat,
            view_mat_inv,
            proj_mat,
            proj_mat_inv,
            cam_pos,
            ..
        } = self.camera.dependents();

        // Update chunk loaded distance smoothly for nice shader fog
        let loaded_distance =
            (0.98 * self.loaded_distance + 0.02 * scene_data.loaded_distance).max(0.01);

        // Reset lights ready for the next tick
        let lights = &mut self.light_data;
        lights.clear();

        // Maintain the particles.
        self.particle_mgr.maintain(
            renderer,
            scene_data,
            &self.terrain,
            &self.figure_mgr,
            lights,
        );

        // Maintain the trails.
        self.trail_mgr.maintain(renderer, scene_data);

        // Update light constants
        let max_light_dist = loaded_distance.powi(2) + LIGHT_DIST_RADIUS;
        lights.extend(
            (
                &scene_data.state.ecs().read_storage::<comp::Pos>(),
                scene_data
                    .state
                    .ecs()
                    .read_storage::<crate::ecs::comp::Interpolated>()
                    .maybe(),
                &scene_data
                    .state
                    .ecs()
                    .read_storage::<comp::LightAnimation>(),
                scene_data
                    .state
                    .ecs()
                    .read_storage::<comp::Health>()
                    .maybe(),
            )
                .join()
                .filter(|(pos, _, light_anim, h)| {
                    light_anim.col != Rgb::zero()
                        && light_anim.strength > 0.0
                        && pos.0.distance_squared(viewpoint_pos) < max_light_dist
                        && h.map_or(true, |h| !h.is_dead)
                })
                .map(|(pos, interpolated, light_anim, _)| {
                    // Use interpolated values if they are available
                    let pos = interpolated.map_or(pos.0, |i| i.pos);
                    Light::new(pos + light_anim.offset, light_anim.col, light_anim.strength)
                })
                .chain(
                    self.event_lights
                        .iter()
                        .map(|el| el.light.with_strength((el.fadeout)(el.timeout))),
                ),
        );
        let voxel_colliders_manifest = VOXEL_COLLIDER_MANIFEST.read();
        let figure_mgr = &self.figure_mgr;
        lights.extend(
            (
                &scene_data.state.ecs().entities(),
                &scene_data
                    .state
                    .read_storage::<crate::ecs::comp::Interpolated>(),
                &scene_data.state.read_storage::<comp::Body>(),
                &scene_data.state.read_storage::<comp::Collider>(),
            )
                .join()
                .filter_map(|(entity, interpolated, body, collider)| {
                    let vol = collider.get_vol(&voxel_colliders_manifest)?;
                    let (blocks_of_interest, offset) =
                        figure_mgr.get_blocks_of_interest(entity, body, Some(collider))?;

                    let mat = Mat4::from(interpolated.ori.to_quat())
                        .translated_3d(interpolated.pos)
                        * Mat4::translation_3d(offset);

                    let p = mat.inverted().mul_point(viewpoint_pos);
                    let aabb = Aabb {
                        min: Vec3::zero(),
                        max: vol.volume().sz.as_(),
                    };
                    if aabb.contains_point(p) || aabb.distance_to_point(p) < max_light_dist {
                        Some(
                            blocks_of_interest
                                .lights
                                .iter()
                                .map(move |(block_offset, level)| {
                                    let wpos = mat.mul_point(block_offset.as_() + 0.5);
                                    (wpos, level)
                                })
                                .filter(move |(wpos, _)| {
                                    wpos.distance_squared(viewpoint_pos) < max_light_dist
                                })
                                .map(|(wpos, level)| {
                                    Light::new(wpos, Rgb::white(), *level as f32 / 7.0)
                                }),
                        )
                    } else {
                        None
                    }
                })
                .flatten(),
        );
        lights.sort_by_key(|light| light.get_pos().distance_squared(viewpoint_pos) as i32);
        lights.truncate(MAX_LIGHT_COUNT);
        renderer.update_consts(&mut self.data.lights, lights);

        // Update event lights
        self.event_lights.retain_mut(|el| {
            el.timeout -= dt;
            el.timeout > 0.0
        });

        // Update shadow constants
        let mut shadows = (
            &scene_data.state.ecs().read_storage::<comp::Pos>(),
            scene_data
                .state
                .ecs()
                .read_storage::<crate::ecs::comp::Interpolated>()
                .maybe(),
            scene_data.state.ecs().read_storage::<comp::Scale>().maybe(),
            &scene_data.state.ecs().read_storage::<comp::Body>(),
            &scene_data.state.ecs().read_storage::<comp::Health>(),
        )
            .join()
            .filter(|(_, _, _, _, health)| !health.is_dead)
            .filter(|(pos, _, _, _, _)| {
                pos.0.distance_squared(viewpoint_pos)
                    < (loaded_distance.min(SHADOW_MAX_DIST) + SHADOW_DIST_RADIUS).powi(2)
            })
            .map(|(pos, interpolated, scale, _, _)| {
                Shadow::new(
                    // Use interpolated values pos if it is available
                    interpolated.map_or(pos.0, |i| i.pos),
                    scale.map_or(1.0, |s| s.0),
                )
            })
            .collect::<Vec<_>>();
        shadows.sort_by_key(|shadow| shadow.get_pos().distance_squared(viewpoint_pos) as i32);
        shadows.truncate(MAX_SHADOW_COUNT);
        renderer.update_consts(&mut self.data.shadows, &shadows);

        // Remember to put the new loaded distance back in the scene.
        self.loaded_distance = loaded_distance;

        // Update light projection matrices for the shadow map.

        // When the target time of day and time of day have a large discrepancy
        // (i.e two days), the linear interpolation causes brght flashing effects
        // in the sky. This will snap the time of day to the target time of day
        // for the client to avoid the flashing effect if flashing lights is
        // disabled.
        const DAY: f64 = 60.0 * 60.0 * 24.0;
        let time_of_day = scene_data.state.get_time_of_day();
        let max_lerp_period = if scene_data.flashing_lights_enabled {
            DAY * 2.0
        } else {
            DAY * 0.25
        };
        self.interpolated_time_of_day =
            Some(self.interpolated_time_of_day.map_or(time_of_day, |tod| {
                if (tod - time_of_day).abs() > max_lerp_period {
                    time_of_day
                } else {
                    Lerp::lerp(tod, time_of_day, dt as f64)
                }
            }));
        let time_of_day = self.interpolated_time_of_day.unwrap_or(time_of_day);
        let focus_pos = self.camera.get_focus_pos();
        let focus_off = focus_pos.map(|e| e.trunc());

        // Update global constants.
        renderer.update_consts(&mut self.data.globals, &[Globals::new(
            view_mat,
            proj_mat,
            cam_pos,
            focus_pos,
            self.loaded_distance,
            self.lod.get_data().tgt_detail as f32,
            self.map_bounds,
            time_of_day,
            scene_data.state.get_time(),
            self.local_time,
            renderer.resolution().as_(),
            Vec2::new(SHADOW_NEAR, SHADOW_FAR),
            lights.len(),
            shadows.len(),
            NUM_DIRECTED_LIGHTS,
            scene_data
                .state
                .terrain()
                .get((cam_pos + focus_off).map(|e| e.floor() as i32))
                .ok()
                // Don't block the camera's view in solid blocks if the player is a moderator
                .filter(|b| !(b.is_filled() && client.is_moderator()))
                .map(|b| b.kind())
                .unwrap_or(BlockKind::Air),
            self.select_pos.map(|e| e - focus_off.map(|e| e as i32)),
            scene_data.gamma,
            scene_data.exposure,
            self.last_lightning.unwrap_or((Vec3::zero(), -1000.0)),
            self.wind_vel,
            scene_data.ambiance,
            self.camera.get_mode(),
            scene_data.sprite_render_distance - 20.0,
        )]);
        renderer.update_clouds_locals(CloudsLocals::new(proj_mat_inv, view_mat_inv));
        renderer.update_postprocess_locals(PostProcessLocals::new(proj_mat_inv, view_mat_inv));

        // Maintain LoD.
        self.lod.maintain(renderer, client, focus_pos, &self.camera);

        // Maintain tethers.
        self.tether_mgr.maintain(renderer, client, focus_pos);

        // Maintain debug shapes
        self.debug.maintain(renderer);

        // Maintain the terrain.
        let (
            _visible_bounds,
            visible_light_volume,
            visible_psr_bounds,
            visible_occlusion_volume,
            visible_por_bounds,
        ) = self.terrain.maintain(
            renderer,
            scene_data,
            focus_pos,
            self.loaded_distance,
            &self.camera,
        );

        // Maintain the figures.
        let _figure_bounds = self.figure_mgr.maintain(
            renderer,
            &mut self.trail_mgr,
            scene_data,
            visible_psr_bounds,
            visible_por_bounds,
            &self.camera,
            Some(&self.terrain),
        );

        let fov = self.camera.get_effective_fov();
        let aspect_ratio = self.camera.get_aspect_ratio();
        let view_dir = ((focus_pos.map(f32::fract)) - cam_pos).normalized();

        // We need to compute these offset matrices to transform world space coordinates
        // to the translated ones we use when multiplying by the light space
        // matrix; this helps avoid precision loss during the
        // multiplication.
        let look_at = math::Vec3::from(cam_pos);
        let new_dir = math::Vec3::from(view_dir);
        let new_dir = new_dir.normalized();
        let up: math::Vec3<f32> = math::Vec3::unit_y();

        // Optimal warping for directed lights:
        //
        // n_opt = 1 / sin y (z_n + √(z_n + (f - n) sin y))
        //
        // where n is near plane, f is far plane, y is the tilt angle between view and
        // light direction, and n_opt is the optimal near plane.
        // We also want a way to transform and scale this matrix (* 0.5 + 0.5) in order
        // to transform it correctly into texture coordinates, as well as
        // OpenGL coordinates.  Note that the matrix for directional light
        // is *already* linear in the depth buffer.
        //
        // Also, observe that we flip the texture sampling matrix in order to account
        // for the fact that DirectX renders top-down.
        let texture_mat = Mat4::<f32>::scaling_3d::<Vec3<f32>>(Vec3::new(0.5, -0.5, 1.0))
            * Mat4::translation_3d(Vec3::new(1.0, -1.0, 0.0));

        let directed_mats = |d_view_mat: math::Mat4<f32>,
                             d_dir: math::Vec3<f32>,
                             volume: &Vec<math::Vec3<f32>>|
         -> (Mat4<f32>, Mat4<f32>) {
            // NOTE: Light view space, right-handed.
            let v_p_orig = math::Vec3::from(d_view_mat * math::Vec4::from_direction(new_dir));
            let mut v_p = v_p_orig.normalized();
            let cos_gamma = new_dir.map(f64::from).dot(d_dir.map(f64::from));
            let sin_gamma = (1.0 - cos_gamma * cos_gamma).sqrt();
            let gamma = sin_gamma.asin();
            let view_mat = math::Mat4::from_col_array(view_mat.into_col_array());
            // coordinates are transformed from world space (right-handed) to view space
            // (right-handed).
            let bounds1 = math::fit_psr(
                view_mat.map_cols(math::Vec4::from),
                volume.iter().copied(),
                math::Vec4::homogenized,
            );
            let n_e = f64::from(-bounds1.max.z);
            let factor = compute_warping_parameter_perspective(
                gamma,
                n_e,
                f64::from(fov),
                f64::from(aspect_ratio),
            );

            v_p.z = 0.0;
            v_p.normalize();
            let l_r: math::Mat4<f32> = if factor > EPSILON_UPSILON {
                // NOTE: Our coordinates are now in left-handed space, but v_p isn't; however,
                // v_p has no z component, so we don't have to adjust it for left-handed
                // spaces.
                math::Mat4::look_at_lh(math::Vec3::zero(), math::Vec3::unit_z(), v_p)
            } else {
                math::Mat4::identity()
            };
            // Convert from right-handed to left-handed coordinates.
            let directed_proj_mat = math::Mat4::new(
                1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
            );

            let light_all_mat = l_r * directed_proj_mat * d_view_mat;
            // coordinates are transformed from world space (right-handed) to rotated light
            // space (left-handed).
            let bounds0 = math::fit_psr(
                light_all_mat,
                volume.iter().copied(),
                math::Vec4::homogenized,
            );
            // Vague idea: project z_n from the camera view to the light view (where it's
            // tilted by γ).
            //
            // NOTE: To transform a normal by M, we multiply by the transpose of the inverse
            // of M. For the cases below, we are transforming by an
            // already-inverted matrix, so the transpose of its inverse is
            // just the transpose of the original matrix.
            let (z_0, z_1) = {
                let f_e = f64::from(-bounds1.min.z).max(n_e);
                // view space, right-handed coordinates.
                let p_z = bounds1.max.z;
                // rotated light space, left-handed coordinates.
                let p_y = bounds0.min.y;
                let p_x = bounds0.center().x;
                // moves from view-space (right-handed) to world space (right-handed)
                let view_inv = view_mat.inverted();
                // moves from rotated light space (left-handed) to world space (right-handed).
                let light_all_inv = light_all_mat.inverted();

                // moves from view-space (right-handed) to world-space (right-handed).
                let view_point = view_inv
                    * math::Vec4::from_point(
                        -math::Vec3::unit_z() * p_z, /* + math::Vec4::unit_w() */
                    );
                let view_plane = view_mat.transposed() * -math::Vec4::unit_z();

                // moves from rotated light space (left-handed) to world space (right-handed).
                let light_point = light_all_inv
                    * math::Vec4::from_point(
                        math::Vec3::unit_y() * p_y, /* + math::Vec4::unit_w() */
                    );
                let light_plane = light_all_mat.transposed() * math::Vec4::unit_y();

                // moves from rotated light space (left-handed) to world space (right-handed).
                let shadow_point = light_all_inv
                    * math::Vec4::from_point(
                        math::Vec3::unit_x() * p_x, /* + math::Vec4::unit_w() */
                    );
                let shadow_plane = light_all_mat.transposed() * math::Vec4::unit_x();

                // Find the point at the intersection of the three planes; note that since the
                // equations are already in right-handed world space, we don't need to negate
                // the z coordinates.
                let solve_p0 = math::Mat4::new(
                    view_plane.x,
                    view_plane.y,
                    view_plane.z,
                    0.0,
                    light_plane.x,
                    light_plane.y,
                    light_plane.z,
                    0.0,
                    shadow_plane.x,
                    shadow_plane.y,
                    shadow_plane.z,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    1.0,
                );

                // in world-space (right-handed).
                let plane_dist = math::Vec4::new(
                    view_plane.dot(view_point),
                    light_plane.dot(light_point),
                    shadow_plane.dot(shadow_point),
                    1.0,
                );
                let p0_world = solve_p0.inverted() * plane_dist;
                // in rotated light-space (left-handed).
                let p0 = light_all_mat * p0_world;
                let mut p1 = p0;
                // in rotated light-space (left-handed).
                p1.y = bounds0.max.y;

                // transforms from rotated light-space (left-handed) to view space
                // (right-handed).
                let view_from_light_mat = view_mat * light_all_inv;
                // z0 and z1 are in view space (right-handed).
                let z0 = view_from_light_mat * p0;
                let z1 = view_from_light_mat * p1;

                // Extract the homogenized forward component (right-handed).
                //
                // NOTE: I don't think the w component should be anything but 1 here, but
                // better safe than sorry.
                (
                    f64::from(z0.homogenized().dot(-math::Vec4::unit_z())).clamp(n_e, f_e),
                    f64::from(z1.homogenized().dot(-math::Vec4::unit_z())).clamp(n_e, f_e),
                )
            };

            // all of this is in rotated light-space (left-handed).
            let mut light_focus_pos: math::Vec3<f32> = math::Vec3::zero();
            light_focus_pos.x = bounds0.center().x;
            light_focus_pos.y = bounds0.min.y;
            light_focus_pos.z = bounds0.center().z;

            let d = f64::from(bounds0.max.y - bounds0.min.y).abs();

            let w_l_y = d;

            // NOTE: See section 5.1.2.2 of Lloyd's thesis.
            // NOTE: Since z_1 and z_0 are in the same coordinate space, we don't have to
            // worry about the handedness of their ratio.
            let alpha = z_1 / z_0;
            let alpha_sqrt = alpha.sqrt();
            let directed_near_normal = if factor < 0.0 {
                // Standard shadow map to LiSPSM
                (1.0 + alpha_sqrt - factor * (alpha - 1.0)) / ((alpha - 1.0) * (factor + 1.0))
            } else {
                // LiSPSM to PSM
                ((alpha_sqrt - 1.0) * (factor * alpha_sqrt + 1.0)).recip()
            };

            // Equation 5.14 - 5.16
            let y_ = |v: f64| w_l_y * (v + directed_near_normal).abs();
            let directed_near = y_(0.0) as f32;
            let directed_far = y_(1.0) as f32;
            light_focus_pos.y = if factor > EPSILON_UPSILON {
                light_focus_pos.y - directed_near
            } else {
                light_focus_pos.y
            };
            // Left-handed translation.
            let w_v: math::Mat4<f32> = math::Mat4::translation_3d(-math::Vec3::new(
                light_focus_pos.x,
                light_focus_pos.y,
                light_focus_pos.z,
            ));
            let shadow_view_mat: math::Mat4<f32> = w_v * light_all_mat;
            let w_p: math::Mat4<f32> = {
                if factor > EPSILON_UPSILON {
                    // Projection for y
                    let near = directed_near;
                    let far = directed_far;
                    let left = -1.0;
                    let right = 1.0;
                    let bottom = -1.0;
                    let top = 1.0;
                    let s_x = 2.0 * near / (right - left);
                    let o_x = (right + left) / (right - left);
                    let s_z = 2.0 * near / (top - bottom);
                    let o_z = (top + bottom) / (top - bottom);

                    let s_y = (far + near) / (far - near);
                    let o_y = -2.0 * far * near / (far - near);

                    math::Mat4::new(
                        s_x, o_x, 0.0, 0.0, 0.0, s_y, 0.0, o_y, 0.0, o_z, s_z, 0.0, 0.0, 1.0, 0.0,
                        0.0,
                    )
                } else {
                    math::Mat4::identity()
                }
            };

            let shadow_all_mat: math::Mat4<f32> = w_p * shadow_view_mat;
            // coordinates are transformed from world space (right-handed)
            // to post-warp light space (left-handed), then homogenized.
            let math::Aabb::<f32> {
                min:
                    math::Vec3 {
                        x: xmin,
                        y: ymin,
                        z: zmin,
                    },
                max:
                    math::Vec3 {
                        x: xmax,
                        y: ymax,
                        z: zmax,
                    },
            } = math::fit_psr(
                shadow_all_mat,
                volume.iter().copied(),
                math::Vec4::homogenized,
            );
            let s_x = 2.0 / (xmax - xmin);
            let s_y = 2.0 / (ymax - ymin);
            let s_z = 1.0 / (zmax - zmin);
            let o_x = -(xmax + xmin) / (xmax - xmin);
            let o_y = -(ymax + ymin) / (ymax - ymin);
            let o_z = -zmin / (zmax - zmin);
            let directed_proj_mat = Mat4::new(
                s_x, 0.0, 0.0, o_x, 0.0, s_y, 0.0, o_y, 0.0, 0.0, s_z, o_z, 0.0, 0.0, 0.0, 1.0,
            );

            let shadow_all_mat: Mat4<f32> = Mat4::from_col_arrays(shadow_all_mat.into_col_arrays());

            let directed_texture_proj_mat = texture_mat * directed_proj_mat;
            (
                directed_proj_mat * shadow_all_mat,
                directed_texture_proj_mat * shadow_all_mat,
            )
        };

        let weather = client
            .state()
            .max_weather_near(focus_off.xy() + cam_pos.xy());
        self.wind_vel = weather.wind_vel();
        if weather.rain > RAIN_THRESHOLD {
            let weather = client.weather_at_player();
            let rain_vel = weather.rain_vel();
            let rain_view_mat = math::Mat4::look_at_rh(look_at, look_at + rain_vel, up);

            self.integrated_rain_vel += rain_vel.magnitude() * dt;
            let rain_dir_mat = Mat4::rotation_from_to_3d(-Vec3::unit_z(), rain_vel);

            let (shadow_mat, texture_mat) =
                directed_mats(rain_view_mat, rain_vel.into(), &visible_occlusion_volume);

            let rain_occlusion_locals = RainOcclusionLocals::new(
                shadow_mat,
                texture_mat,
                rain_dir_mat,
                weather.rain,
                self.integrated_rain_vel,
            );

            renderer.update_consts(&mut self.data.rain_occlusion_mats, &[rain_occlusion_locals]);
        } else if self.integrated_rain_vel > 0.0 {
            self.integrated_rain_vel = 0.0;
            // Need to set rain to zero
            let rain_occlusion_locals = RainOcclusionLocals::default();
            renderer.update_consts(&mut self.data.rain_occlusion_mats, &[rain_occlusion_locals]);
        }

        let sun_dir = scene_data.get_sun_dir();
        let is_daylight = sun_dir.z < 0.0;
        if renderer.pipeline_modes().shadow.is_map() && (is_daylight || !lights.is_empty()) {
            let (point_shadow_res, _directed_shadow_res) = renderer.get_shadow_resolution();
            // NOTE: The aspect ratio is currently always 1 for our cube maps, since they
            // are equal on all sides.
            let point_shadow_aspect = point_shadow_res.x as f32 / point_shadow_res.y as f32;
            // Construct matrices to transform from world space to light space for the sun
            // and moon.
            let directed_light_dir = math::Vec3::from(sun_dir);

            // We upload view matrices as well, to assist in linearizing vertex positions.
            // (only for directional lights, so far).
            let mut directed_shadow_mats = Vec::with_capacity(6);

            let light_view_mat = math::Mat4::look_at_rh(look_at, look_at + directed_light_dir, up);
            let (shadow_mat, texture_mat) =
                directed_mats(light_view_mat, directed_light_dir, &visible_light_volume);

            let shadow_locals = ShadowLocals::new(shadow_mat, texture_mat);

            renderer.update_consts(&mut self.data.shadow_mats, &[shadow_locals]);

            directed_shadow_mats.push(light_view_mat);
            // This leaves us with five dummy slots, which we push as defaults.
            directed_shadow_mats
                .extend_from_slice(&[math::Mat4::default(); 6 - NUM_DIRECTED_LIGHTS] as _);
            // Now, construct the full projection matrices in the first two directed light
            // slots.
            let mut shadow_mats = Vec::with_capacity(6 * (lights.len() + 1));
            shadow_mats.resize_with(6, PointLightMatrix::default);
            // Now, we tackle point lights.
            // First, create a perspective projection matrix at 90 degrees (to cover a whole
            // face of the cube map we're using); we use a negative near plane to exactly
            // match OpenGL's behavior if we use a left-handed coordinate system everywhere
            // else.
            let shadow_proj = camera::perspective_rh_zo_general(
                90.0f32.to_radians(),
                point_shadow_aspect,
                1.0 / SHADOW_NEAR,
                1.0 / SHADOW_FAR,
            );
            // NOTE: We negate here to emulate a right-handed projection with a negative
            // near plane, which produces the correct transformation to exactly match
            // OpenGL's rendering behavior if we use a left-handed coordinate
            // system everywhere else.
            let shadow_proj = shadow_proj * Mat4::scaling_3d(-1.0);

            // Next, construct the 6 orientations we'll use for the six faces, in terms of
            // their (forward, up) vectors.
            let orientations = [
                (Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, -1.0, 0.0)),
                (Vec3::new(-1.0, 0.0, 0.0), Vec3::new(0.0, -1.0, 0.0)),
                (Vec3::new(0.0, 1.0, 0.0), Vec3::new(0.0, 0.0, 1.0)),
                (Vec3::new(0.0, -1.0, 0.0), Vec3::new(0.0, 0.0, -1.0)),
                (Vec3::new(0.0, 0.0, 1.0), Vec3::new(0.0, -1.0, 0.0)),
                (Vec3::new(0.0, 0.0, -1.0), Vec3::new(0.0, -1.0, 0.0)),
            ];

            // NOTE: We could create the shadow map collection at the same time as the
            // lights, but then we'd have to sort them both, which wastes time.  Plus, we
            // want to prepend our directed lights.
            shadow_mats.extend(lights.iter().flat_map(|light| {
                // Now, construct the full projection matrix by making the light look at each
                // cube face.
                let eye = Vec3::new(light.pos[0], light.pos[1], light.pos[2]) - focus_off;
                orientations.iter().map(move |&(forward, up)| {
                    // NOTE: We don't currently try to linearize point lights or need a separate
                    // transform for them.
                    PointLightMatrix::new(shadow_proj * Mat4::look_at_lh(eye, eye + forward, up))
                })
            }));

            for (i, val) in shadow_mats.into_iter().enumerate() {
                self.data.point_light_matrices[i] = val
            }
        }

        // Remove unused figures.
        self.figure_mgr.clean(scene_data.tick);

        // Maintain audio
        self.sfx_mgr.maintain(
            audio,
            scene_data.state,
            scene_data.viewpoint_entity,
            &self.camera,
            &self.terrain,
            client,
        );

        self.ambience_mgr
            .maintain(audio, scene_data.state, client, &self.camera);

        self.music_mgr.maintain(audio, scene_data.state, client);
    }

    pub fn global_bind_group(&self) -> &GlobalsBindGroup { &self.globals_bind_group }

    /// Render the scene using the provided `Drawer`.
    pub fn render(
        &self,
        drawer: &mut Drawer<'_>,
        state: &State,
        viewpoint_entity: EcsEntity,
        tick: u64,
        scene_data: &SceneData,
    ) {
        span!(_guard, "render", "Scene::render");
        let sun_dir = scene_data.get_sun_dir();
        let is_daylight = sun_dir.z < 0.0;
        let focus_pos = self.camera.get_focus_pos();
        let cam_pos = self.camera.dependents().cam_pos + focus_pos.map(|e| e.trunc());
        let is_rain = state.max_weather_near(cam_pos.xy()).rain > RAIN_THRESHOLD;
        let culling_mode = if scene_data
            .state
            .terrain()
            .get_key(scene_data.state.terrain().pos_key(cam_pos.as_()))
            .map_or(false, |c| {
                cam_pos.z < c.meta().alt() - terrain::UNDERGROUND_ALT
            }) {
            CullingMode::Underground
        } else {
            CullingMode::Surface
        };

        let camera_data = (&self.camera, scene_data.figure_lod_render_distance);

        // would instead have this as an extension.
        if drawer.pipeline_modes().shadow.is_map() && (is_daylight || !self.light_data.is_empty()) {
            if is_daylight {
                prof_span!("directed shadows");
                if let Some(mut shadow_pass) = drawer.shadow_pass() {
                    // Render terrain directed shadows.
                    self.terrain.render_shadows(
                        &mut shadow_pass.draw_terrain_shadows(),
                        focus_pos,
                        culling_mode,
                    );

                    // Render figure directed shadows.
                    self.figure_mgr.render_shadows(
                        &mut shadow_pass.draw_figure_shadows(),
                        state,
                        tick,
                        camera_data,
                    );
                    self.debug
                        .render_shadows(&mut shadow_pass.draw_debug_shadows());
                }
            }

            // Render terrain point light shadows.
            {
                prof_span!("point shadows");
                drawer.draw_point_shadows(
                    &self.data.point_light_matrices,
                    self.terrain.chunks_for_point_shadows(focus_pos),
                )
            }
        }
        // Render rain occlusion texture
        if is_rain {
            prof_span!("rain occlusion");
            if let Some(mut occlusion_pass) = drawer.rain_occlusion_pass() {
                self.terrain
                    .render_rain_occlusion(&mut occlusion_pass.draw_terrain_shadows(), cam_pos);

                self.figure_mgr.render_rain_occlusion(
                    &mut occlusion_pass.draw_figure_shadows(),
                    state,
                    tick,
                    camera_data,
                );
            }
        }

        prof_span!(guard, "main pass");
        if let Some(mut first_pass) = drawer.first_pass() {
            self.figure_mgr.render_viewpoint(
                &mut first_pass.draw_figures(),
                state,
                viewpoint_entity,
                tick,
                camera_data,
            );

            self.terrain
                .render(&mut first_pass, focus_pos, culling_mode);

            self.figure_mgr.render(
                &mut first_pass.draw_figures(),
                state,
                viewpoint_entity,
                tick,
                camera_data,
            );

            self.lod.render(&mut first_pass, culling_mode);

            // Render the skybox.
            first_pass.draw_skybox(&self.skybox.model);

            // Draws sprites
            let mut sprite_drawer = first_pass.draw_sprites(
                &self.terrain.sprite_globals,
                &self.terrain.sprite_render_state.sprite_atlas_textures,
            );
            self.figure_mgr.render_sprites(
                &mut sprite_drawer,
                state,
                cam_pos,
                scene_data.sprite_render_distance,
            );
            self.terrain.render_sprites(
                &mut sprite_drawer,
                focus_pos,
                cam_pos,
                scene_data.sprite_render_distance,
                culling_mode,
            );
            drop(sprite_drawer);

            // Render tethers.
            self.tether_mgr.render(&mut first_pass);

            // Draws translucent
            self.terrain.render_translucent(&mut first_pass, focus_pos);

            // Render particle effects.
            self.particle_mgr
                .render(&mut first_pass.draw_particles(), scene_data);

            // Render debug shapes
            self.debug.render(&mut first_pass.draw_debug());
        }
        drop(guard);
    }

    pub fn maintain_debug_hitboxes(
        &mut self,
        client: &Client,
        settings: &Settings,
        hitboxes: &mut HashMap<specs::Entity, DebugShapeId>,
        tracks: &mut HashMap<Vec2<i32>, Vec<DebugShapeId>>,
    ) {
        let ecs = client.state().ecs();
        {
            let mut current_chunks = hashbrown::HashSet::new();
            let terrain_grid = ecs.read_resource::<TerrainGrid>();
            for (key, chunk) in terrain_grid.iter() {
                current_chunks.insert(key);
                tracks.entry(key).or_insert_with(|| {
                    let mut ret = Vec::new();
                    for bezier in chunk.meta().tracks().iter() {
                        let shape_id = self.debug.add_shape(DebugShape::TrainTrack {
                            path: *bezier,
                            rail_width: 0.25,
                            rail_sep: 1.0,
                            plank_width: 0.5,
                            plank_height: 0.125,
                            plank_sep: 2.0,
                        });
                        ret.push(shape_id);
                        self.debug
                            .set_context(shape_id, [0.0; 4], [1.0; 4], [0.0, 0.0, 0.0, 1.0]);
                    }
                    for point in chunk.meta().debug_points().iter() {
                        let shape_id = self.debug.add_shape(DebugShape::Cylinder {
                            radius: 0.1,
                            height: 0.1,
                        });
                        ret.push(shape_id);
                        self.debug.set_context(
                            shape_id,
                            point.with_w(0.0).into_array(),
                            [1.0; 4],
                            [0.0, 0.0, 0.0, 1.0],
                        );
                    }
                    for line in chunk.meta().debug_lines().iter() {
                        let shape_id = self
                            .debug
                            .add_shape(DebugShape::Line([line.start, line.end], 0.1));
                        ret.push(shape_id);
                        self.debug
                            .set_context(shape_id, [0.0; 4], [1.0; 4], [0.0, 0.0, 0.0, 1.0]);
                    }
                    ret
                });
            }
            tracks.retain(|k, v| {
                let keep = current_chunks.contains(k);
                if !keep {
                    for shape in v.iter() {
                        self.debug.remove_shape(*shape);
                    }
                }
                keep
            });
        }
        let mut current_entities = hashbrown::HashSet::new();
        if settings.interface.toggle_hitboxes {
            let positions = ecs.read_component::<comp::Pos>();
            let colliders = ecs.read_component::<comp::Collider>();
            let orientations = ecs.read_component::<comp::Ori>();
            let scales = ecs.read_component::<comp::Scale>();
            let groups = ecs.read_component::<comp::Group>();
            for (entity, pos, collider, ori, scale, group) in (
                &ecs.entities(),
                &positions,
                &colliders,
                &orientations,
                scales.maybe(),
                groups.maybe(),
            )
                .join()
            {
                match collider {
                    comp::Collider::CapsulePrism {
                        p0,
                        p1,
                        radius,
                        z_min,
                        z_max,
                    } => {
                        let scale = scale.map_or(1.0, |s| s.0);
                        current_entities.insert(entity);

                        let shape = DebugShape::CapsulePrism {
                            p0: *p0 * scale,
                            p1: *p1 * scale,
                            radius: *radius * scale,
                            height: (*z_max - *z_min) * scale,
                        };

                        // If this shape no longer matches, remove the old one
                        if let Some(shape_id) = hitboxes.get(&entity) {
                            if self
                                .debug
                                .get_shape(*shape_id)
                                .map_or(false, |s| s != &shape)
                            {
                                self.debug.remove_shape(*shape_id);
                                hitboxes.remove(&entity);
                            }
                        }

                        let shape_id = hitboxes
                            .entry(entity)
                            .or_insert_with(|| self.debug.add_shape(shape));
                        let hb_pos = [pos.0.x, pos.0.y, pos.0.z + *z_min * scale, 0.0];
                        let color = if group == Some(&comp::group::ENEMY) {
                            [1.0, 0.0, 0.0, 0.5]
                        } else if group == Some(&comp::group::NPC) {
                            [0.0, 0.0, 1.0, 0.5]
                        } else {
                            [0.0, 1.0, 0.0, 0.5]
                        };
                        //let color = [1.0, 1.0, 1.0, 1.0];
                        let ori = ori.to_quat();
                        let hb_ori = [ori.x, ori.y, ori.z, ori.w];
                        self.debug.set_context(*shape_id, hb_pos, color, hb_ori);
                    },
                    comp::Collider::Voxel { .. }
                    | comp::Collider::Volume(_)
                    | comp::Collider::Point => {
                        // ignore terrain-like or point-hitboxes
                    },
                }
            }
        }
        hitboxes.retain(|k, v| {
            let keep = current_entities.contains(k);
            if !keep {
                self.debug.remove_shape(*v);
            }
            keep
        });
    }

    pub fn maintain_debug_vectors(&mut self, client: &Client, lines: &mut PlayerDebugLines) {
        lines
            .chunk_normal
            .take()
            .map(|id| self.debug.remove_shape(id));
        lines.fluid_vel.take().map(|id| self.debug.remove_shape(id));
        lines.wind.take().map(|id| self.debug.remove_shape(id));
        lines.vel.take().map(|id| self.debug.remove_shape(id));
        if self.debug_vectors_enabled {
            let ecs = client.state().ecs();

            let vels = &ecs.read_component::<comp::Vel>();
            let Some(vel) = vels.get(client.entity()) else {
                return;
            };

            let phys_states = &ecs.read_component::<comp::PhysicsState>();
            let Some(phys) = phys_states.get(client.entity()) else {
                return;
            };

            let positions = &ecs.read_component::<comp::Pos>();
            let Some(pos) = positions.get(client.entity()) else {
                return;
            };

            let weather = ecs.read_resource::<WeatherGrid>();
            // take id and remove to delete the previous lines.

            const LINE_WIDTH: f32 = 0.05;
            // Fluid Velocity
            {
                let Some(fluid) = phys.in_fluid else {
                    return;
                };
                let shape = DebugShape::Line([pos.0, pos.0 + fluid.flow_vel().0 / 2.], LINE_WIDTH);
                let id = self.debug.add_shape(shape);
                lines.fluid_vel = Some(id);
                self.debug
                    .set_context(id, [0.0; 4], [0.18, 0.72, 0.87, 0.8], [0.0, 0.0, 0.0, 1.0]);
            }
            // Chunk Terrain Normal Vector
            {
                let Some(chunk) = client.current_chunk() else {
                    return;
                };
                let shape = DebugShape::Line(
                    [
                        pos.0,
                        pos.0
                            + chunk
                                .meta()
                                .approx_chunk_terrain_normal()
                                .unwrap_or(Vec3::unit_z())
                                * 2.5,
                    ],
                    LINE_WIDTH,
                );
                let id = self.debug.add_shape(shape);
                lines.chunk_normal = Some(id);
                self.debug
                    .set_context(id, [0.0; 4], [0.22, 0.63, 0.1, 0.8], [0.0, 0.0, 0.0, 1.0]);
            }
            // Wind
            {
                let wind = weather.get_interpolated(pos.0.xy()).wind_vel();
                let shape = DebugShape::Line([pos.0, pos.0 + wind * 5.0], LINE_WIDTH);
                let id = self.debug.add_shape(shape);
                lines.wind = Some(id);
                self.debug
                    .set_context(id, [0.0; 4], [0.76, 0.76, 0.76, 0.8], [0.0, 0.0, 0.0, 1.0]);
            }
            // Player Vel
            {
                let shape = DebugShape::Line([pos.0, pos.0 + vel.0 / 2.0], LINE_WIDTH);
                let id = self.debug.add_shape(shape);
                lines.vel = Some(id);
                self.debug
                    .set_context(id, [0.0; 4], [0.98, 0.76, 0.01, 0.8], [0.0, 0.0, 0.0, 1.0]);
            }
        }
    }
}