-
Notifications
You must be signed in to change notification settings - Fork 0
/
fishy.rs
361 lines (301 loc) · 10.6 KB
/
fishy.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
use bevy::{
prelude::*,
reflect::{TypeUuid, TypePath},
render::{
primitives::Aabb,
render_resource::{
AsBindGroup, ShaderRef,
},
}, math::Vec3A, utils::{HashMap, hashbrown::hash_map::Entry}
};
// This is the struct that will be passed to your shader
#[derive(Asset, TypePath, AsBindGroup, Debug, Clone)]
pub struct CustomMaterial {
#[uniform(2)]
color: Color,
#[uniform(3)]
speed: f32,
#[uniform(4)]
amplitude: f32,
#[uniform(5)]
min_bounds: Vec3,
#[uniform(6)]
max_bounds: Vec3,
#[uniform(7)] // TODO: regroup these into a vector ?
side_to_side_intensity: f32,
#[uniform(8)]
pivot_intensity: f32,
#[uniform(9)]
wave_intensity: f32,
#[uniform(10)]
twist_intensity: f32,
}
/// You only need to implement functions for features that need non-default behavior. See the Material api docs for details!
impl Material for CustomMaterial {
fn fragment_shader() -> ShaderRef {
"shaders/fish-wave.wgsl".into()
}
fn vertex_shader() -> ShaderRef {
"shaders/fish-wave.wgsl".into()
}
}
impl Default for CustomMaterial {
fn default() -> Self {
CustomMaterial {
color: Color::RED,
speed: 1.0,
amplitude: 1.0,
min_bounds: Vec3::new(-1., -1., -1.),
max_bounds: Vec3::new(1., 1., 1.),
side_to_side_intensity: 0.3,
pivot_intensity: 0.2,
wave_intensity: 2.5,
twist_intensity: 0.7
}
}
}
pub fn update_aabbs (
with_custom_materials: Query<(&Aabb, &mut Handle<CustomMaterial>), With<GlobalTransform>>,
mut custom_materials: ResMut<Assets<CustomMaterial>>,
root_entities: Query<Entity, (With<RootEntity>, Without<Aabb>)>,
children: Query<&Children>,
parents: Query<&Parent>,
with_aabbs: Query<&Aabb>,
mut commands: Commands,
) {
// compute compound aabb
let mut entity_to_children_aabbs: HashMap<Entity, Vec<Aabb>> = HashMap::new();
let mut compound_aabbs: HashMap<Entity, Aabb> = HashMap::new();
let mut processed_entities = 0;
for root_entity in root_entities.iter() {
loop {
for child in children.iter_descendants(root_entity) {
let parent = parents.get(child).expect("we should have a parent available").get();
let children_to_process = children.get(parent).unwrap();
let aabb:Option<Aabb>;
if compound_aabbs.contains_key(&child){
aabb = Some(*compound_aabbs.get(&child).unwrap());
} else {
if let Ok(_aabb) = with_aabbs.get(child){
aabb = Some(*_aabb);
}else {
aabb = None;
}
}
// process those entities that already have aabbs
if let Some(aabb) = aabb {
if ! entity_to_children_aabbs.contains_key(&parent) || !entity_to_children_aabbs.get(&parent).unwrap().contains(&aabb) {
processed_entities += 1;
match entity_to_children_aabbs.entry(parent) {
Entry::Vacant(e) => {
e.insert(vec![aabb.clone()]);
}
Entry::Occupied(mut e) => {
e.get_mut().push(aabb.clone());
}
}
}
compound_aabbs.insert(child, aabb.clone());
// if all children of the parent have been processed, compute the parent's aabb
if let Some(aabbs) = entity_to_children_aabbs.get(&parent) {
if aabbs.len() == children_to_process.len() {
let mut min = Vec3A::splat(f32::MAX);
let mut max = Vec3A::splat(f32::MIN);
for aabb in aabbs.iter(){
min = min.min(aabb.min());
max = max.max(aabb.max());
}
let compound_aabb = Aabb::from_min_max(Vec3::from(min), Vec3::from(max));
compound_aabbs.insert(parent, compound_aabb);
}
}
}
}
// if the root node has been processed or NO entities have been processed (ie , even the leaves did not have aabbs)
if entity_to_children_aabbs.contains_key(&root_entity) || processed_entities == 0 {
break;
}
}
// now build the parent's compound aabb
for (entity, aabb) in &compound_aabbs {
commands.entity(*entity).insert(*aabb);
if entity.index() == root_entity.index() {
let root_aabb = aabb.clone();
// then we assign the root aabb to all children with the custom material
for (_, handle) in with_custom_materials.iter() {
let min = Vec3::from(root_aabb.min());
let max = Vec3::from(root_aabb.max());
let material = custom_materials.get_mut(handle).expect("custom material should have been found");
material.min_bounds = min;
material.max_bounds = max;
}
}
}
}
}
#[derive(Component)]
pub struct Inserted;
#[derive(Component)]
pub struct RootEntity;
pub fn replace_standard_material(
gltf_entities: Query<
(Entity, &Handle<Mesh>, &Name, &Handle<StandardMaterial>),
Without<Inserted>,
>,
mut custom_materials: ResMut<Assets<CustomMaterial>>,
standard_materials: ResMut<Assets<StandardMaterial>>,
mut commands: Commands,
){
for (entity, _, _, old_material) in gltf_entities.iter() {
let original_material = standard_materials.get(old_material).unwrap();
let custom_material =
custom_materials.add(CustomMaterial {
color: original_material.base_color,
speed: 1.0,
..default()
});
commands
.entity(entity)
.remove::<Handle<StandardMaterial>>();
commands.entity(entity)
.insert(custom_material)
.insert(Inserted);
}
}
pub fn setup (
asset_server: Res<AssetServer>,
mut commands: Commands,
)
{
commands.spawn(Camera3dBundle {
transform: Transform::from_xyz(5.7, 3.0, 9.0).looking_at(Vec3::new(0.0, 0.0, 0.0), Vec3::Y),
..default()
});
commands.spawn((SceneBundle {
scene: asset_server.load("models/fish1.glb#Scene0"),
..default()
}, RootEntity));
commands.insert_resource(AmbientLight {
color: Color::WHITE,
brightness: 0.2,
});
commands.spawn(DirectionalLightBundle {
directional_light: DirectionalLight {
shadows_enabled: true,
..default()
},
transform: Transform::from_xyz(0.0, 15.0, 0.0).with_rotation(
Quat::from_euler(EulerRot::XYZ,
(10.0_f32).to_radians(),
(10.0_f32).to_radians(),
(0.0_f32).to_radians(),
)
),
..default()
});
commands.spawn(
TextBundle::from_section(
"Material settings",
TextStyle {
font: asset_server.load("fonts/FiraMono-Medium.ttf"),
font_size: 18.0,
color: Color::WHITE,
},
)
.with_style(Style {
position_type: PositionType::Absolute,
top: Val::Px(10.0),
left: Val::Px(10.0),
..default()
}),
);
}
fn update_shader_uniforms(
keycode: Res<Input<KeyCode>>,
mut custom_materials: ResMut<Assets<CustomMaterial>>,
mut text: Query<&mut Text>,
) {
let increment = 0.05;
for (_, material) in custom_materials.iter_mut() {
let mut text = text.single_mut();
let text = &mut text.sections[0].value;
*text = "Vertex animation settings\n".to_string();
text.push_str("------------------------------\n");
text.push_str(&format!("Speed: {}\n", material.speed));
text.push_str(&format!("Side to side: {}\n", material.side_to_side_intensity));
text.push_str(&format!("Pivot: {}\n", material.pivot_intensity));
text.push_str(&format!("Wave: {}\n", material.wave_intensity));
text.push_str(&format!("Twist: {}\n", material.twist_intensity));
text.push_str("\n\n");
text.push_str("Controls (-/+)\n");
text.push_str("---------------\n");
text.push_str("S/D - Speed\n");
text.push_str("Z/E - Side to side\n");
text.push_str("R/T - Pivot\n");
text.push_str("Y/U - Wave\n");
text.push_str("I/O - Twist\n");
if keycode.pressed(KeyCode::S) {
material.speed -= increment;
}
if keycode.pressed(KeyCode::D) {
material.speed += increment;
}
//
if keycode.pressed(KeyCode::Z) {
material.side_to_side_intensity -= increment;
}
if keycode.pressed(KeyCode::E) {
material.side_to_side_intensity += increment;
}
//
if keycode.pressed(KeyCode::R) {
material.pivot_intensity -= increment;
}
if keycode.pressed(KeyCode::T) {
material.pivot_intensity += increment;
}
//
if keycode.pressed(KeyCode::Y) {
material.wave_intensity -= increment;
}
if keycode.pressed(KeyCode::U) {
material.wave_intensity += increment;
}
//
if keycode.pressed(KeyCode::I) {
material.twist_intensity -= increment;
}
if keycode.pressed(KeyCode::O) {
material.twist_intensity += increment;
}
}
}
pub struct VertexAnimationPlugin;
impl Plugin for VertexAnimationPlugin {
fn build(&self, app: &mut App) {
app
.insert_resource(ClearColor(Color::rgb(0.1, 0.6, 0.6)))
.add_plugins((
MaterialPlugin::<CustomMaterial>::default(),
))
.add_systems(Startup, setup)
.add_systems(Update, (
replace_standard_material,
update_aabbs,
update_shader_uniforms
))
;
}
}
fn main(){
App::new()
.add_plugins((
DefaultPlugins.set(
AssetPlugin {
..default()
}
),
VertexAnimationPlugin
))
.run();
}