1 unstable release
new 0.1.0 | Jan 10, 2025 |
---|
#337 in Game dev
59 downloads per month
8MB
2K
SLoC
bevy_eulerian_fluid
This project is a fluid simulation plugin for Bevy.
Try it on here!
Basic Usage
- Add
FluidPlugin
to the app. - Spawn
FluidSettings
, thenFluidSimulationBundle
will be inserted automatically to the entity. By querying components bundled withFluidSimulationBundle
such asVelocityTextures
, the simulation results can be retreived.
Here is a short example. See examples for the detailed implementation!
use bevy_eulerian_fluid::{
definition::{FluidSettings, LevelsetTextures, VelocityTextures},
FluidPlugin,
};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(FluidPlugin)
.add_systems(Startup, setup_scene)
.add_systems(Update, on_initialized)
.run();
}
fn setup_scene(mut commands: Commands) {
commands.spawn(Camera2dBundle::default());
commands.spawn(FluidSettings {
dx: 1.0f32,
dt: 0.5f32,
rho: 997f32, // water
gravity: Vec2::Y,
size: (512, 512),
initial_fluid_level: 0.9,
});
}
fn on_initialized(
mut commands: Commands,
query: Query<(Entity, &LevelsetTextures, &VelocityTextures), Added<LevelsetTextures>>,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<CustomMaterial>>,
mut velocity_materials: ResMut<Assets<VelocityMaterial>>,
) {
for (entity, levelset_textures, velocity_textures) in &query {
// Implement your own code to visualize the results.
}
}
Interact to the fluid
The simulation entity has LocalForces
component, which holds arrays of forces (in m/s^2) and position (in pixels). forces can be applied to the simulation domain by setting LocalForces
.
See also an interaction example for the detailed implementation.
Features
- Incompressible 2D fluid simulation
- Viscosity
- Fluid surface
- Basic implementation
- Fluid source/drain
- Solid body interaction
- One-way solid body to fluid interaction
- Two-way coupling with solid body and fluid
- Various shapes support
- Circle
Examples
There are some examples to demonstrate how to visualize and interact to the simulation results:
-
Imposing forces with mouse and touch input (Also available here)
cargo run --example interaction
-
Solid-to-fluid feedback
cargo run --example demo
-
Spawn multiple fluids
cargo run --example multiple
-
Fluid surface
cargo run --example water_surface
Acknowledgments
The simulation is inspired by and based on the algorithms described in these books:
- Fluid Simulation for Computer Graphics by Robert Bridson
- GPU Gems Chapter 38 by Mark J. Harris
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Dependencies
~23–33MB
~541K SLoC