bevy_elements 0.5.0

Living environments for Bevy: sun, moon and stars, climate-driven weather, GPU wind, volumetric clouds, rain, snow and fog, lightning, terrain with hydraulic and thermal erosion, flooding, snow cover, wetness and dust — simulated in compute shaders.
Documentation
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
//! GPU tests of the cloud kernels that need the crate's internals: the CPU mirrors of
//! `model.rs` and `noise_mirror.rs`, the kernel inputs (`CloudDebug`) and the per-view textures.
//! The tests of the public behaviour are `tests/gpu_clouds.rs`.
//!
//! Needs a GPU: `cargo test --release --lib -- --ignored clouds`.

#![allow(clippy::chunks_exact_to_as_chunks)]

#[path = "../../tests/support/mod.rs"]
mod support;

use std::time::Duration;

use bevy::app::AppExit;
use bevy::camera::Hdr;
use bevy::camera::RenderTarget;
use bevy::core_pipeline::tonemapping::{DebandDither, Tonemapping};
use bevy::prelude::*;
use bevy::render::render_resource::{TextureFormat, TextureUsages};
use bevy::time::TimeUpdateStrategy;

use super::model::{self, MirrorParams};
use super::{CloudDebug, CloudGpu, CloudQuality, CloudWeatherMap};
use crate::climate::{WeatherDirector, WeatherPreset};
use crate::flash::LightningFlash;
use crate::gpu::noise_mirror::{cloud_base_texel, cloud_detail_texel};
use crate::{
    CivilDate, ElementsCamera, ElementsPlugins, ElementsQuality, LightningPlugin,
    PrecipitationPlugin, WorldClock,
};

#[derive(Clone)]
struct Case {
    preset: WeatherPreset,
    hour: f64,
    position: Vec3,
    look: Vec3,
    quality: ElementsQuality,
    debug: CloudDebug,
    /// Add a wall of 2 km × 300 m 500 m ahead (−Z) of the camera.
    wall: bool,
    /// Render into an unclamped `Rgba16Float` target with no tonemapping.
    linear_target: bool,
    /// Civil date of the clock (2026-06-21 unless a test sets it).
    date: CivilDate,
    /// Vertical field of view in degrees, `None` for Bevy's default (45°).
    fov_deg: Option<f32>,
}

impl Case {
    fn sky(preset: WeatherPreset) -> Self {
        Self {
            preset,
            hour: 12.0,
            position: Vec3::new(0.0, 2.0, 0.0),
            look: Vec3::Y,
            quality: ElementsQuality::High,
            debug: CloudDebug::default(),
            wall: false,
            linear_target: false,
            date: CivilDate::new(2026, 6, 21),
            fov_deg: None,
        }
    }
}

#[derive(Resource)]
struct Target(Handle<Image>);

fn app(case: Case) -> App {
    let mut app = App::new();
    // Clouds alone: random strikes would overwrite the tests' own `LightningFlash`, and rain
    // streaks would change the scene pixels the composite tests compare.
    let plugins = ElementsPlugins
        .build()
        .disable::<LightningPlugin>()
        .disable::<PrecipitationPlugin>();
    app.add_plugins((support::headless_plugins(), plugins))
        .insert_resource(TimeUpdateStrategy::ManualDuration(Duration::from_secs_f64(
            1.0 / 60.0,
        )))
        .insert_resource(WorldClock {
            paused: true,
            ..WorldClock::new(case.date).at_hour(case.hour)
        })
        .insert_resource(case.quality)
        .insert_resource(case.debug.clone())
        .add_systems(
            Startup,
            move |mut commands: Commands,
                  mut images: ResMut<Assets<Image>>,
                  mut meshes: ResMut<Assets<Mesh>>,
                  mut materials: ResMut<Assets<StandardMaterial>>,
                  mut director: ResMut<WeatherDirector>| {
                let target = if case.linear_target {
                    let mut image =
                        Image::new_target_texture(64, 64, TextureFormat::Rgba16Float, None);
                    image.texture_descriptor.usage |= TextureUsages::COPY_SRC;
                    let handle = images.add(image);
                    commands.insert_resource(Target(handle.clone()));
                    RenderTarget::Image(handle.into())
                } else {
                    support::image_target(&mut images)
                };
                let up = if case.look.abs().dot(Vec3::Y) > 0.99 {
                    Vec3::Z
                } else {
                    Vec3::Y
                };
                let mut camera = commands.spawn((
                    Camera3d::default(),
                    target,
                    ElementsCamera {
                        auto_exposure: false,
                    },
                    Transform::from_translation(case.position).looking_to(case.look, up),
                ));
                if let Some(fov) = case.fov_deg {
                    camera.insert(Projection::Perspective(PerspectiveProjection {
                        fov: fov.to_radians(),
                        ..default()
                    }));
                }
                if case.linear_target {
                    camera.insert((Tonemapping::None, DebandDither::Disabled, Hdr));
                }
                if case.wall {
                    commands.spawn((
                        Mesh3d(meshes.add(Cuboid::new(2000.0, 300.0, 10.0))),
                        MeshMaterial3d(materials.add(StandardMaterial {
                            base_color: Color::srgb(0.6, 0.5, 0.4),
                            unlit: true,
                            ..default()
                        })),
                        Transform::from_xyz(0.0, 150.0, -500.0),
                    ));
                }
                director.force(case.preset, Duration::ZERO);
            },
        );
    support::finish(&mut app);
    app
}

fn run(app: &mut App, frames: usize) {
    for _ in 0..frames {
        app.update();
    }
}

fn finish(app: &mut App) {
    app.world_mut().write_message(AppExit::Success);
}

fn set_debug(app: &mut App, f: impl FnOnce(&mut CloudDebug)) {
    f(&mut app.world_mut().resource_mut::<CloudDebug>());
}

/// The march result texture, rgba as f32, row-major, with its size.
fn march_texture(app: &mut App) -> (Vec<[f32; 4]>, UVec2) {
    let gpu: CloudGpu = app.world().resource::<CloudGpu>().clone();
    let view = gpu.view.expect("the view textures exist");
    let low = UVec2::new(gpu.params.sizes.z, gpu.params.sizes.w);
    let bytes = support::read_texture(app, view.march, low.x as usize * 8, low.y as usize);
    (rgba16(&bytes), low)
}

fn history_texture(app: &mut App, index: usize) -> (Vec<[f32; 4]>, UVec2) {
    let gpu: CloudGpu = app.world().resource::<CloudGpu>().clone();
    let view = gpu.view.expect("the view textures exist");
    let size = view.size;
    let bytes = support::read_texture(
        app,
        view.history[index].clone(),
        size.x as usize * 8,
        size.y as usize,
    );
    (rgba16(&bytes), size)
}

/// Both history textures as they were in the same frame: this frame's and the previous one's.
fn history_pair(app: &mut App) -> (Vec<[f32; 4]>, Vec<[f32; 4]>) {
    let gpu: CloudGpu = app.world().resource::<CloudGpu>().clone();
    let view = gpu.view.expect("the view textures exist");
    let (row, rows) = (view.size.x as usize * 8, view.size.y as usize);
    let mut both = support::read_textures(
        app,
        vec![
            (view.history[0].clone(), row, rows),
            (view.history[1].clone(), row, rows),
        ],
    );
    let b = rgba16(&both.pop().expect("two textures"));
    let a = rgba16(&both.pop().expect("two textures"));
    (a, b)
}

fn rgba16(bytes: &[u8]) -> Vec<[f32; 4]> {
    bytes
        .chunks_exact(8)
        .map(|t| [0, 2, 4, 6].map(|i| support::half(u16::from_le_bytes([t[i], t[i + 1]]))))
        .collect()
}

fn luminance(c: [f32; 4]) -> f32 {
    0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]
}

fn centre(texels: &[[f32; 4]], size: UVec2) -> [f32; 4] {
    texels[(size.y / 2 * size.x + size.x / 2) as usize]
}

// ---------------------------------------------------------------------------------------------
// Validation 3
// ---------------------------------------------------------------------------------------------

