From 5d9202337efe3e4f1399fb1c748b079c4dfb1521 Mon Sep 17 00:00:00 2001 From: Syniis Date: Sun, 18 Feb 2024 23:58:37 +0100 Subject: [PATCH] Some performance improvements --- world/src/layer/cave.rs | 35 +++++++++++++++++++++++------------ world/src/util/fast_noise.rs | 6 +++--- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/world/src/layer/cave.rs b/world/src/layer/cave.rs index 3b0d9b11c1..796035c2aa 100644 --- a/world/src/layer/cave.rs +++ b/world/src/layer/cave.rs @@ -1,7 +1,7 @@ use super::scatter::close; use crate::{ - util::{sampler::Sampler, FastNoise, RandomField, RandomPerm, StructureGen2d, LOCALITY}, + util::{sampler::Sampler, FastNoise2d, RandomField, RandomPerm, StructureGen2d, LOCALITY}, Canvas, CanvasInfo, ColumnSample, Land, }; use common::{ @@ -774,11 +774,14 @@ fn write_column( let wposf = wpos.map(|e| e as f64); let warp_freq = 1.0 / 32.0; let warp_amp = Vec3::new(12.0, 12.0, 12.0); + let xy = wposf.xy(); + let xz = Vec2::new(wposf.x, wposf.z); + let yz = Vec2::new(wposf.y, wposf.z); let wposf_warped = wposf.map(|e| e as f32) + Vec3::new( - FastNoise::new(seed).get(wposf * warp_freq), - FastNoise::new(seed + 1).get(wposf * warp_freq), - FastNoise::new(seed + 2).get(wposf * warp_freq), + FastNoise2d::new(seed).get(yz * warp_freq), + FastNoise2d::new(seed).get(xz * warp_freq), + FastNoise2d::new(seed).get(xy * warp_freq), ) * warp_amp * (wposf.z as f32 - mushroom.pos.z as f32) .mul(0.1) @@ -809,7 +812,7 @@ fn write_column( { if head_dist < 0.85 { let radial = (rpos.x.atan2(rpos.y) * 10.0).sin() * 0.5 + 0.5; - let block_kind = if head_dist < 0.5 { + let block_kind = if dynamic_rng.gen_bool(0.1) { BlockKind::GlowingMushroom } else { BlockKind::Rock @@ -979,11 +982,14 @@ fn write_column( let wposf = wpos.map(|e| e as f64); let warp_freq = 1.0 / 32.0; let warp_amp = Vec3::new(20.0, 20.0, 20.0); + let xy = wposf.xy(); + let xz = Vec2::new(wposf.x, wposf.z); + let yz = Vec2::new(wposf.y, wposf.z); let wposf_warped = wposf.map(|e| e as f32) + Vec3::new( - FastNoise::new(seed).get(wposf * warp_freq), - FastNoise::new(seed + 1).get(wposf * warp_freq), - FastNoise::new(seed + 2).get(wposf * warp_freq), + FastNoise2d::new(seed).get(yz * warp_freq), + FastNoise2d::new(seed).get(xz * warp_freq), + FastNoise2d::new(seed).get(xy * warp_freq), ) * warp_amp; let rpos = wposf_warped - pos.map(|e| e as f32); let dist_sq = rpos.xy().magnitude_squared(); @@ -1086,9 +1092,10 @@ fn write_column( biome.icy, ); Block::new( - if rand.chance(wpos, (biome.mushroom * biome.mineral * 0.5).max(biome.icy)) { - // BlockKind::GlowingWeakRock - BlockKind::WeakRock + if rand.chance(wpos, biome.mushroom * 0.01) { + BlockKind::GlowingWeakRock + } else if rand.chance(wpos, biome.icy) { + BlockKind::GlowingWeakRock } else if rand.chance(wpos, biome.sandy) { BlockKind::Sand } else { @@ -1164,7 +1171,11 @@ fn write_column( } else if biome.fire.max(biome.snowy) > 0.5 { BlockKind::Rock } else if biome.crystal > 0.5 { - BlockKind::GlowingRock + if rand.chance(wpos, biome.crystal * 0.05) { + BlockKind::GlowingRock + } else { + BlockKind::Rock + } } else { BlockKind::Sand }, diff --git a/world/src/util/fast_noise.rs b/world/src/util/fast_noise.rs index 84ac0dc87a..8e4b3fcc8a 100644 --- a/world/src/util/fast_noise.rs +++ b/world/src/util/fast_noise.rs @@ -110,9 +110,9 @@ impl Sampler<'static> for FastNoise2d { f.powi(2) * (3.0 - 2.0 * f) }); - let v0 = v00 + factor.y * (v10 - v00); - let v1 = v01 + factor.y * (v11 - v01); + let v0 = v00 + factor.x * (v10 - v00); + let v1 = v01 + factor.x * (v11 - v01); - (v0 + factor.x * (v1 - v0)) * 2.0 - 1.0 + (v0 + factor.y * (v1 - v0)) * 2.0 - 1.0 } }