Function bevy_doryen::doryen::color_add
source · pub fn color_add(c1: (u8, u8, u8, u8), c2: (u8, u8, u8, u8)) -> (u8, u8, u8, u8)
Examples found in repository?
examples/demo/light.rs (line 163)
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
fn compute_lightmap(
mut light_map: NonSendMut<LightMap>,
size: Res<LevelSize>,
query: Query<(&Light, &Position)>,
level_map: Res<LevelMap>,
) {
let light_map = &mut light_map.0;
*light_map = Image::new_empty(size.width as u32 * 2, size.height as u32 * 2);
let mut fov = FovRestrictive::new();
for (light, light_position) in &query {
let (px, py, intensity, radius) = if light.flicker {
// alter light position, radius and intensity over time
(
light_position.x + (LIGHT_FLICKER_MOVE * (simplex(light.t) - 0.5)),
light_position.y + (LIGHT_FLICKER_MOVE * (simplex(light.t + 2.0) - 0.5)),
light.intensity + LIGHT_FLICKER_INTENSITY * (simplex(light.t + 4.0) - 0.5),
light.radius * (1.0 + LIGHT_FLICKER_RADIUS * (simplex(light.t + 6.0) - 0.5)),
)
} else {
(
light_position.x,
light_position.y,
light.intensity,
light.radius,
)
};
let minx = ((px - radius).floor() as i32).max(0) as u32;
let maxx = ((px + radius).ceil() as i32).min(light_map.width() as i32 - 1) as u32;
let miny = ((py - radius).floor() as i32).max(0) as u32;
let maxy = ((py + radius).ceil() as i32).min(light_map.height() as i32 - 1) as u32;
let width = maxx - minx + 1;
let height = maxy - miny + 1;
let mut map = MapData::new(width as usize, height as usize);
for y in miny..=maxy {
for x in minx..=maxx {
map.set_transparent(
(x - minx) as usize,
(y - miny) as usize,
level_map.0.is_transparent(x as usize, y as usize),
);
}
}
fov.compute_fov(
&mut map,
px as usize - minx as usize,
py as usize - miny as usize,
radius as usize,
true,
);
let light_color = color_scale(light.color, intensity);
let radius_squared = radius * radius;
let radius_coef = 1.0 / (1.0 + radius_squared / 20.0);
for y in miny..=maxy {
for x in minx..=maxx {
if map.is_in_fov((x - minx) as usize, (y - miny) as usize) {
let dx = x as f32 - px;
let dy = y as f32 - py;
// good looking lights.
let squared_dist = dx * dx + dy * dy;
let intensity_coef = 1.0 / (1.0 + squared_dist / 20.0);
let intensity_coef = intensity_coef - radius_coef;
let intensity_coef = intensity_coef / (1.0 - radius_coef);
if intensity_coef > 0.0 {
let light = color_blend(BLACK, light_color, intensity_coef);
let cur_light = light_map.pixel(x, y).unwrap();
light_map.put_pixel(x, y, color_add(light, cur_light));
}
}
}
}
}
}