Skip to content

Commit

Permalink
Rework texturing evaluators/condition to be a Vec & add InBiome check.
Browse files Browse the repository at this point in the history
Need to add one working directly from noise to allow detailing from noise. Cracks & lines, etc.
  • Loading branch information
TheGrimsey committed Dec 4, 2024
1 parent de6bb68 commit dabb20a
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 107 deletions.
2 changes: 1 addition & 1 deletion benches/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn criterion_benchmark(c: &mut Criterion) {
height: 10.0,
falloff: 1.0,
}
.eval_simd(Vec4::splat(9.5), Vec4::splat(0.0))
.eval_simd(Vec4::splat(9.5), Vec4::splat(0.0), &[])
}))
});
}
Expand Down
12 changes: 7 additions & 5 deletions examples/basic_setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use bevy_world_seed::{
ModifierStrengthLimitProperty, ModifierTileAabb, ShapeModifier, ShapeModifierBundle,
TerrainSplineBundle, TerrainSplineCached, TerrainSplineProperties, TerrainSplineShape,
},
noise::{LayerNoiseSettings, LayerOperation, NoiseGroup, NoiseLayer, NoiseScaling, TerrainNoiseSettings},
noise::{LayerNoiseSettings, LayerOperation, NoiseGroup, NoiseLayer, NoiseScaling, StrengthCombinator, TerrainNoiseSettings},
snap_to_terrain::SnapToTerrain,
terrain::Terrain,
TerrainPlugin, TerrainSettings,
Expand Down Expand Up @@ -82,20 +82,22 @@ fn insert_texturing_rules(
asset_server: Res<AssetServer>,
) {
texturing_rules.rules.push(TexturingRule {
evaluator: TexturingRuleEvaluator::AngleGreaterThan {
evaluators: vec![TexturingRuleEvaluator::AngleGreaterThan {
angle_radians: 30.0_f32.to_radians(),
falloff_radians: 2.5_f32.to_radians(),
},
}],
evaulator_combinator: StrengthCombinator::Min,
texture: asset_server.load("textures/cracked_concrete_diff_1k.dds"),
normal_texture: Some(asset_server.load("textures/cracked_concrete_nor_gl_1k.dds")),
units_per_texture: 4.0,
});

texturing_rules.rules.push(TexturingRule {
evaluator: TexturingRuleEvaluator::AngleLessThan {
evaluators: vec![TexturingRuleEvaluator::AngleLessThan {
angle_radians: 30.0_f32.to_radians(),
falloff_radians: 2.5_f32.to_radians(),
},
}],
evaulator_combinator: StrengthCombinator::Min,
texture: asset_server.load("textures/brown_mud_leaves.dds"),
normal_texture: Some(asset_server.load("textures/brown_mud_leaves_01_nor_gl_2k.dds")),
units_per_texture: 4.0,
Expand Down
66 changes: 47 additions & 19 deletions examples/many_tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use bevy_world_seed::{
},
meshing::TerrainMeshRebuildQueue,
noise::{
calc_filter_strength, DomainWarping, FilterCombinator, FilterComparingTo, LayerNoiseSettings, LayerOperation, NoiseCache, NoiseFilter, NoiseFilterCondition, NoiseGroup, NoiseIndexCache, NoiseLayer, NoiseScaling, TerrainNoiseSettings, TerrainNoiseSplineLayer
calc_filter_strength, BiomeSettings, DomainWarping, StrengthCombinator, FilterComparingTo, LayerNoiseSettings, LayerOperation, NoiseCache, NoiseFilter, NoiseFilterCondition, NoiseGroup, NoiseIndexCache, NoiseLayer, NoiseScaling, TerrainNoiseSettings, TerrainNoiseSplineLayer
},
terrain::{Terrain, TileToTerrain},
RebuildTile, TerrainHeightRebuildQueue, TerrainPlugin, TerrainSettings,
Expand Down Expand Up @@ -77,7 +77,7 @@ fn main() {
falloff_easing_function: EasingFunction::SmoothStep,
compare_to: FilterComparingTo::Spline { index: 0 },
}],
filter_combinator: FilterCombinator::Max
filter_combinator: StrengthCombinator::Max
},
NoiseLayer {
operation: LayerOperation::Noise {
Expand All @@ -90,7 +90,7 @@ fn main() {
},
},
filters: vec![],
filter_combinator: FilterCombinator::Max
filter_combinator: StrengthCombinator::Max
},
NoiseLayer {
operation: LayerOperation::Noise {
Expand All @@ -103,7 +103,7 @@ fn main() {
}
},
filters: vec![],
filter_combinator: FilterCombinator::Max
filter_combinator: StrengthCombinator::Max
},
NoiseLayer {
operation: LayerOperation::Noise {
Expand All @@ -116,7 +116,7 @@ fn main() {
}
},
filters: vec![],
filter_combinator: FilterCombinator::Max
filter_combinator: StrengthCombinator::Max
},
NoiseLayer {
operation: LayerOperation::Noise {
Expand All @@ -129,7 +129,7 @@ fn main() {
},
},
filters: vec![],
filter_combinator: FilterCombinator::Max
filter_combinator: StrengthCombinator::Max
},
NoiseLayer {
operation: LayerOperation::Noise {
Expand All @@ -147,7 +147,7 @@ fn main() {
falloff_easing_function: EasingFunction::SmoothStep,
compare_to: FilterComparingTo::Spline { index: 0 },
}],
filter_combinator: FilterCombinator::Max
filter_combinator: StrengthCombinator::Max
},
NoiseLayer {
operation: LayerOperation::Noise {
Expand All @@ -160,7 +160,7 @@ fn main() {
},
},
filters: vec![],
filter_combinator: FilterCombinator::Max
filter_combinator: StrengthCombinator::Max
},
],
filters: vec![NoiseFilter {
Expand All @@ -169,7 +169,7 @@ fn main() {
falloff_easing_function: EasingFunction::SmoothStep,
compare_to: FilterComparingTo::Spline { index: 0 },
}],
filter_combinator: FilterCombinator::Min
filter_combinator: StrengthCombinator::Min
}
],
data: vec![
Expand All @@ -195,6 +195,29 @@ fn main() {
scaling: NoiseScaling::Unitized
},
],
biome: vec![
BiomeSettings {
filters: vec![NoiseFilter {
condition: NoiseFilterCondition::Above(0.5),
falloff: 0.25,
falloff_easing_function: EasingFunction::SmoothStep,
compare_to: FilterComparingTo::Data { index: 0 }
},
NoiseFilter {
condition: NoiseFilterCondition::Above(0.5),
falloff: 0.25,
falloff_easing_function: EasingFunction::SmoothStep,
compare_to: FilterComparingTo::Data { index: 1 }
},
NoiseFilter {
condition: NoiseFilterCondition::Above(0.5),
falloff: 0.25,
falloff_easing_function: EasingFunction::SmoothStep,
compare_to: FilterComparingTo::Data { index: 2 }
}],
filter_combinator: StrengthCombinator::Min
}
],
..default()
}),
terrain_settings: TerrainSettings {
Expand Down Expand Up @@ -238,7 +261,7 @@ fn insert_rules(
seed: 5,
domain_warp: vec![],
filters: vec![],
filter_combinator: FilterCombinator::Min
filter_combinator: StrengthCombinator::Min
},
TerrainNoiseSplineLayer {
amplitude_curve: peaks_and_valleys.clone(),
Expand All @@ -265,7 +288,7 @@ fn insert_rules(
compare_to: FilterComparingTo::Data { index: 0 }
}
],
filter_combinator: FilterCombinator::Min
filter_combinator: StrengthCombinator::Min
},
]);