/// Mean absolute difference over all channels between two texel planes of a volume.
fn plane_difference(texels: &[u8], n: usize, axis: usize, a: usize, b: usize) -> f32 {
    let at =
        |x: usize, y: usize, z: usize, c: usize| f32::from(texels[((z * n + y) * n + x) * 4 + c]);
    let mut total = 0.0;
    for u in 0..n {
        for v in 0..n {
            for c in 0..4 {
                let (pa, pb) = match axis {
                    0 => (at(a, u, v, c), at(b, u, v, c)),
                    1 => (at(u, a, v, c), at(u, b, v, c)),
                    _ => (at(u, v, a, c), at(u, v, b, c)),
                };
                total += (pa - pb).abs();
            }
        }
    }
    total / (n * n * 4) as f32
}

/// Validation 3: the generated volumes tile (no seam: the wrap-around difference is no larger
/// than 1.5× the difference between neighbours), match the CPU mirror texel for texel within
/// 3/255, and have the channel means the mirror has.
#[test]
#[ignore = "needs a GPU"]
fn the_noise_volumes_tile_and_match_the_cpu_mirror() {
    let mut app = app(Case::sky(WeatherPreset::PartlyCloudy));
    run(&mut app, 12);
    let gpu: CloudGpu = app.world().resource::<CloudGpu>().clone();
    let base = support::read_texture(&mut app, gpu.base.clone(), 128 * 4, 128 * 128);
    let detail = support::read_texture(&mut app, gpu.detail.clone(), 32 * 4, 32 * 32);
    finish(&mut app);

    for (name, texels, n) in [("base", &base, 128usize), ("detail", &detail, 32)] {
        for axis in 0..3 {
            let wrap = plane_difference(texels, n, axis, 0, n - 1);
            let interior = (0..n - 1)
                .map(|i| plane_difference(texels, n, axis, i, i + 1))
                .sum::<f32>()
                / (n - 1) as f32;
            println!("{name} axis {axis}: wrap {wrap:.3}, interior {interior:.3}");
            assert!(
                wrap <= 1.5 * interior,
                "{name} has a seam along axis {axis}: {wrap} vs {interior}"
            );
        }
    }

    let mut state = 0x9E37_79B9_7F4A_7C15_u64;
    let mut next = |m: usize| {
        state ^= state << 13;
        state ^= state >> 7;
        state ^= state << 17;
        (state >> 24) as usize % m
    };
    let mut worst = 0.0_f32;
    for _ in 0..4096 {
        let (x, y, z) = (next(128), next(128), next(128));
        let expect = cloud_base_texel(x as u32, y as u32, z as u32);
        let at = ((z * 128 + y) * 128 + x) * 4;
        for c in 0..4 {
            worst = worst.max((f32::from(base[at + c]) / 255.0 - expect[c]).abs());
        }
        let (x, y, z) = (next(32), next(32), next(32));
        let expect = cloud_detail_texel(x as u32, y as u32, z as u32);
        let at = ((z * 32 + y) * 32 + x) * 4;
        for c in 0..3 {
            worst = worst.max((f32::from(detail[at + c]) / 255.0 - expect[c]).abs());
        }
    }
    println!(
        "worst texel difference to the mirror: {:.2}/255",
        worst * 255.0
    );
    assert!(
        worst <= 3.0 / 255.0,
        "worst difference {} / 255",
        worst * 255.0
    );

    let mean_a = base.chunks_exact(4).map(|t| f32::from(t[3])).sum::<f32>()
        / (base.len() / 4) as f32
        / 255.0;
    let mean_b = detail.chunks_exact(4).map(|t| f32::from(t[2])).sum::<f32>()
        / (detail.len() / 4) as f32
        / 255.0;
    println!("channel means: base A {mean_a:.4}, detail B {mean_b:.4}");
    assert!((mean_a - 0.4987).abs() < 0.05, "base A mean {mean_a}");
    assert!((mean_b - 0.4764).abs() < 0.05, "detail B mean {mean_b}");
}

// ---------------------------------------------------------------------------------------------
// Validation 2 (drift offsets)
// ---------------------------------------------------------------------------------------------

