lenia: more smoothing, better kernel generator
This commit is contained in:
parent
87680b3ec5
commit
d0557ff466
13 changed files with 95 additions and 172 deletions
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
image = { version = "0.24.6", default_features = false, features = ["png"] }
|
||||
|
|
BIN
lenia-kernel/kernel.png
Normal file
BIN
lenia-kernel/kernel.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.5 KiB |
|
@ -3,33 +3,44 @@ use std::env;
|
|||
fn main() {
|
||||
let args: Vec<_> = env::args().skip(1).collect();
|
||||
if args.is_empty() {
|
||||
println!("usage: kernel_radius");
|
||||
println!("specify kernel radius, eg:");
|
||||
println!("cargo run 16 > ../Assets/automata/Lenia/lenia_generated_kernel.cginc");
|
||||
return;
|
||||
}
|
||||
let radius = args[0].parse().unwrap();
|
||||
let k_offset = 0.435;
|
||||
let k_sharpness = 28.0;
|
||||
|
||||
let mut img = image::RgbImage::new(radius * 2 + 1, radius * 2 + 1);
|
||||
|
||||
let mut total_max = 0.0;
|
||||
println!(" // generated by the rust program");
|
||||
println!(" const int Radius = {};", radius);
|
||||
println!(" #define RADIUS {}", radius);
|
||||
println!(" // kernel LUT size is {} bytes", radius * (radius + 1) * 2);
|
||||
println!(" const half Kernel[{}][{}] = {{", radius + 1, radius);
|
||||
println!("// generated by the rust program");
|
||||
println!("const int Radius = {};", radius);
|
||||
println!("#define RADIUS {}", radius);
|
||||
println!("const half Kernel[{}][{}] = {{", radius + 1, radius);
|
||||
for y in 0..=radius {
|
||||
print!(" {{");
|
||||
print!(" {{");
|
||||
for x in 1..=radius {
|
||||
let k = k(x, y, radius, k_offset, k_sharpness);
|
||||
let k = (k(x, y, radius, k_offset, k_sharpness) * 50.0).floor() / 50.0;
|
||||
total_max += k * 4.0;
|
||||
print!("{:.4}, ", k);
|
||||
print!("{:.2}, ", k);
|
||||
{
|
||||
let pixel = image::Rgb([0, (k * 255.0) as u8, 0]);
|
||||
// let cx = radius
|
||||
img.put_pixel(radius + x, radius + y, pixel);
|
||||
img.put_pixel(radius - y, radius + x, pixel);
|
||||
img.put_pixel(radius - x, radius - y, pixel);
|
||||
img.put_pixel(radius + y, radius - x, pixel);
|
||||
}
|
||||
}
|
||||
println!("}},");
|
||||
}
|
||||
println!(" }};");
|
||||
println!(" const float TOTAL_MAX = {};", total_max);
|
||||
println!("}};");
|
||||
println!("const float total_max = {};", total_max);
|
||||
img.save("kernel.png").unwrap();
|
||||
}
|
||||
|
||||
fn k(x: usize, y: usize, radius: usize, k_offset: f32, k_sharpness: f32) -> f32 {
|
||||
fn k(x: u32, y: u32, radius: u32, k_offset: f32, k_sharpness: f32) -> f32 {
|
||||
let x = x as f32;
|
||||
let y = y as f32;
|
||||
let r = (x * x + y * y).sqrt() / radius as f32;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue