From 65ff7c4f23a782e34c44478cedb7c86d5ae51be6 Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Tue, 19 Mar 2024 22:40:15 -0400 Subject: [PATCH] render: retrieve light properties from components --- examples/testbed/src/main.rs | 61 +++++++++---------- lyra-game/src/render/light/directional.rs | 19 ++++-- lyra-game/src/render/light/mod.rs | 20 +++--- lyra-game/src/render/light/point.rs | 25 +++++--- lyra-game/src/render/light_cull_compute.rs | 1 + lyra-game/src/render/shaders/base.wgsl | 19 +----- .../src/render/shaders/light_cull.comp.wgsl | 1 + 7 files changed, 72 insertions(+), 74 deletions(-) diff --git a/examples/testbed/src/main.rs b/examples/testbed/src/main.rs index f7e8e54..a84ac19 100644 --- a/examples/testbed/src/main.rs +++ b/examples/testbed/src/main.rs @@ -120,21 +120,6 @@ async fn main() { Transform::from_xyz(0.0, 0.0, 0.0), )); - /* world.spawn(( - separate_scene.clone(), - Transform::from_xyz(0.0, -5.0, -10.0), - )); */ - - /* { - let cube_tran = Transform::from_xyz(-5.9026427, -1.8953488, -10.0); - //cube_tran.rotate_y(math::Angle::Degrees(180.0)); - world.spawn(( - cube_tran, - crate_mesh.clone(), - CubeFlag, - )); - } */ - { let mut light_tran = Transform::from_xyz(1.5, 2.5, 0.0); light_tran.scale = Vec3::new(0.5, 0.5, 0.5); @@ -142,36 +127,27 @@ async fn main() { light_tran.rotate_y(math::Angle::Degrees(25.0)); world.spawn(( DirectionalLight { - color: Vec3::new(1.0, 1.0, 1.0), - ambient: 0.3, - diffuse: 1.0, - specular: 1.3, + enabled: true, + color: Vec3::ONE, + intensity: 0.35 + //..Default::default() }, light_tran, - //cube_mesh.clone(), )); } { - //let mut light_tran = Transform::from_xyz(-3.5, 0.2, -4.5); - //light_tran.scale = Vec3::new(0.5, 0.5, 0.5); world.spawn(( PointLight { + enabled: true, color: Vec3::new(0.0, 0.0, 1.0), - - intensity: 3.3, - - constant: 1.0, - linear: 0.09, - quadratic: 0.032, - - ambient: 0.2, - diffuse: 1.0, - specular: 1.3, + intensity: 1.0, + range: 2.0, + ..Default::default() }, Transform::new( //Vec3::new(-5.0, 1.0, -1.28), - Vec3::new(-5.0, 1.0, -0.28), + Vec3::new(-5.0, 1.0, -0.0), //Vec3::new(-10.0, 0.94, -0.28), Quat::IDENTITY, @@ -179,6 +155,25 @@ async fn main() { ), cube_mesh.clone(), )); + + world.spawn(( + PointLight { + enabled: true, + color: Vec3::new(0.0, 0.5, 1.0), + intensity: 1.0, + range: 1.0, + ..Default::default() + }, + Transform::new( + Vec3::new(-3.0, 0.2, -1.5), + //Vec3::new(-5.0, 1.0, -0.28), + //Vec3::new(-10.0, 0.94, -0.28), + + Quat::IDENTITY, + Vec3::new(0.15, 0.15, 0.15), + ), + cube_mesh.clone(), + )); } /* { diff --git a/lyra-game/src/render/light/directional.rs b/lyra-game/src/render/light/directional.rs index d3360af..8411b60 100644 --- a/lyra-game/src/render/light/directional.rs +++ b/lyra-game/src/render/light/directional.rs @@ -1,11 +1,18 @@ use lyra_ecs::Component; -#[derive(Default, Debug, Clone, Component)] +#[derive(Debug, Clone, Component)] pub struct DirectionalLight { - //pub direction: glam::Quat, + pub enabled: bool, pub color: glam::Vec3, + pub intensity: f32, +} - pub ambient: f32, - pub diffuse: f32, - pub specular: f32, -} \ No newline at end of file +impl Default for DirectionalLight { + fn default() -> Self { + Self { + enabled: true, + color: glam::Vec3::new(1.0, 1.0, 1.0), + intensity: 1.0, + } + } +} diff --git a/lyra-game/src/render/light/mod.rs b/lyra-game/src/render/light/mod.rs index f523263..7d94e2e 100644 --- a/lyra-game/src/render/light/mod.rs +++ b/lyra-game/src/render/light/mod.rs @@ -216,7 +216,7 @@ pub(crate) enum LightType { #[derive(Default, Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] pub(crate) struct LightUniform { pub position: glam::Vec3, - pub light_type: u32, // LightType + pub light_type: u32, // enum LightType pub direction: glam::Vec3, pub enabled: u32, // bool pub color: glam::Vec3, @@ -225,47 +225,45 @@ pub(crate) struct LightUniform { pub range: f32, pub intensity: f32, + pub smoothness: f32, pub spot_cutoff: f32, pub spot_outer_cutoff: f32, - - pub _padding4: u32, } impl LightUniform { pub fn from_point_light_bundle(light: &PointLight, transform: &Transform) -> Self { Self { light_type: LightType::Point as u32, - enabled: true as u32, // TODO + enabled: light.enabled as u32, position: transform.translation, direction: transform.forward(), color: light.color, - range: 1.5, - intensity: 1.0, + range: light.range, + intensity: light.intensity, + smoothness: light.smoothness, spot_cutoff: 0.0, spot_outer_cutoff: 0.0, - _padding4: 0, } } pub fn from_directional_bundle(light: &DirectionalLight, transform: &Transform) -> Self { Self { light_type: LightType::Directional as u32, - enabled: true as u32, // TODO: take from component + enabled: light.enabled as u32, position: transform.translation, direction: transform.forward(), color: light.color, range: 0.0, - intensity: 0.0, + intensity: light.intensity, + smoothness: 0.0, spot_cutoff: 0.0, spot_outer_cutoff: 0.0, - - _padding4: 0, } } diff --git a/lyra-game/src/render/light/point.rs b/lyra-game/src/render/light/point.rs index 64f7688..4aa43ef 100644 --- a/lyra-game/src/render/light/point.rs +++ b/lyra-game/src/render/light/point.rs @@ -1,13 +1,22 @@ use lyra_ecs::Component; -#[derive(Default, Debug, Clone, Component)] +#[derive(Debug, Clone, Component)] pub struct PointLight { + pub enabled: bool, pub color: glam::Vec3, + pub range: f32, pub intensity: f32, - pub constant: f32, - pub linear: f32, - pub quadratic: f32, - pub ambient: f32, - pub diffuse: f32, - pub specular: f32, -} \ No newline at end of file + pub smoothness: f32, +} + +impl Default for PointLight { + fn default() -> Self { + Self { + enabled: true, + color: glam::Vec3::new(1.0, 1.0, 1.0), + range: 1.0, + intensity: 1.0, + smoothness: 0.75, + } + } +} diff --git a/lyra-game/src/render/light_cull_compute.rs b/lyra-game/src/render/light_cull_compute.rs index f65018d..98b742e 100644 --- a/lyra-game/src/render/light_cull_compute.rs +++ b/lyra-game/src/render/light_cull_compute.rs @@ -6,6 +6,7 @@ use winit::dpi::PhysicalSize; use super::{light::LightUniformBuffers, render_buffer::{BindGroupPair, BufferWrapper}, texture::RenderTexture}; +#[allow(dead_code)] pub(crate) struct LightIndicesGridBuffer { index_counter_buffer: wgpu::Buffer, indices_buffer: wgpu::Buffer, diff --git a/lyra-game/src/render/shaders/base.wgsl b/lyra-game/src/render/shaders/base.wgsl index a7a3a19..aa907a7 100755 --- a/lyra-game/src/render/shaders/base.wgsl +++ b/lyra-game/src/render/shaders/base.wgsl @@ -35,6 +35,7 @@ struct Light { range: f32, intensity: f32, + smoothness: f32, spot_cutoff: f32, spot_outer_cutoff: f32, @@ -200,7 +201,7 @@ fn blinn_phong_dir_light(world_pos: vec3, world_norm: vec3, dir_light: diffuse_color *= dir_light.diffuse; specular_color *= dir_light.specular;*/ - return ambient_color + diffuse_color + specular_color; + return (ambient_color + diffuse_color + specular_color) * dir_light.intensity; } fn blinn_phong_point_light(world_pos: vec3, world_norm: vec3, point_light: Light, material: Material, specular_factor: vec3) -> vec3 { @@ -226,22 +227,8 @@ fn blinn_phong_point_light(world_pos: vec3, world_norm: vec3, point_li var specular_color = specular_strength * (light_color * specular_factor); //// end of specular //// - // TODO: Point light range let distance = length(light_pos - world_pos); - // TODO: make smoothness in this a configurable value - // 0.75 is the smoothness or falloff - let attenuation = 1.0 - smoothstep(point_light.range * 0.75, point_light.range, distance); - - //// point light attenuation //// - /*let distance = length(light_pos - world_pos); - let attenuation = 1.0 / (point_light.constant + point_light.linear * distance + - point_light.quadratic * (distance * distance)); - - //// end of point light attenuation //// - - ambient_color *= point_light.ambient * attenuation; - diffuse_color *= point_light.diffuse * attenuation; - specular_color *= point_light.specular * attenuation;*/ + let attenuation = 1.0 - smoothstep(point_light.range * point_light.smoothness, point_light.range, distance); ambient_color *= attenuation; diffuse_color *= attenuation; diff --git a/lyra-game/src/render/shaders/light_cull.comp.wgsl b/lyra-game/src/render/shaders/light_cull.comp.wgsl index 54c94ac..d8e481b 100644 --- a/lyra-game/src/render/shaders/light_cull.comp.wgsl +++ b/lyra-game/src/render/shaders/light_cull.comp.wgsl @@ -22,6 +22,7 @@ struct Light { range: f32, intensity: f32, + smoothness: f32, spot_cutoff: f32, spot_outer_cutoff: f32,