outfly/src/world.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

2024-03-16 20:44:51 +00:00
use crate::camera;
use bevy::prelude::*;
use bevy::core_pipeline::Skybox;
use bevy::asset::LoadState;
2024-03-16 20:53:13 +00:00
use bevy::render::render_resource::{TextureViewDescriptor, TextureViewDimension};
2024-03-16 20:44:51 +00:00
#[derive(Resource)]
pub struct Cubemap {
is_loaded: bool,
image_handle: Handle<Image>,
}
2024-03-16 20:53:13 +00:00
const CUBEMAP_PATH: &str = "textures/stars_cubemap.png";
2024-03-16 20:44:51 +00:00
pub fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
) {
2024-03-16 20:53:13 +00:00
let skybox_handle = asset_server.load(CUBEMAP_PATH);
2024-03-16 20:44:51 +00:00
commands.spawn((
Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 8.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
},
camera::CameraController::default(),
Skybox {
image: skybox_handle.clone(),
brightness: 150.0,
},
));
commands.insert_resource(Cubemap {
is_loaded: false,
image_handle: skybox_handle,
});
}
pub fn load_cubemap_asset(
mut cubemap: ResMut<Cubemap>,
asset_server: Res<AssetServer>,
) {
2024-03-16 20:53:13 +00:00
cubemap.image_handle = asset_server.load(CUBEMAP_PATH);
2024-03-16 20:44:51 +00:00
cubemap.is_loaded = false;
}
pub fn asset_loaded(
asset_server: Res<AssetServer>,
mut images: ResMut<Assets<Image>>,
mut cubemap: ResMut<Cubemap>,
mut skyboxes: Query<&mut Skybox>,
) {
if !cubemap.is_loaded && asset_server.load_state(&cubemap.image_handle) == LoadState::Loaded {
let image = images.get_mut(&cubemap.image_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 {
dimension: Some(TextureViewDimension::Cube),
..default()
});
}
for mut skybox in &mut skyboxes {
skybox.image = cubemap.image_handle.clone();
}
cubemap.is_loaded = true;
}
}