From b96193e6ca8d722204d2f0c7b99c3d81d6e213b9 Mon Sep 17 00:00:00 2001 From: BD103 <59022059+BD103@users.noreply.github.com> Date: Mon, 26 Feb 2024 12:32:23 -0500 Subject: [PATCH] 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: image After: image ### Spotlight Before: image After: image ### Animated Transform Before: image After: image ### gLTF Skinned Mesh Before: image After: image --- ## Changelog - Increased lighting in `skybox`, `spotlight`, `animated_transform`, and `gltf_skinned_mesh` examples. - Improved usability of `spotlight` example. --- examples/3d/skybox.rs | 2 +- examples/3d/spotlight.rs | 138 ++++++++++++++++------- examples/animation/animated_transform.rs | 10 ++ examples/animation/gltf_skinned_mesh.rs | 5 +- 4 files changed, 110 insertions(+), 45 deletions(-) diff --git a/examples/3d/skybox.rs b/examples/3d/skybox.rs index 6a7efad542..0a5f715b57 100644 --- a/examples/3d/skybox.rs +++ b/examples/3d/skybox.rs @@ -80,7 +80,7 @@ fn setup(mut commands: Commands, asset_server: Res) { CameraController::default(), Skybox { image: skybox_handle.clone(), - brightness: 150.0, + brightness: 1000.0, }, )); diff --git a/examples/3d/spotlight.rs b/examples/3d/spotlight.rs index 81c3472af3..9f72f5f0cf 100644 --- a/examples/3d/spotlight.rs +++ b/examples/3d/spotlight.rs @@ -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>, ) { // 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