From 38a85be608bff4f89d51df77c0fc49b000cee58c Mon Sep 17 00:00:00 2001 From: hut Date: Mon, 18 Mar 2024 15:40:35 +0100 Subject: [PATCH] toggle skybox when toggling AR --- src/world.rs | 71 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/src/world.rs b/src/world.rs index 7bda9db..681a47a 100644 --- a/src/world.rs +++ b/src/world.rs @@ -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, + entities_viewn_through_ar: bool, + cubemap_handle: Handle, + cubemap_ar_handle: Handle, +} +impl WorldState { + pub fn get_cubemap_handle(&self) -> Handle { + 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>, mut materials: ResMut>, mut ambient_light: ResMut, + settings: Res, asset_server: Res, ) { + 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, - asset_server: Res, -) { - 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, mut images: ResMut>, - mut cubemap: ResMut, + mut worldstate: ResMut, mut skyboxes: Query<&mut Skybox>, + settings: Res, ) { - 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; } }