Skip to content
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

Sample color model #6

Open
wants to merge 25 commits into
base: cpic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
07c19b4
compiling 3d (from folder)
Vrixyz Mar 3, 2025
8922108
add an example loading a mesh into a particle
Vrixyz Mar 4, 2025
e94f20d
attempt to sample vertex colors from material
Vrixyz Mar 8, 2025
82e67e2
attempt to load glb into particles ; not working
Vrixyz Mar 10, 2025
ec3aa08
better model to showcase
Vrixyz Mar 11, 2025
56215de
correct color loading for gltf
Vrixyz Mar 11, 2025
6d6bbd1
wip loading banana and knife
Vrixyz Mar 12, 2025
00f01a1
format credits
Vrixyz Mar 12, 2025
d501afa
correctly load rigidbody coupling
Vrixyz Mar 12, 2025
4276c3e
knife cutting through banana (but cut is bugged)
Vrixyz Mar 12, 2025
25dc1f4
knife with open blade correctly cuts :)
Vrixyz Mar 12, 2025
37b7124
better time management
Vrixyz Mar 12, 2025
51c4af3
inside the banana is now white/yellow
Vrixyz Mar 12, 2025
841a565
attempt to use lib examples
Vrixyz Mar 13, 2025
cca0349
knif model follow its rigidbody
Vrixyz Mar 14, 2025
0c1be37
add comment about sample_mesh
Vrixyz Mar 14, 2025
5b38cf6
use binary for examples 3d
Vrixyz Mar 14, 2025
82ffe57
use binary for examples 2d
Vrixyz Mar 14, 2025
1b971d9
fix wasm build
Vrixyz Mar 14, 2025
27768c3
better banana cut ; moved utils functions to wgsparkl behind a featur…
Vrixyz Mar 17, 2025
0b2c38e
less bevy specific examples
Vrixyz Mar 18, 2025
f22a3f6
independant example for loading a glb to point cloud
Vrixyz Mar 18, 2025
c7fe2b3
fix some slippy lints
Vrixyz Mar 18, 2025
996eb87
better banana cut
Vrixyz Mar 18, 2025
b799c92
fix 2d examples
Vrixyz Mar 24, 2025
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
31 changes: 22 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,28 @@ rust.unexpected_cfgs = { level = "warn", check-cfg = [
] }

[patch.crates-io]
parry2d = { path = "../parry/crates/parry2d" }
parry3d = { path = "../parry/crates/parry3d" }
encase = { path = "../encase" }
wgcore = { path = "../wgmath/crates/wgcore" }
wgebra = { path = "../wgmath/crates/wgebra" }
wgparry2d = { path = "../wgmath/crates/wgparry/crates/wgparry2d" }
wgparry3d = { path = "../wgmath/crates/wgparry/crates/wgparry3d" }
wgrapier2d = { path = "../wgmath/crates/wgrapier/crates/wgrapier2d" }
wgrapier3d = { path = "../wgmath/crates/wgrapier/crates/wgrapier3d" }
# parry2d = { path = "../parry/crates/parry2d" }
# parry3d = { path = "../parry/crates/parry3d" }
# encase = { path = "../encase" }
# wgcore = { path = "../wgmath/crates/wgcore" }
# wgebra = { path = "../wgmath/crates/wgebra" }
# wgparry2d = { path = "../wgmath/crates/wgparry/crates/wgparry2d" }
# wgparry3d = { path = "../wgmath/crates/wgparry/crates/wgparry3d" }
# wgrapier2d = { path = "../wgmath/crates/wgrapier/crates/wgrapier2d" }
# wgrapier3d = { path = "../wgmath/crates/wgrapier/crates/wgrapier3d" }

parry2d = { git = "https://github.com/dimforge/parry.git", rev = "407fb449504dde7ba0a3d80a8de7e37ac991a77a" }
parry3d = { git = "https://github.com/dimforge/parry.git", rev = "407fb449504dde7ba0a3d80a8de7e37ac991a77a" }
encase = { git = "https://github.com/sebcrozet/encase.git", rev = "6755c1414c225cbeac9fde1ff9394b70d3b05404" }
# wgcore = { git = "https://github.com/dimforge/wgmath.git", rev = "32b54c802be6040c5f003e7d3def4a33e4730d92" }
wgcore = { git = "https://github.com/dimforge/wgmath.git", branch = "wgml-chat" }
wgcore-derive = { git = "https://github.com/dimforge/wgmath.git", branch = "wgml-chat" }
wgebra = { git = "https://github.com/dimforge/wgmath.git", branch = "wgml-chat" }
# wgparry2d = { git = "https://github.com/dimforge/wgmath.git", rev = "32b54c802be6040c5f003e7d3def4a33e4730d92" }
wgparry3d = { git = "https://github.com/dimforge/wgmath.git", branch = "wgml-chat" }
# wgrapier2d = { git = "https://github.com/dimforge/wgmath.git", rev = "32b54c802be6040c5f003e7d3def4a33e4730d92" }
wgrapier3d = { git = "https://github.com/dimforge/wgmath.git", branch = "wgml-chat" }
rapier3d = { git = "https://github.com/vrixyz/rapier.git", branch = "uber_physics_context" }

[profile.release]
opt-level = 'z'
46 changes: 44 additions & 2 deletions crates/wgsparkl3d/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,47 @@ futures-test = "0.3"
serial_test = "3"
approx = "0.5"
async-std = { version = "1", features = ["attributes"] }
bevy = { version = "0.15.0", features = ["shader_format_glsl", "shader_format_spirv", "webgpu"] }
wgsparkl_testbed3d = { path = "../wgsparkl-testbed3d" }
bevy = { version = "0.15.0", features = [
"shader_format_glsl",
"shader_format_spirv",
"webgpu",
"jpeg",
] }
wgsparkl_testbed3d = { path = "../wgsparkl-testbed3d", features = ["dim3"] }
obj-rs = { version = "0.7", default-features = false }
bevy_editor_cam = "*"
image = "0.25"
gltf = { version = "1.3", features = ["KHR_materials_pbrSpecularGlossiness"] }

[package.metadata.docs.rs]
cargo-args = ["-Zunstable-options", "-Zrustdoc-scrape-examples"]

[[example]]
name = "banana3"
path = "examples/banana3.rs"
doc-scrape-examples = true
crate-type = ["lib"]

[[example]]
name = "elastic_cut3"
path = "examples/elastic_cut.rs"
doc-scrape-examples = true
crate-type = ["lib"]

[[example]]
name = "heightfield3"
path = "examples/heightfield3.rs"
doc-scrape-examples = true
crate-type = ["lib"]

[[example]]
name = "sand3"
path = "examples/sand3.rs"
doc-scrape-examples = true
crate-type = ["lib"]

[[example]]
name = "default_scene"
path = "examples/libs/default_scene.rs"
doc-scrape-examples = true
crate-type = ["lib"]
Binary file added crates/wgsparkl3d/assets/banana.glb
Binary file not shown.
Binary file added crates/wgsparkl3d/assets/chefs_knife_modified.glb
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions crates/wgsparkl3d/assets/credits.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Thanks

- Shiba from zixisun02 at https://sketchfab.com/3d-models/shiba-faef9fe5ace445e7b2989d1c1ece361c
- Banana from ReCo_3D at https://sketchfab.com/3d-models/banana-ed0537c2201d4ad7b8ddb8368d844174
- Chef's knife from Davide Specchi at https://sketchfab.com/3d-models/chefs-knife-85ededce157545e68e51fa87768a08cb
- modifications:
- set origin to the middle of the blade + rotated so blade is facing Y down.
- inverted faces on a blade side to properly cut
Binary file added crates/wgsparkl3d/assets/shiba.glb
Binary file not shown.
152 changes: 152 additions & 0 deletions crates/wgsparkl3d/examples/banana3.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#![allow(unused)]

#[path = "libs/default_scene.rs"]
pub mod default_scene;
#[path = "libs/extract_mesh.rs"]
pub mod extract_mesh;
#[path = "libs/glb_to_point_cloud.rs"]
pub mod glb_to_point_cloud;

use glb_to_point_cloud::load_model_trimeshes;
use glb_to_point_cloud::load_model_with_colors;

use bevy::color::palettes::css;
use bevy::{prelude::*, render::renderer::RenderDevice, scene::SceneInstanceReady};
use nalgebra::vector;
use rapier3d::prelude::RigidBodyHandle;
use rapier3d::prelude::{ColliderBuilder, RigidBodyBuilder, SharedShape, TriMeshFlags};
use wgsparkl3d::pipeline::MpmData;
use wgsparkl3d::solver::SimulationParams;
use wgsparkl_testbed3d::CallBeforeSimulation;
use wgsparkl_testbed3d::RapierData;
use wgsparkl_testbed3d::{AppState, PhysicsContext};