/// Validation 2: at cloud drifts of 0, 7 and 31 km the weather map's area fraction is the cover
/// and every texel agrees with the CPU mirror of the field kernel (exact quantile threshold).
#[test]
#[ignore = "needs a GPU"]
fn the_weather_map_follows_the_drift_and_the_cpu_field() {
    let cover = 0.4;
    let mut case = Case::sky(WeatherPreset::Clear);
    case.debug.cover = Some(cover);
    let mut app = app(case);
    for drift in [[0.0_f32, 0.0], [7_000.0, 0.0], [31_000.0, 12_000.0]] {
        set_debug(&mut app, |d| d.drift = Some(Vec2::from(drift)));
        run(&mut app, 8);
        let map: CloudWeatherMap = app.world().resource::<CloudWeatherMap>().clone();
        let bytes = support::read_texture(&mut app, map.map.clone(), 512 * 4, 512);
        let covered = bytes.chunks_exact(4).filter(|t| t[0] >= 128).count() as f32 / 262_144.0;
        let mirror = MirrorParams::new(
            1500.0,
            3300.0,
            0.5,
            f64::from(cover),
            [f64::from(drift[0]), f64::from(drift[1])],
        );
        let mut worst = 0.0_f64;
        for j in (0..512).step_by(5) {
            for i in (0..512).step_by(5) {
                let xz = [
                    f64::from(map.origin.x) + (i as f64 + 0.5) * 62.5,
                    f64::from(map.origin.y) + (j as f64 + 0.5) * 62.5,
                ];
                let (r, dome) = mirror.weather_at(xz);
                let t = &bytes[(j * 512 + i) * 4..][..4];
                worst = worst
                    .max((f64::from(t[0]) / 255.0 - r).abs())
                    .max((f64::from(t[3]) / 255.0 - dome).abs());
            }
        }
        println!(
            "drift {drift:?}: fraction {covered:.4}, worst texel difference to the mirror {worst:.3}"
        );
        assert!(
            (covered - cover).abs() <= 0.02,
            "{covered} at drift {drift:?}"
        );
        assert!(
            worst <= 0.08,
            "mirror disagrees by {worst} at drift {drift:?}"
        );
    }
    finish(&mut app);
}

// ---------------------------------------------------------------------------------------------
// Validation 7 and 8
// ---------------------------------------------------------------------------------------------

/// Validation 7: through a uniform slab, looking straight up, the march transmits `e^{−σ d}` to
/// 1 %; an opaque slab leaves a finite transmittance in [0, 0.01].
#[test]
#[ignore = "needs a GPU"]
fn the_march_transmits_exp_minus_sigma_d_through_a_uniform_slab() {
    let mut case = Case::sky(WeatherPreset::Clear);
    case.debug = CloudDebug {
        mode: 1.0,
        sigma: 0.002,
        layer: Some((500.0, 1500.0)),
        ..default()
    };
    let mut app = app(case);
    for (sigma, expect) in [(0.002_f32, (-2.0_f32).exp()), (0.0004, (-0.4_f32).exp())] {
        set_debug(&mut app, |d| d.sigma = sigma);
        run(&mut app, 8);
        let (texels, size) = march_texture(&mut app);
        let t = centre(&texels, size)[3];
        println!("sigma {sigma}: T {t:.4}, expected {expect:.4}");
        assert!((t / expect - 1.0).abs() <= 0.01, "T {t}, expected {expect}");
    }
    set_debug(&mut app, |d| d.sigma = 0.05);
    run(&mut app, 8);
    let (texels, size) = march_texture(&mut app);
    let c = centre(&texels, size);
    println!("opaque slab: {c:?}");
    assert!(c.iter().all(|v| v.is_finite()));
    assert!((0.0..=0.01).contains(&c[3]), "T {}", c[3]);
    finish(&mut app);
}

