use crcbl_shaders::grass::{
CLUMP_UNITS, FACING_SALT, SIZE_SALT, TINT_SALT, accepts, clump_of, hash, kept_far, unit_pair,
};
use super::field::{CELLS_PER_TILE, GrassField, SLOT_CAPACITY};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Blade {
pub cell: u32,
pub row: u32,
pub root: [f32; 3],
pub height: f32,
pub half_width: f32,
pub facing: [f32; 2],
pub ground: [f32; 3],
pub tint: f32,
pub clump: u32,
pub clump_offset: [f32; 2],
pub kept_far: bool,
}
#[must_use]
pub fn blades_of_tile(field: &GrassField, slot: u32) -> Vec<Blade> {
let origin = field.tile_origin(slot);
let side = field.tile_size() / CELLS_PER_TILE as f32;
let rows = field.blades();
let [tile_x, tile_z] = [slot % field.tiles()[0], slot / field.tiles()[0]];
let mut blades = Vec::new();
for lane in 0..SLOT_CAPACITY {
let cell = slot * SLOT_CAPACITY + lane;
let jitter_lane = hash(cell);
let jitter = unit_pair(jitter_lane);
let along = [
(lane % CELLS_PER_TILE) as f32,
(lane / CELLS_PER_TILE) as f32,
];
let world = [
origin[0] + (along[0] + jitter[0]) * side,
origin[1] + (along[1] + jitter[1]) * side,
];
let [density, texel_row] = field.cover().under(world[0], world[1]);
if !accepts(cell, density) {
continue;
}
let row = u32::from(texel_row).min(rows.len() as u32 - 1);
let blade = rows[row as usize];
let global = [
tile_x * CELLS_PER_TILE + lane % CELLS_PER_TILE,
tile_z * CELLS_PER_TILE + lane / CELLS_PER_TILE,
];
let clump = clump_of(global, jitter_lane);
let (height, ground) = field.ground().under(world[0], world[1]);
let spread = unit_pair(hash(cell ^ SIZE_SALT));
let clump_height = unit_pair(hash(clump.id ^ SIZE_SALT))[0];
let unit = side * (1.0 / CLUMP_UNITS as f32);
blades.push(Blade {
cell,
row,
root: [world[0], height, world[1]],
height: blade.height
* (1.0 - blade.height_spread * spread[0])
* (1.0 - blade.clumping.height * clump_height),
half_width: blade.half_width * (1.0 - blade.width_spread * spread[1]),
facing: facing_of(cell, clump.id, blade.clumping.facing),
ground,
tint: unit_pair(hash(cell ^ TINT_SALT))[0],
clump: clump.id,
clump_offset: [clump.offset[0] as f32 * unit, clump.offset[1] as f32 * unit],
kept_far: kept_far(global),
});
}
blades
}
#[must_use]
pub fn facing_of(cell: u32, clump: u32, share: f32) -> [f32; 2] {
let square_of = |lane: u32| {
let pair = unit_pair(hash(lane ^ FACING_SALT));
[pair[0] * 2.0 - 1.0, pair[1] * 2.0 - 1.0]
};
let (own, shared) = (square_of(cell), square_of(clump));
let square = [
own[0] + (shared[0] - own[0]) * share,
own[1] + (shared[1] - own[1]) * share,
];
let length_squared = square[0] * square[0] + square[1] * square[1];
if length_squared > 1e-8 {
let scale = 1.0 / length_squared.sqrt();
[square[0] * scale, square[1] * scale]
} else {
[1.0, 0.0]
}
}
#[must_use]
pub fn cover_at(field: &GrassField, x: f32, z: f32) -> Option<f32> {
let [density, row] = field.cover().under(x, z);
if density == 0 {
return None;
}
let rows = field.blades();
let row = usize::from(row).min(rows.len() - 1);
Some(rows[row].height)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::grass::field::tests::{flat_cover, flat_ground, one_blade};
use crate::grass::field::{BladeType, CoverMap};
fn field_with(cover: CoverMap, blades: Vec<BladeType>) -> GrassField {
GrassField::new(
[2, 1],
8.0,
[0.0, 0.0],
1000.0,
flat_ground(32, 0.0),
cover,
blades,
)
.expect("a real field")
}
#[test]
fn a_tile_places_the_same_blades_twice() {
let field = field_with(flat_cover(32, 200), one_blade());
let first = blades_of_tile(&field, 0);
assert_eq!(first, blades_of_tile(&field, 0));
assert!(!first.is_empty(), "the tile placed nothing to compare");
}
#[test]
fn every_blade_lands_in_its_own_tile_under_a_cell_of_its_own() {
let field = field_with(flat_cover(32, 255), one_blade());
let mut cells = std::collections::HashSet::new();
for slot in 0..field.slots() {
let [ox, oz] = field.tile_origin(slot);
let side = field.tile_size();
for blade in blades_of_tile(&field, slot) {
assert!(
(ox..ox + side).contains(&blade.root[0])
&& (oz..oz + side).contains(&blade.root[2]),
"cell {} landed at {:?}, outside tile {slot} at ({ox}, {oz})",
blade.cell,
blade.root
);
assert!(cells.insert(blade.cell), "cell {} twice", blade.cell);
let length = blade.facing[0].hypot(blade.facing[1]);
assert!(
(length - 1.0).abs() < 1e-6,
"{:?} is not unit",
blade.facing
);
}
}
}
#[test]
fn a_denser_cover_texel_places_more_blades() {
let mut last = 0;
for density in [0u8, 32, 64, 128, 192, 255] {
let field = field_with(flat_cover(32, density), one_blade());
let placed = blades_of_tile(&field, 0).len();
eprintln!("grass placement: density {density} placed {placed} blade(s)");
assert!(
placed >= last,
"density {density} placed {placed}, fewer than the {last} the density under it did"
);
if density == 0 {
assert_eq!(placed, 0, "bare ground placed {placed} blade(s)");
}
last = placed;
}
assert!(
last as u32 > SLOT_CAPACITY - 64,
"full density placed only {last} of {SLOT_CAPACITY} cells"
);
}
#[test]
fn a_blade_stays_inside_its_rows_size() {
let row = one_blade()[0];
let field = field_with(flat_cover(32, 255), one_blade());
let blades = blades_of_tile(&field, 0);
let (mut shortest, mut tallest) = (f32::INFINITY, 0.0f32);
for blade in &blades {
assert!(blade.height <= row.height, "{} m", blade.height);
assert!(blade.half_width <= row.half_width);
assert!(blade.height >= row.height * (1.0 - row.height_spread));
assert!((0.0..1.0).contains(&blade.tint));
shortest = shortest.min(blade.height);
tallest = tallest.max(blade.height);
}
eprintln!(
"grass placement: heights {shortest} m to {tallest} m over {} blades",
blades.len()
);
assert!(
tallest - shortest > row.height * row.height_spread * 0.9,
"the heights span only {} m of the {} m the spread allows",
tallest - shortest,
row.height * row.height_spread
);
}
#[test]
fn the_cover_maps_row_picks_the_blade() {
let mut rows = one_blade();
rows.push(BladeType {
height: 0.05,
..rows[0]
});
let mut cover = flat_cover(32, 255);
for (at, texel) in cover.cover.iter_mut().enumerate() {
texel[1] = if at % 2 == 0 { 0 } else { 1 };
}
cover.cover[0][1] = 200;
let field = field_with(cover, rows.clone());
let mut seen = [0usize; 2];
for blade in blades_of_tile(&field, 0) {
seen[blade.row as usize] += 1;
assert!(blade.height <= rows[blade.row as usize].height);
}
assert!(seen[0] > 0 && seen[1] > 0, "{seen:?}");
assert_eq!(
u32::from(200u8).min(rows.len() as u32 - 1),
1,
"a row past the table clamps into it"
);
}
#[test]
fn no_clumping_leaves_every_blade_where_it_stood() {
let row = one_blade()[0];
let field = field_with(flat_cover(32, 255), one_blade());
let blades = blades_of_tile(&field, 1);
for blade in &blades {
let spread = unit_pair(hash(blade.cell ^ SIZE_SALT));
assert_eq!(
blade.height,
row.height * (1.0 - row.height_spread * spread[0])
);
let pair = unit_pair(hash(blade.cell ^ FACING_SALT));
let square = [pair[0] * 2.0 - 1.0, pair[1] * 2.0 - 1.0];
let scale = 1.0 / (square[0] * square[0] + square[1] * square[1]).sqrt();
assert_eq!(blade.facing, [square[0] * scale, square[1] * scale]);
}
let kept = blades.iter().filter(|blade| blade.kept_far).count();
let clumps: std::collections::HashSet<u32> =
blades.iter().map(|blade| blade.clump).collect();
eprintln!(
"grass placement: {kept} of {} blades kept far, {} clumps",
blades.len(),
clumps.len()
);
assert!((blades.len() / 5..blades.len() / 3).contains(&kept));
assert!(clumps.len() > 30, "{} clumps over a tile", clumps.len());
}
#[test]
fn a_clumped_row_faces_and_stands_by_its_clump() {
let clumped = |facing, height| {
vec![BladeType {
clumping: crate::grass::field::Clumping { facing, height },
..one_blade()[0]
}]
};
let plain = blades_of_tile(&field_with(flat_cover(32, 255), clumped(0.0, 0.0)), 0);
let full = blades_of_tile(&field_with(flat_cover(32, 255), clumped(1.0, 0.5)), 0);
assert_eq!(plain.len(), full.len());
let mut by_clump: std::collections::HashMap<u32, Vec<(Blade, Blade)>> =
std::collections::HashMap::new();
for (before, after) in plain.iter().zip(&full) {
assert_eq!((before.cell, before.clump), (after.cell, after.clump));
by_clump
.entry(after.clump)
.or_default()
.push((*before, *after));
}
let mut shared_facings = 0usize;
for blades in by_clump.values() {
let facing = blades[0].1.facing;
let ratio = blades[0].1.height / blades[0].0.height;
for (before, after) in blades {
assert_eq!(after.facing, facing, "clump {} faces two ways", after.clump);
assert!(
(after.height / before.height - ratio).abs() < 1e-5,
"clump {} lowers its blades by two shares",
after.clump
);
assert!((0.5..=1.0).contains(&(after.height / before.height)));
}
shared_facings += usize::from(blades.len() > 1);
}
eprintln!(
"grass placement: {} clumps over a tile, {shared_facings} with more than one blade",
by_clump.len()
);
assert!(shared_facings > 20);
}
#[test]
fn the_gameplay_query_answers_the_rows_height_and_not_a_blades() {
let mut rows = one_blade();
rows.push(BladeType {
height: 1.25,
..rows[0]
});
let mut cover = flat_cover(32, 255);
cover.cover[5 + 32 * 5] = [0, 0];
cover.cover[6 + 32 * 5] = [180, 1];
let field = field_with(cover, rows.clone());
assert_eq!(cover_at(&field, 5.0, 5.0), None);
assert_eq!(cover_at(&field, 6.0, 5.0), Some(1.25));
assert_eq!(cover_at(&field, 7.0, 5.0), Some(rows[0].height));
for blade in blades_of_tile(&field, 0).iter().filter(|b| b.row == 1) {
assert!(blade.height <= 1.25);
}
}
}