Prototype 10 : crate biomes (biome, palette, arbres)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Cette révision appartient à :
1 Parent
3be8691f00
révision
460cd6e33d
5 fichiers modifiés
+437
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,69 @@
|
||||
//! PNG de dessus de la tuile de Lans (1000x1000 m). Usage :
|
||||
//! cargo run --release --example vue_lans -- <mnt.npy f32 1000x1000> <dossier raster-sol-eau> <sortie.png>
|
||||
use biomes::*;
|
||||
use std::io::{BufWriter, Read};
|
||||
|
||||
fn npy(path: &str) -> (Vec<u8>, usize) {
|
||||
let mut b = Vec::new();
|
||||
std::fs::File::open(path).unwrap().read_to_end(&mut b).unwrap();
|
||||
let hl = u16::from_le_bytes([b[8], b[9]]) as usize;
|
||||
(b[10 + hl..].to_vec(), 10 + hl)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a: Vec<String> = std::env::args().collect();
|
||||
let (mnt_b, _) = npy(&a[1]);
|
||||
let z: Vec<f32> = mnt_b.chunks_exact(4).map(|c| f32::from_le_bytes([c[0], c[1], c[2], c[3]])).collect();
|
||||
let rd = |n: &str| npy(&format!("{}/{}.npy", a[2], n)).0;
|
||||
let (sol, eau, routes, bati) = (rd("sol"), rd("eau"), rd("routes"), rd("bati"));
|
||||
let n = 1000usize;
|
||||
let mut img = vec![0u8; n * n * 3];
|
||||
let mut stats = std::collections::BTreeMap::new();
|
||||
let seed = 42;
|
||||
for r in 0..n {
|
||||
for c in 0..n {
|
||||
let i = r * n + c;
|
||||
let g = |dr: isize, dc: isize| z[(r as isize + dr).clamp(0, n as isize - 1) as usize * n + (c as isize + dc).clamp(0, n as isize - 1) as usize];
|
||||
let dx = (g(0, 1) - g(0, -1)) / 2.0;
|
||||
let dy = (g(1, 0) - g(-1, 0)) / 2.0; // vers le sud
|
||||
let pente = dx.hypot(dy).atan().to_degrees();
|
||||
let aspect = (-dx).atan2(dy).to_degrees().rem_euclid(360.0);
|
||||
let col = Colonne { alt_m: z[i], pente_deg: pente, aspect_deg: aspect, sol: sol[i], eau: eau[i], route: routes[i], bati: bati[i] };
|
||||
let (x, zz) = (c as i32, r as i32);
|
||||
let pal = palette(&col, x, zz, seed);
|
||||
let bi = biome(&col, x, zz, seed);
|
||||
*stats.entry(bi.id()).or_insert(0u32) += 1;
|
||||
let mut rgb: [f32; 3] = match pal.haut {
|
||||
"minecraft:grass_block" => match bi { Biome::Forest => [76., 140., 50.], Biome::Taiga => [70., 120., 80.], Biome::Meadow | Biome::Grove => [100., 165., 60.], Biome::Swamp => [90., 110., 60.], _ => [95., 159., 53.] },
|
||||
"minecraft:stone" => [125., 125., 125.], "minecraft:andesite" => [136., 136., 136.],
|
||||
"minecraft:gravel" => [131., 127., 126.], "minecraft:coarse_dirt" => [119., 85., 59.],
|
||||
"minecraft:podzol" => [91., 63., 24.], "minecraft:mud" => [60., 57., 60.],
|
||||
"minecraft:farmland" => [143., 103., 71.], "minecraft:dirt_path" => [148., 122., 65.],
|
||||
"minecraft:gray_concrete_powder" => [76., 81., 84.], "minecraft:stone_bricks" => [150., 150., 150.],
|
||||
_ => [200., 0., 200.],
|
||||
};
|
||||
if eau[i] == 1 || eau[i] == 3 { rgb = [50., 90., 200.]; }
|
||||
if let Some(t) = arbre_en(&col, x, zz, seed) {
|
||||
let (tc, big) = if t.essence.coniferes() { ([20., 70., 40.], 2) } else { ([45., 110., 30.], 2) };
|
||||
for dr in 0..big { for dc in 0..big {
|
||||
let (rr, cc) = (r + dr, c + dc);
|
||||
if rr < n && cc < n { let j = (rr * n + cc) * 3; for k in 0..3 { img[j + k] = tc[k] as u8; } }
|
||||
} }
|
||||
continue;
|
||||
}
|
||||
let sh = (1.0 + (dx - dy) * 0.04).clamp(0.6, 1.3);
|
||||
let j = i * 3;
|
||||
if img[j] == 0 && img[j + 1] == 0 && img[j + 2] == 0 || true {
|
||||
// ne pas ecraser les arbres deja poses (pixels proches deja colores)
|
||||
if !(img[j] != 0 && img[j + 1] != 0 && is_tree(&img[j..j + 3])) { for k in 0..3 { img[j + k] = (rgb[k] * sh).clamp(0., 255.) as u8; } }
|
||||
}
|
||||
}
|
||||
}
|
||||
let f = BufWriter::new(std::fs::File::create(&a[3]).unwrap());
|
||||
let mut e = png::Encoder::new(f, n as u32, n as u32);
|
||||
e.set_color(png::ColorType::Rgb); e.set_depth(png::BitDepth::Eight);
|
||||
e.write_header().unwrap().write_image_data(&img).unwrap();
|
||||
for (k, v) in stats { println!("{k}: {:.1} %", v as f32 / 1e4); }
|
||||
}
|
||||
|
||||
fn is_tree(p: &[u8]) -> bool { (p == [20, 70, 40]) || (p == [45, 110, 30]) }
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur