-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
289 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#import bevy_fluid::fluid_uniform::SimulationUniform; | ||
|
||
@group(0) @binding(0) var<storage, read> force: array<vec2<f32>>; | ||
@group(0) @binding(1) var<storage, read> position: array<vec2<f32>>; | ||
@group(0) @binding(2) var u: texture_storage_2d<r32float, read_write>; | ||
@group(0) @binding(3) var v: texture_storage_2d<r32float, read_write>; | ||
|
||
@group(1) @binding(0) var<uniform> constants: SimulationUniform; | ||
|
||
@compute | ||
@workgroup_size(1, 64, 1) | ||
fn add_force( | ||
@builtin(global_invocation_id) invocation_id: vec3<u32>, | ||
) { | ||
let x_u = vec2<i32>(i32(invocation_id.x), i32(invocation_id.y)); | ||
let x_v = vec2<i32>(x_u.y, x_u.x); | ||
var n = arrayLength(&force); | ||
var net_force = vec2<f32>(0.0, 0.0); | ||
|
||
loop { | ||
if (n == 0) { | ||
break; | ||
} | ||
n = n - 1u; | ||
let f = force[n]; | ||
let p = position[n]; | ||
let force_u = f.x * gaussian_2d(vec2<f32>(x_u), p, 10.0); | ||
let force_v = f.y * gaussian_2d(vec2<f32>(x_v), p, 10.0); | ||
net_force = net_force + vec2<f32>(force_u, force_v); | ||
} | ||
|
||
let u_val = textureLoad(u, x_u).r; | ||
let v_val = textureLoad(v, x_v).r; | ||
textureStore(u, x_u, vec4<f32>(u_val + net_force.x * constants.dt, 0.0, 0.0, 0.0)); | ||
textureStore(v, x_v, vec4<f32>(v_val + net_force.y * constants.dt, 0.0, 0.0, 0.0)); | ||
} | ||
|
||
fn gaussian_2d(x: vec2<f32>, x0: vec2<f32>, sigma: f32) -> f32 { | ||
let b = -1.0 / (2.0 * sigma * sigma); | ||
return exp(b * dot(x - x0, x - x0)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
extern crate bevy_fluid; | ||
|
||
// use advection_plugin::AdvectionPlugin; | ||
use bevy::{ | ||
asset::AssetMetaCheck, | ||
input::mouse::MouseMotion, | ||
prelude::*, | ||
render::{ | ||
settings::{Backends, WgpuSettings}, | ||
RenderPlugin, | ||
}, | ||
sprite::MaterialMesh2dBundle, | ||
window::PrimaryWindow, | ||
}; | ||
|
||
use bevy_fluid::euler_fluid::{ | ||
add_force::AddForceMaterial, advection::AdvectionMaterial, fluid_material::VelocityMaterial, | ||
FluidPlugin, | ||
}; | ||
|
||
const WIDTH: f32 = 1280.0; | ||
const HEIGHT: f32 = 720.0; | ||
|
||
fn main() { | ||
let mut app = App::new(); | ||
// [workaround] Asset meta files cannot be found on browser. | ||
// see also: https://github.com/bevyengine/bevy/issues/10157 | ||
let meta_check = if cfg!(target_arch = "wasm32") { | ||
AssetMetaCheck::Never | ||
} else { | ||
AssetMetaCheck::Always | ||
}; | ||
|
||
app.add_plugins( | ||
DefaultPlugins | ||
.set(WindowPlugin { | ||
primary_window: Some(Window { | ||
resolution: (WIDTH, HEIGHT).into(), | ||
title: "bevy fluid".to_string(), | ||
..default() | ||
}), | ||
..default() | ||
}) | ||
.set(RenderPlugin { | ||
render_creation: bevy::render::settings::RenderCreation::Automatic(WgpuSettings { | ||
backends: Some(Backends::DX12 | Backends::BROWSER_WEBGPU), | ||
..default() | ||
}), | ||
..default() | ||
}) | ||
.set(AssetPlugin { | ||
meta_check, | ||
..default() | ||
}), | ||
) | ||
.add_plugins(FluidPlugin) | ||
.add_systems(Startup, setup_scene) | ||
.add_systems(Update, on_advection_initialized) | ||
.add_systems(Update, mouse_motion); | ||
|
||
app.run(); | ||
} | ||
|
||
fn setup_scene(mut commands: Commands) { | ||
commands | ||
.spawn(Camera2dBundle::default()) | ||
.insert(Name::new("Camera")); | ||
} | ||
|
||
fn on_advection_initialized( | ||
mut commands: Commands, | ||
advection: Option<Res<AdvectionMaterial>>, | ||
mut meshes: ResMut<Assets<Mesh>>, | ||
mut materials: ResMut<Assets<VelocityMaterial>>, | ||
) { | ||
if let Some(advection) = advection { | ||
if advection.is_changed() { | ||
info!("prepare velocity texture"); | ||
// spwan plane to visualize advection | ||
let mesh = meshes.add(Rectangle::default()); | ||
let material = materials.add(VelocityMaterial { | ||
offset: 0.5, | ||
scale: 0.1, | ||
u: Some(advection.u_in.clone()), | ||
v: Some(advection.v_in.clone()), | ||
}); | ||
|
||
commands.spawn(MaterialMesh2dBundle { | ||
mesh: mesh.into(), | ||
transform: Transform::default().with_scale(Vec3::splat(512.)), | ||
material, | ||
..default() | ||
}); | ||
} | ||
} | ||
} | ||
|
||
fn mouse_motion( | ||
mouse_button_input: Res<ButtonInput<MouseButton>>, | ||
mut mouse_motion: EventReader<MouseMotion>, | ||
mut force_material: ResMut<AddForceMaterial>, | ||
q_window: Query<&Window, With<PrimaryWindow>>, | ||
) { | ||
if mouse_button_input.pressed(MouseButton::Left) { | ||
if let Some(cursor_position) = q_window.single().cursor_position() { | ||
let force = mouse_motion | ||
.read() | ||
.map(|mouse| mouse.delta) | ||
.collect::<Vec<_>>(); | ||
let position = vec![cursor_position; force.len()]; | ||
force_material.force = force; | ||
force_material.position = position; | ||
|
||
return; | ||
} | ||
} | ||
|
||
force_material.force = vec![]; | ||
force_material.position = vec![]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use std::borrow::Cow; | ||
|
||
use bevy::{ | ||
prelude::*, | ||
render::{ | ||
extract_resource::ExtractResource, | ||
render_asset::RenderAssets, | ||
render_resource::{ | ||
binding_types::uniform_buffer, AsBindGroup, BindGroup, BindGroupLayout, | ||
BindGroupLayoutEntries, CachedComputePipelineId, ComputePipelineDescriptor, | ||
PipelineCache, ShaderStages, | ||
}, | ||
renderer::RenderDevice, | ||
texture::{FallbackImage, GpuImage}, | ||
}, | ||
}; | ||
|
||
use super::uniform::SimulationUniform; | ||
|
||
pub fn prepare_bind_group( | ||
mut commands: Commands, | ||
gpu_images: Res<RenderAssets<GpuImage>>, | ||
pipeline: Res<AddForcePipeline>, | ||
textures: Res<AddForceMaterial>, | ||
render_device: Res<RenderDevice>, | ||
fallback_image: Res<FallbackImage>, | ||
) { | ||
let bind_group = textures | ||
.as_bind_group( | ||
&pipeline.bind_group_layout, | ||
&render_device, | ||
&gpu_images, | ||
&fallback_image, | ||
) | ||
.unwrap() | ||
.bind_group; | ||
|
||
commands.insert_resource(AddForceBindGroup(bind_group)); | ||
} | ||
|
||
#[derive(Resource, Clone, ExtractResource, AsBindGroup)] | ||
pub struct AddForceMaterial { | ||
#[storage(0, read_only, visibility(compute))] | ||
pub force: Vec<Vec2>, | ||
#[storage(1, read_only, visibility(compute))] | ||
pub position: Vec<Vec2>, | ||
#[storage_texture(2, image_format = R32Float, access = ReadWrite)] | ||
pub u: Handle<Image>, | ||
#[storage_texture(3, image_format = R32Float, access = ReadWrite)] | ||
pub v: Handle<Image>, | ||
} | ||
|
||
#[derive(Resource)] | ||
pub struct AddForceBindGroup(pub BindGroup); | ||
|
||
#[derive(Resource)] | ||
pub struct AddForcePipeline { | ||
bind_group_layout: BindGroupLayout, | ||
pub pipeline: CachedComputePipelineId, | ||
} | ||
|
||
impl FromWorld for AddForcePipeline { | ||
fn from_world(world: &mut World) -> Self { | ||
let render_device = world.resource::<RenderDevice>(); | ||
let bind_group_layout = AddForceMaterial::bind_group_layout(render_device); | ||
let uniform_bind_group_layout = render_device.create_bind_group_layout( | ||
None, | ||
&BindGroupLayoutEntries::single( | ||
ShaderStages::COMPUTE, | ||
uniform_buffer::<SimulationUniform>(false), | ||
), | ||
); | ||
|
||
let shader = world | ||
.resource::<AssetServer>() | ||
.load("shaders/add_force.wgsl"); | ||
let pipeline_cache = world.resource::<PipelineCache>(); | ||
let pipeline = pipeline_cache.queue_compute_pipeline(ComputePipelineDescriptor { | ||
label: None, | ||
layout: vec![bind_group_layout.clone(), uniform_bind_group_layout.clone()], | ||
push_constant_ranges: Vec::new(), | ||
shader: shader.clone(), | ||
shader_defs: vec![], | ||
entry_point: Cow::from("add_force"), | ||
}); | ||
|
||
Self { | ||
bind_group_layout, | ||
pipeline, | ||
} | ||
} | ||
} |