Skip to content

Commit e706c53

Browse files
authored
Fix debug builds (#1)
1 parent 4cf1961 commit e706c53

File tree

7 files changed

+97
-36
lines changed

7 files changed

+97
-36
lines changed

Cargo.lock

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2024"
55
license = "Apache-2.0 OR Custom"
66

77
[dependencies]
8-
bevy = "0.15"
8+
bevy = { version = "0.15", features = ["shader_format_glsl"] }
99
bytemuck = "1"
1010
async-channel = "2"
1111
futures = "0.3"
@@ -20,6 +20,9 @@ wgsparkl3d = { git = "https://github.com/dimforge/wgsparkl.git", rev = "f64d3911
2020
parry3d = "0.18"
2121
bevy_rapier3d = "0.29"
2222

23+
[dev-dependencies]
24+
bevy_editor_cam = "0.5"
25+
2326
[patch.crates-io]
2427
# rapier3d = { path = "../rapier/crates/rapier3d" }
2528
# rapier3d = { path = "../rapier/crates/rapier3d" }

examples/rocks.rs renamed to examples/cube.rs

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use bevy::prelude::*;
22
use bevy::render::renderer::RenderDevice;
3+
use bevy_editor_cam::DefaultEditorCamPlugins;
4+
use bevy_editor_cam::prelude::EditorCam;
35
use bevy_rapier3d::geometry::RapierColliderHandle;
46
use bevy_rapier3d::plugin::ReadRapierContext;
57
use bevy_rapier3d::prelude::{Collider, RigidBody};
@@ -9,6 +11,7 @@ use bevy_wgsparkl::resources::{AppState, PhysicsContext};
911
use nalgebra::{Vector3, vector};
1012
use wgrapier3d::dynamics::body::{BodyCoupling, BodyCouplingEntry};
1113
use wgsparkl3d::models::DruckerPrager;
14+
use wgsparkl3d::solver::ParticlePhase;
1215
use wgsparkl3d::{
1316
models::ElasticCoefficients,
1417
pipeline::MpmData,
@@ -17,7 +20,7 @@ use wgsparkl3d::{
1720

1821
pub fn main() {
1922
App::new()
20-
.add_plugins(DefaultPlugins)
23+
.add_plugins((DefaultPlugins, DefaultEditorCamPlugins))
2124
.add_plugins(bevy_rapier3d::plugin::RapierPhysicsPlugin::<()>::default())
2225
.add_plugins(RapierDebugRenderPlugin::default())
2326
.add_plugins(bevy_wgsparkl::WgSparklPlugin)
@@ -28,13 +31,17 @@ pub fn main() {
2831
pub fn setup_scene(mut commands: Commands) {
2932
commands.spawn((
3033
Camera3d::default(),
34+
EditorCam {
35+
last_anchor_depth: 110f64,
36+
..Default::default()
37+
},
3138
Transform::from_xyz(-30.0, 30.0, 100.0).looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
3239
));
3340
/*
3441
* Ground
3542
*/
3643
let ground_size = 200.1;
37-
let ground_height = 0.1;
44+
let ground_height = 2.0;
3845

3946
commands.spawn((
4047
Transform::from_xyz(0.0, -ground_height, 0.0),
@@ -68,10 +75,9 @@ pub fn setup_mpm_particles(
6875
let grid_size_x = 25;
6976
let grid_size_y = 25;
7077
let grid_size_z = 25;
71-
let num_rocks = grid_size_x * grid_size_y * grid_size_z;
78+
let num_particles = grid_size_x * grid_size_y * grid_size_z;
7279

73-
let rocks = (0..num_rocks)
74-
.into_iter()
80+
let particle_positions = (0..num_particles)
7581
.map(|i| {
7682
let x = i % grid_size_x;
7783
let y = (i / grid_size_x) % grid_size_y;
@@ -103,7 +109,7 @@ pub fn setup_mpm_particles(
103109
let device = device.wgpu_device();
104110

105111
if !app_state.restarting {
106-
app_state.num_substeps = 32;
112+
app_state.num_substeps = 8;
107113
app_state.gravity_factor = 1.0;
108114
};
109115

@@ -112,30 +118,49 @@ pub fn setup_mpm_particles(
112118
dt: (1.0 / 60.0) / (app_state.num_substeps as f32),
113119
};
114120

115-
let cell_width = 0.5;
121+
let cell_width = 1.0;
116122
let mut particles = vec![];
117123

118-
for rock in &rocks {
119-
let position = vector![rock.x, rock.y, rock.z];
120-
121-
let rock_size = vector![1.0, 1.0, 1.0];
122-
let volume = rock_size.x * rock_size.y * rock_size.z;
123-
let density = 2700.0;
124-
particles.push(Particle {
125-
position: vector![position.x, position.y, position.z],
126-
velocity: Vector3::zeros(),
127-
volume: ParticleMassProps::new(density * volume, volume.cbrt() / 2.0),
128-
model: ElasticCoefficients::from_young_modulus(10_000_000.0, 0.2),
129-
plasticity: Some(DruckerPrager {
130-
// TODO: tune these values.
131-
h0: 45.0f32.to_radians(),
132-
h1: 50.0f32.to_radians(),
133-
h2: 0.4,
134-
h3: 15.0f32.to_radians(),
135-
..DruckerPrager::new(10_000_000.0, 0.2)
136-
}),
137-
phase: None,
138-
});
124+
for x in -1..2 {
125+
for z in -1..2 {
126+
let x = x as f32;
127+
let z = z as f32;
128+
let offset = vector![x * grid_size_x as f32, 0f32, z * grid_size_z as f32] * 2f32;
129+
for particle in &particle_positions {
130+
let position = vector![particle.x, particle.y, particle.z];
131+
132+
let particle_size = vector![1.0, 1.0, 1.0];
133+
let volume = particle_size.x * particle_size.y * particle_size.z;
134+
let density = 1700.0;
135+
particles.push(Particle {
136+
position: nalgebra::Rotation::from_axis_angle(
137+
&Vector3::z_axis(),
138+
5f32.to_radians(),
139+
) * vector![position.x, position.y, position.z]
140+
+ offset,
141+
velocity: Vector3::zeros(),
142+
volume: ParticleMassProps::new(density * volume, volume.cbrt() / 2.0),
143+
model: ElasticCoefficients::from_young_modulus(
144+
100_000_000.0 * 10f32.powf(z - 1f32),
145+
0.2 + x * 0.05f32,
146+
),
147+
plasticity: Some(DruckerPrager {
148+
h0: (45.0f32 + x * 20f32).to_radians(),
149+
h1: (70.0f32 + x * 20f32).to_radians() + z,
150+
h2: 1.6 + z * 0.5f32,
151+
h3: 25.0f32.to_radians() + x,
152+
..DruckerPrager::new(
153+
100_000_000.0 * 10f32.powf(z - 1f32),
154+
0.2 + x * 0.05f32,
155+
)
156+
}),
157+
phase: Some(ParticlePhase {
158+
phase: 0.5 + 0.5 * x,
159+
max_stretch: (0.0 + z).clamp(0.0, 1.0),
160+
}),
161+
});
162+
}
163+
}
139164
}
140165

141166
println!("Number of simulated particles: {}", particles.len());

src/instancing3d.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,17 @@ use bevy::{
55
core_pipeline::core_3d::Transparent3d,
66
ecs::{
77
query::QueryItem,
8-
system::{lifetimeless::*, SystemParamItem},
8+
system::{SystemParamItem, lifetimeless::*},
99
},
1010
pbr::{
1111
MeshPipeline, MeshPipelineKey, RenderMeshInstances, SetMeshBindGroup, SetMeshViewBindGroup,
1212
},
1313
prelude::*,
1414
render::{
15+
Render, RenderApp, RenderSet,
1516
extract_component::{ExtractComponent, ExtractComponentPlugin},
1617
mesh::{
17-
allocator::MeshAllocator, MeshVertexBufferLayoutRef, RenderMesh, RenderMeshBufferInfo,
18+
MeshVertexBufferLayoutRef, RenderMesh, RenderMeshBufferInfo, allocator::MeshAllocator,
1819
},
1920
render_asset::RenderAssets,
2021
render_phase::{
@@ -23,7 +24,6 @@ use bevy::{
2324
},
2425
render_resource::*,
2526
view::ExtractedView,
26-
Render, RenderApp, RenderSet,
2727
},
2828
};
2929
use bytemuck::{Pod, Zeroable};

src/prep_vertex_buffer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
use wgcore::Shader;
12
use wgcore::kernel::{KernelInvocationBuilder, KernelInvocationQueue};
23
use wgcore::tensor::GpuScalar;
3-
use wgcore::Shader;
44
use wgebra::WgSvd2;
55
use wgebra::WgSvd3;
66
use wgpu::{Buffer, BufferUsages, ComputePipeline, Device};

src/startup.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub fn setup_app(mut commands: Commands, device: Res<RenderDevice>) {
4848
let features = device.features();
4949
let timestamps = features
5050
.contains(Features::TIMESTAMP_QUERY)
51-
.then(|| GpuTimestamps::new(device.wgpu_device(), 512));
51+
.then(|| GpuTimestamps::new(device.wgpu_device(), 1024));
5252
commands.insert_resource(Timestamps {
5353
timestamps,
5454
..Default::default()

src/step.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,12 @@ fn step_simulation_multisteps(
177177
timestamps: Some(timestamps),
178178
..Default::default()
179179
};
180-
180+
// `GpuTimestamps` uses a buffer of 2 `Timestamps`, one for the start and one for the end of the operation,
181+
// it's holding 9 floats (see `timings` below).
182+
debug_assert!(
183+
timestamps_ms.len() >= num_substeps * 2 * 9,
184+
"GpuTimestamps should be initialized with a bigger size"
185+
);
181186
for i in 0..num_substeps {
182187
let mut timings = [
183188
&mut new_timings.grid_sort,
@@ -190,7 +195,8 @@ fn step_simulation_multisteps(
190195
&mut new_timings.particles_update,
191196
&mut new_timings.integrate_bodies,
192197
];
193-
let times = &timestamps_ms[i * timings.len() * 2..];
198+
let start_index = i * timings.len() * 2;
199+
let times = &timestamps_ms[start_index..];
194200

195201
for (k, timing) in timings.iter_mut().enumerate() {
196202
**timing += times[k * 2 + 1] - times[k * 2];

0 commit comments

Comments
 (0)