Improve lighting in more examples (#12021)

# Objective

- #11868 changed the lighting system, forcing lights to increase their
intensity. The PR fixed most examples, but missed a few. These I later
caught in https://github.com/bevyengine/bevy-website/pull/1023.
- Related: #11982, #11981.
- While there, I noticed that the spotlight example could use a few easy
improvements.

## Solution

- Increase lighting in `skybox`, `spotlight`, `animated_transform`, and
`gltf_skinned_mesh`.
- Improve spotlight example.
- Make ground plane move with cubes, so they don't phase into each
other.
  - Batch spawn cubes.
  - Add controls text.
  - Change controls to allow rotating around spotlights.

## Showcase

### Skybox

Before:

<img width="1392" alt="image"
src="https://github.com/bevyengine/bevy/assets/59022059/8ba00d74-6d68-4414-97a8-28afb8305570">

After:

<img width="1392" alt="image"
src="https://github.com/bevyengine/bevy/assets/59022059/ad15c471-6979-4dda-9889-9189136d8404">

### Spotlight

Before:

<img width="1392" alt="image"
src="https://github.com/bevyengine/bevy/assets/59022059/53f966de-acf3-46b8-8299-0005c4cb8da0">

After:

<img width="1392" alt="image"
src="https://github.com/bevyengine/bevy/assets/59022059/05c73c1e-0739-4226-83d6-e4249a9105e0">

### Animated Transform

Before:

<img width="1392" alt="image"
src="https://github.com/bevyengine/bevy/assets/59022059/6d7d4ea0-e22e-42a5-9905-ea1731d474cf">

After:

<img width="1392" alt="image"
src="https://github.com/bevyengine/bevy/assets/59022059/f1ee08d6-d17a-4391-91a6-d903b9fbdc3c">

### gLTF Skinned Mesh

Before:

<img width="1392" alt="image"
src="https://github.com/bevyengine/bevy/assets/59022059/547569a6-d13b-4fe0-a8c1-e11f02c4f9a2">

After:

<img width="1392" alt="image"
src="https://github.com/bevyengine/bevy/assets/59022059/34517aba-09e4-4e9b-982a-a4a8b893c48a">

---

## Changelog

- Increased lighting in `skybox`, `spotlight`, `animated_transform`, and
`gltf_skinned_mesh` examples.
- Improved usability of `spotlight` example.
This commit is contained in:
BD103 2024-02-26 12:32:23 -05:00 committed by GitHub
parent f45450e26b
commit b96193e6ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 110 additions and 45 deletions

View file

@ -80,7 +80,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
CameraController::default(),
Skybox {
image: skybox_handle.clone(),
brightness: 150.0,
brightness: 1000.0,
},
));

View file

