render: retrieve light properties from components

This commit is contained in:
SeanOMik 2024-03-19 22:40:15 -04:00
parent f63a7ae86a
commit 65ff7c4f23
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
7 changed files with 72 additions and 74 deletions

View File

@ -120,21 +120,6 @@ async fn main() {
Transform::from_xyz(0.0, 0.0, 0.0), 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); let mut light_tran = Transform::from_xyz(1.5, 2.5, 0.0);
light_tran.scale = Vec3::new(0.5, 0.5, 0.5); 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)); light_tran.rotate_y(math::Angle::Degrees(25.0));
world.spawn(( world.spawn((
DirectionalLight { DirectionalLight {
color: Vec3::new(1.0, 1.0, 1.0), enabled: true,
ambient: 0.3, color: Vec3::ONE,
diffuse: 1.0, intensity: 0.35
specular: 1.3, //..Default::default()
}, },
light_tran, 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(( world.spawn((
PointLight { PointLight {
enabled: true,
color: Vec3::new(0.0, 0.0, 1.0), color: Vec3::new(0.0, 0.0, 1.0),
intensity: 1.0,
intensity: 3.3, range: 2.0,
..Default::default()
constant: 1.0,
linear: 0.09,
quadratic: 0.032,
ambient: 0.2,
diffuse: 1.0,
specular: 1.3,
}, },
Transform::new( Transform::new(
//Vec3::new(-5.0, 1.0, -1.28), //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), //Vec3::new(-10.0, 0.94, -0.28),
Quat::IDENTITY, Quat::IDENTITY,
@ -179,6 +155,25 @@ async fn main() {
), ),
cube_mesh.clone(), 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(),
));
} }
/* { /* {

View File

@ -1,11 +1,18 @@
use lyra_ecs::Component; use lyra_ecs::Component;
#[derive(Default, Debug, Clone, Component)] #[derive(Debug, Clone, Component)]
pub struct DirectionalLight { pub struct DirectionalLight {
//pub direction: glam::Quat, pub enabled: bool,
pub color: glam::Vec3, pub color: glam::Vec3,
pub intensity: f32,
}
pub ambient: f32, impl Default for DirectionalLight {
pub diffuse: f32, fn default() -> Self {
pub specular: f32, Self {
} enabled: true,
color: glam::Vec3::new(1.0, 1.0, 1.0),
intensity: 1.0,
}
}
}

View File

@ -216,7 +216,7 @@ pub(crate) enum LightType {
#[derive(Default, Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)] #[derive(Default, Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub(crate) struct LightUniform { pub(crate) struct LightUniform {
pub position: glam::Vec3, pub position: glam::Vec3,
pub light_type: u32, // LightType pub light_type: u32, // enum LightType
pub direction: glam::Vec3, pub direction: glam::Vec3,
pub enabled: u32, // bool pub enabled: u32, // bool
pub color: glam::Vec3, pub color: glam::Vec3,
@ -225,47 +225,45 @@ pub(crate) struct LightUniform {
pub range: f32, pub range: f32,
pub intensity: f32, pub intensity: f32,
pub smoothness: f32,
pub spot_cutoff: f32, pub spot_cutoff: f32,
pub spot_outer_cutoff: f32, pub spot_outer_cutoff: f32,
pub _padding4: u32,
} }
impl LightUniform { impl LightUniform {
pub fn from_point_light_bundle(light: &PointLight, transform: &Transform) -> Self { pub fn from_point_light_bundle(light: &PointLight, transform: &Transform) -> Self {
Self { Self {
light_type: LightType::Point as u32, light_type: LightType::Point as u32,
enabled: true as u32, // TODO enabled: light.enabled as u32,
position: transform.translation, position: transform.translation,
direction: transform.forward(), direction: transform.forward(),
color: light.color, color: light.color,
range: 1.5, range: light.range,
intensity: 1.0, intensity: light.intensity,
smoothness: light.smoothness,
spot_cutoff: 0.0, spot_cutoff: 0.0,
spot_outer_cutoff: 0.0, spot_outer_cutoff: 0.0,
_padding4: 0,
} }
} }
pub fn from_directional_bundle(light: &DirectionalLight, transform: &Transform) -> Self { pub fn from_directional_bundle(light: &DirectionalLight, transform: &Transform) -> Self {
Self { Self {
light_type: LightType::Directional as u32, light_type: LightType::Directional as u32,
enabled: true as u32, // TODO: take from component enabled: light.enabled as u32,
position: transform.translation, position: transform.translation,
direction: transform.forward(), direction: transform.forward(),
color: light.color, color: light.color,
range: 0.0, range: 0.0,
intensity: 0.0, intensity: light.intensity,
smoothness: 0.0,
spot_cutoff: 0.0, spot_cutoff: 0.0,
spot_outer_cutoff: 0.0, spot_outer_cutoff: 0.0,
_padding4: 0,
} }
} }

View File

@ -1,13 +1,22 @@
use lyra_ecs::Component; use lyra_ecs::Component;
#[derive(Default, Debug, Clone, Component)] #[derive(Debug, Clone, Component)]
pub struct PointLight { pub struct PointLight {
pub enabled: bool,
pub color: glam::Vec3, pub color: glam::Vec3,
pub range: f32,
pub intensity: f32, pub intensity: f32,
pub constant: f32, pub smoothness: f32,
pub linear: f32, }
pub quadratic: f32,
pub ambient: f32, impl Default for PointLight {
pub diffuse: f32, fn default() -> Self {
pub specular: f32, Self {
} enabled: true,
color: glam::Vec3::new(1.0, 1.0, 1.0),
range: 1.0,
intensity: 1.0,
smoothness: 0.75,
}
}
}

View File

@ -6,6 +6,7 @@ use winit::dpi::PhysicalSize;
use super::{light::LightUniformBuffers, render_buffer::{BindGroupPair, BufferWrapper}, texture::RenderTexture}; use super::{light::LightUniformBuffers, render_buffer::{BindGroupPair, BufferWrapper}, texture::RenderTexture};
#[allow(dead_code)]
pub(crate) struct LightIndicesGridBuffer { pub(crate) struct LightIndicesGridBuffer {
index_counter_buffer: wgpu::Buffer, index_counter_buffer: wgpu::Buffer,
indices_buffer: wgpu::Buffer, indices_buffer: wgpu::Buffer,

View File

@ -35,6 +35,7 @@ struct Light {
range: f32, range: f32,
intensity: f32, intensity: f32,
smoothness: f32,
spot_cutoff: f32, spot_cutoff: f32,
spot_outer_cutoff: f32, spot_outer_cutoff: f32,
@ -200,7 +201,7 @@ fn blinn_phong_dir_light(world_pos: vec3<f32>, world_norm: vec3<f32>, dir_light:
diffuse_color *= dir_light.diffuse; diffuse_color *= dir_light.diffuse;
specular_color *= dir_light.specular;*/ 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<f32>, world_norm: vec3<f32>, point_light: Light, material: Material, specular_factor: vec3<f32>) -> vec3<f32> { fn blinn_phong_point_light(world_pos: vec3<f32>, world_norm: vec3<f32>, point_light: Light, material: Material, specular_factor: vec3<f32>) -> vec3<f32> {
@ -226,22 +227,8 @@ fn blinn_phong_point_light(world_pos: vec3<f32>, world_norm: vec3<f32>, point_li
var specular_color = specular_strength * (light_color * specular_factor); var specular_color = specular_strength * (light_color * specular_factor);
//// end of specular //// //// end of specular ////
// TODO: Point light range
let distance = length(light_pos - world_pos); let distance = length(light_pos - world_pos);
// TODO: make smoothness in this a configurable value let attenuation = 1.0 - smoothstep(point_light.range * point_light.smoothness, point_light.range, distance);
// 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;*/
ambient_color *= attenuation; ambient_color *= attenuation;
diffuse_color *= attenuation; diffuse_color *= attenuation;

View File

@ -22,6 +22,7 @@ struct Light {
range: f32, range: f32,
intensity: f32, intensity: f32,
smoothness: f32,
spot_cutoff: f32, spot_cutoff: f32,
spot_outer_cutoff: f32, spot_outer_cutoff: f32,