Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ webgpu = ["bevy/webgpu"]

[dependencies]
anyhow = "1.0"
derive_more = { version = "1.0", features = ["debug"] }

bevy = { version = "0.17.0", default-features = false, features = [
"bevy_asset",
"bevy_render",
"bevy_pbr",
"bevy_log",
] }
bevy_easings = "0.17.1"

[dev-dependencies]
bevy = { version = "0.17.0", features = [
Expand Down
44 changes: 42 additions & 2 deletions src/water.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use std::time::Duration;

use bevy::light::{NotShadowCaster, NotShadowReceiver};
use bevy::prelude::*;
use bevy::mesh::*;
use bevy_easings::{Ease, EaseMethod, EasingsPlugin};
pub use bevy_easings::{EaseFunction, EasingType};
use derive_more::Debug;

pub mod material;
use material::*;

pub mod easings {
pub use bevy_easings::*;
}

pub const WATER_SIZE: u32 = 256;
pub const WATER_HALF_SIZE: f32 = WATER_SIZE as f32 / 2.0;
pub const WATER_GRID_SIZE: u32 = 6;
Expand All @@ -30,12 +39,20 @@ impl Into<u32> for WaterQuality {
}

#[derive(Resource, Clone, Debug, Reflect)]
#[reflect(Resource)]
#[reflect(Resource, Default)]
pub struct WaterSettings {
/// StandardMaterial setting.
pub alpha_mode: AlphaMode,
/// Base water height.
pub height: f32,
/// Easing method for `height` changes.
#[reflect(ignore)]
#[debug(skip)]
pub height_easing_method: EaseMethod,
/// Easing type for `height` changes.
#[reflect(ignore)]
#[debug(skip)]
pub height_easing_type: EasingType,
/// Wave amplitude.
pub amplitude: f32,
/// The `StandardMaterial` base_color field. This is the base color of the water.
Expand Down Expand Up @@ -70,6 +87,8 @@ impl Default for WaterSettings {
#[cfg(feature = "ssr")]
alpha_mode: AlphaMode::Opaque,
height: 1.0,
height_easing_method: EaseMethod::Linear,
height_easing_type: EasingType::Once { duration: Duration::from_secs(1) },
amplitude: 1.0,
clarity: 0.25,
base_color: Color::srgba(1.0, 1.0, 1.0, 1.0),
Expand Down Expand Up @@ -188,9 +207,30 @@ pub fn setup_water(
}

pub fn update_materials(
mut commands: Commands,
settings: Res<WaterSettings>,
mut materials: ResMut<Assets<StandardWaterMaterial>>,
water_transforms: Query<(Entity, &Transform), With<WaterTile>>,
) {
// Apply height easing to all water tiles if height has changed
for (entity, transform) in water_transforms.iter() {
if transform.translation.y != settings.height {
let target_transform = Transform::from_xyz(
transform.translation.x,
settings.height,
transform.translation.z,
);

commands.entity(entity).insert(
transform.ease_to(
target_transform,
settings.height_easing_method,
settings.height_easing_type,
)
);
}
}

if !settings.update_materials {
// Don't update water materials.
return;
Expand All @@ -216,7 +256,7 @@ impl Plugin for WaterPlugin {
app
.init_resource::<WaterSettings>()
.register_type::<WaterSettings>()
.add_plugins(WaterMaterialPlugin)
.add_plugins((WaterMaterialPlugin, EasingsPlugin::default()))
.add_systems(Startup, setup_water)
.add_systems(
Update,
Expand Down