-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathdraw_shapes_shader.rs
93 lines (76 loc) · 2.11 KB
/
draw_shapes_shader.rs
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
use notan::draw::*;
use notan::math::Vec2;
use notan::prelude::*;
const POS: Vec2 = Vec2::new(600.0, 300.0);
//language=glsl
const FRAGMENT: ShaderSource = notan::fragment_shader! {
r#"
#version 450
precision mediump float;
layout(location = 0) in vec4 v_color;
layout(location = 0) out vec4 color;
layout(set = 0, binding = 1) uniform Surface {
vec2 u_pos;
};
void main() {
if (gl_FragCoord.y < u_pos.y) {
if (gl_FragCoord.x < u_pos.x) {
color = vec4(1.0, 0.0, 0.0, 1.0) * v_color;
} else {
color = vec4(0.0, 0.0, 1.0, 1.0) * v_color;
}
} else {
if (gl_FragCoord.x < u_pos.x) {
color = vec4(0.0, 1.0, 0.0, 1.0) * v_color;
} else {
color = vec4(0.5, 0.0, 1.0, 1.0) * v_color;
}
}
}
"#
};
#[derive(AppState)]
struct State {
pipeline: Pipeline,
ubo: Buffer,
count: f32,
}
#[notan_main]
fn main() -> Result<(), String> {
notan::init_with(init)
.add_config(DrawConfig)
.draw(draw)
.build()
}
fn init(gfx: &mut Graphics) -> State {
let pipeline = create_shape_pipeline(gfx, Some(&FRAGMENT)).unwrap();
let ubo = gfx
.create_uniform_buffer(1, "Surface")
.with_data(&[POS.x, POS.y])
.build()
.unwrap();
State {
pipeline,
ubo,
count: 0.0,
}
}
fn draw(app: &mut App, gfx: &mut Graphics, state: &mut State) {
state.count += app.timer.delta_f32() * 10.0;
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
// star without custom pipeline
draw.star(5, 150.0, 70.0)
.position(200.0, 300.0)
.rotate_from((200.0, 300.0), state.count.to_radians());
// add custom pipeline for shapes
draw.shape_pipeline()
.pipeline(&state.pipeline)
.uniform_buffer(&state.ubo);
draw.star(5, 150.0, 70.0)
.position(600.0, 300.0)
.rotate_from((600.0, 300.0), state.count.to_radians());
// remove custom pipeline
draw.shape_pipeline().remove();
gfx.render(&draw);
}