/// Validation 8: the light the kernel finds at the layer is the transmittance of the Earth
/// atmosphere integrated along the spherical ray, and the planet shadows the layer once the
/// sun is lower than the layer's horizon dip.
#[test]
#[ignore = "needs a GPU"]
fn the_light_at_the_layer_is_the_atmospheric_transmittance_and_the_planet_shadow() {
    let mut case = Case::sky(WeatherPreset::Clear);
    case.debug = CloudDebug {
        mode: 2.0,
        layer: Some((1500.0, 3300.0)),
        light: Some(Vec3::Y),
        light_e: Some(Vec3::ONE),
        ..default()
    };
    let mut app = app(case);
    let sun = |elevation_deg: f32| {
        let e = elevation_deg.to_radians();
        Vec3::new(e.cos(), e.sin(), 0.0)
    };
    let read = |app: &mut App, base: f32, elevation_deg: f32| {
        set_debug(app, |d| {
            d.layer = Some((base, base + 1800.0));
            d.light = Some(sun(elevation_deg));
        });
        run(app, 8);
        let (texels, size) = march_texture(app);
        centre(&texels, size)
    };
    // The camera is 2 m up: the layer's base is the point straight above it.
    let altitude = |base: f32| f64::from(base);
    for (base, elevation, tolerance) in [
        (1500.0, 90.0, 0.03),
        (3300.0, 90.0, 0.03),
        (1500.0, 10.0, 0.20),
    ] {
        let got = read(&mut app, base, elevation);
        let mu = f64::from(elevation).to_radians().sin();
        let expect = model::bevy_optical_transmittance(altitude(base), mu);
        println!("h {base} sun {elevation}°: kernel {got:?}, integral {expect:?}");
        for c in 0..3 {
            assert!(
                (f64::from(got[c]) / expect[c] - 1.0).abs() <= tolerance,
                "channel {c}: {} vs {}",
                got[c],
                expect[c]
            );
        }
    }
    // Planet shadow: lit one degree below the horizon at 1.5 km (dip 1.24°), dark at −1.5°;
    // lit at −3° at 9.5 km (dip 3.13°), dark by −3.6°.
    assert!(read(&mut app, 1500.0, -1.0)[0] > 0.0);
    assert_eq!(read(&mut app, 1500.0, -1.5)[0], 0.0);
    assert!(read(&mut app, 9500.0, -3.0)[0] > 0.0);
    assert_eq!(read(&mut app, 9500.0, -3.6)[0], 0.0);
    finish(&mut app);
}

// ---------------------------------------------------------------------------------------------
// Validation 10
// ---------------------------------------------------------------------------------------------

fn bits(target: &[u8]) -> &[u8] {
    target
}

/// Validation 10: a pixel whose scene surface is nearer than the cloud entry is exactly the scene
/// colour (the composite adds nothing there), cloud-free sky is unchanged, and the cloud changes
/// the sky where there is cloud. The target is linear `Rgba16Float` with no tonemapping, and the
/// same frame is rendered with and without the composite (`CloudDebug::mode` 4).
#[test]
#[ignore = "needs a GPU"]
fn the_composite_leaves_nearer_surfaces_and_clear_sky_untouched() {
    let mut case = Case::sky(WeatherPreset::Overcast);
    case.look = Vec3::new(0.0, 0.364, -1.0).normalize();
    case.wall = true;
    case.linear_target = true;
    let mut app = app(case);
    // Past the 1 s smoothing of the light factor: the lights must not drift between the frames.
    run(&mut app, 700);
    let target = app.world().resource::<Target>().0.clone();
    let with = support::read_texture(&mut app, target.clone(), 64 * 8, 64);
    set_debug(&mut app, |d| d.mode = 4.0);
    run(&mut app, 8);
    let without = support::read_texture(&mut app, target, 64 * 8, 64);
    finish(&mut app);

    let a = rgba16(bits(&with));
    let b = rgba16(bits(&without));
    // Camera pitch 20°, vertical field of view 45°: the wall (elevation −0.2° to 31°) fills the
    // rows from about the screen's 36 % mark counted from the top down to row 58; the rows
    // below it see the sky under the horizon, which the composite lifts with the sky gain.
    let mut wall_identical = 0;
    let mut sky_changed = 0;
    for y in 0..64 {
        for x in 0..64 {
            let (pa, pb) = (a[y * 64 + x], b[y * 64 + x]);
            if (41..59).contains(&y) {
                // The volumetric fog jitters every frame: equal up to a half-float step or two.
                let delta = (0..4).map(|c| (pa[c] - pb[c]).abs()).fold(0.0, f32::max);
                assert!(
                    delta <= 0.01,
                    "wall pixel ({x}, {y}) changed by {delta}: {pa:?} vs {pb:?}"
                );
                wall_identical += 1;
            } else if y < 6 && pa != pb {
                sky_changed += 1;
            }
        }
    }
    println!(
        "{wall_identical} wall pixels identical, {sky_changed} sky pixels changed by the cloud"
    );
    assert!(wall_identical > 1000);
    assert!(
        sky_changed > 100,
        "the overcast deck should show in the sky"
    );
}

