Last active
January 18, 2023 23:58
-
-
Save rparrett/fdc16838feba639e7aa1c20e1a334360 to your computer and use it in GitHub Desktop.
Bevy 0.9 Directional Light Playground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::f32::consts::PI; | |
use bevy::{pbr::NotShadowCaster, prelude::*}; | |
#[derive(Component)] | |
struct Movable; | |
#[derive(Component)] | |
struct Rotation { | |
x: f32, | |
y: f32, | |
} | |
#[derive(Component)] | |
struct HalfSize(f32); | |
fn main() { | |
App::new() | |
.add_plugins(DefaultPlugins) | |
.add_startup_system(setup) | |
.add_system(movement) | |
.add_system(rotation) | |
.add_system(scale) | |
.add_system(proj) | |
.run(); | |
} | |
fn setup( | |
mut commands: Commands, | |
mut meshes: ResMut<Assets<Mesh>>, | |
mut materials: ResMut<Assets<StandardMaterial>>, | |
) { | |
let extents = 2000.0; | |
let num = 75; | |
let spacing = extents / (num - 1) as f32; | |
// plane | |
commands.spawn(PbrBundle { | |
mesh: meshes.add(Mesh::from(shape::Plane { size: 2100.0 })), | |
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()), | |
..default() | |
}); | |
// gnomon | |
let unlit = materials.add(StandardMaterial { | |
base_color: Color::rgb(0.8, 0.7, 0.6), | |
unlit: true, | |
..default() | |
}); | |
let gnomon = meshes.add(Mesh::from(shape::Cube { size: 1.0 })); | |
for x in 0..num { | |
for z in 0..num { | |
commands.spawn(PbrBundle { | |
mesh: gnomon.clone(), | |
material: unlit.clone(), | |
transform: Transform::from_xyz( | |
-extents / 2. + x as f32 * spacing, | |
0.5, | |
-extents / 2. + z as f32 * spacing, | |
) | |
.with_scale(Vec3::new(1., 20., 1.)), | |
..default() | |
}); | |
} | |
} | |
// light marker | |
let red = materials.add(StandardMaterial { | |
base_color: Color::rgb(1., 0.2, 0.2), | |
unlit: true, | |
..default() | |
}); | |
commands.spawn(( | |
PbrBundle { | |
mesh: meshes.add(Mesh::from(shape::Cube { size: 2.0 })), | |
material: red, | |
transform: Transform::from_xyz(0., 15., 0.), | |
..default() | |
}, | |
NotShadowCaster, | |
Movable, | |
)); | |
// directional 'sun' light | |
commands.spawn(( | |
DirectionalLightBundle { | |
directional_light: DirectionalLight { | |
shadows_enabled: true, | |
..default() | |
}, | |
..default() | |
}, | |
Movable, | |
Rotation { | |
x: -PI / 4., | |
y: -PI / 4., | |
}, | |
HalfSize(40.), | |
)); | |
// camera | |
commands.spawn(Camera3dBundle { | |
transform: Transform::from_xyz(100.0, 400.5, 800.0).looking_at(Vec3::ZERO, Vec3::Y), | |
..default() | |
}); | |
} | |
fn movement( | |
input: Res<Input<KeyCode>>, | |
time: Res<Time>, | |
mut query: Query<&mut Transform, With<Movable>>, | |
) { | |
for mut transform in &mut query { | |
let mut direction = Vec3::ZERO; | |
let mut speed = 20.; | |
if input.pressed(KeyCode::Up) { | |
direction.z -= 1.0; | |
} | |
if input.pressed(KeyCode::Down) { | |
direction.z += 1.0; | |
} | |
if input.pressed(KeyCode::Left) { | |
direction.x -= 1.0; | |
} | |
if input.pressed(KeyCode::Right) { | |
direction.x += 1.0; | |
} | |
if input.any_pressed([KeyCode::LShift, KeyCode::RShift]) { | |
speed *= 10.; | |
} | |
transform.translation += time.delta_seconds() * speed * direction; | |
} | |
} | |
fn rotation( | |
input: Res<Input<KeyCode>>, | |
time: Res<Time>, | |
mut query: Query<(&mut Transform, &mut Rotation), With<Movable>>, | |
) { | |
for (mut transform, mut rotation) in &mut query { | |
let mut direction = 0.; | |
if input.pressed(KeyCode::Q) { | |
direction -= 1.0; | |
} | |
if input.pressed(KeyCode::E) { | |
direction += 1.0; | |
} | |
rotation.y += time.delta_seconds() * 1.0 * direction; | |
let mut direction = 0.; | |
if input.pressed(KeyCode::W) { | |
direction -= 1.0; | |
} | |
if input.pressed(KeyCode::S) { | |
direction += 1.0; | |
} | |
rotation.x += time.delta_seconds() * 1.0 * direction; | |
transform.rotation = Quat::from_euler(EulerRot::YXZ, rotation.y, rotation.x, 0.); | |
} | |
} | |
fn scale( | |
input: Res<Input<KeyCode>>, | |
time: Res<Time>, | |
mut query: Query<&mut Transform, With<Movable>>, | |
) { | |
for mut transform in &mut query { | |
let mut direction = 0.; | |
if input.pressed(KeyCode::Z) { | |
direction -= 1.; | |
} | |
if input.pressed(KeyCode::X) { | |
direction += 1.; | |
} | |
transform.scale += time.delta_seconds() * 1.0 * direction; | |
} | |
} | |
fn proj( | |
input: Res<Input<KeyCode>>, | |
time: Res<Time>, | |
mut query: Query<(&mut DirectionalLight, &mut HalfSize)>, | |
) { | |
for (mut light, mut half_size) in &mut query { | |
let mut direction = 0.; | |
if input.pressed(KeyCode::R) { | |
direction -= 1.; | |
} | |
if input.pressed(KeyCode::T) { | |
direction += 1.; | |
} | |
half_size.0 += time.delta_seconds() * 10.0 * direction; | |
light.shadow_projection.left = -half_size.0; | |
light.shadow_projection.right = half_size.0; | |
light.shadow_projection.bottom = -half_size.0; | |
light.shadow_projection.top = half_size.0; | |
light.shadow_projection.near = -half_size.0; | |
light.shadow_projection.far = half_size.0; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment