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
use bevy::{
    asset::LoadState,
    core_pipeline::Skybox,
    prelude::*,
    render::render_resource::{TextureViewDescriptor, TextureViewDimension},
};
use de_core::{gamestate::GameState, state::AppState};
use iyes_progress::prelude::*;

pub(crate) struct SkyboxPlugin;

impl Plugin for SkyboxPlugin {
    fn build(&self, app: &mut App) {
        app.add_systems(OnEnter(AppState::AppLoading), load)
            .add_systems(
                Update,
                (
                    configure_cubemap
                        .track_progress()
                        .run_if(in_state(AppState::AppLoading)),
                    setup_camera.run_if(in_state(GameState::Loading)),
                ),
            );
    }
}

#[derive(Resource)]
struct SkyboxSource {
    handle: Handle<Image>,
    configured: bool,
}

fn load(mut commands: Commands, server: Res<AssetServer>) {
    commands.insert_resource(SkyboxSource {
        handle: server.load("textures/skybox.png"),
        configured: false,
    });
}

fn configure_cubemap(
    server: Res<AssetServer>,
    mut source: ResMut<SkyboxSource>,
    mut images: ResMut<Assets<Image>>,
) -> Progress {
    if source.configured {
        return true.into();
    }

    match server.get_load_state(&source.handle) {
        Some(LoadState::Loaded) => (),
        Some(LoadState::NotLoaded) | Some(LoadState::Loading) => return false.into(),
        _ => panic!("Unexpected loading state."),
    }

    let image = images.get_mut(&source.handle).unwrap();
    image.reinterpret_stacked_2d_as_array(
        image.texture_descriptor.size.height / image.texture_descriptor.size.width,
    );
    image.texture_view_descriptor = Some(TextureViewDescriptor {
        dimension: Some(TextureViewDimension::Cube),
        ..default()
    });

    source.configured = true;
    true.into()
}

fn setup_camera(
    mut commands: Commands,
    skybox: ResMut<SkyboxSource>,
    camera_query: Query<(Entity, Has<Skybox>), With<Camera>>,
) {
    let (entity, configured) = camera_query.single();
    if configured {
        return;
    }
    commands.entity(entity).insert(Skybox {
        image: skybox.handle.clone(),
        brightness: 300.,
    });
}