@ -5,6 +5,13 @@ use std::f32::consts::*;
use bevy::{pbr::NotShadowCaster, prelude::*};
use rand::{rngs::StdRng, Rng, SeedableRng};
const INSTRUCTIONS: &str = "\
Controls
--------
Horizontal Movement: WASD
Vertical Movement: Space and Shift
Rotate Camera: Left and Right Arrows";
fn main() {
App::new()
.insert_resource(AmbientLight {
@ -13,7 +20,7 @@ fn main() {
})
.add_plugins(DefaultPlugins)
.add_systems(Startup, setup)
.add_systems(Update, (light_sway, movement))
.add_systems(Update, (light_sway, movement, rotation))
.run();
}
@ -27,30 +34,38 @@ fn setup(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
// ground plane
commands.spawn(PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(100.0, 100.0)),
material: materials.add(LegacyColor::WHITE),
..default()
});
commands.spawn((
PbrBundle {
mesh: meshes.add(Plane3d::default().mesh().size(100.0, 100.0)),
material: materials.add(LegacyColor::WHITE),
..default()
},
Movable,
));
// cubes
let mut rng = StdRng::seed_from_u64(19878367467713);
let cube_mesh = meshes.add(Cuboid::new(0.5, 0.5, 0.5));
let blue = materials.add(LegacyColor::rgb_u8(124, 144, 255));
for _ in 0..40 {
let x = rng.gen_range(-5.0..5.0);
let y = rng.gen_range(0.0..3.0);
let z = rng.gen_range(-5.0..5.0);
commands.spawn((
PbrBundle {
mesh: cube_mesh.clone(),
material: blue.clone(),
transform: Transform::from_xyz(x, y, z),
..default()
},
Movable,
));
}
commands.spawn_batch(
std::iter::repeat_with(move || {
let x = rng.gen_range(-5.0..5.0);
let y = rng.gen_range(0.0..3.0);
let z = rng.gen_range(-5.0..5.0);
(
PbrBundle {
mesh: cube_mesh.clone(),
material: blue.clone(),
transform: Transform::from_xyz(x, y, z),
..default()
},
Movable,
)
})
.take(40),
);
let sphere_mesh = meshes.add(Sphere::new(0.05).mesh().uv(32, 18));
let sphere_mesh_direction = meshes.add(Sphere::new(0.1).mesh().uv(32, 18));
@ -64,6 +79,7 @@ fn setup(
emissive: LegacyColor::rgba_linear(50.0, 0.0, 0.0, 0.0),
..default()
});
for x in 0..4 {
for z in 0..4 {
let x = x as f32 - 2.0;
@ -74,7 +90,7 @@ fn setup(
transform: Transform::from_xyz(1.0 + x, 2.0, z)
.looking_at(Vec3::new(1.0 + x, 0.0, z), Vec3::X),
spot_light: SpotLight {
intensity: 4000.0, // lumens
intensity: 40_000.0, // lumens
color: LegacyColor::WHITE,
shadows_enabled: true,
inner_angle: PI / 4.0 * 0.85,
@ -111,6 +127,22 @@ fn setup(
transform: Transform::from_xyz(-4.0, 5.0, 10.0).looking_at(Vec3::ZERO, Vec3::Y),
..default()
});
commands.spawn(
TextBundle::from_section(
INSTRUCTIONS,
TextStyle {
font_size: 20.0,
..default()
},
)
.with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(12.0),
left: Val::Px(12.0),
..default()
}),
);
}
fn light_sway(time: Res<Time>, mut query: Query<(&mut Transform, &mut SpotLight)>) {
@ -132,27 +164,49 @@ fn movement(
time: Res<Time>,
mut query: Query<&mut Transform, With<Movable>>,
) {
for mut transform in &mut query {
let mut direction = Vec3::ZERO;
if input.pressed(KeyCode::ArrowUp) {
direction.z -= 1.0;
}
if input.pressed(KeyCode::ArrowDown) {
direction.z += 1.0;
}
if input.pressed(KeyCode::ArrowLeft) {
direction.x -= 1.0;
}
if input.pressed(KeyCode::ArrowRight) {
direction.x += 1.0;
}
if input.pressed(KeyCode::PageUp) {
direction.y += 1.0;
}
if input.pressed(KeyCode::PageDown) {
direction.y -= 1.0;
}
// Calculate translation to move the cubes and ground plane
let mut translation = Vec3::ZERO;
transform.translation += time.delta_seconds() * 2.0 * direction;
// Horizontal forward and backward movement
if input.pressed(KeyCode::KeyW) {
translation.z += 1.0;
} else if input.pressed(KeyCode::KeyS) {
translation.z -= 1.0;
}
// Horizontal left and right movement
if input.pressed(KeyCode::KeyA) {
translation.x += 1.0;
} else if input.pressed(KeyCode::KeyD) {
translation.x -= 1.0;
}
// Vertical movement
if input.pressed(KeyCode::ShiftLeft) {
translation.y += 1.0;
} else if input.pressed(KeyCode::Space) {
translation.y -= 1.0;
}
translation *= 2.0 * time.delta_seconds();
// Apply translation
for mut transform in &mut query {
transform.translation += translation;
}
}
fn rotation(
mut query: Query<&mut Transform, With<Camera>>,
input: Res<ButtonInput<KeyCode>>,
time: Res<Time>,
) {
let mut transform = query.single_mut();
let delta = time.delta_seconds();
if input.pressed(KeyCode::ArrowLeft) {
transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(delta));
} else if input.pressed(KeyCode::ArrowRight) {
transform.rotate_around(Vec3::ZERO, Quat::from_rotation_y(-delta));
}
}

View file

@ -28,6 +28,16 @@ fn setup(
..default()
});
// Light
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 500_000.0,
..default()
},
transform: Transform::from_xyz(0.0, 2.5, 0.0),
..default()
});
// Let's use the `Name` component to target entities. We can use anything we
// like, but names are convenient.
let planet = Name::new("planet");

View file

@ -9,7 +9,7 @@ fn main() {
App::new()
.add_plugins(DefaultPlugins)
.insert_resource(AmbientLight {
brightness: 150.0,
brightness: 750.0,
..default()
})
.add_systems(Startup, setup)
@ -20,7 +20,8 @@ fn main() {
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Create a camera
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(-2.0, 2.5, 5.0).looking_at(Vec3::ZERO, Vec3::Y),
transform: Transform::from_xyz(-2.0, 2.5, 5.0)
.looking_at(Vec3::new(0.0, 1.0, 0.0), Vec3::Y),
..default()
});