1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//! CPU driven Cloth engine for Bevy using Verlet integration.
//!
//! ## Get started
//!
//! ### Dependency
//!
//! Add `bevy_silk` as a dependency in the `Cargo.toml`
//!
//! `bevy_silk = "0.10"`
//!
//! Or follow the main git branch
//!
//! `bevy_silk = { git = "https://github.com/ManevilleF/bevy_silk" }`
//!
//! ### Supported Bevy Versions
//!
//! | `bevy_silk` | `bevy` |
//! |-------------|--------|
//! | 0.1.0 | 0.7 |
//! | 0.2.0 | 0.7 |
//! | 0.3.0 | 0.8 |
//! | 0.4.0 | 0.9 |
//! | 0.5.0 | 0.10 |
//! | 0.6.0 | 0.11 |
//! | 0.7.0 | 0.12 |
//! | 0.8.0 | 0.13 |
//! | 0.9.0 | 0.14 |
//! | 0.10.0 | 0.17 |
//!
//! ### Plugin
//!
//! Add the `ClothPlugin` to your bevy app
//!
//! ```rust no_run
//! use bevy::prelude::*;
//! use bevy_silk::prelude::*;
//!
//! fn main() {
//! App::new()
//! .add_plugins((DefaultPlugins, 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
//! > Note: The mesh render asset usage must allow for _main world_ edits
//!
//! 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.spawn((
//! // Add your mesh, material and your custom PBR data
//! // Mesh3d(...),
//! // MeshMaterial3d(...),
//! ClothBuilder::new()
//! // Define pinned vertices ids using an Iterator
//! .with_pinned_vertex_ids(0..9)
//! // Define the stick generation mode
//! .with_stick_generation(StickGeneration::Quads)
//! // Defines the sticks target length option
//! .with_stick_length(StickLen::Auto)
//! // The cloth will compute flat mesh normals
//! .with_flat_normals(),
//! ));
//! }
//! ```
//!
//! #### Vertex anchoring
//!
//! Specifying vertex anchors allows to pin some cloth vertices to various
//! entities. The `ClothBuilder` has multiple methods allowing to anchor
//! vertices through their id or color.
//!
//! For example you can pin some cloth vertices to the cloth entity's
//! `GlobalTransform`:
//!
//! ```rust
//! use bevy::color::{palettes::css::*, Color};
//! use bevy_silk::prelude::*;
//!
//! let cloth = ClothBuilder::new()
//! // Adds pinned vertices ids using an Iterator
//! .with_pinned_vertex_ids(0..9)
//! // Adds a single pinned vertex id
//! .with_pinned_vertex_id(10)
//! // Adds pinned vertex colors using an Iterator
//! .with_pinned_vertex_colors([Color::from(WHITE), Color::from(BLACK)].into_iter())
//! // Adds a single pinned vertex color
//! .with_pinned_vertex_color(Color::from(YELLOW))
//! // Adds pinned vertex positions
//! .with_pinned_vertex_positions(|pos| pos.x > 0.0 && pos.z <= 5.0);
//! ```
//!
//! For more anchoring options, for example to specify a custom entity to pin
//! the vertices to:
//!
//! ```rust
//! use bevy::{color::palettes::css::*, prelude::*};
//! use bevy_silk::prelude::*;
//!
//! fn spawn(mut commands: Commands) {
//! // Spawn an entity and get its id
//! let entity_a = commands
//! .spawn((
//! // Add your components
//! // ...
//! ))
//! .id();
//! let anchor_to_a = VertexAnchor {
//! // The anchor will pin the vertices to `entity_a`
//! custom_target: Some(entity_a),
//! // Specify an extra offset from the target's `GlobalTransform`
//! custom_offset: Some(Vec3::new(1.0, 1.2, 0.0)),
//! ..Default::default()
//! };
//! let anchor_to_self = VertexAnchor {
//! // The anchor will pin the cloth entity
//! custom_target: None,
//! // Specify an extra offset from the target's `GlobalTransform`
//! custom_offset: Some(Vec3::new(-1.0, 0.0, -0.1)),
//! ..Default::default()
//! };
//!
//! let cloth = ClothBuilder::new()
//! // Adds pinned vertices ids using an Iterator
//! .with_anchored_vertex_ids(0..9, anchor_to_a)
//! // Adds a single pinned vertex id
//! .with_anchored_vertex_id(10, anchor_to_self)
//! // Adds pinned vertex colors using an Iterator
//! .with_anchored_vertex_colors(
//! [Color::from(WHITE), Color::from(BLACK)].into_iter(),
//! anchor_to_a,
//! )
//! // Adds a single pinned vertex color
//! .with_anchored_vertex_color(Color::from(YELLOW), anchor_to_self)
//! // Adds pinned vertex positions
//! .with_anchored_vertex_positions(|pos| pos.x > 0.0 && pos.z <= 5.0, anchor_to_self);
//! }
//! ```
//!
//! Custom anchoring allows to :
//!
//! * pin vertices to various entities, like skeletal mesh joints
//! * define custom offsets to customize the distance between the anchored
//! vertices an the target
//! * use world space pinning and ignore the target's rotation for example
//! * override the vertex positions, using only the offset
//!
//! ### Configuration
//!
//! You can customize the global cloth physics by inserting the `ClothConfig`
//! resource to your app:
//!
//! ```rust no_run
//! use bevy::prelude::*;
//! use bevy_silk::prelude::*;
//!
//! fn main() {
//! App::new()
//! .add_plugins((DefaultPlugins, ClothPlugin))
//! .insert_resource(ClothConfig {
//! gravity: Vec3::new(0.0, -9.81, 0.0),
//! friction: 0.02,
//! sticks_computation_depth: 5,
//! acceleration_smoothing: AccelerationSmoothing::default()
//! })
//! // ... 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 sin wave following wind intensity with custom force
//! and frequency.
//!
//! `Wind` forces can be added as a resource to your app through the `Winds`
//! container:
//!
//! ```rust no_run
//! use bevy::prelude::*;
//! use bevy_silk::prelude::*;
//!
//! fn main() {
//! App::new()
//! .add_plugins((DefaultPlugins, ClothPlugin))
//! .insert_resource(Winds {
//! wind_forces: vec![Wind::SinWave {
//! max_velocity: Vec3::new(10.0, 15.0, -5.0),
//! frequency: 3.0,
//! normalize: false,
//! abs: false
//! }]
//! })
//! // ... Add your resources and systems
//! .run();
//! }
//! ```
//!
//! > Check the flag example for simple wind effect.
//!
//! ## Collisions
//!
//! Both [`bevy_rapier`] and [`avian`] are supported for cloth interactions
//! with colliders. They can be enabled with the `rapier_collisions` and
//! `avian_collisions` features respectively.
//!
//! > Note: Collision support is still experimental for now and is not suited
//! > for production use. Feedback is welcome!
//!
//! ### `bevy_rapier`
//!
//! Add `bevy_rapier3d::RapierPhysicsPlugin` to your app and a `ClothCollider`
//! to your entity to enable collisions:
//!
//! ```rust
//! use bevy::prelude::*;
//! use bevy_silk::prelude::*;
//!
//! fn spawn(mut commands: Commands) {
//! commands.spawn((
//! // Add your mesh, material and your custom PBR data
//! // Mesh3d(...),
//! // MeshMaterial3d(...),
//! ClothBuilder::new(),
//! ClothCollider::default(),
//! ));
//! }
//! ```
//!
//! Three `bevy_rapier` components will be automatically inserted:
//!
//! * a `RigidBody::KinematicPositionBased`
//! * a `Collider` which will be updated every frame to follow the cloth bounds
//! (AABB)
//! * a `SolverGroup` set to 0 (`Group::NONE`) in everything, avoiding default
//! collision solving.
//!
//! You can customize what collisions will be checked by specifying
//! `CollisionGroups`. (See the [`bevy_rapier` docs](https://rapier.rs/docs/user_guides/bevy_plugin/colliders#collision-groups-and-solver-groups)).
//!
//! ### `avian` (previously `bevy_xpbd`)
//!
//! Add `avian3d::PhysicsPlugins` to your app and a `ClothCollider`
//! to your entity to enable collisions:
//!
//! ```rust
//! use bevy::prelude::*;
//! use bevy_silk::prelude::*;
//!
//! fn spawn(mut commands: Commands) {
//! commands.spawn((
//! // Add your mesh, material and your custom PBR data
//! // Mesh3d(...),
//! // MeshMaterial3d(...),
//! ClothBuilder::new(),
//! ClothCollider::default(),
//! ));
//! }
//! ```
//!
//! Three `avian3d` components will be automatically inserted:
//!
//! * a `RigidBody::Kinematic`
//! * a `Collider` which will be updated every frame to follow the cloth bounds
//! (AABB)
//! * a `Sensor` used for avoiding default collision solving.
//!
//! You can customize what collisions will be checked by specifying
//! `CollisionLayers`. (See the [`avian` docs](https://docs.rs/avian3d/latest/avian3d/collision/struct.CollisionLayers.html)).
//!
//! ## Mesh utils
//!
//! `bevy_silk` provides a plane mesh generation function `rectangle_mesh`
//! useful for classic cloth uses like flags or capes
//!
//! ## Q&A
//!
//! * `My mesh falls immediately and infinitely when I add a Cloth component,
//! how to fix it?`
//!
//! You probably didn't specify any *pinned points*, meaning there are no
//! vertices anchored to your entity's `GlobalTransform`.
//!
//! * `My cloth jitters a lot/ suddenly falls down/ has strange sudden
//! behaviour`
//!
//! Gravity and winds are by default smoothed out by the framerate, if the
//! framerate drops suddenly gravity and wind get much stronger.
//! If your simulation suffers from this you can specify a custom smooth
//! value in `ClothConfig::acceleration_smoothing`.
//!
//! [`bevy_rapier`]: https://github.com/dimforge/bevy_rapier
//! [`avian`]: https://github.com/Jondolf/avian
// TODO: remove when https://github.com/rust-lang/rust-clippy/issues/13058 is closed
/// components module
/// config module
/// error module
/// mesh module
/// stick module
/// systems module
/// vertex anchor module
/// wind module
use crate*;
use *;
/// Prelude module, providing every public type of the lib
/// Plugin for cloth physics
;