Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add skybox #659

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions assets/textures/skybox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions crates/camera/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ pub use camera::{
};
use distance::DistancePlugin;
pub use distance::{CameraDistance, DistanceSet};
use skybox::SkyboxPlugin;

mod camera;
mod distance;
mod skybox;

pub struct CameraPluginGroup;

Expand All @@ -17,5 +19,6 @@ impl PluginGroup for CameraPluginGroup {
PluginGroupBuilder::start::<Self>()
.add(CameraPlugin)
.add(DistancePlugin)
.add(SkyboxPlugin)
}
}
81 changes: 81 additions & 0 deletions crates/camera/src/skybox.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use bevy::{
asset::LoadState,
core_pipeline::Skybox,
ecs::query::Has,
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) {
LoadState::Loaded => (),
LoadState::NotLoaded | 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(skybox.handle.clone()));
}