#fluid #bevy #graphics #simulation #physics

bevy_eulerian_fluid

An eularian fluid simulation plugin for Bevy

1 unstable release

new 0.1.0 Jan 10, 2025

#337 in Game dev

Download history 55/week @ 2025-01-04

59 downloads per month

MIT/Apache

8MB
2K SLoC

Rust 1K SLoC // 0.0% comments WebGPU Shader Language 634 SLoC // 0.0% comments

bevy_eulerian_fluid

This project is a fluid simulation plugin for Bevy.

img

Try it on here!

Basic Usage

  1. Add FluidPlugin to the app.
  2. Spawn FluidSettings, then FluidSimulationBundle will be inserted automatically to the entity. By querying components bundled with FluidSimulationBundle such as VelocityTextures, 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
    

    img

  • Solid-to-fluid feedback

    cargo run --example demo
    

    img

  • Spawn multiple fluids

    cargo run --example multiple
    

    img

  • Fluid surface

    cargo run --example water_surface
    

    img

Acknowledgments

The simulation is inspired by and based on the algorithms described in these books:

License

Licensed under either of

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