Repository navigation
Expand file tree
/
Copy pathsprite_material.rs
More file actions
152 lines (136 loc) · 4.35 KB
/
Copy pathsprite_material.rs
File metadata and controls
152 lines (136 loc) · 4.35 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
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
//! Demonstrates using `SpriteMaterial`s to create custom materials for sprites
use bevy::{
input::common_conditions::input_just_pressed, prelude::*, render::render_resource::AsBindGroup,
shader::ShaderRef,
};
/// This example uses a shader source file from the assets subdirectory
const SHADER_ASSET_PATH: &str = "shaders/sprite_material.wesl";
fn main() {
App::new()
.add_plugins((
DefaultPlugins,
SpriteMaterialPlugin::<DissolveMaterial>::default(),
))
.add_systems(Startup, setup)
.add_systems(
Update,
(
dissolve_on_input.run_if(input_just_pressed(KeyCode::Space)),
update_dissolve,
)
.chain(),
)
.run();
}
#[derive(AsBindGroup, Asset, Reflect, Clone)]
struct DissolveMaterial {
// Start at 20 to not collide with the sprite's bindings
#[uniform(20)]
burn_edge_color: LinearRgba,
#[uniform(20)]
amount: f32,
#[uniform(20)]
burn_edge: f32,
// WebGL2 requires structs to be aligned to 16 bytes
#[cfg(feature = "webgl2")]
#[uniform(20)]
_webgl2_padding_12b: u32,
#[cfg(feature = "webgl2")]
#[uniform(20)]
_webgl2_padding_16b: u32,
}
impl MaterialExtension2d for DissolveMaterial {
fn fragment_shader() -> Option<ShaderRef> {
Some(SHADER_ASSET_PATH.into())
}
}
impl DissolveMaterial {
fn new(amount: f32) -> Self {
Self {
amount,
burn_edge: 0.05,
burn_edge_color: LinearRgba::rgb(1.0, 0.66, 0.0),
#[cfg(feature = "webgl2")]
_webgl2_padding_12b: 0,
#[cfg(feature = "webgl2")]
_webgl2_padding_16b: 0,
}
}
}
#[derive(Component, FromTemplate, Clone, Copy)]
#[component(immutable)]
enum DissolveState {
#[default]
Visible,
Dissolving {
progress: f32,
},
Appearing {
progress: f32,
},
Hidden,
}
fn setup(mut commands: Commands) {
commands.spawn(Camera2d);
commands.spawn(Text::new("Space to dissolve"));
commands.queue_spawn_scene(bsn! {
Sprite {
image: "branding/bevy_bird_dark.png",
}
DissolveState::Visible
SpriteMaterial<DissolveMaterial>(asset_value(DissolveMaterial::new(0.0)))
});
}
fn dissolve_on_input(dissolving: Query<(Entity, &DissolveState)>, mut commands: Commands) {
for (entity, state) in dissolving {
let new = match *state {
DissolveState::Visible => DissolveState::Dissolving { progress: 0.0 },
DissolveState::Hidden => DissolveState::Appearing { progress: 0.0 },
DissolveState::Dissolving { progress } => DissolveState::Appearing {
progress: 1.0 - progress,
},
DissolveState::Appearing { progress } => DissolveState::Dissolving {
progress: 1.0 - progress,
},
};
commands.entity(entity).insert(new);
}
}
fn update_dissolve(
dissolving: Query<(Entity, &SpriteMaterial<DissolveMaterial>, &DissolveState)>,
time: Res<Time>,
mut materials: ResMut<Assets<DissolveMaterial>>,
mut commands: Commands,
) {
for (entity, material, state) in dissolving {
match *state {
DissolveState::Dissolving { mut progress } => {
progress += time.delta_secs();
let new = if progress >= 1.0 {
progress = 1.0;
DissolveState::Hidden
} else {
DissolveState::Dissolving { progress }
};
commands.entity(entity).insert(new);
if let Some(mut material) = materials.get_mut(&material.0) {
material.amount = progress;
}
}
DissolveState::Appearing { mut progress } => {
progress += time.delta_secs();
let new = if progress >= 1.0 {
progress = 1.0;
DissolveState::Visible
} else {
DissolveState::Appearing { progress }
};
commands.entity(entity).insert(new);
if let Some(mut material) = materials.get_mut(&material.0) {
material.amount = 1.0 - progress;
}
}
_ => { /* ignore */ }
}
}
}