Skip to content

Commit

Permalink
Initial 0.15 support
Browse files Browse the repository at this point in the history
More work needed to move things over to required components and to ensure
extraction is done correctly, but gets us to the point where bevy_light_2d
is usable with bevy 0.15.
  • Loading branch information
jgayfer committed Oct 28, 2024
1 parent c67a1c4 commit cf4013b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 62 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ readme = "README.md"
exclude = ["assets/*", "static/*"]

[dependencies]
bevy = { version = "0.14", default-features = false, features = [
bevy = { version = "0.15.0-rc.2", default-features = false, features = [
"bevy_render",
"bevy_core_pipeline",
"bevy_winit",
Expand All @@ -20,7 +20,7 @@ bevy = { version = "0.14", default-features = false, features = [
smallvec = "1.13"

[dev-dependencies]
bevy = { version = "0.14", default-features = false, features = [
bevy = { version = "0.15.0-rc.2", default-features = false, features = [
"bevy_render",
"bevy_core_pipeline",
"bevy_winit",
Expand Down
2 changes: 1 addition & 1 deletion examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn main() {
}

fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

commands.spawn(PointLight2dBundle {
point_light: PointLight2d {
Expand Down
50 changes: 24 additions & 26 deletions examples/dungeon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ struct Candle;
struct AnimationTimer(Timer);

fn setup_camera(mut commands: Commands) {
let mut camera = Camera2dBundle::default();
camera.projection.scale = 0.25;
let mut projection = OrthographicProjection::default_2d();
projection.scale = 0.25;
commands.spawn((
camera,
Camera2d,
projection,
AmbientLight2d {
brightness: 0.1,
..default()
Expand All @@ -55,12 +56,14 @@ fn set_clear_color(mut clear_color: ResMut<ClearColor>) {

fn animate_candles(
time: Res<Time>,
mut query: Query<(&mut AnimationTimer, &mut TextureAtlas), With<Candle>>,
mut query: Query<(&mut AnimationTimer, &mut Sprite), With<Candle>>,
) {
for (mut timer, mut atlas) in &mut query {
for (mut timer, mut sprite) in &mut query {
timer.tick(time.delta());
if timer.just_finished() {
atlas.index = (atlas.index + 1) % 4;
if let Some(ref mut texture_atlas) = sprite.texture_atlas {
texture_atlas.index = (texture_atlas.index + 1) % 4;
}
}
}
}
Expand All @@ -84,15 +87,14 @@ fn spawn_candles(mut commands: Commands, spritesheet: Res<CandleSpritesheet>) {
.spawn((
Candle,
AnimationTimer(Timer::from_seconds(0.2, TimerMode::Repeating)),
SpriteBundle {
transform: Transform::from_xyz(0., 2., ENTITY_INDEX),
texture: spritesheet.texture.clone(),
..default()
},
TextureAtlas {
layout: spritesheet.layout.clone(),
..default()
},
Sprite::from_atlas_image(
spritesheet.texture.clone(),
TextureAtlas {
layout: spritesheet.layout.clone(),
index: 0,
},
),
Transform::from_xyz(0., 2., ENTITY_INDEX),
))
.add_child(light);
}
Expand Down Expand Up @@ -166,18 +168,14 @@ fn spawn_from_atlas(
texture: Handle<Image>,
) {
commands.spawn((
SpriteBundle {
transform: Transform {
translation,
..default()
},
Sprite::from_atlas_image(
texture,
..default()
},
TextureAtlas {
index: sprite_index,
layout: atlas_handle,
},
TextureAtlas {
index: sprite_index,
layout: atlas_handle,
},
),
Transform::from_translation(translation),
));
}

Expand Down
11 changes: 4 additions & 7 deletions examples/multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ fn main() {

fn setup(mut commands: Commands) {
commands.spawn((
Camera2dBundle::default(),
Camera2d,
AmbientLight2d {
brightness: 0.1,
..default()
},
));

commands.spawn(SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::splat(150.)),
color: Color::WHITE,
..default()
},
commands.spawn(Sprite {
custom_size: Some(Vec2::splat(150.)),
color: Color::WHITE,
..default()
});

Expand Down
8 changes: 4 additions & 4 deletions examples/occlusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct YellowLight;
struct BlueLight;

fn setup(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn(Camera2d);

commands.spawn((
PointLight2dBundle {
Expand Down Expand Up @@ -118,10 +118,10 @@ fn move_lights(
time: Res<Time>,
) {
for mut light_transform in &mut yellow_query {
light_transform.translation.x = time.elapsed_seconds().sin() * 500.
light_transform.translation.x = time.elapsed_secs().sin() * 500.
}
for mut light_transform in &mut blue_query {
light_transform.translation.x = time.elapsed_seconds().cos() * 500.
light_transform.translation.x = time.elapsed_secs().cos() * 500.
}
}

Expand Down Expand Up @@ -177,7 +177,7 @@ fn control_camera_zoom(
}

for mut camera in cameras.iter_mut() {
camera.scale = (camera.scale - projection_delta * time.delta_seconds())
camera.scale = (camera.scale - projection_delta * time.delta_secs())
.clamp(MIN_CAMERA_SCALE, MAX_CAMERA_SCALE);
}
}
6 changes: 5 additions & 1 deletion src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ use bevy::{
color::Color,
ecs::{bundle::Bundle, component::Component},
reflect::Reflect,
render::view::{InheritedVisibility, ViewVisibility, Visibility},
render::{
sync_world::SyncToRenderWorld,
view::{InheritedVisibility, ViewVisibility, Visibility},
},
transform::components::{GlobalTransform, Transform},
};

Expand All @@ -21,6 +24,7 @@ use bevy::{
/// [A better point light attenutation function](https://lisyarus.github.io/blog/posts/point-light-attenuation.html#section-the-solution)
/// by [lisyarus](https://lisyarus.github.io/blog/).
#[derive(Component, Clone, Reflect)]
#[require(SyncToRenderWorld)]
pub struct PointLight2d {
/// The light's color tint.
pub color: Color,
Expand Down
6 changes: 5 additions & 1 deletion src/occluder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
use bevy::{
ecs::{bundle::Bundle, component::Component},
math::Vec2,
render::view::{InheritedVisibility, ViewVisibility, Visibility},
render::{
sync_world::SyncToRenderWorld,
view::{InheritedVisibility, ViewVisibility, Visibility},
},
transform::components::{GlobalTransform, Transform},
};

/// A light occluder that prevents light passing through it, casting shadows.
///
/// This is commonly used as a component within [`LightOcluder2dBundle`].
#[derive(Default, Component)]
#[require(SyncToRenderWorld)]
pub struct LightOccluder2d {
/// The shape of the light occluder.
pub shape: LightOccluder2dShape,
Expand Down
57 changes: 37 additions & 20 deletions src/render/extract.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::{
prelude::*,
render::{render_resource::ShaderType, Extract},
render::{render_resource::ShaderType, sync_world::RenderEntity, Extract},
};

use crate::{
Expand Down Expand Up @@ -31,30 +31,45 @@ pub struct ExtractedAmbientLight2d {

pub fn extract_point_lights(
mut commands: Commands,
point_light_query: Extract<Query<(Entity, &PointLight2d, &GlobalTransform, &ViewVisibility)>>,
point_light_query: Extract<
Query<(
&RenderEntity,
&PointLight2d,
&GlobalTransform,
&ViewVisibility,
)>,
>,
) {
for (entity, point_light, global_transform, view_visibility) in &point_light_query {
for (render_entity, point_light, global_transform, view_visibility) in &point_light_query {
if !view_visibility.get() {
continue;
}
commands.get_or_spawn(entity).insert(ExtractedPointLight2d {
color: point_light.color.to_linear(),
transform: global_transform.translation().xy(),
radius: point_light.radius,
intensity: point_light.intensity,
falloff: point_light.falloff,
cast_shadows: if point_light.cast_shadows { 1 } else { 0 },
});
commands
.entity(render_entity.id())
.insert(ExtractedPointLight2d {
color: point_light.color.to_linear(),
transform: global_transform.translation().xy(),
radius: point_light.radius,
intensity: point_light.intensity,
falloff: point_light.falloff,
cast_shadows: if point_light.cast_shadows { 1 } else { 0 },
});
}
}

pub fn extract_light_occluders(
mut commands: Commands,
light_occluders_query: Extract<
Query<(Entity, &LightOccluder2d, &GlobalTransform, &ViewVisibility)>,
Query<(
&RenderEntity,
&LightOccluder2d,
&GlobalTransform,
&ViewVisibility,
)>,
>,
) {
for (entity, light_occluder, global_transform, view_visibility) in &light_occluders_query {
for (render_entity, light_occluder, global_transform, view_visibility) in &light_occluders_query
{
if !view_visibility.get() {
continue;
}
Expand All @@ -66,28 +81,30 @@ pub fn extract_light_occluders(
},
};

commands.get_or_spawn(entity).insert(extracted_occluder);
commands
.entity(render_entity.id())
.insert(extracted_occluder);
}
}

pub fn extract_ambient_lights(
mut commands: Commands,
ambient_light_query: Extract<Query<(Entity, &AmbientLight2d)>>,
camera_query: Extract<Query<Entity, (With<Camera2d>, Without<AmbientLight2d>)>>,
ambient_light_query: Extract<Query<(&RenderEntity, &AmbientLight2d)>>,
camera_query: Extract<Query<&RenderEntity, (With<Camera2d>, Without<AmbientLight2d>)>>,
) {
for (entity, ambient_light) in &ambient_light_query {
for (render_entity, ambient_light) in &ambient_light_query {
commands
.get_or_spawn(entity)
.entity(render_entity.id())
.insert(ExtractedAmbientLight2d {
color: ambient_light.color.to_linear() * ambient_light.brightness,
});
}

// Our lighting pass only runs on views with an ambient light component,
// so let's add a no-op ambient light to any 2d cameras don't have one.
for entity in &camera_query {
for render_entity in &camera_query {
commands
.get_or_spawn(entity)
.entity(render_entity.id())
.insert(ExtractedAmbientLight2d {
color: Color::WHITE.into(),
});
Expand Down

0 comments on commit cf4013b

Please sign in to comment.