Skip to content

Commit

Permalink
Added: Spotlight
Browse files Browse the repository at this point in the history
  • Loading branch information
443eb9 committed Mar 10, 2024
1 parent bfdbd57 commit 4c4b143
Show file tree
Hide file tree
Showing 18 changed files with 338 additions and 88 deletions.
1 change: 1 addition & 0 deletions RELEASE_DRAFT.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Removed redundant components in `ShadowCaster2dBundle`.
- Moved redundant Bevy features into dev-dependencies.
- Added `alpha_threshold` to `ShadowMap2dConfig`.
- Samples of PCF is no longer limited to 32.

# What's Fixed:

Expand Down
26 changes: 20 additions & 6 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ use bevy::{
DefaultPlugins,
};
use bevy_incandescent::{
ecs::{
bundle::{PointLight2dBundle, ShadowCaster2dBundle},
PointLight2d,
},
ecs::{PointLight2d, PointLight2dBundle, ShadowCaster2dBundle, SpotLight2d, SpotLight2dBundle},
math::CircularSector,
IncandescentPlugin,
};
use helpers::HelpersPlugin;
Expand Down Expand Up @@ -91,6 +89,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
transform: Transform::from_xyz(-7.7, -94.5, 0.),
..Default::default()
},
NoFrustumCulling,
ShadowCaster2dBundle::default(),
));

Expand All @@ -105,12 +104,27 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
..Default::default()
});