// ---------------------------------------------------------------------------------------------
// Finite values, temporal behaviour, flash
// ---------------------------------------------------------------------------------------------

/// Validation 16: no infinity or NaN, and no negative or above-one transmittance, in the march
/// result and the history, at every quality, in the clearest and the thickest weather.
#[test]
#[ignore = "needs a GPU"]
fn the_cloud_textures_stay_finite_at_every_quality() {
    for quality in [
        ElementsQuality::Low,
        ElementsQuality::Medium,
        ElementsQuality::High,
        ElementsQuality::Ultra,
    ] {
        for (preset, hour) in [
            (WeatherPreset::PartlyCloudy, 12.0),
            (WeatherPreset::Thunderstorm, 19.0),
            (WeatherPreset::Overcast, 5.0),
        ] {
            let mut case = Case::sky(preset);
            case.quality = quality;
            case.hour = hour;
            case.look = Vec3::new(1.0, 0.2, 0.3).normalize();
            let mut app = app(case);
            run(&mut app, 40);
            let window = CloudQuality::of(quality).window as usize;
            run(&mut app, 2 * window);
            let (march, _) = march_texture(&mut app);
            let (h0, _) = history_texture(&mut app, 0);
            let (h1, _) = history_texture(&mut app, 1);
            finish(&mut app);
            for (name, texels) in [("march", &march), ("history 0", &h0), ("history 1", &h1)] {
                for t in texels {
                    assert!(
                        t.iter().all(|v| v.is_finite())
                            && t[3] >= -1e-3
                            && t[3] <= 1.001
                            && t[..3].iter().all(|v| *v >= -1e-3),
                        "{name} at {quality:?}/{preset:?}: {t:?}"
                    );
                }
            }
            println!(
                "{quality:?} / {preset:?}: all {} texels finite",
                march.len() + h0.len() + h1.len()
            );
        }
    }
}

/// Validation 9 (static camera): with the camera, time and drift frozen the history changes
/// little between consecutive frames, after the accumulation window has run.
#[test]
#[ignore = "needs a GPU"]
fn a_still_camera_gives_a_steady_history() {
    for quality in [ElementsQuality::High, ElementsQuality::Low] {
        let mut case = Case::sky(WeatherPreset::PartlyCloudy);
        case.quality = quality;
        case.look = Vec3::new(1.0, 0.25, 0.4).normalize();
        case.debug.drift = Some(Vec2::new(3_000.0, 2_000.0));
        let mut app = app(case);
        let window = CloudQuality::of(quality).window as usize;
        run(&mut app, 40 + 6 * window);
        let (a, b) = history_pair(&mut app);
        finish(&mut app);
        let mean = a.iter().map(|t| luminance(*t)).sum::<f32>() / a.len() as f32;
        let rms = (a
            .iter()
            .zip(&b)
            .map(|(x, y)| (luminance(*x) - luminance(*y)).powi(2))
            .sum::<f32>()
            / a.len() as f32)
            .sqrt();
        println!(
            "{quality:?}: mean luminance {mean:.4}, consecutive-frame rms {rms:.5} ({:.2} %)",
            100.0 * rms / mean
        );
        assert!(mean > 0.0);
        assert!(rms / mean <= 0.10, "{quality:?}: {}", rms / mean);
    }
}

/// Validation 13: with no flash the cloud is what it was; a 7.96×10⁶ cd flash at the layer base
/// lights the cloud over it at dusk by at least 50×, and changes the exposed noon sky by under
/// 1 %.
#[test]
#[ignore = "needs a GPU"]
fn a_flash_lights_the_cloud_at_dusk_and_not_at_noon() {
    for (hour, minimum_ratio, maximum_change) in [(21.0, 50.0, f32::MAX), (12.0, 0.0, 0.02)] {
        let mut case = Case::sky(WeatherPreset::Thunderstorm);
        case.hour = hour;
        let mut app = app(case);
        app.insert_resource(LightningFlash::default());
        run(&mut app, 80);
        let centre_mean = |texels: &[[f32; 4]], size: UVec2| {
            let mut sum = 0.0;
            let mut n = 0.0;
            for y in size.y / 2 - 8..size.y / 2 + 8 {
                for x in size.x / 2 - 8..size.x / 2 + 8 {
                    sum += luminance(texels[(y * size.x + x) as usize]);
                    n += 1.0;
                }
            }
            sum / n
        };
        let (off, size) = march_texture(&mut app);
        let dark = centre_mean(&off, size);
        *app.world_mut().resource_mut::<LightningFlash>() = LightningFlash {
            position: Vec3::new(0.0, 1500.0, 0.0),
            intensity_cd: 7.96e6,
        };
        run(&mut app, 8);
        let (on, _) = march_texture(&mut app);
        finish(&mut app);
        let lit = centre_mean(&on, size);
        println!(
            "hour {hour}: no flash {dark:.5}, flash {lit:.5}, ratio {:.1}",
            lit / dark.max(1e-9)
        );
        assert!(lit / dark.max(1e-9) >= minimum_ratio);
        assert!((lit - dark).abs() / dark.max(1e-9) <= maximum_change);
    }
}