pub fn demo(
mut commands: Commands,
device: Res<RenderDevice>,
mut app_state: ResMut<AppState>,
asset_server: Res<AssetServer>,
) {
let pc_grid = load_model_with_colors(
"assets/banana.glb",
Transform::from_scale(Vec3::splat(0.35))
.with_translation(Vec3::Y * 2.6f32)
.with_rotation(Quat::from_rotation_y(-90f32.to_radians())),
Some(css::BLANCHED_ALMOND.into()),
);
let params = SimulationParams {
gravity: vector![0.0, -9.81, 0.0] * app_state.gravity_factor,
dt: (1.0 / 60.0) / (app_state.num_substeps as f32),
};
let mut rapier_data = RapierData::default();
default_scene::set_default_app_state(app_state);
default_scene::spawn_ground_and_walls(&mut rapier_data);

let mut particles = vec![];
for (pos, color) in pc_grid {
let particle = default_scene::create_particle(&pos, Some(color));
particles.push(particle);
}
// Slicer
let mut slicer_trimeshes = load_model_trimeshes("assets/chefs_knife_open_blade.glb");
slicer_trimeshes.iter_mut().for_each(|trimesh| {
trimesh.0.iter_mut().for_each(|v| {
*v *= 10f32;
});
});

let rb = RigidBodyBuilder::kinematic_position_based().translation(vector![0.0, 5.0, 0.0]);
let parent_handle = rapier_data.bodies.insert(rb);
for (vertices, indices) in slicer_trimeshes.iter() {
// Insert collider into rapier state.

let collider = ColliderBuilder::new(
SharedShape::trimesh_with_flags(
vertices.clone(),
indices
.chunks_exact(3)
.map(|i| [i[0] as u32, i[1] as u32, i[2] as u32])
.collect(),
TriMeshFlags::FIX_INTERNAL_EDGES,
)
.unwrap(),
);
rapier_data
.colliders
.insert_with_parent(collider, parent_handle, &mut rapier_data.bodies);
}
commands.spawn((
Knife(parent_handle),
SceneRoot(
// This is a bit redundant with load_model_trimeshes, but it's a simple way to get the scene loaded visually.
asset_server.load(GltfAssetLabel::Scene(0).from_asset("chefs_knife_modified.glb")),
),
Transform::from_scale(Vec3::splat(10f32)).with_translation(Vec3::Y * 5f32),
));

let data = MpmData::new(
device.wgpu_device(),
params,
&particles,
&rapier_data.bodies,
&rapier_data.colliders,
1f32 / default_scene::SAMPLE_PER_UNIT,
60_000,
);

let physics = PhysicsContext {
data,
rapier_data,
particles,
};
commands.insert_resource(physics);
//
let system_id = commands.register_system(move_knife);
commands.spawn(CallBeforeSimulation(system_id));
}

#[derive(Debug, Component)]
pub struct Knife(pub RigidBodyHandle);

fn move_knife(
app_state: Res<AppState>,
mut knife: Query<(&mut Transform, &Knife)>,
mut physics: ResMut<PhysicsContext>,
) {
for (mut pos, knife) in knife.iter_mut() {
let t = app_state.physics_time_seconds as f32;

let body = physics.rapier_data.bodies.get_mut(knife.0).unwrap();
let length = 1.50;
let width = 0.5;
let x_pos = 3.0;
let y_pos = 1.5;
let z_pos = -1.5;
let velocity = 0.8;
let period = (2f32 * length + 3f32 * width) / velocity;

let i = (t / period).floor();
let dis = velocity * (t - period * i);

let new_pos = if dis < length {
vector![x_pos - width * i, y_pos - dis, z_pos]
} else if length <= dis && dis < length + width {
vector![x_pos - width * i + (dis - length), y_pos - length, z_pos]
} else if length + width <= dis && dis < 2f32 * length + width {
vector![
x_pos - width * i + width,
y_pos - (2.0 * length + width - dis),
z_pos
]
} else {
vector![
x_pos - width * i + width - (dis - 2.0 * length - width),
y_pos,
z_pos
]
};

body.set_translation(new_pos, true);
pos.translation = Vec3::new(new_pos[0], new_pos[1], new_pos[2]);
}
}
13 changes: 3 additions & 10 deletions crates/wgsparkl3d/examples/elastic_cut3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ use wgsparkl_testbed3d::{wgsparkl, RapierData};

use bevy::prelude::*;
use bevy::render::renderer::RenderDevice;
use nalgebra::{vector, DMatrix, Isometry3, Similarity3, Vector3};
use nalgebra::{vector, DMatrix, Isometry3};
use rapier3d::geometry::HeightField;
use rapier3d::prelude::{ColliderBuilder, RigidBodyBuilder};
use wgebra::GpuSim3;
use wgparry3d::parry::shape::Cuboid;
use wgrapier3d::dynamics::BodyDesc;
use wgsparkl::models::DruckerPrager;
use wgsparkl::{
models::ElasticCoefficients,
pipeline::MpmData,
solver::{Particle, ParticleDynamics, ParticlePhase, SimulationParams},
};
use wgsparkl_testbed3d::{init_testbed, AppState, PhysicsContext, SceneInits};

fn main() {
panic!("Run the `testbed3` example instead.");
}
use wgsparkl_testbed3d::{AppState, PhysicsContext};

pub fn elastic_cut_demo(
mut commands: Commands,
Expand Down Expand Up @@ -51,6 +43,7 @@ pub fn elastic_cut_demo(
phase: 1.0,
max_stretch: f32::MAX,
}),
color: None,
});
}
}
Expand Down
Loading