Bevy Silk

workflow

MIT licensed unsafe forbidden Crates.io Docs.rs dependency status

CPU driven Cloth engine for Bevy using Verlet integration.

by FĂ©lix Lescaudey de Maneville

Get started

Dependency

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" }

Plugin

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(); } ```

Add cloth to a mesh

For a mesh to be used as cloth, add the ClothBuilder component to any entity with a Handle<Mesh> component.

Note: Transform and GlobalTransform 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 .with
fixedpoints(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() // ... ); } ```

Configuration

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.

Wind

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.

Mesh utils

bevy_silk provides a plane mesh generation function rectangle_mesh useful for classic cloth uses like flags or capes

Q&A

You probably didn't specify any fixed points, meaning there are no vertices anchored to your entity's GlobalTransform.

TODO list

Examples

Bevy Flag

  1. Flag example

run cargo run --example flag_example --features debug

  1. Balloon example

run cargo run --example balloon_example --features debug

  1. Moving example

run cargo run --example moving_example --features debug