toggle skybox when toggling AR

This commit is contained in:
yuni 2024-03-18 15:40:35 +01:00
parent 871da1be78
commit 38a85be608

View file

@ -1,4 +1,4 @@
use crate::{actor, camera};
use crate::{actor, camera, settings};
use bevy::prelude::*;
use bevy::core_pipeline::Skybox;
use bevy::asset::LoadState;
@ -14,23 +14,37 @@ const SUN_SIZE: f32 = 5000.0;
const ASTRONAUT_SIZE: f32 = 5.0;
const SUN_BRIGHTNESS: f32 = 1e5;
const SKYBOX_BRIGHTNESS: f32 = 300.0;
const SKYBOX_BRIGHTNESS: f32 = 500.0;
const SKYBOX_BRIGHTNESS_AR: f32 = 100.0;
const ASSET_CUBEMAP: &str = "textures/stars_cubemap.png";
const ASSET_CUBEMAP_AR: &str = "tmp/cubemap.png";
const ASSET_ASTRONAUT: &str = "tmp/alien.glb#Scene0";
pub struct WorldPlugin;
impl Plugin for WorldPlugin {
fn build(&self, app: &mut App) {
app.add_systems(Startup, setup);
app.add_systems(Update, asset_loaded.after(load_cubemap_asset));
//app.add_systems(Update, asset_loaded.after(load_cubemap_asset));
app.add_systems(Update, swap_world_on_ar_toggle);
}
}
#[derive(Resource)]
pub struct Cubemap {
pub struct WorldState {
is_loaded: bool,
image_handle: Handle<Image>,
entities_viewn_through_ar: bool,
cubemap_handle: Handle<Image>,
cubemap_ar_handle: Handle<Image>,
}
impl WorldState {
pub fn get_cubemap_handle(&self) -> Handle<Image> {
if self.entities_viewn_through_ar {
self.cubemap_ar_handle.clone()
} else {
self.cubemap_handle.clone()
}
}
}
pub fn setup(
@ -38,8 +52,17 @@ pub fn setup(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
mut ambient_light: ResMut<AmbientLight>,
settings: Res<settings::Settings>,
asset_server: Res<AssetServer>,
) {
let cubemap_handle = asset_server.load(ASSET_CUBEMAP);
commands.insert_resource(WorldState {
is_loaded: false,
entities_viewn_through_ar: settings.hud_active,
cubemap_handle: asset_server.load(ASSET_CUBEMAP),
cubemap_ar_handle: asset_server.load(ASSET_CUBEMAP_AR),
});
// Add player
commands.spawn((
actor::Player,
@ -52,7 +75,6 @@ pub fn setup(
));
// Add skybox
let skybox_handle = asset_server.load(ASSET_CUBEMAP);
commands.spawn((
Camera3dBundle {
camera: Camera {
@ -64,7 +86,7 @@ pub fn setup(
},
camera::CameraController::default(),
Skybox {
image: skybox_handle.clone(),
image: cubemap_handle,
brightness: SKYBOX_BRIGHTNESS,
},
BloomSettings {
@ -72,10 +94,6 @@ pub fn setup(
..default()
},
));
commands.insert_resource(Cubemap {
is_loaded: false,
image_handle: skybox_handle,
});
// Add some hand-placed asteroids
let sphere_handle = meshes.add(Sphere::new(1.0));
@ -203,22 +221,20 @@ pub fn setup(
});
}
pub fn load_cubemap_asset(
mut cubemap: ResMut<Cubemap>,
asset_server: Res<AssetServer>,
) {
cubemap.image_handle = asset_server.load(ASSET_CUBEMAP);
cubemap.is_loaded = false;
}
pub fn asset_loaded(
pub fn swap_world_on_ar_toggle(
asset_server: Res<AssetServer>,
mut images: ResMut<Assets<Image>>,
mut cubemap: ResMut<Cubemap>,
mut worldstate: ResMut<WorldState>,
mut skyboxes: Query<&mut Skybox>,
settings: Res<settings::Settings>,
) {
if !cubemap.is_loaded && asset_server.load_state(&cubemap.image_handle) == LoadState::Loaded {
let image = images.get_mut(&cubemap.image_handle).unwrap();
if settings.hud_active != worldstate.entities_viewn_through_ar {
worldstate.is_loaded = false;
worldstate.entities_viewn_through_ar = settings.hud_active;
}
if !worldstate.is_loaded && asset_server.load_state(&worldstate.get_cubemap_handle()) == LoadState::Loaded {
let cubemap_handle = &worldstate.get_cubemap_handle();
let image = images.get_mut(cubemap_handle).unwrap();
if image.texture_descriptor.array_layer_count() == 1 {
image.reinterpret_stacked_2d_as_array(image.height() / image.width());
image.texture_view_descriptor = Some(TextureViewDescriptor {
@ -228,9 +244,14 @@ pub fn asset_loaded(
}
for mut skybox in &mut skyboxes {
skybox.image = cubemap.image_handle.clone();
skybox.image = cubemap_handle.clone();
skybox.brightness = if settings.hud_active {
SKYBOX_BRIGHTNESS
} else {
SKYBOX_BRIGHTNESS_AR
}
}
cubemap.is_loaded = true;
worldstate.is_loaded = true;
}
}