130 lines
3.6 KiB
Rust
130 lines
3.6 KiB
Rust
use crate::camera;
|
|
use bevy::prelude::*;
|
|
use bevy::core_pipeline::Skybox;
|
|
use bevy::asset::LoadState;
|
|
use bevy::render::render_resource::{TextureViewDescriptor, TextureViewDimension};
|
|
use bevy::pbr::CascadeShadowConfigBuilder;
|
|
use std::f32::consts::PI;
|
|
|
|
#[derive(Resource)]
|
|
pub struct Cubemap {
|
|
is_loaded: bool,
|
|
image_handle: Handle<Image>,
|
|
}
|
|
|
|
const CUBEMAP_PATH: &str = "textures/stars_cubemap.png";
|
|
|
|
pub fn setup(
|
|
mut commands: Commands,
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
|
mut ambient_light: ResMut<AmbientLight>,
|
|
asset_server: Res<AssetServer>,
|
|
) {
|
|
// Add skybox
|
|
let skybox_handle = asset_server.load(CUBEMAP_PATH);
|
|
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,
|
|
});
|
|
|
|
// Add spheres
|
|
let sphere_radius = 1.0;
|
|
let sphere_handle = meshes.add(Sphere::new(sphere_radius));
|
|
let white_handle = materials.add(StandardMaterial {
|
|
base_color: Color::GRAY,
|
|
perceptual_roughness: 1.0,
|
|
..default()
|
|
});
|
|
commands.spawn(PbrBundle {
|
|
mesh: sphere_handle.clone(),
|
|
material: white_handle.clone(),
|
|
transform: Transform::from_xyz(
|
|
0.0,
|
|
sphere_radius,
|
|
0.0,
|
|
),
|
|
..default()
|
|
});
|
|
|
|
// Space is DARK
|
|
ambient_light.brightness = 0.0;
|
|
|
|
// Add Light
|
|
commands.spawn(PointLightBundle {
|
|
transform: Transform::from_xyz(5.0, 5.0, 0.0),
|
|
point_light: PointLight {
|
|
intensity: 0.0,
|
|
range: 500.0,
|
|
color: Color::WHITE,
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
..default()
|
|
});
|
|
|
|
commands.spawn(DirectionalLightBundle {
|
|
directional_light: DirectionalLight {
|
|
illuminance: light_consts::lux::OVERCAST_DAY,
|
|
shadows_enabled: true,
|
|
..default()
|
|
},
|
|
transform: Transform::from_rotation(Quat::from_euler(
|
|
EulerRot::ZYX,
|
|
0.0,
|
|
PI / 2.,
|
|
-PI / 4.,
|
|
)),
|
|
cascade_shadow_config: CascadeShadowConfigBuilder {
|
|
first_cascade_far_bound: 7.0,
|
|
maximum_distance: 25.0,
|
|
..default()
|
|
}
|
|
.into(),
|
|
..default()
|
|
});
|
|
}
|
|
|
|
pub fn load_cubemap_asset(
|
|
mut cubemap: ResMut<Cubemap>,
|
|
asset_server: Res<AssetServer>,
|
|
) {
|
|
cubemap.image_handle = asset_server.load(CUBEMAP_PATH);
|
|
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;
|
|
}
|
|
}
|