proc-relief: generateur procedural multi-echelle (bruit, ridges, warp, terrasses, dolines, vallees, erosion par tuile 512 m avec marge et raccord)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cette révision appartient à :
Theo GastineletClaude Opus 5.5 a révisé 2026-10-30 09:13:51 +01:00
1 Parent 21a50dc5b2
révision a25690363d
13 fichiers modifiés
+1703

Aucun fichier ne correspond à votre recherche

+153
Voir le fichier
@@ -0,0 +1,153 @@
//! Érosion hydraulique (gouttelettes, d'après S. Lague) et thermique (talus),
//! sur une grille carrée `n × n` de pas `pas` m. Entièrement déterministe :
//! les gouttelettes sont traitées dans l'ordre, chacune avec sa graine `hash(seed, i)`.
use crate::bruit::{hash2, unif};
use crate::relief::ParamsErosion;
const INERTIE: f32 = 0.05;
const PENTE_MIN: f32 = 0.01;
const VITESSE_EROSION: f32 = 0.3;
const GRAVITE: f32 = 4.0;
const PAS_MAX: usize = 64;
/// Limites anti-ravines sur pentes très raides (fjord, glaciaire) [À VÉRIFIER].
const EROSION_MAX_PAS: f32 = 0.5;
const VITESSE_MAX: f32 = 6.0;
#[inline]
fn hauteur_grad(h: &[f32], n: usize, px: f32, pz: f32) -> (f32, f32, f32) {
let (ix, iz) = (px as usize, pz as usize);
let (fx, fz) = (px - ix as f32, pz - iz as f32);
let i = iz * n + ix;
let (a, b, c, d) = (h[i], h[i + 1], h[i + n], h[i + n + 1]);
let gx = (b - a) * (1.0 - fz) + (d - c) * fz;
let gz = (c - a) * (1.0 - fx) + (d - b) * fx;
let hh = a * (1.0 - fx) * (1.0 - fz) + b * fx * (1.0 - fz) + c * (1.0 - fx) * fz + d * fx * fz;
(hh, gx, gz)
}
#[inline]
fn deposer(h: &mut [f32], n: usize, px: f32, pz: f32, q: f32) {
let (ix, iz) = (px as usize, pz as usize);
let (fx, fz) = (px - ix as f32, pz - iz as f32);
let i = iz * n + ix;
h[i] += q * (1.0 - fx) * (1.0 - fz);
h[i + 1] += q * fx * (1.0 - fz);
h[i + n] += q * (1.0 - fx) * fz;
h[i + n + 1] += q * fx * fz;
}
/// `h` en mètres ; les pentes sont exprimées en m par maille.
pub fn hydraulique(h: &mut [f32], n: usize, e: &ParamsErosion, seed: u64) {
let lim = (n - 1) as f32;
for g in 0..e.iterations_hydro {
let r = hash2(seed, g as i64, 0x51);
let mut px = unif(r) * (lim - 1.0);
let mut pz = unif(r.rotate_left(29)) * (lim - 1.0);
let (mut dx, mut dz) = (0.0f32, 0.0f32);
let (mut v, mut eau, mut sed) = (1.0f32, e.pluie, 0.0f32);
for _ in 0..PAS_MAX {
let (h0, gx, gz) = hauteur_grad(h, n, px, pz);
dx = dx * INERTIE - gx * (1.0 - INERTIE);
dz = dz * INERTIE - gz * (1.0 - INERTIE);
let l = (dx * dx + dz * dz).sqrt();
if l < 1e-6 {
break;
}
dx /= l;
dz /= l;
let (nx, nz) = (px + dx, pz + dz);
if nx < 0.0 || nz < 0.0 || nx >= lim || nz >= lim {
break;
}
let (h1, _, _) = hauteur_grad(h, n, nx, nz);
let dh = h1 - h0;
let cap = (-dh).max(PENTE_MIN) * v * eau * e.capacite;
if sed > cap || dh > 0.0 {
let q = if dh > 0.0 { dh.min(sed) } else { (sed - cap) * e.depot };
sed -= q;
deposer(h, n, px, pz, q);
} else {
let q = ((cap - sed) * VITESSE_EROSION).min(-dh).min(EROSION_MAX_PAS);
sed += q;
deposer(h, n, px, pz, -q);
}
v = (v * v - dh * GRAVITE).max(0.0).sqrt().min(VITESSE_MAX);
eau *= 1.0 - e.evaporation;
px = nx;
pz = nz;
}
}
}
/// Glissement thermique (Jacobi, donc indépendant de l'ordre de parcours).
pub fn thermique(h: &mut [f32], n: usize, pas: f32, iterations: u32, angle_deg: f32) {
let talus = angle_deg.to_radians().tan() * pas;
let mut d = vec![0.0f32; n * n];
for _ in 0..iterations {
d.iter_mut().for_each(|v| *v = 0.0);
for z in 1..n - 1 {
for x in 1..n - 1 {
let i = z * n + x;
let h0 = h[i];
let vois = [i - 1, i + 1, i - n, i + n];
let (mut tot, mut maxd) = (0.0f32, 0.0f32);
for &j in &vois {
let df = h0 - h[j] - talus;
if df > 0.0 {
tot += df;
maxd = maxd.max(df);
}
}
if tot > 0.0 {
let mv = 0.25 * maxd;
for &j in &vois {
let df = h0 - h[j] - talus;
if df > 0.0 {
let s = mv * df / tot;
d[i] -= s;
d[j] += s;
}
}
}
}
}
for (a, b) in h.iter_mut().zip(&d) {
*a += *b;
}
}
}
/// Accumulation de flux D8 (en nombre de mailles drainées, y compris la maille).
pub fn accumulation_d8(h: &[f32], n: usize) -> Vec<f32> {
let mut ordre: Vec<u32> = (0..(n * n) as u32).collect();
ordre.sort_unstable_by(|&a, &b| h[b as usize].total_cmp(&h[a as usize]).then(a.cmp(&b)));
let mut acc = vec![1.0f32; n * n];
const D: [(i32, i32, f32); 8] = [
(1, 0, 1.0), (-1, 0, 1.0), (0, 1, 1.0), (0, -1, 1.0),
(1, 1, std::f32::consts::SQRT_2), (-1, 1, std::f32::consts::SQRT_2),
(1, -1, std::f32::consts::SQRT_2), (-1, -1, std::f32::consts::SQRT_2),
];
for &c in &ordre {
let c = c as usize;
let (x, z) = ((c % n) as i32, (c / n) as i32);
let mut best = None;
let mut bp = 0.0f32;
for &(dx, dz, l) in &D {
let (xx, zz) = (x + dx, z + dz);
if xx < 0 || zz < 0 || xx >= n as i32 || zz >= n as i32 {
continue;
}
let j = zz as usize * n + xx as usize;
let p = (h[c] - h[j]) / l;
if p > bp {
bp = p;
best = Some(j);
}
}
if let Some(j) = best {
acc[j] += acc[c];
}
}
acc
}