Fichiers
moteur-terrain/proc-relief/examples/rendu.rs
T

105 lignes
4.7 KiB
Rust
Brut Annotations Historique

Ce fichier contient des caractères Unicode ambigus.
Ce fichier contient des caractères Unicode qui peuvent être confondus avec d'autres caractères. Si vous pensez que c'est intentionnel, vous pouvez ignorer cet avertissement. Utilisez le bouton Échappe pour les dévoiler.
//! Rendu ombré par biome (3 × 3 tuiles = 1536 m, 1 px = 1 m) + mesures de vitesse
//! + export des ParamsRelief initiaux en JSON.
//! Usage : cargo run --release --example rendu -- <dossier_png> <dossier_params>
use std::io::BufWriter;
use std::time::Instant;
use proc_relief::*;
fn teinte(h: f32, lo: f32, hi: f32) -> [f32; 3] {
if h < 0.0 {
let t = (-h / 200.0).clamp(0.0, 1.0);
return [0.35 - 0.2 * t, 0.55 - 0.25 * t, 0.80 - 0.25 * t];
}
let t = ((h - lo) / (hi - lo).max(1.0)).clamp(0.0, 1.0);
let c = [[0.42, 0.60, 0.33], [0.78, 0.74, 0.50], [0.62, 0.48, 0.36], [0.85, 0.85, 0.85]];
let s = t * 3.0;
let i = (s as usize).min(2);
let f = s - i as f32;
[0, 1, 2].map(|k| c[i][k] + (c[i + 1][k] - c[i][k]) * f)
}
fn main() {
let args: Vec<String> = std::env::args().collect();
let sortie = args.get(1).cloned().unwrap_or_else(|| "sorties".into());
let dparams = args.get(2).cloned();
std::fs::create_dir_all(&sortie).unwrap();
let seed = 20261030u64;
let nt = 3usize;
let w = nt * 512;
println!("| biome | µs/colonne brute | µs/chunk brut | ms érosion/tuile | ms tuile finale (parallèle) | min | max |");
println!("|---|---|---|---|---|---|---|");
for b in BiomeRelief::TOUS {
let p = params_initiaux(b);
if let Some(d) = &dparams {
std::fs::create_dir_all(d).unwrap();
std::fs::write(format!("{d}/{}.json", b.id()), serde_json::to_string_pretty(&p).unwrap()).unwrap();
}
// vitesse colonne brute
let t = Instant::now();
let mut acc = 0.0f32;
let n = 200_000;
for i in 0..n {
acc += hauteur_brute(b, &p, seed, (i % 977) as f64 * 1.3, (i / 977) as f64 * 1.7);
}
let us_col = t.elapsed().as_secs_f64() * 1e6 / n as f64;
let g = Generateur::biome_unique(seed, b);
// érosion d'une tuile (mono-thread, sans cache)
let t = Instant::now();
let e = g.eroder_tuile(TileId { tx: 50, tz: 50 });
let ms_ero = t.elapsed().as_secs_f64() * 1e3;
// chunk final, tuiles déjà en cache
for dz in -1..=1 {
for dx in -1..=1 {
g.erodee(TileId { tx: 50 + dx, tz: 50 + dz });
}
}
let t = Instant::now();
for k in 0..64 {
acc += g.chunk(50 * 32 + k % 8, 50 * 32 + k / 8)[0];
}
let us_chunk = t.elapsed().as_secs_f64() * 1e6 / 64.0;
let dmax = e.delta.iter().fold(0.0f32, |a, d| a.max(d.abs()));
let dmoy = e.delta.iter().map(|d| d.abs()).sum::<f32>() / e.delta.len() as f32;
eprintln!("{} delta max {dmax:.1} m, moyen {dmoy:.2} m", b.id());
std::hint::black_box(acc);
// rendu 3×3 tuiles
let t = Instant::now();
let tuiles: Vec<TuileRelief> =
(0..nt * nt).map(|k| g.tuile(TileId { tx: (k % nt) as i32, tz: (k / nt) as i32 })).collect();
let ms_tuile = t.elapsed().as_secs_f64() * 1e3 / (nt * nt) as f64;
let mut h = vec![0.0f32; w * w];
for (k, tr) in tuiles.iter().enumerate() {
let (ox, oz) = ((k % nt) * 512, (k / nt) * 512);
for j in 0..512 {
h[(oz + j) * w + ox..(oz + j) * w + ox + 512].copy_from_slice(&tr.h[j * 512..j * 512 + 512]);
}
}
let lo = h.iter().cloned().fold(f32::MAX, f32::min);
let hi = h.iter().cloned().fold(f32::MIN, f32::max);
println!("| {} | {:.2} | {:.0} | {:.0} | {:.0} | {:.0} | {:.0} |", b.id(), us_col, us_chunk, ms_ero, ms_tuile, lo, hi);
let (az, el) = (315f32.to_radians(), 45f32.to_radians());
let (lx, lz, ly) = (el.cos() * az.sin(), -el.cos() * az.cos(), el.sin());
let mut img = vec![0u8; w * w * 3];
for z in 0..w {
for x in 0..w {
let hx = h[z * w + (x + 1).min(w - 1)] - h[z * w + x.saturating_sub(1)];
let hz = h[(z + 1).min(w - 1) * w + x] - h[z.saturating_sub(1) * w + x];
let (nx, nz, ny) = (-hx / 2.0, -hz / 2.0, 1.0f32);
let l = (nx * nx + nz * nz + ny * ny).sqrt();
let s = ((nx * lx + nz * lz + ny * ly) / l).clamp(0.0, 1.0);
let c = teinte(h[z * w + x], lo.max(0.0), hi);
let o = 0.35 + 0.65 * s;
for k in 0..3 {
img[(z * w + x) * 3 + k] = (c[k] * o * 255.0).clamp(0.0, 255.0) as u8;
}
}
}
let f = std::fs::File::create(format!("{sortie}/{}.png", b.id())).unwrap();
let mut enc = png::Encoder::new(BufWriter::new(f), w as u32, w as u32);
enc.set_color(png::ColorType::Rgb);
enc.set_depth(png::BitDepth::Eight);
enc.write_header().unwrap().write_image_data(&img).unwrap();
}
}