Skip to content

Commit

Permalink
Add filter to splines
Browse files Browse the repository at this point in the history
  • Loading branch information
TheGrimsey committed Nov 28, 2024
1 parent 4c66d34 commit 6724516
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 17 deletions.
36 changes: 26 additions & 10 deletions examples/many_tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ fn main() {
scaling: NoiseScaling::Normalized
}
},
filters: vec![NoiseFilter {
condition: NoiseFilterCondition::Above(0.4),
falloff: 0.1,
falloff_easing_function: EasingFunction::CubicInOut,
compare_to: FilterComparingTo::Spline { index: 0 },
}],
filters: vec![],
filter_combinator: FilterCombinator::Max
},
NoiseLayer {
Expand Down Expand Up @@ -131,7 +126,13 @@ fn main() {
filter_combinator: FilterCombinator::Max
},
],
..default()
filters: vec![NoiseFilter {
condition: NoiseFilterCondition::Above(0.4),
falloff: 0.1,
falloff_easing_function: EasingFunction::CubicInOut,
compare_to: FilterComparingTo::Spline { index: 0 },
}],
filter_combinator: FilterCombinator::Min
}
]
}),
Expand Down Expand Up @@ -174,13 +175,22 @@ fn insert_rules(
amplitude_curve: continentallness.clone(),
frequency: 0.001,
seed: 5,
domain_warp: vec![]
domain_warp: vec![],
filters: vec![],
filter_combinator: FilterCombinator::Min
},
TerrainNoiseSplineLayer {
amplitude_curve: peaks_and_valleys.clone(),
frequency: 0.005,
seed: 6,
domain_warp: vec![]
domain_warp: vec![],
filters: vec![NoiseFilter {
condition: NoiseFilterCondition::Above(0.3),
falloff: 0.2,
falloff_easing_function: EasingFunction::SmoothStep,
compare_to: FilterComparingTo::Spline { index: 0 }
}],
filter_combinator: FilterCombinator::Min
},
]);

Expand Down Expand Up @@ -412,13 +422,19 @@ impl EditorWindow for NoiseDebugWindow {
let noise = spline.sample(
translation.x,
translation.z,
noise_settings,
noise_cache,
&noise_index_cache.data_index_cache,
&noise_index_cache.spline_index_cache,
cached_noise,
lookup_curves,
);

let strength = calc_filter_strength(translation.xz(), &spline.filters, spline.filter_combinator, noise_settings, noise_cache, &noise_index_cache.data_index_cache, &noise_index_cache.spline_index_cache);

if let Some(lookup_curve) = lookup_curves.get(&spline.amplitude_curve) {
ui.label(format!(
"- {}: {noise:.3} ({noise_raw:.3})",
"- {}: {noise:.3} (Noise: {noise_raw:.3}, Strength: {strength:.3})",
lookup_curve.name.as_ref().map_or("", |name| name.as_str())
));
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bevy_asset::Assets;
use bevy_math::{FloatExt, IVec2, Vec2, Vec3, Vec3Swizzles};
use bevy_ecs::prelude::{any_with_component, resource_changed, AnyOf, Component, DetectChanges, Event, EventReader, EventWriter, Query, Res, ResMut, Resource, SystemSet, IntoSystemConfigs, ReflectResource};
use bevy_transform::prelude::{GlobalTransform, TransformSystem};
use bevy_log::{info, info_span};
use bevy_log::info_span;
use bevy_reflect::Reflect;
use bevy_derive::Deref;

Expand Down
3 changes: 2 additions & 1 deletion src/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ fn insert_texture_map(
) {
let resolution = texture_settings.resolution();
let texture_format = TextureFormat::Rgba8Unorm;
let size = texture_format.pixel_size() * resolution as usize * resolution as usize;

query.iter().for_each(|entity| {
let image = Image::new(
Expand All @@ -409,7 +410,7 @@ fn insert_texture_map(
depth_or_array_layers: 1,
},
TextureDimension::D2,
vec![0; texture_format.pixel_size() * resolution as usize * resolution as usize],
vec![0; size],
texture_format,
RenderAssetUsages::all(),
);
Expand Down
35 changes: 30 additions & 5 deletions src/noise.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ pub struct TerrainNoiseSplineLayer {
/// Seed for the noise function.
pub seed: u32,
/// Applies domain warping to the noise layer.
pub domain_warp: Vec<DomainWarping>
pub domain_warp: Vec<DomainWarping>,

pub filters: Vec<NoiseFilter>,
pub filter_combinator: FilterCombinator
}
impl TerrainNoiseSplineLayer {
/// Sample noise at the x & z coordinates WITHOUT amplitude curve.
Expand All @@ -140,12 +143,20 @@ impl TerrainNoiseSplineLayer {
&self,
x: f32,
z: f32,
noise_settings: &TerrainNoiseSettings,
noise_cache: &NoiseCache,
data_noise_cache: &[u32],
spline_noise_cache: &[u32],
noise: &Simplex,
lookup_curves: &Assets<LookupCurve>,
) -> f32 {
lookup_curves
.get(&self.amplitude_curve)
.map_or(0.0, |curve| curve.lookup(self.sample_raw(x, z, noise)))
if let Some(curve) = lookup_curves.get(&self.amplitude_curve) {
let strength = calc_filter_strength(Vec2::new(x, z), &self.filters, self.filter_combinator, noise_settings, noise_cache, data_noise_cache, spline_noise_cache);

curve.lookup(self.sample_raw(x, z, noise)) * strength
} else {
0.0
}
}

/// Sample the spline layer excluding the curve.
Expand All @@ -172,19 +183,25 @@ impl TerrainNoiseSplineLayer {
&self,
x: Vec4,
z: Vec4,
noise_settings: &TerrainNoiseSettings,
noise_cache: &NoiseCache,
data_noise_cache: &[u32],
spline_noise_cache: &[u32],
noise: &Simplex,
lookup_curves: &Assets<LookupCurve>,
) -> Vec4 {
// Fetch the lookup curve and apply it to all 4 noise values
if let Some(curve) = lookup_curves.get(&self.amplitude_curve) {
let strength = calc_filter_strength_simd(x, z, &self.filters, self.filter_combinator, noise_settings, noise_cache, data_noise_cache, spline_noise_cache);

let normalized_noise = self.sample_simd_raw(x, z, noise);

Vec4::new(
curve.lookup(normalized_noise.x),
curve.lookup(normalized_noise.y),
curve.lookup(normalized_noise.z),
curve.lookup(normalized_noise.w),
)
) * strength
} else {
Vec4::ZERO // Default to 0 if the curve isn't found
}
Expand Down Expand Up @@ -737,6 +754,10 @@ impl TerrainNoiseSettings {
acc + layer.sample(
pos.x,
pos.y,
self,
noise_cache,
&noise_index_cache.data_index_cache,
&noise_index_cache.spline_index_cache,
noise_cache.get_by_index(noise_index_cache.spline_index_cache[i] as usize),
lookup_curves,
)
Expand All @@ -763,6 +784,10 @@ impl TerrainNoiseSettings {
acc + layer.sample_simd(
x,
z,
self,
noise_cache,
&noise_index_cache.data_index_cache,
&noise_index_cache.spline_index_cache,
noise_cache.get_by_index(noise_index_cache.spline_index_cache[i] as usize),
lookup_curves,
)
Expand Down

0 comments on commit 6724516

Please sign in to comment.