diff --git a/crates/comet_colors/src/hsla.rs b/crates/comet_colors/src/hsla.rs index 19aa2dc..f9c14b0 100644 --- a/crates/comet_colors/src/hsla.rs +++ b/crates/comet_colors/src/hsla.rs @@ -1,4 +1,4 @@ -use crate::{sRgba, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza}; +use crate::{sRgba, Color, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza}; #[derive(Debug, Clone, PartialEq)] pub struct Hsla { @@ -98,4 +98,10 @@ impl Hsla { pub fn to_oklcha(&self) -> Oklcha { self.to_oklaba().to_oklcha() } +} + +impl Color for Hsla { + fn to_wgpu(&self) -> wgpu::Color { + self.to_linear().to_wgpu() + } } \ No newline at end of file diff --git a/crates/comet_colors/src/hsva.rs b/crates/comet_colors/src/hsva.rs index 0261012..e97cf58 100644 --- a/crates/comet_colors/src/hsva.rs +++ b/crates/comet_colors/src/hsva.rs @@ -1,4 +1,4 @@ -use crate::{sRgba, Hsla, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza}; +use crate::{sRgba, Color, Hsla, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza}; #[derive(Debug, Clone, PartialEq)] pub struct Hsva { @@ -96,4 +96,10 @@ impl Hsva { pub fn to_oklcha(&self) -> Oklcha { self.to_oklaba().to_oklcha() } +} + +impl Color for Hsva { + fn to_wgpu(&self) -> wgpu::Color { + self.to_linear().to_wgpu() + } } \ No newline at end of file diff --git a/crates/comet_colors/src/hwba.rs b/crates/comet_colors/src/hwba.rs index f99692d..f3a9bcc 100644 --- a/crates/comet_colors/src/hwba.rs +++ b/crates/comet_colors/src/hwba.rs @@ -1,4 +1,4 @@ -use crate::{sRgba, Hsla, Hsva, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza}; +use crate::{sRgba, Color, Hsla, Hsva, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza}; #[derive(Debug, Clone, PartialEq)] pub struct Hwba { @@ -159,4 +159,10 @@ impl Hwba { self.to_oklaba().to_oklcha() } +} + +impl Color for Hwba { + fn to_wgpu(&self) -> wgpu::Color { + self.to_linear().to_wgpu() + } } \ No newline at end of file diff --git a/crates/comet_colors/src/laba.rs b/crates/comet_colors/src/laba.rs index 13dd601..8374a08 100644 --- a/crates/comet_colors/src/laba.rs +++ b/crates/comet_colors/src/laba.rs @@ -1,4 +1,4 @@ -use crate::{sRgba, Hsla, Hsva, Hwba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza}; +use crate::{sRgba, Color, Hsla, Hsva, Hwba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza}; #[derive(Debug, Clone, PartialEq)] pub struct Laba { @@ -140,4 +140,10 @@ impl Laba { pub fn to_hsla(&self) -> Hsla { self.to_hsva().to_hsla() } +} + +impl Color for Laba { + fn to_wgpu(&self) -> wgpu::Color { + self.to_linear().to_wgpu() + } } \ No newline at end of file diff --git a/crates/comet_colors/src/lcha.rs b/crates/comet_colors/src/lcha.rs index b1e9183..77eb0df 100644 --- a/crates/comet_colors/src/lcha.rs +++ b/crates/comet_colors/src/lcha.rs @@ -1,4 +1,4 @@ -use crate::{sRgba, Hsla, Hsva, Hwba, Laba, LinearRgba, Oklaba, Oklcha, Xyza}; +use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, LinearRgba, Oklaba, Oklcha, Xyza}; #[derive(Debug, Clone, PartialEq)] pub struct Lcha { @@ -90,4 +90,10 @@ impl Lcha { self.to_hsva().to_hsla() } +} + +impl Color for Lcha { + fn to_wgpu(&self) -> wgpu::Color { + self.to_linear().to_wgpu() + } } \ No newline at end of file diff --git a/crates/comet_colors/src/lib.rs b/crates/comet_colors/src/lib.rs index da0bcdc..a000205 100644 --- a/crates/comet_colors/src/lib.rs +++ b/crates/comet_colors/src/lib.rs @@ -19,4 +19,8 @@ mod xyza; mod laba; mod lcha; mod oklaba; -mod oklcha; \ No newline at end of file +mod oklcha; + +pub trait Color { + fn to_wgpu(&self) -> wgpu::Color; +} \ No newline at end of file diff --git a/crates/comet_colors/src/linear_rgba.rs b/crates/comet_colors/src/linear_rgba.rs index 8b4516c..75d4628 100644 --- a/crates/comet_colors/src/linear_rgba.rs +++ b/crates/comet_colors/src/linear_rgba.rs @@ -1,5 +1,5 @@ -use wgpu::Color; -use crate::{sRgba, Hsla, Hsva, Hwba, Laba, Lcha, Oklaba, Oklcha, Xyza}; +use wgpu; +use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, Oklaba, Oklcha, Xyza}; #[derive(Debug, Clone, PartialEq)] pub struct LinearRgba { @@ -123,9 +123,11 @@ impl LinearRgba { pub fn to_hsla(&self) -> Hsla { self.to_hsva().to_hsla() } +} - pub fn to_wgpu(&self) -> Color { - Color { +impl Color for LinearRgba { + fn to_wgpu(&self) -> wgpu::Color { + wgpu::Color { r: self.red as f64, g: self.green as f64, b: self.blue as f64, diff --git a/crates/comet_colors/src/oklaba.rs b/crates/comet_colors/src/oklaba.rs index 5d150d4..05adf2c 100644 --- a/crates/comet_colors/src/oklaba.rs +++ b/crates/comet_colors/src/oklaba.rs @@ -1,4 +1,4 @@ -use crate::{sRgba, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklcha, Xyza}; +use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklcha, Xyza}; #[derive(Debug, Clone, PartialEq)] pub struct Oklaba { @@ -110,4 +110,10 @@ impl Oklaba { pub fn to_hsla(&self) -> Hsla { self.to_hsva().to_hsla() } +} + +impl Color for Oklaba { + fn to_wgpu(&self) -> wgpu::Color { + self.to_linear().to_wgpu() + } } \ No newline at end of file diff --git a/crates/comet_colors/src/oklcha.rs b/crates/comet_colors/src/oklcha.rs index 083f433..88d1e0c 100644 --- a/crates/comet_colors/src/oklcha.rs +++ b/crates/comet_colors/src/oklcha.rs @@ -1,4 +1,4 @@ -use crate::{sRgba, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Xyza}; +use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Xyza}; #[derive(Debug, Clone, PartialEq)] pub struct Oklcha { @@ -89,4 +89,10 @@ impl Oklcha { pub fn to_hsla(&self) -> Hsla { self.to_hsva().to_hsla() } +} + +impl Color for Oklcha { + fn to_wgpu(&self) -> wgpu::Color { + self.to_linear().to_wgpu() + } } \ No newline at end of file diff --git a/crates/comet_colors/src/rgba.rs b/crates/comet_colors/src/rgba.rs index 156a904..036b604 100644 --- a/crates/comet_colors/src/rgba.rs +++ b/crates/comet_colors/src/rgba.rs @@ -1,4 +1,4 @@ -use crate::{math::Vec4, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza}; +use crate::{math::Vec4, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha, Xyza}; /// sRGB representation of color /// There are two variants: `sRgba` and `sRgba` @@ -342,4 +342,16 @@ impl sRgba { self.alpha ) } +} + +impl Color for sRgba { + fn to_wgpu(&self) -> wgpu::Color { + self.to_linear().to_wgpu() + } +} + +impl Color for sRgba { + fn to_wgpu(&self) -> wgpu::Color { + self.to_linear().to_wgpu() + } } \ No newline at end of file diff --git a/crates/comet_colors/src/xyza.rs b/crates/comet_colors/src/xyza.rs index 59931b9..d1e9f61 100644 --- a/crates/comet_colors/src/xyza.rs +++ b/crates/comet_colors/src/xyza.rs @@ -1,4 +1,4 @@ -use crate::{sRgba, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha}; +use crate::{sRgba, Color, Hsla, Hsva, Hwba, Laba, Lcha, LinearRgba, Oklaba, Oklcha}; #[derive(Debug, Clone, PartialEq)] pub struct Xyza { @@ -106,4 +106,10 @@ impl Xyza { pub fn to_hsla(&self) -> Hsla { self.to_hsva().to_hsla() } +} + +impl Color for Xyza { + fn to_wgpu(&self) -> wgpu::Color { + self.to_linear().to_wgpu() + } } \ No newline at end of file diff --git a/crates/comet_renderer/src/renderer.rs b/crates/comet_renderer/src/renderer.rs index af148b3..f2f5981 100644 --- a/crates/comet_renderer/src/renderer.rs +++ b/crates/comet_renderer/src/renderer.rs @@ -1,10 +1,10 @@ use std::sync::Arc; use winit::dpi::PhysicalSize; use winit::window::Window; -use comet_colors::LinearRgba; +use comet_colors::Color; pub trait Renderer: Sized + Send + Sync { - async fn new(window: Arc, clear_color: Option) -> Self; + async fn new(window: Arc, clear_color: Option) -> Self; fn size(&self) -> PhysicalSize; fn resize(&mut self, new_size: winit::dpi::PhysicalSize); fn update(&mut self) -> f32; diff --git a/crates/comet_renderer/src/renderer2d.rs b/crates/comet_renderer/src/renderer2d.rs index 9bcdfd5..c456edf 100644 --- a/crates/comet_renderer/src/renderer2d.rs +++ b/crates/comet_renderer/src/renderer2d.rs @@ -2,12 +2,12 @@ use std::iter; use std::path::PathBuf; use std::sync::Arc; use std::time::Instant; -use wgpu::{BufferUsages, Color, ShaderModule}; +use wgpu::{BufferUsages, ShaderModule}; use wgpu::naga::ShaderStage; use wgpu::util::DeviceExt; use winit::dpi::PhysicalSize; use winit::window::Window; -use comet_colors::LinearRgba; +use comet_colors::{ Color, LinearRgba }; use comet_ecs::{Camera, Camera2D, Component, Position2D, Render, Render2D, Transform2D, Scene}; use comet_log::{debug, info}; use comet_math::{Point3, Vec2, Vec3}; @@ -28,13 +28,13 @@ pub struct Renderer2D<'a> { pipelines: Vec, render_pass: Vec, last_frame_time: Instant, - deltatime: f32, + delta_time: f32, vertex_buffer: wgpu::Buffer, vertex_data: Vec, index_buffer: wgpu::Buffer, index_data: Vec, num_indices: u32, - clear_color: Color, + clear_color: wgpu::Color, diffuse_texture: Texture, diffuse_bind_group_layout: wgpu::BindGroupLayout, diffuse_bind_group: wgpu::BindGroup, @@ -46,7 +46,7 @@ pub struct Renderer2D<'a> { } impl<'a> Renderer2D<'a> { - pub async fn new(window: Arc, clear_color: Option) -> Renderer2D<'a> { + pub async fn new(window: Arc, clear_color: Option) -> Renderer2D<'a> { let vertex_data: Vec = vec![]; let index_data: Vec = vec![]; @@ -107,13 +107,13 @@ impl<'a> Renderer2D<'a> { let vertex_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Vertex Buffer"), contents: bytemuck::cast_slice(&vertex_data), - usage: wgpu::BufferUsages::VERTEX | BufferUsages::COPY_DST, + usage: BufferUsages::VERTEX | BufferUsages::COPY_DST, }); let index_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Index Buffer"), contents: bytemuck::cast_slice(&index_data), - usage: wgpu::BufferUsages::INDEX | BufferUsages::COPY_DST + usage: BufferUsages::INDEX | BufferUsages::COPY_DST }); let num_indices = index_data.len() as u32; @@ -170,7 +170,7 @@ impl<'a> Renderer2D<'a> { let camera_buffer = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Camera Buffer"), contents: bytemuck::cast_slice(&[camera_uniform]), - usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, + usage: BufferUsages::UNIFORM | BufferUsages::COPY_DST, }); let camera_bind_group_layout = @@ -261,7 +261,7 @@ impl<'a> Renderer2D<'a> { let clear_color = match clear_color { Some(color) => color.to_wgpu(), - None => Color { + None => wgpu::Color { r: 0.0, g: 0.0, b: 0.0, @@ -279,7 +279,7 @@ impl<'a> Renderer2D<'a> { pipelines, render_pass: vec![], last_frame_time: Instant::now(), - deltatime: 0.0, + delta_time: 0.0, vertex_buffer, vertex_data, index_buffer, @@ -298,7 +298,7 @@ impl<'a> Renderer2D<'a> { } pub fn dt(&self) -> f32 { - self.deltatime + self.delta_time } pub fn config(&self) -> &wgpu::SurfaceConfiguration { @@ -340,28 +340,6 @@ impl<'a> Renderer2D<'a> { pub fn apply_shader(&mut self, shader: &str) { let shader_module = self.graphic_resource_manager.get_shader( ((Self::get_project_root().unwrap().as_os_str().to_str().unwrap().to_string() + "\\resources\\shaders\\").as_str().to_string() + shader).as_str()).unwrap(); - let texture_bind_group_layout = self.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { - entries: &[ - wgpu::BindGroupLayoutEntry { - binding: 0, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Texture { - multisampled: false, - view_dimension: wgpu::TextureViewDimension::D2, - sample_type: wgpu::TextureSampleType::Float { filterable: true }, - }, - count: None, - }, - wgpu::BindGroupLayoutEntry { - binding: 1, - visibility: wgpu::ShaderStages::FRAGMENT, - ty: wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), - count: None, - }, - ], - label: Some("texture_bind_group_layout"), - }); - let camera_bind_group_layout = self.device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { entries: &[wgpu::BindGroupLayoutEntry { binding: 0, @@ -380,7 +358,7 @@ impl<'a> Renderer2D<'a> { self.device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { label: Some("Render Pipeline Layout"), bind_group_layouts: &[ - &texture_bind_group_layout, + &self.diffuse_bind_group_layout, &camera_bind_group_layout, ], push_constant_ranges: &[], @@ -742,13 +720,13 @@ impl<'a> Renderer2D<'a> { self.vertex_buffer = self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Updated Vertex Buffer"), contents: bytemuck::cast_slice(&self.vertex_data), - usage: wgpu::BufferUsages::VERTEX | wgpu::BufferUsages::COPY_DST, + usage: BufferUsages::VERTEX | BufferUsages::COPY_DST, }); self.index_buffer = self.device.create_buffer_init(&wgpu::util::BufferInitDescriptor { label: Some("Updated Index Buffer"), contents: bytemuck::cast_slice(&self.index_data), - usage: wgpu::BufferUsages::INDEX | wgpu::BufferUsages::COPY_DST, + usage: BufferUsages::INDEX | BufferUsages::COPY_DST, }); self.num_indices = self.index_data.len() as u32; @@ -780,7 +758,7 @@ impl<'a> Renderer2D<'a> { } /// A function to draw text at a given position. - pub fn draw_text_at(&mut self, text: &str, font: String, size: f32, position: Point3) { + pub fn draw_text_at(&mut self, text: &str, font: String, size: f32, position: Point3, color: impl Color) { self.set_font_atlas(font.clone()); let screen_position = Point3::new(position.x()/self.config.width as f32, position.y()/self.config.height as f32, position.z()); @@ -841,7 +819,6 @@ impl<'a> Renderer2D<'a> { position = i; } } - position } @@ -949,81 +926,6 @@ impl<'a> Renderer2D<'a> { self.set_buffers(vertex_buffer, index_buffer); } - /*pub fn render_scene_2d(&mut self, world: &World) { - let cameras = world.get_entities_with(ComponentSet::from_ids(vec![Camera2D::type_id()])); - - if cameras == vec![] { - return; - } - - let (camera_position, camera_component) = self.setup_camera(cameras, world); - - let mut visible_entities: Vec = vec![]; - - for entity in world.get_entities_with(ComponentSet::from_ids(vec![Transform2D::type_id(), Render2D::type_id()])) { - let entity_id = entity as usize; - - if !camera_component - .in_view_frustum(*camera_position, *world.get_component::(entity_id).unwrap().position()) - { - continue; - } - - match world.get_component::(entity_id) { - Some(render) => { - if !render.is_visible() { - continue; - } - if let Some(cam) = world.get_component::(entity_id) { - continue; - } - visible_entities.push(entity_id); - } - None => { - continue; - } - } - } - - let mut vertex_buffer: Vec = Vec::new(); - let mut index_buffer: Vec = Vec::new(); - - //debug!("Visible entities: {:?}", visible_entities); - - for entity in visible_entities { - let renderer_component = world.get_component::(entity); - let transform_component = world.get_component::(entity); - - if renderer_component.unwrap().is_visible() { - let mut position = transform_component.unwrap().position().clone(); - position.set_x(position.x() / self.config().width as f32); - position.set_y(position.y() / self.config().height as f32); - let region = self.get_texture_region(renderer_component.unwrap().get_texture().to_string()); - let (dim_x, dim_y) = region.dimensions(); - - let (bound_x, bound_y) = - ((dim_x as f32 / self.config().width as f32) * 0.5, (dim_y as f32 / self.config().height as f32) * 0.5); - - let buffer_size = vertex_buffer.len() as u16; - - vertex_buffer.append(&mut vec![ - Vertex::new([-bound_x + position.x(), bound_y + position.y(), 0.0], [region.x0(), region.y0()], [0.0, 0.0, 0.0, 0.0]), - Vertex::new([-bound_x + position.x(), -bound_y + position.y(), 0.0], [region.x0(), region.y1()], [0.0, 0.0, 0.0, 0.0]), - Vertex::new([bound_x + position.x(), -bound_y + position.y(), 0.0], [region.x1(), region.y1()], [0.0, 0.0, 0.0, 0.0]), - Vertex::new([bound_x + position.x(), bound_y + position.y(), 0.0], [region.x1(), region.y0()], [0.0, 0.0, 0.0, 0.0]) - ]); - - index_buffer.append(&mut vec![ - 0 + buffer_size, 1 + buffer_size, 3 + buffer_size, - 1 + buffer_size, 2 + buffer_size, 3 + buffer_size - ]); - } - } - - self.set_buffers(vertex_buffer, index_buffer); - }*/ - - fn sort_entities_by_position(&self, entity_data: Vec<(usize, Position2D)>) -> Vec { let mut sorted_entities: Vec = vec![]; @@ -1124,9 +1026,9 @@ impl<'a> Renderer2D<'a> { pub fn update(&mut self) -> f32 { let now = Instant::now(); - self.deltatime = now.duration_since(self.last_frame_time).as_secs_f32(); // Time delta in seconds + self.delta_time = now.duration_since(self.last_frame_time).as_secs_f32(); // Time delta in seconds self.last_frame_time = now; - self.deltatime + self.delta_time } pub fn render(&mut self) -> Result<(), wgpu::SurfaceError> { @@ -1174,7 +1076,7 @@ impl<'a> Renderer2D<'a> { impl<'a> Renderer for Renderer2D<'a> { - async fn new(window: Arc, clear_color: Option) -> Renderer2D<'a> { + async fn new(window: Arc, clear_color: Option) -> Renderer2D<'a> { Self::new(window, clear_color).await }