Skip to content

Fix debug builds #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Mar 13, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2024"
license = "Apache-2.0 OR Custom"

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

[dev-dependencies]
bevy_editor_cam = "0.5"

[patch.crates-io]
# rapier3d = { path = "../rapier/crates/rapier3d" }
# rapier3d = { path = "../rapier/crates/rapier3d" }
Expand Down
81 changes: 53 additions & 28 deletions examples/rocks.rs → examples/cube.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use bevy::prelude::*;
use bevy::render::renderer::RenderDevice;
use bevy_editor_cam::DefaultEditorCamPlugins;
use bevy_editor_cam::prelude::EditorCam;
use bevy_rapier3d::geometry::RapierColliderHandle;
use bevy_rapier3d::plugin::ReadRapierContext;
use bevy_rapier3d::prelude::{Collider, RigidBody};
Expand All @@ -9,6 +11,7 @@ use bevy_wgsparkl::resources::{AppState, PhysicsContext};
use nalgebra::{Vector3, vector};
use wgrapier3d::dynamics::body::{BodyCoupling, BodyCouplingEntry};
use wgsparkl3d::models::DruckerPrager;
use wgsparkl3d::solver::ParticlePhase;
use wgsparkl3d::{
models::ElasticCoefficients,
pipeline::MpmData,
Expand All @@ -17,7 +20,7 @@ use wgsparkl3d::{

pub fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins((DefaultPlugins, DefaultEditorCamPlugins))
.add_plugins(bevy_rapier3d::plugin::RapierPhysicsPlugin::<()>::default())
.add_plugins(RapierDebugRenderPlugin::default())
.add_plugins(bevy_wgsparkl::WgSparklPlugin)
Expand All @@ -28,13 +31,17 @@ pub fn main() {
pub fn setup_scene(mut commands: Commands) {
commands.spawn((
Camera3d::default(),
EditorCam {
last_anchor_depth: 110f64,
..Default::default()
},
Transform::from_xyz(-30.0, 30.0, 100.0).looking_at(Vec3::new(0.0, 10.0, 0.0), Vec3::Y),
));
/*
* Ground
*/
let ground_size = 200.1;
let ground_height = 0.1;
let ground_height = 2.0;

commands.spawn((
Transform::from_xyz(0.0, -ground_height, 0.0),
Expand Down Expand Up @@ -68,10 +75,9 @@ pub fn setup_mpm_particles(
let grid_size_x = 25;
let grid_size_y = 25;
let grid_size_z = 25;
let num_rocks = grid_size_x * grid_size_y * grid_size_z;
let num_particles = grid_size_x * grid_size_y * grid_size_z;

let rocks = (0..num_rocks)
.into_iter()
let particle_positions = (0..num_particles)
.map(|i| {
let x = i % grid_size_x;
let y = (i / grid_size_x) % grid_size_y;
Expand Down Expand Up @@ -103,7 +109,7 @@ pub fn setup_mpm_particles(
let device = device.wgpu_device();

if !app_state.restarting {
app_state.num_substeps = 32;
app_state.num_substeps = 8;
app_state.gravity_factor = 1.0;
};

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

let cell_width = 0.5;
let cell_width = 1.0;
let mut particles = vec![];

for rock in &rocks {
let position = vector![rock.x, rock.y, rock.z];

let rock_size = vector![1.0, 1.0, 1.0];
let volume = rock_size.x * rock_size.y * rock_size.z;
let density = 2700.0;
particles.push(Particle {
position: vector![position.x, position.y, position.z],
velocity: Vector3::zeros(),
volume: ParticleMassProps::new(density * volume, volume.cbrt() / 2.0),
model: ElasticCoefficients::from_young_modulus(10_000_000.0, 0.2),
plasticity: Some(DruckerPrager {
// TODO: tune these values.
h0: 45.0f32.to_radians(),
h1: 50.0f32.to_radians(),
h2: 0.4,
h3: 15.0f32.to_radians(),
..DruckerPrager::new(10_000_000.0, 0.2)
}),
phase: None,
});
for x in -1..2 {
for z in -1..2 {
let x = x as f32;
let z = z as f32;
let offset = vector![x * grid_size_x as f32, 0f32, z * grid_size_z as f32] * 2f32;
for particle in &particle_positions {
let position = vector![particle.x, particle.y, particle.z];

Comment on lines +124 to +131
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

those are way outside the scope of this PR, and lacking proper polish, but I'll roll with it as this project is still heavily WIP.

let particle_size = vector![1.0, 1.0, 1.0];
let volume = particle_size.x * particle_size.y * particle_size.z;
let density = 1700.0;
particles.push(Particle {
position: nalgebra::Rotation::from_axis_angle(
&Vector3::z_axis(),
5f32.to_radians(),
) * vector![position.x, position.y, position.z]
+ offset,
velocity: Vector3::zeros(),
volume: ParticleMassProps::new(density * volume, volume.cbrt() / 2.0),
model: ElasticCoefficients::from_young_modulus(
100_000_000.0 * 10f32.powf(z - 1f32),
0.2 + x * 0.05f32,
),
plasticity: Some(DruckerPrager {
h0: (45.0f32 + x * 20f32).to_radians(),
h1: (70.0f32 + x * 20f32).to_radians() + z,
h2: 1.6 + z * 0.5f32,
h3: 25.0f32.to_radians() + x,
..DruckerPrager::new(
100_000_000.0 * 10f32.powf(z - 1f32),
0.2 + x * 0.05f32,
)
}),
phase: Some(ParticlePhase {
phase: 0.5 + 0.5 * x,
max_stretch: (0.0 + z).clamp(0.0, 1.0),
}),
});
}
}
}

println!("Number of simulated particles: {}", particles.len());
Expand Down
6 changes: 3 additions & 3 deletions src/instancing3d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ use bevy::{
core_pipeline::core_3d::Transparent3d,
ecs::{
query::QueryItem,
system::{lifetimeless::*, SystemParamItem},
system::{SystemParamItem, lifetimeless::*},
},
pbr::{
MeshPipeline, MeshPipelineKey, RenderMeshInstances, SetMeshBindGroup, SetMeshViewBindGroup,
},
prelude::*,
render::{
Render, RenderApp, RenderSet,
extract_component::{ExtractComponent, ExtractComponentPlugin},
mesh::{
allocator::MeshAllocator, MeshVertexBufferLayoutRef, RenderMesh, RenderMeshBufferInfo,
MeshVertexBufferLayoutRef, RenderMesh, RenderMeshBufferInfo, allocator::MeshAllocator,
},
render_asset::RenderAssets,
render_phase::{
Expand All @@ -23,7 +24,6 @@ use bevy::{
},
render_resource::*,
view::ExtractedView,
Render, RenderApp, RenderSet,
},
};
use bytemuck::{Pod, Zeroable};
Expand Down
2 changes: 1 addition & 1 deletion src/prep_vertex_buffer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use wgcore::Shader;
use wgcore::kernel::{KernelInvocationBuilder, KernelInvocationQueue};
use wgcore::tensor::GpuScalar;
use wgcore::Shader;
use wgebra::WgSvd2;
use wgebra::WgSvd3;
use wgpu::{Buffer, BufferUsages, ComputePipeline, Device};
Expand Down
2 changes: 1 addition & 1 deletion src/startup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub fn setup_app(mut commands: Commands, device: Res<RenderDevice>) {
let features = device.features();
let timestamps = features
.contains(Features::TIMESTAMP_QUERY)
.then(|| GpuTimestamps::new(device.wgpu_device(), 512));
.then(|| GpuTimestamps::new(device.wgpu_device(), 1024));
commands.insert_resource(Timestamps {
timestamps,
..Default::default()
Expand Down
8 changes: 6 additions & 2 deletions src/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ fn step_simulation_multisteps(
timestamps: Some(timestamps),
..Default::default()
};

debug_assert!(
timestamps_ms.len() >= num_substeps * 2 * 9,
"GpuTimestamps should be initialized with a bigger size"
);
for i in 0..num_substeps {
let mut timings = [
&mut new_timings.grid_sort,
Expand All @@ -190,7 +193,8 @@ fn step_simulation_multisteps(
&mut new_timings.particles_update,
&mut new_timings.integrate_bodies,
];
let times = &timestamps_ms[i * timings.len() * 2..];
let start_index = i * timings.len() * 2;
let times = &timestamps_ms[start_index..];

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