wip camera

This commit is contained in:
griffi-gh 2023-01-22 03:24:28 +01:00
parent c761688f81
commit 26b43ed2ca
4 changed files with 71 additions and 7 deletions

View file

@ -0,0 +1,47 @@
use glam::{Mat4, Vec3, Vec3A};
use shipyard::{Component, ViewMut, View, IntoIter, Workload, IntoWorkload};
use crate::transform::Transform;
#[derive(Component)]
pub struct Camera {
pub view_matrix: Mat4,
pub perspective_matrix: Mat4,
pub up: Vec3,
pub fov: f32,
pub z_near: f32,
pub z_far: f32,
}
pub fn compute_cameras() -> Workload {
(
update_perspective_matrix,
update_view_matrix,
).into_workload()
}
fn update_view_matrix(
mut vm_camera: ViewMut<Camera>,
v_transform: View<Transform>
) {
for (camera, transform) in (&mut vm_camera, v_transform.inserted_or_modified()).iter() {
let (_, rotation, translation) = transform.0.to_scale_rotation_translation();
let dir = rotation * Vec3::Z; //this may be incorrect!
camera.view_matrix = Mat4::look_to_rh(translation, dir, camera.up);
}
}
fn update_perspective_matrix(
mut vm_camera: ViewMut<Camera>,
v_transform: View<Transform>
) {
//todo compute this on win resize!
const ASPECT_RATIO: f32 = 9. / 16.;
for (camera, transform) in (&mut vm_camera, &v_transform).iter() {
camera.perspective_matrix = Mat4::perspective_rh_gl(
camera.fov,
ASPECT_RATIO,
camera.z_near,
camera.z_far,
)
}
}

View file

@ -15,17 +15,20 @@ use std::time::{Instant, Duration};
mod logging; mod logging;
pub(crate) mod rendering; pub(crate) mod rendering;
pub(crate) mod player;
pub(crate) mod world; pub(crate) mod world;
pub(crate) mod player;
pub(crate) mod prefabs; pub(crate) mod prefabs;
pub(crate) mod transform; pub(crate) mod transform;
pub(crate) mod settings; pub(crate) mod settings;
pub(crate) mod state;
pub(crate) mod camera;
use rendering::{Renderer, RenderTarget, BackgroundColor, clear_background}; use rendering::{Renderer, RenderTarget, BackgroundColor, clear_background};
use world::{ChunkStorage, ChunkMeshStorage, loading::update_loaded_world_around_player};
use player::spawn_player; use player::spawn_player;
use world::{ChunkStorage, ChunkMeshStorage, loading::load_world_around_player};
use prefabs::load_prefabs; use prefabs::load_prefabs;
use settings::GameSettings; use settings::GameSettings;
use camera::compute_cameras;
#[derive(Unique)] #[derive(Unique)]
pub(crate) struct DeltaTime(Duration); pub(crate) struct DeltaTime(Duration);
@ -37,13 +40,14 @@ fn startup() -> Workload {
} }
fn update() -> Workload { fn update() -> Workload {
( (
load_world_around_player update_loaded_world_around_player,
compute_cameras,
).into_workload() ).into_workload()
} }
fn render() -> Workload { fn render() -> Workload {
( (
clear_background, clear_background,
).into_workload() ).into_sequential_workload()
} }
fn main() { fn main() {
@ -57,8 +61,8 @@ fn main() {
//Add systems and uniques, Init and load things //Add systems and uniques, Init and load things
world.add_unique_non_send_sync(Renderer::init(&event_loop)); world.add_unique_non_send_sync(Renderer::init(&event_loop));
world.add_unique(ChunkStorage::new());
world.add_unique_non_send_sync(ChunkMeshStorage::new()); world.add_unique_non_send_sync(ChunkMeshStorage::new());
world.add_unique(ChunkStorage::new());
world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.))); world.add_unique(BackgroundColor(vec3(0.5, 0.5, 1.)));
world.add_unique(DeltaTime(Duration::default())); world.add_unique(DeltaTime(Duration::default()));
world.add_unique(GameSettings::default()); world.add_unique(GameSettings::default());

6
src/state.rs Normal file
View file

@ -0,0 +1,6 @@
pub enum GameState {
MainMenu,
Connecting,
LoadingWorld,
InGame
}

View file

@ -3,7 +3,7 @@ use shipyard::{View, UniqueView, UniqueViewMut, IntoIter, Workload, IntoWorkload
use crate::{player::LocalPlayer, transform::Transform, settings::GameSettings}; use crate::{player::LocalPlayer, transform::Transform, settings::GameSettings};
use super::{ChunkStorage, chunk::{Chunk, ChunkState, CHUNK_SIZE}, ChunkMeshStorage}; use super::{ChunkStorage, chunk::{Chunk, ChunkState, CHUNK_SIZE}, ChunkMeshStorage};
pub fn load_world_around_player() -> Workload { pub fn update_loaded_world_around_player() -> Workload {
( (
update_chunks_if_player_moved, update_chunks_if_player_moved,
unload_marked_chunks unload_marked_chunks
@ -16,7 +16,8 @@ pub fn update_chunks_if_player_moved(
v_transform: View<Transform>, v_transform: View<Transform>,
mut vm_world: UniqueViewMut<ChunkStorage>, mut vm_world: UniqueViewMut<ChunkStorage>,
) { ) {
//Check if the player actually moved //Check if the player actually moved
//TODO fix this also triggers on rotation, only activate when the player crosses the chnk border
let Some((_, transform)) = (&v_local_player, v_transform.inserted_or_modified()).iter().next() else { let Some((_, transform)) = (&v_local_player, v_transform.inserted_or_modified()).iter().next() else {
return return
}; };
@ -81,3 +82,9 @@ fn unload_marked_chunks(
} }
}) })
} }
fn process_tasks(
) {
}