Skip to content

Commit 6ac097c

Browse files
committed
ability to change example if loading is too long or failed
1 parent 069bbb6 commit 6ac097c

File tree

5 files changed

+134
-131
lines changed

5 files changed

+134
-131
lines changed

examples3d/src/glb_to_point_cloud_color.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ use nalgebra::{vector, Isometry3, Transform3, UnitQuaternion, Vector3};
55
use std::{fs::File, io::Read};
66
use wgsparkl3d::load_mesh3d::load_gltf::load_model_with_colors;
77
use wgsparkl3d::{pipeline::MpmData, solver::SimulationParams};
8-
use wgsparkl_testbed3d::load_scene::Dependencies;
8+
use wgsparkl_testbed3d::load_scene::{Dependencies, Loader};
99
use wgsparkl_testbed3d::{AppState, Callbacks, PhysicsContext, RapierData};
1010

11+
pub fn load(loader: &mut Loader) {
12+
loader.load_raw_file("shiba.glb");
13+
}
14+
1115
pub fn elastic_color_model_demo(
1216
device: RenderDevice,
1317
app_state: &mut AppState,
1418
_callbacks: &mut Callbacks,
1519
assets: &Dependencies,
1620
) -> PhysicsContext {
17-
let mut file = File::open("assets/shiba.glb").expect("Failed to open GLB file");
18-
let mut buffer = Vec::new();
19-
file.read_to_end(&mut buffer).expect("Failed to read file");
21+
let buffer = &assets.get_data("shiba.glb").unwrap().data;
2022
let pc_grid = load_model_with_colors(
21-
&buffer,
23+
buffer,
2224
Transform3::from_matrix_unchecked(
2325
Isometry3::from_parts(
2426
Vector3::new(0.0, 6.0, 0.0).into(),

examples3d/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ fn register_scenes(world: &mut World) {
4040
(
4141
"elastic_model_colors".to_string(),
4242
Box::new(glb_to_point_cloud_color::elastic_color_model_demo),
43-
None,
43+
Some(Box::new(glb_to_point_cloud_color::load)),
4444
),
4545
(
4646
"taichi_banana".to_string(),

src_testbed/file_loader.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::time::Duration;
2+
13
use bevy::{
24
asset::{io::Reader, Asset, AssetLoader, LoadContext},
35
reflect::Reflect,

src_testbed/lib.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,21 @@ pub fn init_testbed(app: &mut App) {
5555
.add_plugins(bevy_egui::EguiPlugin)
5656
.init_resource::<Callbacks>()
5757
.add_systems(Startup, startup::setup_app)
58-
.add_systems(
59-
Update,
60-
ui::update_ui_loading.run_if(not(in_state(SceneState::Loaded))),
61-
)
6258
.add_systems(
6359
Update,
6460
(
6561
ui::update_ui,
66-
(step::step_simulation, step::callbacks)
62+
(
63+
(step::step_simulation, step::callbacks)
64+
.chain()
65+
.run_if(|state: Res<AppState>| state.run_state != RunState::Paused),
66+
rigid_graphics::update_rigid_graphics,
67+
hot_reload::handle_hot_reloading,
68+
)
6769
.chain()
68-
.run_if(|state: Res<AppState>| state.run_state != RunState::Paused),
69-
rigid_graphics::update_rigid_graphics,
70-
hot_reload::handle_hot_reloading,
70+
.run_if(in_state(SceneState::Loaded)),
7171
)
72-
.chain()
73-
.run_if(in_state(SceneState::Loaded)),
72+
.chain(),
7473
);
7574

7675
#[cfg(not(target_arch = "wasm32"))]

src_testbed/ui.rs

Lines changed: 115 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::load_scene::SceneInits;
1+
use crate::load_scene::{SceneInits, SceneState};
22
use crate::prep_vertex_buffer::RenderMode;
33
use crate::startup::RigidParticlesTag;
44
use crate::{AppState, PhysicsContext, RunState, Timestamps};
@@ -9,17 +9,13 @@ use bevy_egui::{egui, EguiContexts};
99
use nalgebra::vector;
1010
use wgsparkl::solver::SimulationParams;
1111

12-
pub fn update_ui_loading(mut ui_context: EguiContexts) {
13-
egui::Window::new("Loading").show(ui_context.ctx_mut(), |ui| {
14-
ui.label("Loading...");
15-
});
16-
}
1712
#[allow(clippy::too_many_arguments)]
1813
pub fn update_ui(
1914
mut commands: Commands,
2015
mut ui_context: EguiContexts,
21-
physics: ResMut<PhysicsContext>,
16+
physics: Option<ResMut<PhysicsContext>>,
2217
mut app_state: ResMut<AppState>,
18+
scene_state: Res<State<SceneState>>,
2319
scenes: Res<SceneInits>,
2420
timings: Res<Timestamps>,
2521
queue: Res<RenderQueue>,
@@ -42,127 +38,131 @@ pub fn update_ui(
4238
app_state.restarting = false;
4339
}
4440

45-
let mut changed = false;
46-
egui::ComboBox::from_label("render mode")
47-
.selected_text(RenderMode::from_u32(app_state.render_config.mode).text())
48-
.show_ui(ui, |ui| {
49-
for i in 0..6 {
50-
changed = ui
51-
.selectable_value(
52-
&mut app_state.render_config.mode,
53-
i,
54-
RenderMode::from_u32(i).text(),
55-
)
56-
.changed()
57-
|| changed;
58-
}
59-
});
41+
if scene_state.get() == &SceneState::Loading {
42+
ui.label("Loading...");
43+
} else if let Some(physics) = physics {
44+
let mut changed = false;
45+
egui::ComboBox::from_label("render mode")
46+
.selected_text(RenderMode::from_u32(app_state.render_config.mode).text())
47+
.show_ui(ui, |ui| {
48+
for i in 0..6 {
49+
changed = ui
50+
.selectable_value(
51+
&mut app_state.render_config.mode,
52+
i,
53+
RenderMode::from_u32(i).text(),
54+
)
55+
.changed()
56+
|| changed;
57+
}
58+
});
6059

61-
if changed {
62-
queue.write_buffer(
63-
app_state.gpu_render_config.buffer.buffer(),
64-
0,
65-
bytemuck::bytes_of(&app_state.render_config.mode),
66-
);
67-
queue.submit([]);
68-
}
60+
if changed {
61+
queue.write_buffer(
62+
app_state.gpu_render_config.buffer.buffer(),
63+
0,
64+
bytemuck::bytes_of(&app_state.render_config.mode),
65+
);
66+
queue.submit([]);
67+
}
6968

70-
let mut sim_params_changed = false;
71-
sim_params_changed = ui
72-
.add(Slider::new(&mut app_state.num_substeps, 1..=200).text("substeps"))
73-
.changed()
74-
|| sim_params_changed;
75-
sim_params_changed = ui
76-
.add(Slider::new(&mut app_state.gravity_factor, 0.0..=10.0).text("gravity factor"))
77-
.changed()
78-
|| sim_params_changed;
69+
let mut sim_params_changed = false;
70+
sim_params_changed = ui
71+
.add(Slider::new(&mut app_state.num_substeps, 1..=200).text("substeps"))
72+
.changed()
73+
|| sim_params_changed;
74+
sim_params_changed = ui
75+
.add(Slider::new(&mut app_state.gravity_factor, 0.0..=10.0).text("gravity factor"))
76+
.changed()
77+
|| sim_params_changed;
7978

80-
if ui
81-
.checkbox(&mut app_state.show_rigid_particles, "show rigid_particles")
82-
.changed()
83-
{
84-
for mut visibility in rigid_particles.iter_mut() {
85-
if app_state.show_rigid_particles {
86-
*visibility = Visibility::Inherited;
87-
} else {
88-
*visibility = Visibility::Hidden;
79+
if ui
80+
.checkbox(&mut app_state.show_rigid_particles, "show rigid_particles")
81+
.changed()
82+
{
83+
for mut visibility in rigid_particles.iter_mut() {
84+
if app_state.show_rigid_particles {
85+
*visibility = Visibility::Inherited;
86+
} else {
87+
*visibility = Visibility::Hidden;
88+
}
8989
}
9090
}
91-
}
9291

93-
#[cfg(feature = "dim2")]
94-
let gravity = vector![0.0, -9.81];
95-
#[cfg(feature = "dim3")]
96-
let gravity = vector![0.0, -9.81, 0.0];
92+
#[cfg(feature = "dim2")]
93+
let gravity = vector![0.0, -9.81];
94+
#[cfg(feature = "dim3")]
95+
let gravity = vector![0.0, -9.81, 0.0];
9796

98-
if sim_params_changed {
99-
let new_params = SimulationParams {
100-
gravity: gravity * app_state.gravity_factor,
101-
dt: (1.0 / 60.0) / (app_state.num_substeps as f32),
102-
#[cfg(feature = "dim2")]
103-
padding: 0.0,
104-
};
105-
queue.write_buffer(
106-
physics.data.sim_params.params.buffer(),
107-
0,
108-
bytemuck::bytes_of(&new_params),
109-
);
110-
queue.submit([]);
111-
}
97+
if sim_params_changed {
98+
let new_params = SimulationParams {
99+
gravity: gravity * app_state.gravity_factor,
100+
dt: (1.0 / 60.0) / (app_state.num_substeps as f32),
101+
#[cfg(feature = "dim2")]
102+
padding: 0.0,
103+
};
104+
queue.write_buffer(
105+
physics.data.sim_params.params.buffer(),
106+
0,
107+
bytemuck::bytes_of(&new_params),
108+
);
109+
queue.submit([]);
110+
}
112111

113-
ui.label(format!("Particle count: {}", physics.particles.len()));
114-
ui.label(format!(
115-
"Rigid particle count: {}",
116-
physics.data.rigid_particles.len()
117-
));
112+
ui.label(format!("Particle count: {}", physics.particles.len()));
113+
ui.label(format!(
114+
"Rigid particle count: {}",
115+
physics.data.rigid_particles.len()
116+
));
118117

119-
CollapsingHeader::new(format!("GPU runtime: {:.3}ms", timings.total_time()))
120-
.id_salt("GPU runtimes")
121-
.show(ui, |ui| {
122-
ui.label(format!(
123-
"Rigid update: {:.3}ms",
124-
timings.update_rigid_particles
125-
));
126-
ui.label(format!("Grid sort: {:.3}ms", timings.grid_sort));
127-
ui.label(format!("CDF Grid update: {:.3}ms", timings.grid_update_cdf));
128-
ui.label(format!("CDF P2G: {:.3}ms", timings.p2g_cdf));
129-
ui.label(format!("CDF G2P: {:.3}ms", timings.g2p_cdf));
130-
ui.label(format!("P2G: {:.3}ms", timings.p2g));
131-
ui.label(format!("Grid update: {:.3}ms", timings.grid_update));
132-
ui.label(format!("G2P: {:.3}ms", timings.g2p));
133-
ui.label(format!(
134-
"Particles update: {:.3}ms",
135-
timings.particles_update
136-
));
137-
ui.label(format!(
138-
"Integrate bodies: {:.3}ms",
139-
timings.integrate_bodies
140-
));
141-
});
118+
CollapsingHeader::new(format!("GPU runtime: {:.3}ms", timings.total_time()))
119+
.id_salt("GPU runtimes")
120+
.show(ui, |ui| {
121+
ui.label(format!(
122+
"Rigid update: {:.3}ms",
123+
timings.update_rigid_particles
124+
));
125+
ui.label(format!("Grid sort: {:.3}ms", timings.grid_sort));
126+
ui.label(format!("CDF Grid update: {:.3}ms", timings.grid_update_cdf));
127+
ui.label(format!("CDF P2G: {:.3}ms", timings.p2g_cdf));
128+
ui.label(format!("CDF G2P: {:.3}ms", timings.g2p_cdf));
129+
ui.label(format!("P2G: {:.3}ms", timings.p2g));
130+
ui.label(format!("Grid update: {:.3}ms", timings.grid_update));
131+
ui.label(format!("G2P: {:.3}ms", timings.g2p));
132+
ui.label(format!(
133+
"Particles update: {:.3}ms",
134+
timings.particles_update
135+
));
136+
ui.label(format!(
137+
"Integrate bodies: {:.3}ms",
138+
timings.integrate_bodies
139+
));
140+
});
142141

143-
ui.horizontal(|ui| {
144-
let label = if app_state.run_state == RunState::Paused {
145-
"Run"
146-
} else {
147-
"Pause"
148-
};
149-
150-
if ui.button(label).clicked() {
151-
if app_state.run_state == RunState::Paused {
152-
app_state.run_state = RunState::Running
142+
ui.horizontal(|ui| {
143+
let label = if app_state.run_state == RunState::Paused {
144+
"Run"
153145
} else {
154-
app_state.run_state = RunState::Paused
146+
"Pause"
147+
};
148+
149+
if ui.button(label).clicked() {
150+
if app_state.run_state == RunState::Paused {
151+
app_state.run_state = RunState::Running
152+
} else {
153+
app_state.run_state = RunState::Paused
154+
}
155155
}
156-
}
157156

158-
if ui.button("Step").clicked() {
159-
app_state.run_state = RunState::Step;
160-
}
157+
if ui.button("Step").clicked() {
158+
app_state.run_state = RunState::Step;
159+
}
161160

162-
if ui.button("Restart").clicked() {
163-
scenes.init_scene(&mut commands, app_state.selected_scene);
164-
app_state.restarting = true;
165-
}
166-
});
161+
if ui.button("Restart").clicked() {
162+
scenes.init_scene(&mut commands, app_state.selected_scene);
163+
app_state.restarting = true;
164+
}
165+
});
166+
}
167167
});
168168
}

0 commit comments

Comments
 (0)