Skip to content

Commit

Permalink
Improve lighting in more examples (bevyengine#12021)
Browse files Browse the repository at this point in the history
# Objective

- bevyengine#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 bevyengine/bevy-website#1023.
- Related: bevyengine#11982, bevyengine#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.
  • Loading branch information
BD103 authored and msvbg committed Feb 26, 2024
1 parent d094031 commit 7e14643
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 44 deletions.
2 changes: 1 addition & 1 deletion examples/3d/skybox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
));

Expand Down
136 changes: 95 additions & 41 deletions examples/3d/spotlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}

Expand All @@ -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));
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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)>) {
Expand All @@ -132,27 +164,49 @@ fn movement(
time: Res<Time>,
mut query: Query<&mut Transform, With<Movable>>,
) {
// Calculate translation to move the cubes and ground plane
let mut translation = Vec3::ZERO;

// 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 {
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;
}
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();

transform.translation += time.delta_seconds() * 2.0 * direction;
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));
}
}
10 changes: 10 additions & 0 deletions examples/animation/animated_transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions examples/animation/gltf_skinned_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
});

Expand Down

0 comments on commit 7e14643

Please sign in to comment.