Expand All @@ -290,19 +313,21 @@ fn insert_rules(

texturing_rules.rules.extend([
TexturingRule {
evaluator: TexturingRuleEvaluator::AngleGreaterThan {
evaluators: vec![TexturingRuleEvaluator::AngleGreaterThan {
angle_radians: 40.0_f32.to_radians(),
falloff_radians: 2.5_f32.to_radians(),
},
}],
evaulator_combinator: StrengthCombinator::Min,
texture: asset_server.load("textures/cracked_concrete_diff_1k.dds"),
normal_texture: Some(asset_server.load("textures/cracked_concrete_nor_gl_1k.dds")),
units_per_texture: 4.0,
},
TexturingRule {
evaluator: TexturingRuleEvaluator::AngleLessThan {
evaluators: vec![TexturingRuleEvaluator::AngleLessThan {
angle_radians: 40.0_f32.to_radians(),
falloff_radians: 2.5_f32.to_radians(),
},
}],
evaulator_combinator: StrengthCombinator::Min,
texture: asset_server.load("textures/brown_mud_leaves.dds"),
normal_texture: Some(asset_server.load("textures/brown_mud_leaves_01_nor_gl_2k.dds")),
units_per_texture: 4.0,
Expand Down Expand Up @@ -478,7 +503,8 @@ impl EditorWindow for NoiseDebugWindow {
noise_index_cache,
translation.xz(),
lookup_curves,
&data
&data,
&[]
);
ui.heading(format!("Height: {height}"));

Expand All @@ -504,12 +530,13 @@ impl EditorWindow for NoiseDebugWindow {
noise_settings,
noise_cache,
&data,
&[],
&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, &data, &noise_index_cache.spline_index_cache);
let strength = calc_filter_strength(translation.xz(), &spline.filters, spline.filter_combinator, noise_settings, noise_cache, &data, &[], &noise_index_cache.spline_index_cache);

if let Some(lookup_curve) = lookup_curves.get(&spline.amplitude_curve) {
ui.label(format!(
Expand All @@ -528,17 +555,18 @@ impl EditorWindow for NoiseDebugWindow {
noise_settings,
noise_cache,
&data,
&[],
&noise_index_cache.spline_index_cache,
group_noises,
translation.xz()
);

let strength = calc_filter_strength(translation.xz(), &group.filters, group.filter_combinator, noise_settings, noise_cache, &data, &noise_index_cache.spline_index_cache);
let strength = calc_filter_strength(translation.xz(), &group.filters, group.filter_combinator, noise_settings, noise_cache, &data, &[], &noise_index_cache.spline_index_cache);

egui::CollapsingHeader::new(format!("GROUP {i}: {noise:.3} ({strength:.3})")).id_source(format!("group_{i}")).show(ui, |ui| {
unsafe {
for (i,layer) in group.layers.iter().enumerate() {
let strength = calc_filter_strength(translation.xz(), &layer.filters, layer.filter_combinator, noise_settings, noise_cache, &data, &noise_index_cache.spline_index_cache);
let strength = calc_filter_strength(translation.xz(), &layer.filters, layer.filter_combinator, noise_settings, noise_cache, &data, &[], &noise_index_cache.spline_index_cache);

match &layer.operation {
LayerOperation::Noise { noise } => {
Expand Down
17 changes: 10 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use modifiers::{
ShapeModifier, TerrainSplineCached, TerrainSplineProperties, TerrainSplineShape,
TileToModifierMapping,
};
use noise::{apply_noise_simd, LayerNoiseSettings, NoiseCache, NoiseIndexCache, TerrainNoiseSettings};
use noise::{apply_noise_simd, LayerNoiseSettings, NoiseCache, NoiseIndexCache, TerrainNoiseSettings, TileBiomes};
use snap_to_terrain::TerrainSnapToTerrainPlugin;
use terrain::{insert_components, update_tiling, Holes, Terrain, TileToTerrain};
use utils::{distance_squared_to_line_segment, index_to_x_z};
Expand Down Expand Up @@ -127,7 +127,9 @@ impl Plugin for TerrainPlugin {
app.init_resource::<TileToModifierMapping>()
.init_resource::<TileToTerrain>()
.init_resource::<NoiseCache>()
.init_resource::<NoiseIndexCache>()
.init_resource::<NoiseIndexCache>();

app
.register_type::<TerrainSplineShape>()
.register_type::<TerrainSplineCached>()
.register_type::<ModifierTileAabb>()
Expand All @@ -147,7 +149,8 @@ impl Plugin for TerrainPlugin {

{
app.register_type::<LayerNoiseSettings>()
.register_type::<TerrainNoiseSettings>();
.register_type::<TerrainNoiseSettings>()
.register_type::<TileBiomes>();
}

app.init_resource::<TerrainHeightRebuildQueue>();
Expand Down Expand Up @@ -234,7 +237,7 @@ fn update_terrain_heights(
Option<&ModifierFalloffProperty>,
Option<&ModifierStrengthLimitProperty>,
)>,
mut heights: Query<(&mut Heights, &mut Holes)>,
mut terrain_tile_query: Query<(&mut Heights, &mut Holes, &mut TileBiomes)>,
terrain_settings: Res<TerrainSettings>,
tile_to_modifier: Res<TileToModifierMapping>,
tile_to_terrain: Res<TileToTerrain>,
Expand Down Expand Up @@ -290,16 +293,16 @@ fn update_terrain_heights(
let shape_modifiers = tile_to_modifier.shape.get(&tile);
let splines = tile_to_modifier.splines.get(&tile);

let mut iter = heights.iter_many_mut(tiles.iter());
while let Some((mut heights, mut holes)) = iter.fetch_next() {
let mut iter = terrain_tile_query.iter_many_mut(tiles.iter());
while let Some((mut heights, mut holes, mut biomes)) = iter.fetch_next() {
// Clear heights.
heights.0.fill(0.0);
holes.0.clear();

// First, set by noise.
if let Some(terrain_noise_layers) = terrain_noise_settings.as_ref() {
let _span = info_span!("Apply noise").entered();
apply_noise_simd(
*biomes = apply_noise_simd(
&mut heights.0,
&terrain_settings,
terrain_translation,
Expand Down
Loading

0 comments on commit dabb20a

Please sign in to comment.