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
use std::usize;
use bevy::{
color::palettes::css::WHITE,
prelude::*,
render::{extract_component::ExtractComponent, render_resource::ShaderType},
};
#[derive(Component, Default, Clone, ExtractComponent, Reflect)]
pub(crate) struct ExtractedWorldData {
pub camera_pos: Vec2,
}
/// Component that needs to be added to a camera in order to have it render lights.
///
/// # Panics
/// Panics if added to multiple cameras at once.
#[derive(Debug, Component, ExtractComponent, Clone, Reflect)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[require(Transform)]
pub struct FireflyConfig {
/// Ambient light that will be added over all other lights.
///
/// **Default:** White.
pub ambient_color: Color,
/// Brightness for the ambient light. If 0 and no lights are present, everything will be completely black.
///
/// **Default:** 0.
pub ambient_brightness: f32,
/// Light bands will divide the lightmap into brackets of the given size.
///
/// E.g. with `light_bands: Some(0.3)`, all color channels in the `[0-0.3]` interval will be the same color,
/// in `[0.3-0.6]` another color, and so on.
///
/// **Performance Impact:** None.
///
/// **Default:** None.
pub light_bands: Option<f32>,
/// Whether you want to use soft shadows or not.
///
/// **Default:** true.
pub soft_shadows: bool,
/// Whether to use occlusion z-sorting or not.
///
/// If this is enabled, shadows cast by occluders won't affect sprites with a higher z position.
///
/// Very useful for top-down games.
///
/// **Performance Impact:** None.
///
/// **Default:** true.
pub z_sorting: bool,
pub z_sorting_error_margin: f32,
/// Field that controls how the normal maps are applied relative to perspective.
///
/// **Performance Impact:** Very minor.
///
/// **Default:** [None](NormalMapMode::None).
pub normal_mode: NormalMode,
/// This will control how much the normal map is attenuated before being applied.
///
/// Inside the shader, we perform `mix(normal_map, vec3f(0), attenuation)` to decrease the 'hardness' of the normal map.
///
/// This has the effect of pulling all channels towards (128, 128, 128), making the overall lighting over the surface more plain.
///
/// **Default:** 0.5.
pub normal_attenuation: f32,
}
/// Options for how the normal maps should be read and used.
///
/// In order to fully use normal maps, you will need to add the [NormalMap](crate::prelude::NormalMap) component to Sprites.
///
/// **Default:** [None](NormalMapMode::None).
#[derive(Debug, Clone, Reflect)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum NormalMode {
/// No normal maps will be used in rendering.
None,
/// This will make it the normal mapping simply be based on the (x, y, z) difference between each light and sprite.
///
/// [LightHeight](crate::prelude::LightHeight) and [SpriteHeight](crate::prelude::SpriteHeight) will be completely ignored.
///
/// This is recommended for classic 2d perspectives, such as those of side-scroller games.
Simple,
/// This will make the normal mapping be based on the difference between the light's and sprite's x-axis and z-axis, but for the y-axis
/// it will use the [LightHeight](crate::prelude::LightHeight) and [SpriteHeight](crate::prelude::SpriteHeight) components.
///
/// This is recommended for 2d perspectives where you want to simulate 3d lighting, such as top-down games.
TopDownY,
TopDownZ,
}
impl Default for FireflyConfig {
fn default() -> Self {
Self {
ambient_color: Color::Srgba(WHITE),
ambient_brightness: 0.0,
light_bands: None,
soft_shadows: true,
z_sorting: true,
z_sorting_error_margin: 0.0,
normal_mode: NormalMode::None,
normal_attenuation: 0.5,
}
}
}
/// GPU-alligned data from [`FireflyConfig`].
#[derive(ShaderType, Clone)]
pub struct UniformFireflyConfig {
pub ambient_color: Vec3,
pub ambient_brightness: f32,
pub light_bands: f32,
pub soft_shadows: u32,
pub z_sorting: u32,
pub z_sorting_error_margin: f32,
pub normal_mode: u32,
pub normal_attenuation: f32,
}