CPU driven Cloth engine for Bevy using Verlet integration.
by FĂ©lix Lescaudey de Maneville
Add bevy_silk
as a dependency in the Cargo.toml
bevy_silk = "0.1"
Or follow the main git branch
bevy_silk = { git = "https://github.com/ManevilleF/bevy_silk" }
Add the ClothPlugin
to your bevy app
```rust norun use bevy::prelude::*; use bevysilk::prelude::*;
fn main() { App::new() .addplugins(DefaultPlugins) .addplugin(ClothPlugin) // ... Add your resources and systems .run(); } ```
For a mesh to be used as cloth, add the ClothBuilder
component to any entity with a Handle<Mesh>
component.
Note:
Transform
andGlobalTransform
are also required
cloth data which will be populated automatically from the associated Handle<Mesh>
.
```rust use bevy::prelude::; use bevy_silk::prelude::;
fn spawn(mut commands: Commands) {
commands.spawnbundle(PbrBundle {
// Add your mesh, material and your custom PBR data
..Default::default()
}).insert(ClothBuilder::new()
// Define fixed vertices using an Iterator
.withfixedpoints(0..9)
// Define the stick generation mode
.withstickgeneration(StickGeneration::Quads)
// Defines the sticks target length option
.withsticklength(StickLen::Auto)
// The cloth will compute flat mesh normals
.withflatnormalcomputation()
// ...
);
}
```
You can customize the global cloth physics by inserting the ClothConfig
resource to your app:
```rust norun use bevy::prelude::*; use bevysilk::prelude::*;
fn main() { App::new() .addplugins(DefaultPlugins) .insertresource(ClothConfig { gravity: Vec3::new(0.0, -9.81, 0.0), friction: 0.02, stickscomputationdepth: 5 }) .add_plugin(ClothPlugin) // ... Add your resources and systems .run(); } ```
ClothConfig
can also be used as a component to override the global configuration.
You may add wind forces to the simulation for a more dynamic clothing effect, for each force you may choose from:
- Wind::Constant
for constant wind force
- Wind::SinWave
for a sinwave following wind intensity with custom force and frequency.
Wind
forces can be added as a resource to your app through the Winds
container:
```rust norun use bevy::prelude::*; use bevysilk::prelude::*;
fn main() { App::new() .addplugins(DefaultPlugins) .insertresource(Winds { windforces: vec![Wind::SinWave { maxvelocity: Vec3::new(10.0, 15.0, -5.0), frequency: 3.0, normalize: false, abs: false }] }) .add_plugin(ClothPlugin) // ... Add your resources and systems .run(); } ```
Check the flag example for simple wind effect.
bevy_silk
provides a plane mesh generation function rectangle_mesh
useful for classic cloth uses like flags or capes
My mesh falls immediately and infinitely when I add a Cloth component, how to fix it?
You probably didn't specify any fixed points, meaning there are no vertices anchored to your entity's GlobalTransform
.
run cargo run --example flag_example --features debug
run cargo run --example balloon_example --features debug
run cargo run --example moving_example --features debug