Skip to content

Commit 5c4f355

Browse files
mtsrcart
andcommitted
Rename Light => PointLight and remove unused properties (#1778)
After an inquiry on Reddit about support for Directional Lights and the unused properties on Light, I wanted to clean it up, to hopefully make it ever so slightly more clear for anyone wanting to add additional light types. Co-authored-by: Carter Anderson <[email protected]>
1 parent 8f1eaa6 commit 5c4f355

21 files changed

+66
-79
lines changed

crates/bevy_pbr/src/entity.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{light::Light, material::StandardMaterial, render_graph::PBR_PIPELINE_HANDLE};
1+
use crate::{light::PointLight, material::StandardMaterial, render_graph::PBR_PIPELINE_HANDLE};
22
use bevy_asset::Handle;
33
use bevy_ecs::bundle::Bundle;
44
use bevy_render::{
@@ -42,8 +42,8 @@ impl Default for PbrBundle {
4242

4343
/// A component bundle for "light" entities
4444
#[derive(Debug, Bundle, Default)]
45-
pub struct LightBundle {
46-
pub light: Light,
45+
pub struct PointLightBundle {
46+
pub point_light: PointLight,
4747
pub transform: Transform,
4848
pub global_transform: GlobalTransform,
4949
}

crates/bevy_pbr/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ pub use light::*;
99
pub use material::*;
1010

1111
pub mod prelude {
12-
pub use crate::{entity::*, light::Light, material::StandardMaterial};
12+
pub use crate::{entity::*, light::PointLight, material::StandardMaterial};
1313
}
1414

1515
use bevy_app::prelude::*;
@@ -26,7 +26,7 @@ pub struct PbrPlugin;
2626
impl Plugin for PbrPlugin {
2727
fn build(&self, app: &mut AppBuilder) {
2828
app.add_asset::<StandardMaterial>()
29-
.register_type::<Light>()
29+
.register_type::<PointLight>()
3030
.add_system_to_stage(
3131
CoreStage::PostUpdate,
3232
shader::asset_shader_defs_system::<StandardMaterial>.system(),

crates/bevy_pbr/src/light.rs

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,22 @@
11
use bevy_core::Byteable;
22
use bevy_ecs::reflect::ReflectComponent;
33
use bevy_reflect::Reflect;
4-
use bevy_render::{
5-
camera::{CameraProjection, PerspectiveProjection},
6-
color::Color,
7-
};
4+
use bevy_render::color::Color;
85
use bevy_transform::components::GlobalTransform;
9-
use std::ops::Range;
106

117
/// A point light
128
#[derive(Debug, Reflect)]
139
#[reflect(Component)]
14-
pub struct Light {
10+
pub struct PointLight {
1511
pub color: Color,
16-
pub fov: f32,
17-
pub depth: Range<f32>,
1812
pub intensity: f32,
1913
pub range: f32,
2014
}
2115

22-
impl Default for Light {
16+
impl Default for PointLight {
2317
fn default() -> Self {
24-
Light {
18+
PointLight {
2519
color: Color::rgb(1.0, 1.0, 1.0),
26-
depth: 0.1..50.0,
27-
fov: f32::to_radians(60.0),
2820
intensity: 200.0,
2921
range: 20.0,
3022
}
@@ -33,33 +25,25 @@ impl Default for Light {
3325

3426
#[repr(C)]
3527
#[derive(Debug, Clone, Copy)]
36-
pub(crate) struct LightRaw {
37-
pub proj: [[f32; 4]; 4],
28+
pub(crate) struct PointLightUniform {
3829
pub pos: [f32; 4],
3930
pub color: [f32; 4],
31+
pub inverse_range_squared: f32,
4032
}
4133

42-
unsafe impl Byteable for LightRaw {}
34+
unsafe impl Byteable for PointLightUniform {}
4335

44-
impl LightRaw {
45-
pub fn from(light: &Light, global_transform: &GlobalTransform) -> LightRaw {
46-
let perspective = PerspectiveProjection {
47-
fov: light.fov,
48-
aspect_ratio: 1.0,
49-
near: light.depth.start,
50-
far: light.depth.end,
51-
};
52-
53-
let proj = perspective.get_projection_matrix() * global_transform.compute_matrix();
36+
impl PointLightUniform {
37+
pub fn from(light: &PointLight, global_transform: &GlobalTransform) -> PointLightUniform {
5438
let (x, y, z) = global_transform.translation.into();
5539

5640
// premultiply color by intensity
5741
// we don't use the alpha at all, so no reason to multiply only [0..3]
5842
let color: [f32; 4] = (light.color * light.intensity).into();
59-
LightRaw {
60-
proj: proj.to_cols_array_2d(),
61-
pos: [x, y, z, 1.0 / (light.range * light.range)], // pos.w is the attenuation.
43+
PointLightUniform {
44+
pos: [x, y, z, 1.0],
6245
color,
46+
inverse_range_squared: 1.0 / (light.range * light.range),
6347
}
6448
}
6549
}

crates/bevy_pbr/src/render_graph/lights_node.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
light::{AmbientLight, Light, LightRaw},
2+
light::{AmbientLight, PointLight, PointLightUniform},
33
render_graph::uniform,
44
};
55
use bevy_core::{AsBytes, Byteable};
@@ -20,13 +20,13 @@ use bevy_transform::prelude::*;
2020
#[derive(Debug, Default)]
2121
pub struct LightsNode {
2222
command_queue: CommandQueue,
23-
max_lights: usize,
23+
max_point_lights: usize,
2424
}
2525

2626
impl LightsNode {
2727
pub fn new(max_lights: usize) -> Self {
2828
LightsNode {
29-
max_lights,
29+
max_point_lights: max_lights,
3030
command_queue: CommandQueue::default(),
3131
}
3232
}
@@ -57,7 +57,7 @@ impl SystemNode for LightsNode {
5757
let system = lights_node_system.system().config(|config| {
5858
config.0 = Some(LightsNodeSystemState {
5959
command_queue: self.command_queue.clone(),
60-
max_lights: self.max_lights,
60+
max_point_lights: self.max_point_lights,
6161
light_buffer: None,
6262
staging_buffer: None,
6363
})
@@ -72,7 +72,7 @@ pub struct LightsNodeSystemState {
7272
light_buffer: Option<BufferId>,
7373
staging_buffer: Option<BufferId>,
7474
command_queue: CommandQueue,
75-
max_lights: usize,
75+
max_point_lights: usize,
7676
}
7777

7878
pub fn lights_node_system(
@@ -82,7 +82,7 @@ pub fn lights_node_system(
8282
// TODO: this write on RenderResourceBindings will prevent this system from running in parallel
8383
// with other systems that do the same
8484
mut render_resource_bindings: ResMut<RenderResourceBindings>,
85-
query: Query<(&Light, &GlobalTransform)>,
85+
query: Query<(&PointLight, &GlobalTransform)>,
8686
) {
8787
let state = &mut state;
8888
let render_resource_context = &**render_resource_context;
@@ -91,16 +91,16 @@ pub fn lights_node_system(
9191
let ambient_light: [f32; 4] =
9292
(ambient_light_resource.color * ambient_light_resource.brightness).into();
9393
let ambient_light_size = std::mem::size_of::<[f32; 4]>();
94-
let light_count = query.iter().count();
95-
let size = std::mem::size_of::<LightRaw>();
94+
let point_light_count = query.iter().count();
95+
let size = std::mem::size_of::<PointLightUniform>();
9696
let light_count_size = ambient_light_size + std::mem::size_of::<LightCount>();
97-
let light_array_size = size * light_count;
98-
let light_array_max_size = size * state.max_lights;
99-
let current_light_uniform_size = light_count_size + light_array_size;
100-
let max_light_uniform_size = light_count_size + light_array_max_size;
97+
let point_light_array_size = size * point_light_count;
98+
let point_light_array_max_size = size * state.max_point_lights;
99+
let current_point_light_uniform_size = light_count_size + point_light_array_size;
100+
let max_light_uniform_size = light_count_size + point_light_array_max_size;
101101

102102
if let Some(staging_buffer) = state.staging_buffer {
103-
if light_count == 0 {
103+
if point_light_count == 0 {
104104
return;
105105
}
106106

@@ -132,21 +132,22 @@ pub fn lights_node_system(
132132
let staging_buffer = state.staging_buffer.unwrap();
133133
render_resource_context.write_mapped_buffer(
134134
staging_buffer,
135-
0..current_light_uniform_size as u64,
135+
0..current_point_light_uniform_size as u64,
136136
&mut |data, _renderer| {
137137
// ambient light
138138
data[0..ambient_light_size].copy_from_slice(ambient_light.as_bytes());
139139

140140
// light count
141141
data[ambient_light_size..light_count_size]
142-
.copy_from_slice([light_count as u32, 0, 0, 0].as_bytes());
142+
.copy_from_slice([point_light_count as u32, 0, 0, 0].as_bytes());
143143

144144
// light array
145-
for ((light, global_transform), slot) in query
146-
.iter()
147-
.zip(data[light_count_size..current_light_uniform_size].chunks_exact_mut(size))
148-
{
149-
slot.copy_from_slice(LightRaw::from(&light, &global_transform).as_bytes());
145+
for ((point_light, global_transform), slot) in query.iter().zip(
146+
data[light_count_size..current_point_light_uniform_size].chunks_exact_mut(size),
147+
) {
148+
slot.copy_from_slice(
149+
PointLightUniform::from(&point_light, &global_transform).as_bytes(),
150+
);
150151
}
151152
},
152153
);

crates/bevy_pbr/src/render_graph/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use bevy_render::{
2626
};
2727
use bevy_transform::prelude::GlobalTransform;
2828

29+
pub const MAX_POINT_LIGHTS: usize = 10;
2930
pub(crate) fn add_pbr_graph(world: &mut World) {
3031
{
3132
let mut graph = world.get_resource_mut::<RenderGraph>().unwrap();
@@ -37,7 +38,8 @@ pub(crate) fn add_pbr_graph(world: &mut World) {
3738
node::STANDARD_MATERIAL,
3839
AssetRenderResourcesNode::<StandardMaterial>::new(true),
3940
);
40-
graph.add_system_node(node::LIGHTS, LightsNode::new(10));
41+
42+
graph.add_system_node(node::LIGHTS, LightsNode::new(MAX_POINT_LIGHTS));
4143

4244
// TODO: replace these with "autowire" groups
4345
graph

crates/bevy_pbr/src/render_graph/pbr_pipeline/pbr.frag

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@
3636

3737
const int MAX_LIGHTS = 10;
3838

39-
struct Light {
40-
mat4 proj;
39+
struct PointLight {
4140
vec4 pos;
4241
vec4 color;
42+
float inverseRangeSquared;
4343
};
4444

4545
layout(location = 0) in vec3 v_WorldPosition;
@@ -62,7 +62,7 @@ layout(std140, set = 0, binding = 1) uniform CameraPosition {
6262
layout(std140, set = 1, binding = 0) uniform Lights {
6363
vec4 AmbientColor;
6464
uvec4 NumLights;
65-
Light SceneLights[MAX_LIGHTS];
65+
PointLight PointLights[MAX_LIGHTS];
6666
};
6767

6868
layout(set = 3, binding = 0) uniform StandardMaterial_base_color {
@@ -130,9 +130,8 @@ float pow5(float x) {
130130
//
131131
// light radius is a non-physical construct for efficiency purposes,
132132
// because otherwise every light affects every fragment in the scene
133-
float getDistanceAttenuation(const vec3 posToLight, float inverseRadiusSquared) {
134-
float distanceSquare = dot(posToLight, posToLight);
135-
float factor = distanceSquare * inverseRadiusSquared;
133+
float getDistanceAttenuation(float distanceSquare, float inverseRangeSquared) {
134+
float factor = distanceSquare * inverseRangeSquared;
136135
float smoothFactor = saturate(1.0 - factor * factor);
137136
float attenuation = smoothFactor * smoothFactor;
138137
return attenuation * 1.0 / max(distanceSquare, 1e-4);
@@ -343,13 +342,14 @@ void main() {
343342
// accumulate color
344343
vec3 light_accum = vec3(0.0);
345344
for (int i = 0; i < int(NumLights.x) && i < MAX_LIGHTS; ++i) {
346-
Light light = SceneLights[i];
345+
PointLight light = PointLights[i];
347346

348-
vec3 lightDir = light.pos.xyz - v_WorldPosition.xyz;
349-
vec3 L = normalize(lightDir);
347+
vec3 light_to_frag = light.pos.xyz - v_WorldPosition.xyz;
348+
vec3 L = normalize(light_to_frag);
349+
float distance_square = dot(light_to_frag, light_to_frag);
350350

351351
float rangeAttenuation =
352-
getDistanceAttenuation(lightDir, light.pos.w);
352+
getDistanceAttenuation(distance_square, light.inverseRangeSquared);
353353

354354
vec3 H = normalize(L + V);
355355
float NoL = saturate(dot(N, L));

examples/3d/3d_scene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn setup(
2828
..Default::default()
2929
});
3030
// light
31-
commands.spawn_bundle(LightBundle {
31+
commands.spawn_bundle(PointLightBundle {
3232
transform: Transform::from_xyz(4.0, 8.0, 4.0),
3333
..Default::default()
3434
});

examples/3d/load_gltf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
2020
..Default::default()
2121
});
2222
commands
23-
.spawn_bundle(LightBundle {
23+
.spawn_bundle(PointLightBundle {
2424
transform: Transform::from_xyz(3.0, 5.0, 3.0),
2525
..Default::default()
2626
})

examples/3d/msaa.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn setup(
2525
..Default::default()
2626
});
2727
// light
28-
commands.spawn_bundle(LightBundle {
28+
commands.spawn_bundle(PointLightBundle {
2929
transform: Transform::from_xyz(4.0, 8.0, 4.0),
3030
..Default::default()
3131
});

examples/3d/orthographic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn setup(
5454
..Default::default()
5555
});
5656
// light
57-
commands.spawn_bundle(LightBundle {
57+
commands.spawn_bundle(PointLightBundle {
5858
transform: Transform::from_xyz(3.0, 8.0, 5.0),
5959
..Default::default()
6060
});

examples/3d/parenting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn setup(
5252
});
5353
});
5454
// light
55-
commands.spawn_bundle(LightBundle {
55+
commands.spawn_bundle(PointLightBundle {
5656
transform: Transform::from_xyz(4.0, 5.0, -4.0),
5757
..Default::default()
5858
});

examples/3d/pbr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ fn setup(
3939
}
4040
}
4141
// light
42-
commands.spawn_bundle(LightBundle {
42+
commands.spawn_bundle(PointLightBundle {
4343
transform: Transform::from_translation(Vec3::new(0.0, 5.0, 5.0)),
4444
..Default::default()
4545
});

examples/3d/spawner.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn setup(
4141
mut materials: ResMut<Assets<StandardMaterial>>,
4242
) {
4343
// light
44-
commands.spawn_bundle(LightBundle {
44+
commands.spawn_bundle(PointLightBundle {
4545
transform: Transform::from_xyz(4.0, -4.0, 5.0),
4646
..Default::default()
4747
});

examples/3d/update_gltf_scene.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn setup(
2424
mut scene_spawner: ResMut<SceneSpawner>,
2525
mut scene_instance: ResMut<SceneInstance>,
2626
) {
27-
commands.spawn_bundle(LightBundle {
27+
commands.spawn_bundle(PointLightBundle {
2828
transform: Transform::from_xyz(4.0, 5.0, 4.0),
2929
..Default::default()
3030
});

examples/3d/wireframe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ fn setup(
4646
// This enables wireframe drawing on this entity
4747
.insert(Wireframe);
4848
// light
49-
commands.spawn_bundle(LightBundle {
49+
commands.spawn_bundle(PointLightBundle {
5050
transform: Transform::from_xyz(4.0, 8.0, 4.0),
5151
..Default::default()
5252
});

examples/android/android.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn setup(
3030
..Default::default()
3131
});
3232
// light
33-
commands.spawn_bundle(LightBundle {
33+
commands.spawn_bundle(PointLightBundle {
3434
transform: Transform::from_xyz(4.0, 8.0, 4.0),
3535
..Default::default()
3636
});

examples/asset/asset_loading.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn setup(
6565
..Default::default()
6666
});
6767
// light
68-
commands.spawn_bundle(LightBundle {
68+
commands.spawn_bundle(PointLightBundle {
6969
transform: Transform::from_xyz(4.0, 5.0, 4.0),
7070
..Default::default()
7171
});

examples/asset/hot_asset_reloading.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
2323
// mesh
2424
commands.spawn_scene(scene_handle);
2525
// light
26-
commands.spawn_bundle(LightBundle {
26+
commands.spawn_bundle(PointLightBundle {
2727
transform: Transform::from_xyz(4.0, 5.0, 4.0),
2828
..Default::default()
2929
});

0 commit comments

Comments
 (0)