Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change light defaults & fix light examples #11581

Merged
merged 23 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_pbr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub mod prelude {
SpotLightBundle,
},
fog::{FogFalloff, FogSettings},
light::{AmbientLight, DirectionalLight, PointLight, SpotLight},
light::{light_consts, AmbientLight, DirectionalLight, PointLight, SpotLight},
light_probe::{
environment_map::{EnvironmentMapLight, ReflectionProbeBundle},
LightProbe,
Expand Down
73 changes: 67 additions & 6 deletions crates/bevy_pbr/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,61 @@ use bevy_utils::{tracing::warn, EntityHashMap};

use crate::*;

/// Constants for operating with the light units: lumens, and lux.
pub mod light_consts {
/// Approximations for converting the wattage of lamps to lumens.
///
/// The **lumen** (symbol: **lm**) is the unit of [luminous flux], a measure
/// of the total quantity of [visible light] emitted by a source per unit of
/// time, in the [International System of Units] (SI).
///
/// For more information, see [wikipedia](https://en.wikipedia.org/wiki/Lumen_(unit))
///
/// [luminous flux]: https://en.wikipedia.org/wiki/Luminous_flux
/// [visible light]: https://en.wikipedia.org/wiki/Visible_light
/// [International System of Units]: https://en.wikipedia.org/wiki/International_System_of_Units
pub mod lumens {
pub const LUMENS_PER_LED_WATTS: f32 = 90.0;
pub const LUMENS_PER_INCANDESCENT_WATTS: f32 = 13.8;
pub const LUMENS_PER_HALOGEN_WATTS: f32 = 19.8;
}

/// Predefined for lux values in several locations.
///
/// The **lux** (symbol: **lx**) is the unit of [illuminance], or [luminous flux] per unit area,
/// in the [International System of Units] (SI). It is equal to one lumen per square metre.
///
/// For more information, see [wikipedia](https://en.wikipedia.org/wiki/Lux)
///
/// [illuminance]: https://en.wikipedia.org/wiki/Illuminance
/// [luminous flux]: https://en.wikipedia.org/wiki/Luminous_flux
/// [International System of Units]: https://en.wikipedia.org/wiki/International_System_of_Units
pub mod lux {
/// The amount of light (lux) in a moonless, overcast night sky. (starlight)
pub const MOONLESS_NIGHT: f32 = 0.0001;
/// The amount of light (lux) during a full moon on a clear night.
pub const FULL_MOON_NIGHT: f32 = 0.05;
/// The amount of light (lux) during the dark limit of civil twilight under a clear sky.
pub const CIVIL_TWILIGHT: f32 = 3.4;
doonv marked this conversation as resolved.
Show resolved Hide resolved
/// The amount of light (lux) in family living room lights.
pub const LIVING_ROOM: f32 = 50.;
/// The amount of light (lux) in a office building hallway/toilet lighting.
doonv marked this conversation as resolved.
Show resolved Hide resolved
pub const HALLWAY: f32 = 80.;
/// The amount of light (lux) in a family living room lights.
doonv marked this conversation as resolved.
Show resolved Hide resolved
pub const DARK_OVERCAST_DAY: f32 = 100.;
/// The amount of light (lux) in a office.
doonv marked this conversation as resolved.
Show resolved Hide resolved
pub const OFFICE: f32 = 320.;
/// The amount of light (lux) during sunrise or sunset on a clear day.
pub const CLEAR_SUNRISE: f32 = 400.;
/// The amount of light (lux) on very dark overcast day.
pub const OVERCAST_DAY: f32 = 1000.;
doonv marked this conversation as resolved.
Show resolved Hide resolved
/// The amount of light (lux) in full daylight. (not direct sun)
doonv marked this conversation as resolved.
Show resolved Hide resolved
pub const FULL_DAYLIGHT: f32 = 10_000.;
/// The amount of light (lux) in direct sunlight.
pub const DIRECT_SUNLIGHT: f32 = 50_000.;
}
}

/// A light that emits light in all directions from a central point.
///
/// Real-world values for `intensity` (luminous power in lumens) based on the electrical power
Expand Down Expand Up @@ -57,7 +112,7 @@ impl Default for PointLight {
fn default() -> Self {
PointLight {
color: Color::rgb(1.0, 1.0, 1.0),
intensity: 800.0, // Roughly a 60W non-halogen incandescent bulb
intensity: 4000.0, // Roughly a 300W non-halogen incandescent bulb
doonv marked this conversation as resolved.
Show resolved Hide resolved
range: 20.0,
radius: 0.0,
shadows_enabled: false,
Expand Down Expand Up @@ -125,7 +180,7 @@ impl Default for SpotLight {
// a quarter arc attenuating from the center
Self {
color: Color::rgb(1.0, 1.0, 1.0),
intensity: 800.0, // Roughly a 60W non-halogen incandescent bulb
intensity: 4000.0, // Roughly a 300W non-halogen incandescent bulb
doonv marked this conversation as resolved.
Show resolved Hide resolved
range: 20.0,
radius: 0.0,
shadows_enabled: false,
Expand Down Expand Up @@ -206,7 +261,7 @@ impl Default for DirectionalLight {
fn default() -> Self {
DirectionalLight {
color: Color::rgb(1.0, 1.0, 1.0),
illuminance: 100000.0,
illuminance: light_consts::lux::FULL_DAYLIGHT,
shadows_enabled: false,
shadow_depth_bias: Self::DEFAULT_SHADOW_DEPTH_BIAS,
shadow_normal_bias: Self::DEFAULT_SHADOW_NORMAL_BIAS,
Expand Down Expand Up @@ -566,7 +621,7 @@ fn calculate_cascade(
/// # use bevy_ecs::system::ResMut;
/// # use bevy_pbr::AmbientLight;
/// fn setup_ambient_light(mut ambient_light: ResMut<AmbientLight>) {
/// ambient_light.brightness = 20.0;
/// ambient_light.brightness = 100.0;
/// }
/// ```
#[derive(Resource, Clone, Debug, ExtractResource, Reflect)]
Expand All @@ -580,11 +635,17 @@ pub struct AmbientLight {
impl Default for AmbientLight {
fn default() -> Self {
Self {
color: Color::rgb(1.0, 1.0, 1.0),
brightness: 8.0,
color: Color::WHITE,
brightness: 500.0,
}
}
}
impl AmbientLight {
pub const NONE: AmbientLight = AmbientLight {
color: Color::WHITE,
brightness: 0.0,
};
}

/// Add this component to make a [`Mesh`](bevy_render::mesh::Mesh) not cast shadows.
#[derive(Component, Reflect, Default)]
Expand Down
14 changes: 11 additions & 3 deletions crates/bevy_render/src/camera/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ pub struct ExposureSettings {
}

impl ExposureSettings {
pub const SUNLIGHT: Self = Self {
ev100: Self::EV100_SUNLIGHT,
};
pub const OVERCAST: Self = Self {
ev100: Self::EV100_OVERCAST,
};
pub const INDOOR: Self = Self {
ev100: Self::EV100_INDOOR,
};

pub const EV100_SUNLIGHT: f32 = 15.0;
pub const EV100_OVERCAST: f32 = 12.0;
pub const EV100_INDOOR: f32 = 7.0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth creating several constants for indoor lighting. The linked wikipedia article says:

Sports events, stage shows, and the like = 8–9
Offices and work areas = 7–8
Home interiors = 5–7

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kind of like the current granularity. Something like "office" feels too specific to be correct. All of these will be +- 2 or 3. Anyone manually setting exposure will likely be doing it artistically / without exact constants anyway.

Expand All @@ -115,9 +125,7 @@ impl ExposureSettings {

impl Default for ExposureSettings {
fn default() -> Self {
Self {
ev100: Self::EV100_INDOOR,
}
Self::OVERCAST
}
}

Expand Down
8 changes: 4 additions & 4 deletions examples/3d/3d_gizmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ fn setup(
..default()
});
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 250000.0,
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: light_consts::lux::FULL_DAYLIGHT,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
transform: Transform::from_xyz(4.0, 8.0, 4.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});

Expand Down
8 changes: 4 additions & 4 deletions examples/3d/3d_scene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ fn setup(
..default()
});
// light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 250_000.0,
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: light_consts::lux::FULL_DAYLIGHT,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
transform: Transform::from_xyz(4.0, 8.0, 4.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
// camera
Expand Down
9 changes: 4 additions & 5 deletions examples/3d/3d_shapes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,13 @@ fn setup(
));
}

commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500000.0,
range: 100.,
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: light_consts::lux::FULL_DAYLIGHT,
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(8.0, 16.0, 8.0),
transform: Transform::from_xyz(8.0, 16.0, 8.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});

Expand Down
5 changes: 1 addition & 4 deletions examples/3d/3d_viewport_to_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ fn setup(
// light
commands.spawn(DirectionalLightBundle {
transform: Transform::from_translation(Vec3::ONE).looking_at(Vec3::ZERO, Vec3::Y),
directional_light: DirectionalLight {
illuminance: 2000.0,
..default()
},
directional_light: DirectionalLight::default(),
..default()
});

Expand Down
2 changes: 1 addition & 1 deletion examples/3d/animated_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
EnvironmentMapLight {
diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
intensity: 1500.0,
intensity: 10_000.0,
},
));

Expand Down
2 changes: 1 addition & 1 deletion examples/3d/anti_aliasing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ fn setup(
// Light
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 3000.0,
illuminance: light_consts::lux::FULL_DAYLIGHT,
shadows_enabled: true,
..default()
},
Expand Down
2 changes: 1 addition & 1 deletion examples/3d/atmospheric_fog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ fn setup_terrain_scene(
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
color: Color::rgb(0.98, 0.95, 0.82),
illuminance: 3000.0,
illuminance: light_consts::lux::FULL_DAYLIGHT,
shadows_enabled: true,
..default()
},
Expand Down
8 changes: 4 additions & 4 deletions examples/3d/blend_modes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,12 @@ fn setup(
}

// Light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 150_000.0,
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: light_consts::lux::FULL_DAYLIGHT,
..default()
},
transform: Transform::from_xyz(4.0, 8.0, 4.0),
transform: Transform::from_xyz(4.0, 8.0, 4.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});

Expand Down
2 changes: 2 additions & 0 deletions examples/3d/bloom_3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use bevy::{
tonemapping::Tonemapping,
},
prelude::*,
render::camera::ExposureSettings,
};
use std::{
collections::hash_map::DefaultHasher,
Expand Down Expand Up @@ -36,6 +37,7 @@ fn setup_scene(
..default()
},
BloomSettings::default(), // 3. Enable bloom for the camera
ExposureSettings::INDOOR,
doonv marked this conversation as resolved.
Show resolved Hide resolved
));

let material_emissive1 = materials.add(StandardMaterial {
Expand Down
8 changes: 2 additions & 6 deletions examples/3d/deferred_rendering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ fn main() {
App::new()
.insert_resource(Msaa::Off)
.insert_resource(DefaultOpaqueRendererMethod::deferred())
.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 1.0 / 5.0f32,
})
.insert_resource(DirectionalLightShadowMap { size: 4096 })
.add_plugins(DefaultPlugins)
.insert_resource(Normal(None))
Expand Down Expand Up @@ -62,7 +58,7 @@ fn setup(
EnvironmentMapLight {
diffuse_map: asset_server.load("environment_maps/pisa_diffuse_rgb9e5_zstd.ktx2"),
specular_map: asset_server.load("environment_maps/pisa_specular_rgb9e5_zstd.ktx2"),
intensity: 150.0,
intensity: 1000.0,
},
DepthPrepass,
MotionVectorPrepass,
Expand All @@ -72,7 +68,7 @@ fn setup(

commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 4000.0,
illuminance: light_consts::lux::FULL_DAYLIGHT,
shadows_enabled: true,
..default()
},
Expand Down
6 changes: 4 additions & 2 deletions examples/3d/fog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
use bevy::{
pbr::{NotShadowCaster, NotShadowReceiver},
prelude::*,
render::camera::ExposureSettings,
};

fn main() {
App::new()
.insert_resource(AmbientLight::NONE)
.add_plugins(DefaultPlugins)
.add_systems(
Startup,
Expand All @@ -41,6 +43,7 @@ fn setup_camera_fog(mut commands: Commands) {
},
..default()
},
ExposureSettings::INDOOR,
doonv marked this conversation as resolved.
Show resolved Hide resolved
));
}

Expand Down Expand Up @@ -128,8 +131,7 @@ fn setup_pyramid_scene(
commands.spawn(PointLightBundle {
transform: Transform::from_xyz(0.0, 1.0, 0.0),
point_light: PointLight {
intensity: 300_000.,
range: 100.,
intensity: 10_000.,
doonv marked this conversation as resolved.
Show resolved Hide resolved
shadows_enabled: true,
..default()
},
Expand Down
11 changes: 5 additions & 6 deletions examples/3d/generate_custom_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,15 @@ fn setup(
Transform::from_xyz(1.8, 1.8, 1.8).looking_at(Vec3::ZERO, Vec3::Y);

// Camera in 3D space.
commands.spawn(Camera3dBundle {
commands.spawn((Camera3dBundle {
transform: camera_and_light_transform,
..default()
});
},));
doonv marked this conversation as resolved.
Show resolved Hide resolved

// Light up the scene.
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 100_000.0,
range: 100.0,
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: light_consts::lux::FULL_DAYLIGHT,
..default()
},
transform: camera_and_light_transform,
Expand Down
8 changes: 4 additions & 4 deletions examples/3d/lighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::f32::consts::PI;

use bevy::{
pbr::CascadeShadowConfigBuilder,
pbr::{light_consts, CascadeShadowConfigBuilder},
prelude::*,
render::camera::{ExposureSettings, PhysicalCameraParameters},
};
Expand All @@ -14,8 +14,8 @@ fn main() {
.add_plugins(DefaultPlugins)
.insert_resource(Parameters(PhysicalCameraParameters {
aperture_f_stops: 1.0,
shutter_speed_s: 1.0 / 15.0,
sensitivity_iso: 400.0,
shutter_speed_s: 1.0 / 100.0,
sensitivity_iso: 100.0,
}))
.add_systems(Startup, setup)
.add_systems(Update, (update_exposure, movement, animate_light_direction))
Expand Down Expand Up @@ -220,7 +220,7 @@ fn setup(
// directional 'sun' light
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
illuminance: 100.0,
illuminance: light_consts::lux::OVERCAST_DAY,
shadows_enabled: true,
..default()
},
Expand Down
Loading