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
use crate::prelude::*;
use rayon::prelude::*;
pub struct TheSDFCanvas {
pub background: crate::thecolor::TheColor,
pub highlight: crate::thecolor::TheColor,
pub hover_highlight: crate::thecolor::TheColor,
pub error_highlight: crate::thecolor::TheColor,
pub selected: Option<usize>,
pub hover: Option<usize>,
pub error: Option<usize>,
pub sdfs: Vec<TheSDF>,
pub patterns: Vec<ThePattern>,
}
impl Default for TheSDFCanvas {
fn default() -> Self {
Self::new()
}
}
impl TheSDFCanvas {
pub fn new() -> Self {
Self {
sdfs: vec![],
patterns: vec![],
selected: None,
hover: None,
error: None,
background: TheColor::black(),
highlight: TheColor::white(),
hover_highlight: TheColor::white(),
error_highlight: crate::thecolor::TheColor::from_u8_array([209, 42, 42, 255]),
}
}
/// Adds an SDF to the canvas.
pub fn add(&mut self, sdf: TheSDF, pattern: ThePattern) {
self.sdfs.push(sdf);
self.patterns.push(pattern);
}
/// Renders the sdfs into the given buffer.
pub fn render(&self, buffer: &mut TheRGBABuffer) {
let width = buffer.dim().width as usize;
let height = buffer.dim().height;
let pixels = buffer.pixels_mut();
pixels
.par_rchunks_exact_mut(width * 4)
.enumerate()
.for_each(|(j, line)| {
for (i, pixel) in line.chunks_exact_mut(4).enumerate() {
let i = j * width + i;
let x = (i % width) as i32;
let y = height - (i / width) as i32 - 1;
let mut color = self.background.clone();
let mut distance = f32::MAX;
let p = Vec2::new(x as f32, y as f32);
let mut i: Option<usize> = None;
for (index, sdf) in self.sdfs.iter().enumerate() {
let d = sdf.distance(p);
if d < distance {
// let c1 = self.patterns[index].get_color(
// p,
// &d,
// &color,
// self.highlight(index),
// );
// if i.is_some() {
// //let cx = (x as f32 /*- width as f32 / 2.0*/) / width as f32;
// //color = color.mix(&c1, cx);
// let k = 50.0;
// let h = clamp( 0.5 + 0.5*(distance-d)/k, 0.0, 1.0 );
// } else {
// color = c1;
// }
i = Some(index);
distance = d;
}
}
if let Some(index) = i {
color = self.patterns[index].get_color(
p,
&distance,
&color,
self.highlight(index),
);
}
// if let Some(index) = self.selected {
// let d = self.sdfs[index].distance(p);
// color = self.patterns[index].get_color(
// p,
// &d,
// &color,
// self.highlight(index),
// );
// }
pixel.copy_from_slice(&color.to_u8_array());
}
});
}
/// Returns the index of the sdf at the given position.
pub fn index_at(&self, p: Vec2<f32>) -> Option<usize> {
for (index, sdf) in self.sdfs.iter().enumerate() {
let d = sdf.distance(p);
if d < 0.0 {
return Some(index);
}
}
None
}
/// Returns the selected color if the given sdf index is highlighted.
#[inline(always)]
fn highlight(&self, index: usize) -> Option<&TheColor> {
if self.error == Some(index) {
Some(&self.error_highlight)
} else if self.selected == Some(index) {
Some(&self.highlight)
} else if self.hover == Some(index) {
Some(&self.hover_highlight)
} else {
None
}
}
/// Clear the canvas.
pub fn clear(&mut self) {
self.sdfs.clear();
self.patterns.clear();
self.selected = None;
self.hover = None;
}
/// Returns true if the canvas is empty.
pub fn is_empty(&self) -> bool {
self.sdfs.is_empty()
}
}