mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-29 02:08:48 -06:00
wip camera
This commit is contained in:
parent
c761688f81
commit
26b43ed2ca
|
@ -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,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
14
src/main.rs
14
src/main.rs
|
@ -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
6
src/state.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
pub enum GameState {
|
||||||
|
MainMenu,
|
||||||
|
Connecting,
|
||||||
|
LoadingWorld,
|
||||||
|
InGame
|
||||||
|
}
|
|
@ -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
|
||||||
|
@ -17,6 +17,7 @@ pub fn update_chunks_if_player_moved(
|
||||||
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(
|
||||||
|
|
||||||
|
) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue