Repository navigation
Expand file tree
/
Copy pathfilter.rs
More file actions
93 lines (81 loc) · 2.64 KB
/
Copy pathfilter.rs
File metadata and controls
93 lines (81 loc) · 2.64 KB
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
//! Cycles through all implemented filters.
use std::time::Instant;
use bevy::color::Color;
use processing_glfw::GlfwContext;
use processing::prelude::*;
use processing_render::render::command::DrawCommand;
use processing_render::shader_value::ShaderValue;
fn main() {
match sketch() {
Ok(_) => exit(0).unwrap(),
Err(e) => {
eprintln!("Sketch error: {:?}", e);
exit(1).unwrap();
}
};
}
fn sketch() -> error::Result<()> {
let mut glfw_ctx = GlfwContext::new(400, 400, false)?;
init(Config::default())?;
let surface = glfw_ctx.create_surface(400, 400, false)?;
let graphics = graphics_create(surface, 400, 400, TextureFormat::Rgba16Float)?;
let palette = [
(40.0, 40.0, Color::srgb(1.0, 0.2, 0.2)),
(200.0, 40.0, Color::srgb(0.2, 1.0, 0.2)),
(40.0, 200.0, Color::srgb(0.2, 0.4, 1.0)),
(200.0, 200.0, Color::srgb(0.95, 0.85, 0.2)),
];
let threshold = filter_threshold()?;
filter_set(threshold, "cutoff", ShaderValue::Float(0.5))?;
let posterize = filter_posterize()?;
filter_set(posterize, "levels", ShaderValue::UInt(4))?;
let blur = filter_blur()?;
filter_set(blur, "radius", ShaderValue::Float(4.0))?;
let filters = [
filter_invert()?,
filter_gray()?,
threshold,
posterize,
filter_opaque()?,
filter_erode()?,
filter_dilate()?,
blur,
];
let start = Instant::now();
while glfw_ctx.poll_events() {
graphics_begin_draw(graphics)?;
graphics_record_command(
graphics,
DrawCommand::BackgroundColor(Color::srgb(0.1, 0.1, 0.15)),
)?;
graphics_record_command(graphics, DrawCommand::NoStroke)?;
for &(x, y, color) in &palette {
graphics_record_command(graphics, DrawCommand::Fill(color))?;
graphics_record_command(
graphics,
DrawCommand::Rect {
x,
y,
w: 160.0,
h: 160.0,
radii: [0.0; 4],
},
)?;
}
let idx = (start.elapsed().as_secs() / 2) as usize % filters.len();
graphics_apply_filter(graphics, filters[idx])?;
graphics_record_command(graphics, DrawCommand::Fill(Color::srgb(0.0, 0.0, 1.0)))?;
graphics_record_command(
graphics,
DrawCommand::Rect {
x: 160.0,
y: 160.0,
w: 80.0,
h: 80.0,
radii: [0.0; 4],
},
)?;
graphics_end_draw(graphics)?;
}
Ok(())
}