// ---------------------------------------------------------------------------------------------
// Deviation 27: the far veil continues the marched deck
// ---------------------------------------------------------------------------------------------

/// Elevation, degrees, of the ray from a camera `camera_m` above the surface that enters the
/// shell whose lower surface is `base_m` high at distance `distance_m`.
fn entry_elevation_deg(base_m: f64, camera_m: f64, distance_m: f64) -> f64 {
    let r = model::PLANET_RADIUS_M;
    let (o, s) = (r + camera_m, r + base_m);
    ((s * s - o * o - distance_m * distance_m) / (2.0 * o * distance_m))
        .asin()
        .to_degrees()
}

/// Chroma of a linear colour: (max − min) / max.
fn chroma(c: [f32; 3]) -> f32 {
    let max = c.iter().copied().fold(0.0, f32::max);
    let min = c.iter().copied().fold(f32::MAX, f32::min);
    (max - min) / max.max(1e-9)
}

/// The mean colour of the rows of a 64 × 64 target whose centres lie between `from_deg` and
/// `to_deg` of elevation, for a camera at `pitch_deg` with vertical field of view `fov_deg`.
fn rows_mean(
    texels: &[[f32; 4]],
    pitch_deg: f64,
    fov_deg: f64,
    from_deg: f64,
    to_deg: f64,
) -> [f32; 3] {
    let half = (fov_deg.to_radians() / 2.0).tan();
    let mut sum = [0.0f32; 3];
    let mut n = 0.0;
    for y in 0..64 {
        let ndc = 1.0 - 2.0 * (y as f64 + 0.5) / 64.0;
        let elevation = pitch_deg + (ndc * half).atan().to_degrees();
        if elevation >= from_deg && elevation <= to_deg {
            for x in 0..64 {
                for c in 0..3 {
                    sum[c] += texels[y * 64 + x][c];
                }
            }
            n += 64.0;
        }
    }
    assert!(n > 0.0, "no rows between {from_deg}° and {to_deg}°");
    sum.map(|v| v / n)
}

/// Blue-ness of a linear colour: (b − r) / (r + g + b), positive for a bluish colour, negative
/// for a peach one.
fn tint(c: [f32; 3]) -> f32 {
    (c[2] - c[0]) / (c[0] + c[1] + c[2]).max(1e-9)
}

