bevy_firefly 0.18.2

2d lighting crate for the Bevy game engine
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
use bevy::{
    camera::visibility::{RenderLayers, VisibilityClass, add_visibility_class},
    color::palettes::css::WHITE,
    core_pipeline::tonemapping::{DebandDither, Tonemapping},
    ecs::{
        change_detection::Tick,
        query::ROQueryItem,
        system::{
            SystemParamItem,
            lifetimeless::{Read, SRes},
        },
    },
    platform::collections::HashMap,
    prelude::*,
    render::{
        Render, RenderApp, RenderSystems,
        batching::sort_binned_render_phase,
        render_phase::{
            AddRenderCommand, BinnedRenderPhaseType, DrawFunctions, InputUniformIndex, PhaseItem,
            RenderCommand, RenderCommandResult, SetItemPipeline, TrackedRenderPass,
            ViewBinnedRenderPhases,
        },
        render_resource::{
            BindGroup, PipelineCache, ShaderType, SpecializedRenderPipelines, StorageBuffer,
        },
        sync_world::SyncToRenderWorld,
        view::{ExtractedView, RenderVisibleEntities, RetainedViewEntity, ViewUniformOffset},
    },
};
use bytemuck::NoUninit;

use crate::{
    LightBatchSetKey,
    buffers::{BinBuffers, BufferIndex},
    change::Changes,
    data::ExtractedCombineLightmapTo,
    phases::LightmapPhase,
    pipelines::{LightPipelineKey, LightmapCreationPipeline},
    visibility::VisibilityTimer,
};

/// Point light with adjustable fields.
#[derive(Debug, Component, Clone, Reflect)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[require(
    SyncToRenderWorld,
    Transform,
    VisibilityClass,
    ViewVisibility,
    VisibilityTimer,
    LightHeight,
    Changes,
    RenderLayers
)]
#[component(on_add = add_visibility_class::<PointLight2d>)]
pub struct PointLight2d {
    /// Color of the point light. Alpha is ignored.
    ///
    /// **Default:** White.
    pub color: Color,

    /// Intensity of the point light.
    ///
    /// **Default:** 1.
    pub intensity: f32,

    /// Outer range of the point light.
    pub radius: f32,

    /// Type of falloff for this light.
    ///
    /// **Default:** [InverseSquare](Falloff::InverseSquare).
    pub falloff: Falloff,

    /// The core of the light.
    ///
    /// This is the inner section of the light that is usually brighter.
    ///
    /// The soft shadows are cast based on the radius of the core.
    pub core: LightCore,

    /// Optional parameter to constrain the angle of a light.
    ///
    /// The direction of the angle is based on the **UP** direction of the entity.
    /// Can be moved by rotating the entity.  
    ///
    /// **Default:** LightAngle::FULL.
    pub angle: LightAngle,

    /// Whether this light should cast shadows or not with the existent occluders.
    ///
    /// **Performance Impact:** Major.
    ///
    /// **Default:** true.
    pub cast_shadows: bool,

    /// Offset position of the light.
    ///
    /// Useful if you want to add a light component on an entity and change it's position,
    /// without needing to create a child entity for it.
    ///
    /// **Default:** [Vec3::ZERO].
    pub offset: Vec3,
}

impl Default for PointLight2d {
    fn default() -> Self {
        Self {
            color: bevy::prelude::Color::Srgba(WHITE),
            intensity: 1.,
            radius: 100.,
            falloff: Falloff::InverseSquare { intensity: 0.0 },
            core: default(),
            angle: LightAngle::FULL,
            cast_shadows: true,
            offset: Vec3::ZERO,
        }
    }
}

/// Optional component you can add to lights.
///
/// Describes the light's 2d height, useful for emulating 3d lighting in top-down 2d games.
///
/// This is currently used along with the normal maps.
///
/// **Default:** 0.   
#[derive(Component, Default, Reflect)]
pub struct LightHeight(pub f32);

