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
|
use rapier2d::pipeline::{DebugColor, DebugRenderBackend, DebugRenderObject};
use crate::config::CELLS_TO_METRES;
#[repr(C)]
#[derive(Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct DebugVertex {
pub position: [f32; 2],
pub color: [f32; 4],
}
#[derive(Default)]
pub struct DebugLineBuffer {
pub vertices: Vec<DebugVertex>,
}
impl DebugRenderBackend for DebugLineBuffer {
fn draw_line(
&mut self,
_object: DebugRenderObject,
a: rapier2d::math::Vector,
b: rapier2d::math::Vector,
color: DebugColor,
) {
let color = hsla_to_linear_rgba(color);
self.vertices.push(DebugVertex {
position: [a.x * CELLS_TO_METRES, a.y * CELLS_TO_METRES],
color,
});
self.vertices.push(DebugVertex {
position: [b.x * CELLS_TO_METRES, b.y * CELLS_TO_METRES],
color,
});
}
}
fn hsla_to_linear_rgba(hsla: DebugColor) -> [f32; 4] {
let [hue, saturation, lightness, alpha] = hsla;
let hue = hue.rem_euclid(360.0) / 60.0;
let saturation = saturation.clamp(0.0, 1.0);
let lightness = lightness.clamp(0.0, 1.0);
let chroma = (1.0 - (2.0 * lightness - 1.0).abs()) * saturation;
let second = chroma * (1.0 - (hue % 2.0 - 1.0).abs());
let (r, g, b) = match hue as u32 {
0 => (chroma, second, 0.0),
1 => (second, chroma, 0.0),
2 => (0.0, chroma, second),
3 => (0.0, second, chroma),
4 => (second, 0.0, chroma),
_ => (chroma, 0.0, second),
};
let m = lightness - chroma / 2.0;
[
srgb_to_linear(r + m),
srgb_to_linear(g + m),
srgb_to_linear(b + m),
alpha.clamp(0.0, 1.0),
]
}
fn srgb_to_linear(channel: f32) -> f32 {
let channel = channel.clamp(0.0, 1.0);
if channel <= 0.04045 {
channel / 12.92
} else {
((channel + 0.055) / 1.055).powf(2.4)
}
}
|