/// Deviation 27: at full cover the far veil continues the marched deck. The camera looks at the
/// elevation where the layer entry is 32 km away (the end of the march: below it only the veil
/// shows) at 12° field of view; the rows 0.15–0.6° above that elevation show the fading deck and
/// the rows 0.15–0.6° below it the veil. Their mean colours differ in luminance by under 20 %
/// (measured 4–13 %, about the natural gradient of the aerial perspective over 0.9°; before
/// Deviation 27 the step was 1.7× at sun 60° and 2.4× at 20°), in blue-ness by under 0.03
/// (measured 0.013 at most; before: about 0.25, a peach band under a blue deck at 20°) and in
/// chroma by under 0.05 (measured 0.032 at most; before: 0.23); the veil rows 1.5–2.1° below the
/// boundary have the blue-ness and chroma of the deck rows 1.5–2.1° above it to within 0.06 and
/// 0.1 (measured 0.034 and 0.068). Sun 60° and 20° (the second is the low winter sun of the
/// `snowfall` example), Snow (cover 1) and Overcast (0.95), looking away from the sun and
/// toward it.
#[test]
#[ignore = "needs a GPU"]
fn the_far_veil_continues_the_marched_deck_at_full_cover() {
    let fov_deg = 12.0_f64;
    let camera_m = 2.0_f32;
    for (preset, date, hour, sun_range) in [
        (
            WeatherPreset::Snow,
            CivilDate::new(2026, 5, 1),
            12.0,
            58.0..63.0,
        ),
        (
            WeatherPreset::Snow,
            CivilDate::new(2026, 1, 15),
            10.5,
            17.0..23.0,
        ),
        (
            WeatherPreset::Overcast,
            CivilDate::new(2026, 4, 20),
            12.0,
            55.0..58.0,
        ),
        (
            WeatherPreset::Overcast,
            CivilDate::new(2026, 1, 15),
            10.5,
            17.0..23.0,
        ),
    ] {
        let mut case = Case::sky(preset);
        case.date = date;
        case.hour = hour;
        case.linear_target = true;
        case.fov_deg = Some(fov_deg as f32);
        let mut app = app(case);
        run(&mut app, 4);
        let sun = *app.world().resource::<crate::celestial::SunState>();
        let base_m = app.world().resource::<crate::clouds::CloudLayer>().base_m;
        assert!(
            sun_range.contains(&sun.altitude_deg),
            "sun {}°",
            sun.altitude_deg
        );
        let boundary = entry_elevation_deg(f64::from(base_m), f64::from(camera_m), 32_000.0);
        for rel_azimuth_deg in [180.0_f32, 0.0] {
            let pitch = (boundary as f32).to_radians();
            let azimuth = (sun.azimuth_deg + rel_azimuth_deg).to_radians();
            let look = Dir3::new(Vec3::new(
                azimuth.sin() * pitch.cos(),
                pitch.sin(),
                -azimuth.cos() * pitch.cos(),
            ))
            .expect("a unit look direction");
            let mut cameras = app
                .world_mut()
                .query_filtered::<&mut Transform, With<ElementsCamera>>();
            for mut transform in cameras.iter_mut(app.world_mut()) {
                *transform = Transform::from_translation(Vec3::new(0.0, camera_m, 0.0))
                    .looking_to(look, Vec3::Y);
            }
            // Past the 1 s smoothing of the light factor and a full accumulation window.
            run(&mut app, 400);
            let target = app.world().resource::<Target>().0.clone();
            let texels = rgba16(&support::read_texture(&mut app, target, 64 * 8, 64));

            let rows = |from: f64, to: f64| {
                rows_mean(&texels, boundary, fov_deg, boundary + from, boundary + to)
            };
            let (deck, veil) = (rows(0.15, 0.6), rows(-0.6, -0.15));
            let (deck_far, veil_far) = (rows(1.5, 2.1), rows(-2.1, -1.5));
            let lum = |c: [f32; 3]| luminance([c[0], c[1], c[2], 0.0]);
            let ratio = lum(veil) / lum(deck);
            println!(
                "{preset:?} sun {:.1}° rel {rel_azimuth_deg}: boundary {boundary:.2}°, luminance deck {:.3} veil {:.3} (ratio {ratio:.3}); tint deck {:.3} veil {:.3}, chroma deck {:.3} veil {:.3}; 1.5–2.1° away: tint {:.3} / {:.3}, chroma {:.3} / {:.3}",
                sun.altitude_deg,
                lum(deck),
                lum(veil),
                tint(deck),
                tint(veil),
                chroma(deck),
                chroma(veil),
                tint(deck_far),
                tint(veil_far),
                chroma(deck_far),
                chroma(veil_far),
            );
            assert!(
                (0.85..=1.2).contains(&ratio),
                "{preset:?}: luminance ratio {ratio}"
            );
            assert!((tint(veil) - tint(deck)).abs() <= 0.03, "{preset:?}: tint");
            assert!(
                (chroma(veil) - chroma(deck)).abs() <= 0.05,
                "{preset:?}: chroma"
            );
            assert!(
                (tint(veil_far) - tint(deck_far)).abs() <= 0.06,
                "{preset:?}: far tint"
            );
            assert!(
                (chroma(veil_far) - chroma(deck_far)).abs() <= 0.1,
                "{preset:?}: far chroma"
            );
        }
        finish(&mut app);
    }
}