#[derive(Debug, Clone, Copy, Reflect)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
/// The angle of the light. Value is interpolated between inner and outer angles to create a smooth transition.
pub struct LightAngle {
    /// The inner angle of a light, in degrees. Should be less than or equial to the outer angle.
    pub inner: f32,
    /// The outer angle of a light, in degrees. Should be greater than or equal to the inner angle.
    pub outer: f32,
}

impl Default for LightAngle {
    fn default() -> Self {
        Self::FULL
    }
}

impl LightAngle {
    pub const FULL: Self = Self {
        inner: 360.0,
        outer: 360.0,
    };
}

/// An enum describing the falloff of a light's intensity.
#[derive(Debug, Clone, Copy, Reflect)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Falloff {
    /// The light decreases inversely proportial to the square distance towards the source.  
    ///
    /// The intensity parameter will increase the speed at which the light fades. Can be negative or positive.
    InverseSquare { intensity: f32 },
    /// The light decreases linearly with the distance towards the source.
    ///
    /// The intensity parameter will increase the speed at which the light fades. Can be negative or positive.
    Linear { intensity: f32 },
    /// There is no falloff. The light will have a constant intensity.  
    None,
}

impl Falloff {
    pub const INVERSE_SQUARE: Self = Self::InverseSquare { intensity: 0.0 };
    pub const LINEAR: Self = Self::Linear { intensity: 0.0 };
    pub const NONE: Self = Self::None;

    pub fn inverse_square(intensity: f32) -> Falloff {
        Falloff::InverseSquare { intensity }
    }

    pub fn linear(intensity: f32) -> Falloff {
        Falloff::Linear { intensity }
    }

    pub fn none() -> Falloff {
        Falloff::None
    }

    pub fn intensity(&self) -> f32 {
        match *self {
            Falloff::InverseSquare { intensity } => intensity,
            Falloff::Linear { intensity } => intensity,
            Falloff::None => 0.0,
        }
    }
}

/// The light's core. This is what determines the softness of shadows if [soft_shadows](crate::prelude::FireflyConfig::soft_shadows) is enabled.
#[derive(Clone, Copy, Debug, Reflect)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct LightCore {
    /// The radius of the core. This must be less than the actual radius of the light.
    ///
    /// **Default:** 5.0.
    pub radius: f32,
    /// A boost to the core's intensity.
    ///
    /// If set to 0, the core will have a constant intensity equal to that of the light.
    ///
    /// Otherwise, the core will interpolate between `intensity + boost` and `intensity` based on the provided [`Falloff`].
    ///
    /// **Default:** 5.0.
    pub boost: f32,
    /// The core's falloff.
    ///
    ///  **Default:** InverseSquare { intensity: 0.0 }
    pub falloff: Falloff,
}

impl Default for LightCore {
    fn default() -> Self {
        LightCore {
            radius: 5.0,
            boost: 0.0,
            falloff: Falloff::InverseSquare { intensity: 0.0 },
        }
    }
}

impl LightCore {
    pub const NONE: Self = LightCore {
        radius: 0.0,
        boost: 0.0,
        falloff: Falloff::None,
    };

    pub fn from_radius_boost(radius: f32, boost: f32) -> LightCore {
        LightCore {
            radius,
            boost,
            falloff: Falloff::InverseSquare { intensity: 0.0 },
        }
    }
    pub fn from_radius(radius: f32) -> LightCore {
        LightCore {
            radius,
            boost: 5.0,
            falloff: Falloff::InverseSquare { intensity: 0.0 },
        }
    }
    pub fn with_boost(&self, boost: f32) -> LightCore {
        let mut res = *self;
        res.boost = boost;
        res
    }
    pub fn with_falloff(&self, falloff: Falloff) -> LightCore {
        let mut res = *self;
        res.falloff = falloff;
        res
    }
}

/// The data that is extracted to the render world from a [`PointLight2d`].
#[derive(Component, Clone)]
#[require(BinBuffers, LightIndex, LightPointer)]
pub struct ExtractedPointLight {
    pub pos: Vec2,
    pub color: Color,
    pub intensity: f32,
    pub radius: f32,
    pub falloff: Falloff,
    pub core: LightCore,
    pub angle: LightAngle,
    pub cast_shadows: bool,
    pub dir: Vec2,
    pub z: f32,
    pub height: f32,
    pub changes: Changes,
    pub render_layers: RenderLayers,
}

