2024-03-17 22:49:50 +00:00
|
|
|
use crate::{actor, camera};
|
2024-03-16 20:44:51 +00:00
|
|
|
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 22:37:18 +00:00
|
|
|
use bevy::pbr::CascadeShadowConfigBuilder;
|
2024-03-17 14:46:51 +00:00
|
|
|
use bevy::core_pipeline::bloom::{BloomCompositeMode, BloomSettings};
|
2024-03-16 22:37:18 +00:00
|
|
|
use std::f32::consts::PI;
|
2024-03-16 20:44:51 +00:00
|
|
|
|
2024-03-17 23:04:23 +00:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2024-03-16 22:11:56 +00:00
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
2024-03-16 22:37:18 +00:00
|
|
|
mut ambient_light: ResMut<AmbientLight>,
|
2024-03-16 20:44:51 +00:00
|
|
|
asset_server: Res<AssetServer>,
|
|
|
|
) {
|
2024-03-17 22:49:50 +00:00
|
|
|
// Add player
|
|
|
|
commands.spawn((
|
|
|
|
actor::Player,
|
|
|
|
actor::Actor::default(),
|
|
|
|
actor::LifeForm::default(),
|
|
|
|
actor::Suit {
|
|
|
|
oxygen: actor::OXY_M,
|
|
|
|
..default()
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2024-03-16 22:11:56 +00:00
|
|
|
// Add skybox
|
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 {
|
2024-03-17 13:16:25 +00:00
|
|
|
camera: Camera {
|
|
|
|
hdr: true, // HDR is required for bloom
|
|
|
|
..default()
|
|
|
|
},
|
2024-03-16 20:44:51 +00:00
|
|
|
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(),
|
2024-03-17 13:19:12 +00:00
|
|
|
brightness: 200.0,
|
2024-03-16 20:44:51 +00:00
|
|
|
},
|
2024-03-17 13:16:25 +00:00
|
|
|
BloomSettings {
|
2024-03-17 13:29:33 +00:00
|
|
|
composite_mode: BloomCompositeMode::EnergyConserving,
|
2024-03-17 13:16:25 +00:00
|
|
|
..default()
|
|
|
|
},
|
2024-03-16 20:44:51 +00:00
|
|
|
));
|
|
|
|
commands.insert_resource(Cubemap {
|
|
|
|
is_loaded: false,
|
|
|
|
image_handle: skybox_handle,
|
|
|
|
});
|
2024-03-16 22:11:56 +00:00
|
|
|
|
|
|
|
// Add spheres
|
2024-03-16 22:52:26 +00:00
|
|
|
let sphere_radius = 50.0;
|
2024-03-16 22:11:56 +00:00
|
|
|
let sphere_handle = meshes.add(Sphere::new(sphere_radius));
|
2024-03-17 01:00:41 +00:00
|
|
|
let gray_handle = materials.add(StandardMaterial {
|
2024-03-16 22:37:18 +00:00
|
|
|
base_color: Color::GRAY,
|
2024-03-16 22:11:56 +00:00
|
|
|
perceptual_roughness: 1.0,
|
|
|
|
..default()
|
|
|
|
});
|
2024-03-17 01:00:41 +00:00
|
|
|
let brown_handle = materials.add(StandardMaterial {
|
2024-03-17 00:23:35 +00:00
|
|
|
base_color: Color::Rgba { alpha: 1.0, red: 0.8, green: 0.5, blue: 0.1 },
|
2024-03-17 00:05:03 +00:00
|
|
|
perceptual_roughness: 1.0,
|
|
|
|
..default()
|
|
|
|
});
|
2024-03-16 22:11:56 +00:00
|
|
|
commands.spawn(PbrBundle {
|
|
|
|
mesh: sphere_handle.clone(),
|
2024-03-17 01:00:41 +00:00
|
|
|
material: gray_handle.clone(),
|
2024-03-16 22:11:56 +00:00
|
|
|
transform: Transform::from_xyz(
|
|
|
|
0.0,
|
|
|
|
0.0,
|
2024-03-17 00:05:03 +00:00
|
|
|
-500.0,
|
|
|
|
),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
|
|
|
|
let sphere_radius = 10.0;
|
|
|
|
let sphere_handle = meshes.add(Sphere::new(sphere_radius));
|
|
|
|
commands.spawn(PbrBundle {
|
|
|
|
mesh: sphere_handle.clone(),
|
2024-03-17 01:00:41 +00:00
|
|
|
material: brown_handle.clone(),
|
2024-03-17 00:05:03 +00:00
|
|
|
transform: Transform::from_xyz(
|
|
|
|
300.0,
|
|
|
|
40.0,
|
|
|
|
250.0,
|
2024-03-16 22:11:56 +00:00
|
|
|
),
|
|
|
|
..default()
|
|
|
|
});
|
2024-03-16 22:37:18 +00:00
|
|
|
|
2024-03-17 13:16:25 +00:00
|
|
|
// Add THE SUN
|
|
|
|
let hydrogenfusion_handle = materials.add(StandardMaterial {
|
2024-03-17 13:29:40 +00:00
|
|
|
emissive: Color::rgb_linear(1e6, 0.9e6, 1e6),
|
2024-03-17 13:16:25 +00:00
|
|
|
..default()
|
|
|
|
});
|
|
|
|
let sphere_radius = 5000.0;
|
|
|
|
let sphere_handle = meshes.add(Sphere::new(sphere_radius));
|
|
|
|
commands.spawn(PbrBundle {
|
|
|
|
mesh: sphere_handle.clone(),
|
|
|
|
material: hydrogenfusion_handle.clone(),
|
|
|
|
transform: Transform::from_xyz(
|
|
|
|
0.0,
|
|
|
|
30000.0,
|
2024-03-17 13:19:17 +00:00
|
|
|
-500000.0,
|
2024-03-17 13:16:25 +00:00
|
|
|
),
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
|
2024-03-17 00:25:51 +00:00
|
|
|
// Add alien
|
|
|
|
commands.spawn(SceneBundle {
|
|
|
|
transform: Transform {
|
|
|
|
translation: Vec3::new(
|
|
|
|
0.0,
|
|
|
|
0.0,
|
|
|
|
100.0,
|
|
|
|
),
|
|
|
|
rotation: Quat::from_rotation_y(-PI / 3.),
|
|
|
|
scale: Vec3::splat(5.0),
|
|
|
|
},
|
2024-03-17 19:28:23 +00:00
|
|
|
scene: asset_server.load("tmp/alien.glb#Scene0"),
|
2024-03-17 00:25:51 +00:00
|
|
|
..default()
|
|
|
|
});
|
|
|
|
|
2024-03-16 22:37:18 +00:00
|
|
|
// Space is DARK
|
|
|
|
ambient_light.brightness = 0.0;
|
|
|
|
|
|
|
|
// Add Light
|
|
|
|
commands.spawn(DirectionalLightBundle {
|
|
|
|
directional_light: DirectionalLight {
|
2024-03-16 22:44:46 +00:00
|
|
|
illuminance: 1000.0,
|
2024-03-16 22:52:26 +00:00
|
|
|
shadows_enabled: false,
|
2024-03-16 22:37:18 +00:00
|
|
|
..default()
|
|
|
|
},
|
|
|
|
transform: Transform::from_rotation(Quat::from_euler(
|
|
|
|
EulerRot::ZYX,
|
|
|
|
0.0,
|
2024-03-16 22:52:26 +00:00
|
|
|
PI / 1.0,
|
2024-03-18 01:15:44 +00:00
|
|
|
-PI / 7.0,
|
2024-03-16 22:37:18 +00:00
|
|
|
)),
|
|
|
|
cascade_shadow_config: CascadeShadowConfigBuilder {
|
|
|
|
first_cascade_far_bound: 7.0,
|
|
|
|
maximum_distance: 25.0,
|
|
|
|
..default()
|
|
|
|
}
|
|
|
|
.into(),
|
|
|
|
..default()
|
|
|
|
});
|
2024-03-16 20:44:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|