Finished skybox

This commit is contained in:
Franklin 2023-09-12 19:06:37 -04:00
parent c31a84ea61
commit 165e71f1c9
11 changed files with 62 additions and 8 deletions

3
Readme.md Normal file
View File

@ -0,0 +1,3 @@
# Experimental game
- [ ] Skybox

BIN
assets/.DS_Store vendored Normal file

Binary file not shown.

BIN
assets/skybox/.DS_Store vendored Normal file

Binary file not shown.

BIN
assets/skybox/skybox.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

View File

@ -1 +0,0 @@

View File

@ -1,14 +1,16 @@
use bevy::prelude::*; use bevy::{prelude::*, core_pipeline::Skybox};
use bevy_rapier3d::prelude::*; use bevy_rapier3d::prelude::*;
use crate::{ use crate::{
comps::core::{camera::MainCamera, markers::player::Player}, comps::core::{camera::MainCamera, markers::player::Player},
constants::player_values::{PLAYER_HEIGHT, PLAYER_INITIAL_WEIGHT, PLAYER_GRAVITY_SCALE, PLAYER_LINEAR_DAMPING}, constants::player_values::{PLAYER_HEIGHT, PLAYER_INITIAL_WEIGHT, PLAYER_GRAVITY_SCALE, PLAYER_LINEAR_DAMPING}, scenes::scene1::skybox::{CUBEMAPS, Cubemap},
}; };
use super::player_movement::{PlayerLinearXZState, PlayerLinearYState}; use super::player_movement::{PlayerLinearXZState, PlayerLinearYState};
pub fn spawn_player(mut commands: Commands) { pub fn spawn_player(mut commands: Commands, asset_server: Res<AssetServer>) {
let skybox_handle = asset_server.load(CUBEMAPS[0].0);
commands commands
.spawn(Player) .spawn(Player)
.insert(RigidBody::Dynamic) .insert(RigidBody::Dynamic)
@ -44,5 +46,11 @@ pub fn spawn_player(mut commands: Commands) {
commands.spawn(MainCamera).insert(Camera3dBundle { commands.spawn(MainCamera).insert(Camera3dBundle {
transform: Transform::from_xyz(0.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y), transform: Transform::from_xyz(0.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default() ..Default::default()
})
.insert(Skybox(skybox_handle.clone()));
commands.insert_resource(Cubemap {
is_loaded: false,
image_handle: skybox_handle,
}); });
} }

View File

@ -2,7 +2,6 @@ use bevy::prelude::*;
use bevy_rapier3d::prelude::*; use bevy_rapier3d::prelude::*;
use scenes::scene1; use scenes::scene1;
mod assets;
mod comps; mod comps;
mod constants; mod constants;
mod logic; mod logic;

View File

@ -7,11 +7,11 @@ pub fn spawn_ground(
mut materials: ResMut<Assets<StandardMaterial>>, mut materials: ResMut<Assets<StandardMaterial>>,
) { ) {
commands commands
.spawn(Collider::cuboid(20.0, 0.1, 20.0)) .spawn(Collider::cuboid(30.0, 0.1, 30.0))
.insert(TransformBundle::from(Transform::from_xyz(0.0, -2.0, 0.0))) .insert(TransformBundle::from(Transform::from_xyz(0.0, -2.0, 0.0)))
.insert(RigidBody::Fixed) .insert(RigidBody::Fixed)
.insert(PbrBundle { .insert(PbrBundle {
mesh: meshes.add(shape::Plane::from_size(40.0).into()), mesh: meshes.add(shape::Plane::from_size(60.0).into()),
material: materials.add(StandardMaterial { material: materials.add(StandardMaterial {
base_color: Color::WHITE, base_color: Color::WHITE,
perceptual_roughness: 1.0, perceptual_roughness: 1.0,

View File

@ -11,7 +11,7 @@ use crate::{
}, },
}; };
use super::{ground::spawn_ground, lighting::setup_lighting, obstacles::spawn_obstacles}; use super::{ground::spawn_ground, lighting::setup_lighting, obstacles::spawn_obstacles, skybox::asset_loaded};
pub fn load_scene(application: &mut App) { pub fn load_scene(application: &mut App) {
application.insert_resource(MouseMovementSettings::default()); application.insert_resource(MouseMovementSettings::default());
@ -26,6 +26,7 @@ pub fn load_scene(application: &mut App) {
application.add_systems(Update, capture_cursor); application.add_systems(Update, capture_cursor);
application.add_systems(Update, sync_player_y_state); application.add_systems(Update, sync_player_y_state);
application.add_systems(Update, follow_cursor_with_camera); application.add_systems(Update, follow_cursor_with_camera);
application.add_systems(Update, asset_loaded);
application.add_systems(Startup, setup_lighting); application.add_systems(Startup, setup_lighting);
} }

View File

@ -3,3 +3,4 @@ pub mod init;
pub mod ground; pub mod ground;
pub mod lighting; pub mod lighting;
pub mod obstacles; pub mod obstacles;
pub mod skybox;

View File

@ -0,0 +1,43 @@
use bevy::{prelude::*, render::{texture::CompressedImageFormats, render_resource::{TextureViewDescriptor, TextureViewDimension}}, core_pipeline::Skybox, asset::LoadState};
pub const CUBEMAPS: &[(&str, CompressedImageFormats)] = &[
(
"skybox/skybox.png",
CompressedImageFormats::NONE,
),
];
#[derive(Resource)]
pub struct Cubemap {
pub is_loaded: bool,
pub image_handle: Handle<Image>,
}
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.get_load_state(&cubemap.image_handle) == LoadState::Loaded {
let image = images.get_mut(&cubemap.image_handle).unwrap();
// NOTE: PNGs do not have any metadata that could indicate they contain a cubemap texture,
// so they appear as one texture. The following code reconfigures the texture as necessary.
if image.texture_descriptor.array_layer_count() == 1 {
image.reinterpret_stacked_2d_as_array(
image.texture_descriptor.size.height / image.texture_descriptor.size.width,
);
image.texture_view_descriptor = Some(TextureViewDescriptor {
dimension: Some(TextureViewDimension::Cube),
..default()
});
}
for mut skybox in &mut skyboxes {
skybox.0 = cubemap.image_handle.clone();
}
cubemap.is_loaded = true;
}
}