commands.spawn(PointLight2dBundle {
point_light: PointLight2d {
// commands.spawn(PointLight2dBundle {
// point_light: PointLight2d {
// color: Color::rgb(rd.gen(), rd.gen(), rd.gen()),
// intensity: 0.8,
// range: 400.,
// radius: 30.,
// },
// transform: Transform::from_xyz(-50., -25., 0.),
// ..Default::default()
// });

commands.spawn(SpotLight2dBundle {
spot_light: SpotLight2d {
color: Color::rgb(rd.gen(), rd.gen(), rd.gen()),
intensity: 0.8,
range: 400.,
radius: 30.,
sector: CircularSector::Angles {
start: std::f32::consts::FRAC_PI_6,
end: std::f32::consts::FRAC_PI_2,
},
},
transform: Transform::from_xyz(-50., -25., 0.),
..Default::default()
Expand Down
30 changes: 18 additions & 12 deletions examples/pbr.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
use bevy::{
app::{App, Startup, Update},
asset::AssetServer,
asset::{AssetServer, Assets},
core_pipeline::core_2d::Camera2dBundle,
ecs::{
component::Component,
query::With,
system::{Commands, Query, Res},
system::{Commands, Query, Res, ResMut},
},
input::{keyboard::KeyCode, ButtonInput},
math::Vec2,
math::{primitives::Rectangle, Vec2},
render::{
camera::{CameraProjection, OrthographicProjection},
color::Color,
mesh::Mesh,
view::{Msaa, NoFrustumCulling},
},
sprite::{Sprite, SpriteBundle},
transform::components::{GlobalTransform, Transform},
window::Window,
DefaultPlugins,
};
use bevy_incandescent::{
ecs::{
bundle::{PointLight2dBundle, ShadowCaster2dBundle},
PointLight2d,
pbr::{PbrMesh2dBundle, StandardMaterial2d},
PointLight2d, PointLight2dBundle, ShadowCaster2dBundle,
},
IncandescentPlugin,
};
Expand All @@ -46,7 +46,12 @@ fn main() {
#[derive(Component)]
struct ControllableLight;

fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
fn setup(
mut commands: Commands,
asset_server: Res<AssetServer>,
mut materials: ResMut<Assets<StandardMaterial2d>>,
mut meshes: ResMut<Assets<Mesh>>,
) {
commands.spawn(Camera2dBundle::default());

commands.spawn((
Expand All @@ -65,12 +70,13 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
));

commands.spawn((
SpriteBundle {
sprite: Sprite {
custom_size: Some(Vec2::splat(512.)),
PbrMesh2dBundle {
material: materials.add(StandardMaterial2d {
base_color_texture: Some(asset_server.load("pbr/suzzane-color.png")),
normal_map_texture: Some(asset_server.load("pbr/suzzane-normal.png")),
..Default::default()
},
texture: asset_server.load("pbr/suzzane-color.png"),
}),
mesh: meshes.add(Rectangle::new(512., 512.)),
..Default::default()
},
ShadowCaster2dBundle::default(),
Expand Down
2 changes: 1 addition & 1 deletion examples/stress_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bevy::{
DefaultPlugins,
};
use bevy_incandescent::{
ecs::{bundle::PointLight2dBundle, PointLight2d},
ecs::{PointLight2d, PointLight2dBundle},
IncandescentPlugin,
};
use helpers::HelpersPlugin;
Expand Down
22 changes: 17 additions & 5 deletions src/debug/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#![allow(unused)]
use bevy::{
app::{App, Plugin, Update},
ecs::{query::With, system::Query},
ecs::{
query::{Or, With},
system::Query,
},
gizmos::gizmos::Gizmos,
math::Vec3Swizzles,
render::view::VisibleEntities,
transform::components::GlobalTransform,
};

use crate::ecs::PointLight2d;
use crate::ecs::{PointLight2d, SpotLight2d};

pub struct IncandescentDebugPlugin;

Expand All @@ -24,13 +27,22 @@ impl Plugin for IncandescentDebugPlugin {
}
}

fn draw_light_range(mut gizmos: Gizmos, lights_query: Query<(&GlobalTransform, &PointLight2d)>) {
for (transform, light) in lights_query.iter() {
fn draw_light_range(
mut gizmos: Gizmos,
point_lights_query: Query<(&GlobalTransform, &PointLight2d)>,
spot_lights_query: Query<(&GlobalTransform, &SpotLight2d)>,
) {
for (transform, light) in point_lights_query.iter() {
gizmos.circle_2d(transform.translation().xy(), light.range, light.color);
}
for (transform, light) in spot_lights_query.iter() {
gizmos.circle_2d(transform.translation().xy(), light.range, light.color);
}
}

fn print_light_visible_entities(lights_query: Query<&VisibleEntities, With<PointLight2d>>) {
fn print_light_visible_entities(
lights_query: Query<&VisibleEntities, Or<(With<PointLight2d>, With<SpotLight2d>)>>,
) {
for visible_entities in lights_query.iter() {
println!("{:?}", visible_entities);
}
Expand Down
37 changes: 0 additions & 37 deletions src/ecs/bundle.rs

This file was deleted.

83 changes: 75 additions & 8 deletions src/ecs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
use bevy::ecs::reflect::ReflectResource;
use bevy::ecs::system::Resource;
use bevy::render::extract_resource::ExtractResource;
use bevy::{ecs::component::Component, math::bounding::Aabb2d, render::color::Color};
use bevy::{
app::{App, Plugin},
ecs::{bundle::Bundle, component::Component, reflect::ReflectResource, system::Resource},
reflect::Reflect,
render::{
color::Color,
extract_resource::ExtractResource,
primitives::Frustum,
view::{InheritedVisibility, ViewVisibility, Visibility, VisibleEntities},
},
transform::components::{GlobalTransform, Transform},
};

pub mod bundle;
use crate::math::CircularSector;

#[cfg(feature = "catalinzz")]
pub mod catalinzz;
#[cfg(feature = "pbr")]
pub mod pbr;

pub struct IncandescentEcsPlugin;

use bevy::reflect::Reflect;
impl Plugin for IncandescentEcsPlugin {
fn build(&self, app: &mut App) {
app.register_type::<PointLight2d>()
.register_type::<SpotLight2d>()
.register_type::<AmbientLight2d>();
}
}

#[derive(Component, Default, Clone, Copy, Reflect)]
pub struct PointLight2d {
Expand All @@ -16,8 +36,14 @@ pub struct PointLight2d {
pub radius: f32,
}

#[derive(Component)]
pub struct Light2dAabb(pub Aabb2d);
#[derive(Component, Default, Clone, Copy, Reflect)]
pub struct SpotLight2d {
pub color: Color,
pub intensity: f32,
pub range: f32,
pub radius: f32,
pub sector: CircularSector,
}

#[derive(Component)]
pub struct ShadowCaster2d;
Expand All @@ -37,3 +63,44 @@ impl Default for AmbientLight2d {
}
}
}

#[derive(Bundle, Default)]
pub struct PointLight2dBundle {
pub point_light: PointLight2d,
pub transform: Transform,
pub global_transform: GlobalTransform,
#[cfg(feature = "catalinzz")]
pub frustum: Frustum,
#[cfg(feature = "catalinzz")]
pub visible_casters: VisibleEntities,
pub visibility: Visibility,
pub view_visibility: ViewVisibility,
pub inherited_visibility: InheritedVisibility,
}

#[derive(Bundle, Default)]
pub struct SpotLight2dBundle {
pub spot_light: SpotLight2d,
pub transform: Transform,
pub global_transform: GlobalTransform,
#[cfg(feature = "catalinzz")]
pub frustum: Frustum,
#[cfg(feature = "catalinzz")]
pub visible_casters: VisibleEntities,
pub visibility: Visibility,
pub view_visibility: ViewVisibility,
pub inherited_visibility: InheritedVisibility,
}

#[derive(Bundle)]
pub struct ShadowCaster2dBundle {
pub shadow_caster: ShadowCaster2d,
}

impl Default for ShadowCaster2dBundle {
fn default() -> Self {
Self {
shadow_caster: ShadowCaster2d,
}
}
}
31 changes: 31 additions & 0 deletions src/ecs/pbr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use bevy::{
asset::{Asset, Handle},
ecs::bundle::Bundle,
reflect::TypePath,
render::{
color::Color,
mesh::Mesh,
render_resource::AsBindGroup,
texture::Image,
view::{InheritedVisibility, ViewVisibility, Visibility},
},
transform::components::{GlobalTransform, Transform},
};

#[derive(Asset, AsBindGroup, TypePath, Default, Clone)]
pub struct StandardMaterial2d {
pub base_color: Color,
pub base_color_texture: Option<Handle<Image>>,
pub normal_map_texture: Option<Handle<Image>>,
}

#[derive(Bundle, Default)]
pub struct PbrMesh2dBundle {
pub material: Handle<StandardMaterial2d>,
pub mesh: Handle<Mesh>,
pub transform: Transform,
pub global_transform: GlobalTransform,
pub visibility: Visibility,
pub inherited_visibility: InheritedVisibility,
pub view_visibility: ViewVisibility,
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use bevy::app::{App, Plugin};
use ecs::IncandescentEcsPlugin;
use render::IncandescentRenderPlugin;

#[cfg(feature = "debug")]
pub mod debug;
pub mod ecs;
pub mod math;
pub mod render;

const APPRACHES: [LightingApproach; 1] = [
Expand Down Expand Up @@ -32,6 +34,7 @@ impl Plugin for IncandescentPlugin {
fn build(&self, app: &mut App) {
app.add_plugins((
IncandescentRenderPlugin,
IncandescentEcsPlugin,
#[cfg(feature = "debug")]
debug::IncandescentDebugPlugin,
));
Expand Down
Loading

0 comments on commit 4c4b143

Please sign in to comment.