impl PartialEq for ExtractedPointLight {
    fn eq(&self, other: &Self) -> bool {
        self.pos == other.pos && self.radius == other.radius
    }
}

/// Data that is sent to the GPU for each visible [`PointLight2d`].
#[repr(C)]
#[derive(Default, Clone, Copy, ShaderType, NoUninit)]
pub struct UniformPointLight {
    pub pos: Vec2,
    pub intensity: f32,
    pub radius: f32,

    pub color: Vec4,

    pub core_radius: f32,
    pub core_boost: f32,
    pub core_falloff: u32,
    pub core_falloff_intensity: f32,

    pub falloff: u32,
    pub falloff_intensity: f32,

    pub inner_angle: f32,
    pub outer_angle: f32,

    pub dir: Vec2,

    pub z: f32,
    pub height: f32,
}

/// Render World component that contains the buffer a [`PointLight2d`] writes to each frame.   
#[derive(Component, Default)]
pub struct LightPointer(pub StorageBuffer<u32>);

/// Plugin responsible for functionality related to lights. Added automatically
/// by [`FireflyPlugin`](crate::prelude::FireflyPlugin).
pub struct LightPlugin;
impl Plugin for LightPlugin {
    fn build(&self, app: &mut App) {
        if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
            render_app.init_resource::<LightBindGroups>();
            render_app.init_resource::<DrawFunctions<LightmapPhase>>();
            render_app.init_resource::<ViewBinnedRenderPhases<LightmapPhase>>();

            render_app.add_render_command::<LightmapPhase, DrawLightmap>();

            render_app.add_systems(
                Render,
                sort_binned_render_phase::<LightmapPhase>.in_set(RenderSystems::PhaseSort),
            );

            render_app.add_systems(Render, queue_lights.in_set(RenderSystems::Queue));
        }
    }

    fn finish(&self, app: &mut App) {
        if let Some(render_app) = app.get_sub_app_mut(RenderApp) {
            render_app.init_resource::<LightBatches>();
        }
    }
}

#[derive(Resource, Deref, DerefMut, Default)]
pub(crate) struct LightBatches(pub HashMap<(RetainedViewEntity, Entity), LightBatch>);

#[derive(PartialEq, Eq, Clone, Debug)]
pub(crate) struct LightBatch {
    pub id: Entity,
}

#[derive(Resource, Default)]
pub(crate) struct LightBindGroups {
    pub values: HashMap<Entity, HashMap<RetainedViewEntity, BindGroup>>,
}

#[derive(Component)]
pub(crate) struct LightLut(pub BindGroup);

fn queue_lights(
    light_draw_functions: Res<DrawFunctions<LightmapPhase>>,
    pipeline: Res<LightmapCreationPipeline>,
    mut pipelines: ResMut<SpecializedRenderPipelines<LightmapCreationPipeline>>,
    mut lightmap_phases: ResMut<ViewBinnedRenderPhases<LightmapPhase>>,
    views: Query<(
        &ExtractedView,
        &RenderVisibleEntities,
        &Msaa,
        Option<&Tonemapping>,
        Option<&DebandDither>,
        Option<&ExtractedCombineLightmapTo>,
    )>,
    pipeline_cache: Res<PipelineCache>,
) {
    let draw_lightmap_function = light_draw_functions.read().id::<DrawLightmap>();

    for (view, visible_entities, msaa, tonemapping, dither, combined_lightmap) in &views {
        let Some(lightmap_phase) = lightmap_phases.get_mut(&view.retained_view_entity) else {
            continue;
        };

        let (hdr, msaa) = if let Some(combined_lightmap) = combined_lightmap {
            let view = views.get(combined_lightmap.0).unwrap();
            (view.0.hdr, view.2)
        } else {
            (view.hdr, msaa)
        };

        let msaa_key = LightPipelineKey::from_msaa_samples(msaa.samples());
        let mut view_key = LightPipelineKey::from_hdr(hdr) | msaa_key;

        if !hdr {
            if let Some(tonemapping) = tonemapping {
                view_key |= LightPipelineKey::TONEMAP_IN_SHADER;
                view_key |= match tonemapping {
                    Tonemapping::None => LightPipelineKey::TONEMAP_METHOD_NONE,
                    Tonemapping::Reinhard => LightPipelineKey::TONEMAP_METHOD_REINHARD,
                    Tonemapping::ReinhardLuminance => {
                        LightPipelineKey::TONEMAP_METHOD_REINHARD_LUMINANCE
                    }
                    Tonemapping::AcesFitted => LightPipelineKey::TONEMAP_METHOD_ACES_FITTED,
                    Tonemapping::AgX => LightPipelineKey::TONEMAP_METHOD_AGX,
                    Tonemapping::SomewhatBoringDisplayTransform => {
                        LightPipelineKey::TONEMAP_METHOD_SOMEWHAT_BORING_DISPLAY_TRANSFORM
                    }
                    Tonemapping::TonyMcMapface => LightPipelineKey::TONEMAP_METHOD_TONY_MC_MAPFACE,
                    Tonemapping::BlenderFilmic => LightPipelineKey::TONEMAP_METHOD_BLENDER_FILMIC,
                };
            }
            if let Some(DebandDither::Enabled) = dither {
                view_key |= LightPipelineKey::DEBAND_DITHER;
            }
        }

        let pipeline = pipelines.specialize(&pipeline_cache, &pipeline, view_key);

        for (render_entity, visible_entity) in visible_entities.iter::<PointLight2d>() {
            let batch_set_key = LightBatchSetKey {
                pipeline,
                draw_function: draw_lightmap_function,
            };

            lightmap_phase.add(
                batch_set_key,
                (),
                (*render_entity, *visible_entity),
                InputUniformIndex::default(),
                BinnedRenderPhaseType::NonMesh,
                Tick::new(10),
            );
        }
    }
}

pub(crate) type DrawLightmap = (SetItemPipeline, SetLightTextureBindGroup, DrawLightBatch);

pub(crate) struct SetLightTextureBindGroup;
impl<P: PhaseItem> RenderCommand<P> for SetLightTextureBindGroup {
    type Param = (SRes<LightBindGroups>, SRes<LightBatches>);
    type ViewQuery = (Read<ExtractedView>, Read<ViewUniformOffset>, Read<LightLut>);
    type ItemQuery = ();

    fn render<'w>(
        item: &P,
        (view, view_uniform_offset, lut): ROQueryItem<'w, '_, Self::ViewQuery>,
        _entity: Option<()>,
        (image_bind_groups, batches): SystemParamItem<'w, '_, Self::Param>,
        pass: &mut TrackedRenderPass<'w>,
    ) -> RenderCommandResult {
        let image_bind_groups = image_bind_groups.into_inner();
        let Some(batch) = batches.get(&(view.retained_view_entity, item.entity())) else {
            return RenderCommandResult::Skip;
        };

        pass.set_bind_group(0, &lut.0, &[view_uniform_offset.offset]);
        pass.set_bind_group(
            1,
            image_bind_groups
                .values
                .get(&batch.id)
                .unwrap()
                .get(&view.retained_view_entity)
                .unwrap(),
            &[],
        );

        RenderCommandResult::Success
    }
}

pub(crate) struct DrawLightBatch;
impl<P: PhaseItem> RenderCommand<P> for DrawLightBatch {
    type Param = ();
    type ViewQuery = Read<ExtractedView>;
    type ItemQuery = ();

    fn render<'w>(
        _: &P,
        _: ROQueryItem<'w, '_, Self::ViewQuery>,
        _entity: Option<()>,
        _: SystemParamItem<'w, '_, Self::Param>,
        pass: &mut TrackedRenderPass<'w>,
    ) -> RenderCommandResult {
        pass.draw(0..3, 0..1);
        RenderCommandResult::Success
    }
}

/// Buffer index that each visible light gets assigned
/// corresponding to its [`BufferManager`](crate::buffers::BufferManager) slot.  
#[derive(Component, Default)]
pub struct LightIndex(pub Option<BufferIndex>);