mirror of
https://github.com/lisk77/comet.git
synced 2025-10-23 21:38:50 +00:00
feat: finally added a working multi-camera system where the highest priority Camera2D
will be used to make the RenderCamera
(unfortunately not very efficient because it creates a new RenderCamera
every tick, but it works i guess)
This commit is contained in:
parent
4e9e296ba4
commit
445818b79b
4 changed files with 20 additions and 41 deletions
|
@ -11,7 +11,6 @@ use comet_log::*;
|
|||
use comet_structs::*;
|
||||
use crate::archetypes::Archetypes;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct World {
|
||||
id_queue: IdQueue,
|
||||
next_id: u32,
|
||||
|
|
|
@ -10,13 +10,13 @@ pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
|
|||
|
||||
const SAFE_FRAC_PI_2: f32 = std::f32::consts::FRAC_PI_2 - 0.0001;
|
||||
|
||||
pub struct Camera {
|
||||
pub struct RenderCamera {
|
||||
zoom: f32,
|
||||
dimension: Vec2,
|
||||
position: Vec3
|
||||
}
|
||||
|
||||
impl Camera {
|
||||
impl RenderCamera {
|
||||
pub fn new(
|
||||
zoom: f32,
|
||||
dimension: Vec2,
|
||||
|
@ -64,7 +64,7 @@ impl CameraUniform {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn update_view_proj(&mut self, camera: &Camera) {
|
||||
pub fn update_view_proj(&mut self, camera: &RenderCamera) {
|
||||
self.view_proj = camera.build_view_projection_matrix().into();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,32 +1,5 @@
|
|||
use comet_math::Mat4;
|
||||
|
||||
mod camera;
|
||||
pub mod renderer;
|
||||
pub mod renderer2d;
|
||||
mod render_pass;
|
||||
mod render_group;
|
||||
|
||||
pub struct Projection {
|
||||
aspect: f32,
|
||||
fovy: f32,
|
||||
znear: f32,
|
||||
zfar: f32
|
||||
}
|
||||
|
||||
impl Projection {
|
||||
pub fn new(width: u32, height: u32, fovy: f32, znear: f32, zfar: f32) -> Self {
|
||||
Self {
|
||||
aspect: width as f32 / height as f32,
|
||||
fovy,
|
||||
znear,
|
||||
zfar
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resize(&mut self, width: u32, height: u32) { self.aspect = width as f32 / height as f32; }
|
||||
|
||||
/*pub fn calc_matrix(&self) -> Mat4 {
|
||||
Mat4::perspective_matrix(self.fovy, self.aspect, self.znear, self.zfar)
|
||||
}*/
|
||||
}
|
||||
|
||||
mod render_group;
|
|
@ -14,7 +14,7 @@ use comet_math::{Point3, Vec2, Vec3};
|
|||
use comet_resources::{texture, graphic_resource_manager::GraphicResorceManager, Texture, Vertex};
|
||||
use comet_resources::texture_atlas::TextureRegion;
|
||||
use comet_structs::ComponentSet;
|
||||
use crate::camera::{Camera as OldCam, CameraUniform};
|
||||
use crate::camera::{RenderCamera, CameraUniform};
|
||||
use crate::render_pass::RenderPassInfo;
|
||||
use crate::renderer::Renderer;
|
||||
|
||||
|
@ -38,7 +38,7 @@ pub struct Renderer2D<'a> {
|
|||
diffuse_texture: Texture,
|
||||
diffuse_bind_group: wgpu::BindGroup,
|
||||
graphic_resource_manager: GraphicResorceManager,
|
||||
camera: OldCam,
|
||||
camera: RenderCamera,
|
||||
camera_uniform: CameraUniform,
|
||||
camera_buffer: wgpu::Buffer,
|
||||
camera_bind_group: wgpu::BindGroup,
|
||||
|
@ -161,7 +161,7 @@ impl<'a> Renderer2D<'a> {
|
|||
label: Some("diffuse_bind_group"),
|
||||
});
|
||||
|
||||
let camera = OldCam::new(1.0, Vec2::new(2.0, 2.0), Vec3::new(0.0, 0.0, 0.0));
|
||||
let camera = RenderCamera::new(1.0, Vec2::new(2.0, 2.0), Vec3::new(0.0, 0.0, 0.0));
|
||||
|
||||
let mut camera_uniform = CameraUniform::new();
|
||||
camera_uniform.update_view_proj(&camera);
|
||||
|
@ -261,9 +261,9 @@ impl<'a> Renderer2D<'a> {
|
|||
let clear_color = match clear_color {
|
||||
Some(color) => color.to_wgpu(),
|
||||
None => Color {
|
||||
r: 0.1,
|
||||
g: 0.2,
|
||||
b: 0.3,
|
||||
r: 0.0,
|
||||
g: 0.0,
|
||||
b: 0.0,
|
||||
a: 1.0,
|
||||
}
|
||||
};
|
||||
|
@ -754,7 +754,7 @@ impl<'a> Renderer2D<'a> {
|
|||
let camera_component = world.get_component::<Camera2D>(*cam).unwrap();
|
||||
let camera_position = world.get_component::<Transform2D>(*cam).unwrap().position();
|
||||
|
||||
let camera = OldCam::new(
|
||||
let camera = RenderCamera::new(
|
||||
camera_component.zoom(),
|
||||
camera_component.dimensions(),
|
||||
Vec3::new(camera_position.as_vec().x(),
|
||||
|
@ -804,7 +804,15 @@ impl<'a> Renderer2D<'a> {
|
|||
/// A function to automatically render all the entities of the `World` struct.
|
||||
/// The entities must have the `Render2D` and `Transform2D` components to be rendered as well as set visible.
|
||||
pub fn render_scene_2d(&mut self, world: &World) {
|
||||
let entities = world.get_entities_with(ComponentSet::from_ids(vec![Render2D::type_id()]));
|
||||
let cameras = world.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Camera2D::type_id()]));
|
||||
let entities = world.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Render2D::type_id()]));
|
||||
|
||||
if cameras.is_empty() {
|
||||
return;
|
||||
}
|
||||
|
||||
self.setup_camera(cameras, world);
|
||||
|
||||
let mut vertex_buffer: Vec<Vertex> = Vec::new();
|
||||
let mut index_buffer: Vec<u16> = Vec::new();
|
||||
|
||||
|
@ -813,7 +821,6 @@ impl<'a> Renderer2D<'a> {
|
|||
let transform_component = world.get_component::<Transform2D>(entity).unwrap();
|
||||
|
||||
if renderer_component.is_visible() {
|
||||
//renderer.draw_texture_at(renderer_component.get_texture(), Point3::new(transform_component.position().x(), transform_component.position().y(), 0.0));
|
||||
let mut position = transform_component.position().clone();
|
||||
position.set_x(position.x() / self.config().width as f32);
|
||||
position.set_y(position.y() / self.config().height as f32);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue