-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbasic.rs
More file actions
77 lines (69 loc) · 2.45 KB
/
basic.rs
File metadata and controls
77 lines (69 loc) · 2.45 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
use bevy::color::palettes::css;
use bevy::prelude::*;
// The prelude contains the basic things needed to create shapes
use bevy_smud::prelude::*;
fn main() {
App::new()
.add_plugins((DefaultPlugins, SmudPlugin))
.add_systems(Startup, setup)
.run();
}
fn setup(
mut commands: Commands,
mut shaders: ResMut<Assets<Shader>>,
asset_server: Res<AssetServer>,
) {
// add_sdf_expr expects a wgsl expression
// p is the position of a fragment within the sdf shape, with 0, 0 at the center.
// Here we are using the built-in sd_circle function, which accepts the
// radius as a parameter.
let circle = shaders.add_sdf_expr("smud::sd_circle(p, 70.)");
// There are other ways to define sdfs as well:
// .add_sdf_body let's you add multiple lines and needs to end with a return statements
let peanut = shaders.add_sdf_body(
r"
// Taking the absolute value of p.x creates a vertical line of symmetry
let p_2 = vec2<f32>(abs(p.x), p.y);
// By subtracting from p, we can move shapes
return smud::sd_circle(p_2 - vec2<f32>(20., 0.), 40.);
",
);
// If the sdf gets very complicated, you can keep it in a .wgsl file:
let bevy = asset_server.load("bevy.wgsl");
commands.spawn(SmudShape {
color: css::TOMATO.into(),
sdf: circle,
// The bounds need to be bigger than the shape we're drawing
// Since the circle has radius 70, we make the bounds 160 (with some padding).
bounds: Rectangle::from_length(160.),
..default()
});
commands.spawn((
Transform::from_translation(Vec3::X * 200.),
SmudShape {
color: Color::srgb(0.7, 0.6, 0.4),
sdf: peanut,
bounds: Rectangle::from_length(160.),
..default()
},
));
commands.spawn((
Transform {
translation: Vec3::X * -200.,
scale: Vec3::splat(0.4),
..default()
},
SmudShape {
color: Color::WHITE,
sdf: bevy,
// You can also specify a custom type of fill
// The simple fill is just a simple anti-aliased opaque fill
fill: SIMPLE_FILL_HANDLE,
bounds: Rectangle::from_length(590.),
..default()
},
));
// bevy_smud comes with anti-aliasing built into the standard fills
// which is more efficient than MSAA, and also works on Linux, wayland
commands.spawn((Camera2d, Msaa::Off));
}