use crcbl_shaders::grass::{
BLADE_STRIDE, DEFAULT_SHELLS, GrassBlade, INSTANCE_STRIDE, LOOK_BLADES, LOOK_CARDS,
LOOK_SHELLS, MAX_SHELLS, NORMAL_GROUND, NORMAL_UP,
};
pub const CELLS_PER_TILE: u32 = 64;
pub const SLOT_CAPACITY: u32 = CELLS_PER_TILE * CELLS_PER_TILE;
pub const MAX_TILES: u32 = 64;
pub const MAX_BLADES: u32 = 16;
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum GrassError {
#[error("a {x}x{z} field of tiles is not one this crate will lay out")]
Tiles {
x: u32,
z: u32,
},
#[error("a grass field's {what} of {metres} m is not a distance")]
Metres {
what: &'static str,
metres: f32,
},
#[error("a {width}x{height} grass {what} map has no texels")]
EmptyMap {
what: &'static str,
width: u32,
height: u32,
},
#[error("a {width}x{height} grass {what} map holds {expected} texels, and {found} arrived")]
MapSize {
what: &'static str,
width: u32,
height: u32,
expected: usize,
found: usize,
},
#[error("the grass ground field holds {height} at texel {at}, which is not a height")]
Height {
height: f32,
at: usize,
},
#[error("a blade table of {rows} row(s) is not one this crate will hold")]
Blades {
rows: usize,
},
#[error("blade row {row}'s {what} of {value} is not one a blade is drawn with")]
Blade {
row: usize,
what: &'static str,
value: f32,
},
#[error("a stack of {count} shells is not one a field holds; 1 to {MAX_SHELLS} are")]
Shells {
count: u32,
},
#[error(
"a blade level switch at {distance} m over a band of {band} m is not one a field draws"
)]
BladeLod {
distance: f32,
band: f32,
},
}
#[derive(Clone, Debug, PartialEq)]
pub struct Heightfield {
pub texels: [u32; 2],
pub metres_per_texel: f32,
pub origin: [f32; 2],
pub heights: Vec<f32>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct CoverMap {
pub texels: [u32; 2],
pub metres_per_texel: f32,
pub origin: [f32; 2],
pub cover: Vec<[u8; 2]>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BladeLook {
#[default]
Cards,
Shells,
Blades,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum BladeNormal {
#[default]
Ground,
Up,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BladeStyle {
pub root_occlusion: [f32; 3],
pub occlusion_reach: f32,
pub tip_glow: [f32; 3],
pub glow_start: f32,
pub patch_color: [f32; 3],
pub patch_share: f32,
pub normal: BladeNormal,
}
impl BladeStyle {
pub const PLAIN: Self = Self {
root_occlusion: [0.0; 3],
occlusion_reach: 0.0,
tip_glow: [0.0; 3],
glow_start: 0.0,
patch_color: [0.0; 3],
patch_share: 0.0,
normal: BladeNormal::Ground,
};
}
impl Default for BladeStyle {
fn default() -> Self {
Self::PLAIN
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BladeShape {
pub tilt: f32,
pub bow: f32,
pub rounding: f32,
}
impl BladeShape {
pub const STRAIGHT: Self = Self {
tilt: 0.0,
bow: 0.0,
rounding: 0.0,
};
}
impl Default for BladeShape {
fn default() -> Self {
Self::STRAIGHT
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Clumping {
pub facing: f32,
pub height: f32,
}
impl Clumping {
pub const NONE: Self = Self {
facing: 0.0,
height: 0.0,
};
}
impl Default for Clumping {
fn default() -> Self {
Self::NONE
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BladeLod {
pub distance: f32,
pub band: f32,
}
impl Default for BladeLod {
fn default() -> Self {
Self {
distance: 12.0,
band: 4.0,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Shells {
pub count: u32,
pub fins: bool,
}
impl Default for Shells {
fn default() -> Self {
Self {
count: DEFAULT_SHELLS,
fins: true,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BladeType {
pub root_color: [f32; 3],
pub tip_color: [f32; 3],
pub height: f32,
pub half_width: f32,
pub height_spread: f32,
pub width_spread: f32,
pub look: BladeLook,
pub style: BladeStyle,
pub shape: BladeShape,
pub clumping: Clumping,
}
impl BladeType {
#[must_use]
pub fn row(&self) -> GrassBlade {
let with = |[r, g, b]: [f32; 3], a: f32| [r, g, b, a];
let style = self.style;
GrassBlade {
root_color: with(self.root_color, 0.0),
tip_color: with(self.tip_color, 0.0),
size: [
self.height,
self.half_width,
self.height_spread,
self.width_spread,
],
occlusion: with(style.root_occlusion, style.occlusion_reach),
glow: with(style.tip_glow, style.glow_start),
patch: with(style.patch_color, style.patch_share),
shape: [self.shape.tilt, self.shape.bow, self.shape.rounding, 0.0],
clump: [self.clumping.facing, self.clumping.height, 0.0, 0.0],
flags: [
match self.look {
BladeLook::Cards => LOOK_CARDS,
BladeLook::Shells => LOOK_SHELLS,
BladeLook::Blades => LOOK_BLADES,
},
match style.normal {
BladeNormal::Ground => NORMAL_GROUND,
BladeNormal::Up => NORMAL_UP,
},
0,
0,
],
}
}
fn refusal(&self) -> Option<(&'static str, f32)> {
let style = self.style;
let unit = |value: f32| value.is_finite() && (0.0..=1.0).contains(&value);
let below_one = |value: f32| value.is_finite() && (0.0..1.0).contains(&value);
let colour = |value: f32| value.is_finite() && value >= 0.0;
let checks = [
(
"height",
self.height,
self.height.is_finite() && self.height > 0.0,
),
(
"half-width",
self.half_width,
self.half_width.is_finite() && self.half_width > 0.0,
),
(
"height spread",
self.height_spread,
below_one(self.height_spread),
),
(
"width spread",
self.width_spread,
below_one(self.width_spread),
),
(
"occlusion reach",
style.occlusion_reach,
unit(style.occlusion_reach),
),
("glow start", style.glow_start, below_one(style.glow_start)),
("patch share", style.patch_share, unit(style.patch_share)),
("tilt", self.shape.tilt, below_one(self.shape.tilt)),
("bow", self.shape.bow, unit(self.shape.bow)),
("rounding", self.shape.rounding, unit(self.shape.rounding)),
(
"clump facing",
self.clumping.facing,
unit(self.clumping.facing),
),
(
"clump height",
self.clumping.height,
below_one(self.clumping.height),
),
];
let colours = [
("root occlusion colour", style.root_occlusion),
("tip glow", style.tip_glow),
("patch colour", style.patch_color),
]
.into_iter()
.flat_map(|(what, rgb)| rgb.map(|value| (what, value, colour(value))));
checks
.into_iter()
.chain(colours)
.find(|(_, _, ok)| !ok)
.map(|(what, value, _)| (what, value))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct GrassField {
tiles: [u32; 2],
tile_size: f32,
origin: [f32; 2],
reach: f32,
ground: Heightfield,
cover: CoverMap,
blades: Vec<BladeType>,
shells: Shells,
blade_lod: BladeLod,
}
impl GrassField {
pub fn new(
tiles: [u32; 2],
tile_size: f32,
origin: [f32; 2],
reach: f32,
ground: Heightfield,
cover: CoverMap,
blades: Vec<BladeType>,
) -> Result<Self, GrassError> {
let [x, z] = tiles;
if x == 0 || z == 0 || x.saturating_mul(z) > MAX_TILES {
return Err(GrassError::Tiles { x, z });
}
for (what, metres) in [("tile side", tile_size), ("reach", reach)] {
if !(metres.is_finite() && metres > 0.0) {
return Err(GrassError::Metres { what, metres });
}
}
for value in origin {
if !value.is_finite() {
return Err(GrassError::Metres {
what: "origin",
metres: value,
});
}
}
ground.check("ground")?;
cover.check()?;
if blades.is_empty() || blades.len() > MAX_BLADES as usize {
return Err(GrassError::Blades { rows: blades.len() });
}
for (row, blade) in blades.iter().enumerate() {
if let Some((what, value)) = blade.refusal() {
return Err(GrassError::Blade { row, what, value });
}
}
Ok(Self {
tiles,
tile_size,
origin,
reach,
ground,
cover,
blades,
shells: Shells::default(),
blade_lod: BladeLod::default(),
})
}
pub fn with_shells(mut self, shells: Shells) -> Result<Self, GrassError> {
if !(1..=MAX_SHELLS).contains(&shells.count) {
return Err(GrassError::Shells {
count: shells.count,
});
}
self.shells = shells;
Ok(self)
}
pub fn with_blade_lod(mut self, lod: BladeLod) -> Result<Self, GrassError> {
let BladeLod { distance, band } = lod;
if !(distance.is_finite()
&& distance > 0.0
&& band.is_finite()
&& (0.0..=distance).contains(&band))
{
return Err(GrassError::BladeLod { distance, band });
}
self.blade_lod = lod;
Ok(self)
}
#[must_use]
pub const fn blade_lod(&self) -> BladeLod {
self.blade_lod
}
#[must_use]
pub fn draws_blades(&self) -> bool {
self.blades
.iter()
.any(|blade| blade.look == BladeLook::Blades)
}
#[must_use]
pub const fn shells(&self) -> Shells {
self.shells
}
#[must_use]
pub fn draws_shells(&self) -> bool {
self.blades
.iter()
.any(|blade| blade.look == BladeLook::Shells)
}
#[must_use]
pub fn shell_stack(&self) -> f32 {
self.blades
.iter()
.filter(|blade| blade.look == BladeLook::Shells)
.fold(0.0, |tallest, blade| tallest.max(blade.height))
}
#[must_use]
pub const fn tiles(&self) -> [u32; 2] {
self.tiles
}
#[must_use]
pub const fn slots(&self) -> u32 {
self.tiles[0] * self.tiles[1]
}
#[must_use]
pub const fn tile_size(&self) -> f32 {
self.tile_size
}
#[must_use]
pub const fn origin(&self) -> [f32; 2] {
self.origin
}
#[must_use]
pub const fn reach(&self) -> f32 {
self.reach
}
#[must_use]
pub const fn ground(&self) -> &Heightfield {
&self.ground
}
#[must_use]
pub const fn cover(&self) -> &CoverMap {
&self.cover
}
#[must_use]
pub fn blades(&self) -> &[BladeType] {
&self.blades
}
#[must_use]
pub fn tile_origin(&self, slot: u32) -> [f32; 2] {
assert!(
slot < self.slots(),
"slot {slot} is not one of this field's"
);
let [x, z] = [slot % self.tiles[0], slot / self.tiles[0]];
[
self.origin[0] + x as f32 * self.tile_size,
self.origin[1] + z as f32 * self.tile_size,
]
}
#[must_use]
pub fn cell_bytes(&self) -> u64 {
u64::from(self.slots()) * u64::from(SLOT_CAPACITY) * INSTANCE_STRIDE as u64
}
#[must_use]
pub fn instance_bytes(&self) -> u64 {
2 * self.cell_bytes()
}
#[must_use]
pub fn blade_bytes(&self) -> u64 {
self.blades.len() as u64 * BLADE_STRIDE as u64
}
}
impl Heightfield {
#[must_use]
pub fn under(&self, x: f32, z: f32) -> (f32, [f32; 3]) {
let inverse = 1.0 / self.metres_per_texel;
let at = [
(x - self.origin[0]) * inverse,
(z - self.origin[1]) * inverse,
];
let base = [at[0].floor(), at[1].floor()];
let blend = [at[0] - base[0], at[1] - base[1]];
#[expect(
clippy::cast_possible_truncation,
reason = "`floor` has already made these integral, so the cast is exact"
)]
let texel = [base[0] as i32, base[1] as i32];
let corner = |dx: i32, dz: i32| self.texel(texel[0] + dx, texel[1] + dz);
let (h00, h10, h01, h11) = (corner(0, 0), corner(1, 0), corner(0, 1), corner(1, 1));
let lower = h00 + blend[0] * (h10 - h00);
let upper = h01 + blend[0] * (h11 - h01);
let height = lower + blend[1] * (upper - lower);
let rise_x = 0.5 * ((h10 - h00) + (h11 - h01));
let rise_z = 0.5 * ((h01 - h00) + (h11 - h10));
let normal = normalise([-rise_x, self.metres_per_texel, -rise_z]);
(height, normal)
}
fn texel(&self, x: i32, z: i32) -> f32 {
let [width, height] = self.texels;
#[expect(
clippy::cast_possible_wrap,
reason = "`check` refuses a map larger than an `i32` holds"
)]
let last = [width as i32 - 1, height as i32 - 1];
let clamped = [x.clamp(0, last[0]), z.clamp(0, last[1])];
#[expect(
clippy::cast_sign_loss,
reason = "clamped into `0..=last`, which is non-negative"
)]
let at = clamped[1] as usize * width as usize + clamped[0] as usize;
self.heights[at]
}
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
self.heights
.iter()
.flat_map(|height| height.to_le_bytes())
.collect()
}
fn check(&self, what: &'static str) -> Result<(), GrassError> {
let [width, height] = self.texels;
check_grid(what, self.texels, self.metres_per_texel, self.origin)?;
let expected = width as usize * height as usize;
if self.heights.len() != expected {
return Err(GrassError::MapSize {
what,
width,
height,
expected,
found: self.heights.len(),
});
}
for (at, value) in self.heights.iter().enumerate() {
if !value.is_finite() {
return Err(GrassError::Height { height: *value, at });
}
}
Ok(())
}
}
impl CoverMap {
#[must_use]
pub fn under(&self, x: f32, z: f32) -> [u8; 2] {
let inverse = 1.0 / self.metres_per_texel;
let at = [
(x - self.origin[0]) * inverse,
(z - self.origin[1]) * inverse,
];
let [width, height] = self.texels;
#[expect(
clippy::cast_possible_truncation,
clippy::cast_possible_wrap,
reason = "`floor` has made these integral and `check` bounds the extents"
)]
let texel = [
((at[0] + 0.5).floor() as i32).clamp(0, width as i32 - 1),
((at[1] + 0.5).floor() as i32).clamp(0, height as i32 - 1),
];
#[expect(
clippy::cast_sign_loss,
reason = "clamped into `0..=last`, which is non-negative"
)]
let at = texel[1] as usize * width as usize + texel[0] as usize;
self.cover[at]
}
#[must_use]
pub fn to_bytes(&self) -> Vec<u8> {
self.cover
.iter()
.flat_map(|[density, row]| [*density, *row, 0, 0])
.collect()
}
fn check(&self) -> Result<(), GrassError> {
let [width, height] = self.texels;
check_grid("cover", self.texels, self.metres_per_texel, self.origin)?;
let expected = width as usize * height as usize;
if self.cover.len() != expected {
return Err(GrassError::MapSize {
what: "cover",
width,
height,
expected,
found: self.cover.len(),
});
}
Ok(())
}
}
fn check_grid(
what: &'static str,
texels: [u32; 2],
metres_per_texel: f32,
origin: [f32; 2],
) -> Result<(), GrassError> {
let [width, height] = texels;
if width == 0 || height == 0 || width > i32::MAX as u32 || height > i32::MAX as u32 {
return Err(GrassError::EmptyMap {
what,
width,
height,
});
}
if !(metres_per_texel.is_finite() && metres_per_texel > 0.0) {
return Err(GrassError::Metres {
what: "texel scale",
metres: metres_per_texel,
});
}
for value in origin {
if !value.is_finite() {
return Err(GrassError::Metres {
what: "map origin",
metres: value,
});
}
}
Ok(())
}
fn normalise(vector: [f32; 3]) -> [f32; 3] {
let length_squared = vector[0] * vector[0] + vector[1] * vector[1] + vector[2] * vector[2];
if length_squared > 0.0 {
let scale = 1.0 / length_squared.sqrt();
[vector[0] * scale, vector[1] * scale, vector[2] * scale]
} else {
[0.0, 1.0, 0.0]
}
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
pub(crate) fn flat_ground(texels: u32, height: f32) -> Heightfield {
Heightfield {
texels: [texels, texels],
metres_per_texel: 1.0,
origin: [0.0, 0.0],
heights: vec![height; (texels * texels) as usize],
}
}
pub(crate) fn flat_cover(texels: u32, density: u8) -> CoverMap {
CoverMap {
texels: [texels, texels],
metres_per_texel: 1.0,
origin: [0.0, 0.0],
cover: vec![[density, 0]; (texels * texels) as usize],
}
}
pub(crate) fn one_blade() -> Vec<BladeType> {
vec![BladeType {
root_color: [0.05, 0.12, 0.03],
tip_color: [0.35, 0.55, 0.15],
height: 0.4,
half_width: 0.03,
height_spread: 0.4,
width_spread: 0.3,
look: BladeLook::Cards,
style: BladeStyle::PLAIN,
shape: BladeShape::STRAIGHT,
clumping: Clumping::NONE,
}]
}
pub(super) fn field() -> GrassField {
GrassField::new(
[2, 2],
8.0,
[0.0, 0.0],
100.0,
flat_ground(32, 0.0),
flat_cover(32, 255),
one_blade(),
)
.expect("a real field")
}
#[test]
fn the_slot_holds_one_instance_per_cell() {
assert_eq!(SLOT_CAPACITY, CELLS_PER_TILE * CELLS_PER_TILE);
}
#[test]
fn the_widest_field_fits_a_browsers_storage_binding() {
const WEBGPU_MAX_STORAGE_BINDING: u64 = 128 * 1024 * 1024;
let widest = GrassField::new(
[MAX_TILES, 1],
8.0,
[0.0, 0.0],
100.0,
flat_ground(8, 0.0),
flat_cover(8, 8),
one_blade(),
)
.expect("the widest field is a field")
.instance_bytes();
eprintln!("grass: the widest field's instances are {widest} byte(s)");
assert!(
widest <= WEBGPU_MAX_STORAGE_BINDING,
"{MAX_TILES} tiles of {SLOT_CAPACITY} instances is {widest} bytes, past the \
{WEBGPU_MAX_STORAGE_BINDING} a browser guarantees"
);
}
#[test]
fn a_field_refuses_every_number_a_shader_could_not_survive() {
let build = |tiles, tile_size, reach, ground, cover, blades| {
GrassField::new(tiles, tile_size, [0.0, 0.0], reach, ground, cover, blades)
};
assert_eq!(
build(
[0, 2],
8.0,
10.0,
flat_ground(8, 0.0),
flat_cover(8, 8),
one_blade()
),
Err(GrassError::Tiles { x: 0, z: 2 })
);
assert_eq!(
build(
[MAX_TILES, 2],
8.0,
10.0,
flat_ground(8, 0.0),
flat_cover(8, 8),
one_blade()
),
Err(GrassError::Tiles { x: MAX_TILES, z: 2 })
);
assert!(matches!(
build(
[1, 1],
0.0,
10.0,
flat_ground(8, 0.0),
flat_cover(8, 8),
one_blade()
),
Err(GrassError::Metres {
what: "tile side",
..
})
));
assert!(matches!(
build(
[1, 1],
8.0,
f32::NAN,
flat_ground(8, 0.0),
flat_cover(8, 8),
one_blade()
),
Err(GrassError::Metres { what: "reach", .. })
));
let mut short = flat_ground(8, 0.0);
short.heights.pop();
assert!(matches!(
build([1, 1], 8.0, 10.0, short, flat_cover(8, 8), one_blade()),
Err(GrassError::MapSize { what: "ground", .. })
));
let mut nan = flat_ground(8, 0.0);
nan.heights[3] = f32::INFINITY;
assert!(matches!(
build([1, 1], 8.0, 10.0, nan, flat_cover(8, 8), one_blade()),
Err(GrassError::Height { at: 3, .. })
));
let mut scaleless = flat_cover(8, 8);
scaleless.metres_per_texel = 0.0;
assert!(matches!(
build(
[1, 1],
8.0,
10.0,
flat_ground(8, 0.0),
scaleless,
one_blade()
),
Err(GrassError::Metres {
what: "texel scale",
..
})
));
assert_eq!(
build(
[1, 1],
8.0,
10.0,
flat_ground(8, 0.0),
flat_cover(8, 8),
Vec::new()
),
Err(GrassError::Blades { rows: 0 })
);
let mut flat_blade = one_blade();
flat_blade[0].height = 0.0;
assert!(matches!(
build(
[1, 1],
8.0,
10.0,
flat_ground(8, 0.0),
flat_cover(8, 8),
flat_blade
),
Err(GrassError::Blade {
row: 0,
what: "height",
..
})
));
}
#[test]
fn the_tiles_tile_the_field() {
let field = GrassField::new(
[3, 2],
4.0,
[-6.0, 10.0],
50.0,
flat_ground(32, 0.0),
flat_cover(32, 128),
one_blade(),
)
.expect("a real field");
assert_eq!(field.slots(), 6);
assert_eq!(field.tile_origin(0), [-6.0, 10.0]);
assert_eq!(field.tile_origin(2), [2.0, 10.0]);
assert_eq!(field.tile_origin(3), [-6.0, 14.0]);
assert_eq!(field.tile_origin(5), [2.0, 14.0]);
}
#[test]
fn the_ground_answers_its_own_height_and_slope() {
let flat = flat_ground(8, 2.5);
let (height, normal) = flat.under(3.25, 4.75);
assert_eq!(height, 2.5);
assert_eq!(normal, [0.0, 1.0, 0.0]);
let ramp = Heightfield {
texels: [8, 8],
metres_per_texel: 1.0,
origin: [0.0, 0.0],
heights: (0..64).map(|at| (at % 8) as f32).collect(),
};
let (height, normal) = ramp.under(3.5, 2.0);
assert!(
(height - 3.5).abs() < 1e-6,
"the ramp read {height} at x=3.5"
);
let root_half = core::f32::consts::FRAC_1_SQRT_2;
assert!((normal[0] + root_half).abs() < 1e-6, "{normal:?}");
assert!((normal[1] - root_half).abs() < 1e-6, "{normal:?}");
assert!(normal[2].abs() < 1e-6, "{normal:?}");
}
#[test]
fn the_cover_map_reads_one_texel() {
let mut cover = flat_cover(4, 0);
cover.cover[2 + 4] = [200, 1];
for (x, z) in [(2.0, 1.0), (1.51, 0.51), (2.49, 1.49)] {
assert_eq!(cover.under(x, z), [200, 1], "at ({x}, {z})");
}
assert_eq!(cover.under(1.49, 1.0), [0, 0]);
assert_eq!(cover.under(2.0, 1.51), [0, 0]);
assert_eq!(cover.under(-100.0, -100.0), [0, 0]);
assert_eq!(cover.under(100.0, 100.0), [0, 0]);
}
#[test]
fn the_maps_upload_as_the_formats_they_are_bound_as() {
let ground = flat_ground(2, 1.5);
let bytes = ground.to_bytes();
assert_eq!(bytes.len(), 4 * 4);
assert_eq!(f32::from_le_bytes(bytes[4..8].try_into().expect("4")), 1.5);
let cover = flat_cover(2, 77);
let bytes = cover.to_bytes();
assert_eq!(bytes.len(), 4 * 4);
assert_eq!(&bytes[0..4], &[77, 0, 0, 0]);
}
#[test]
fn a_blade_row_is_the_shaders_row() {
let row = one_blade()[0].row();
assert_eq!(row.root_color, [0.05, 0.12, 0.03, 0.0]);
assert_eq!(row.tip_color, [0.35, 0.55, 0.15, 0.0]);
assert_eq!(row.size, [0.4, 0.03, 0.4, 0.3]);
assert_eq!(row.occlusion, [0.0; 4]);
assert_eq!(row.glow, [0.0; 4]);
assert_eq!(row.patch, [0.0; 4]);
assert_eq!(row.flags, [LOOK_CARDS, NORMAL_GROUND, 0, 0]);
let styled = BladeType {
look: BladeLook::Shells,
style: BladeStyle {
root_occlusion: [0.1, 0.2, 0.3],
occlusion_reach: 0.4,
tip_glow: [0.5, 0.6, 0.7],
glow_start: 0.8,
patch_color: [0.9, 1.0, 1.1],
patch_share: 0.25,
normal: BladeNormal::Up,
},
..one_blade()[0]
}
.row();
assert_eq!(styled.occlusion, [0.1, 0.2, 0.3, 0.4]);
assert_eq!(styled.glow, [0.5, 0.6, 0.7, 0.8]);
assert_eq!(styled.patch, [0.9, 1.0, 1.1, 0.25]);
assert_eq!(styled.flags, [LOOK_SHELLS, NORMAL_UP, 0, 0]);
assert_eq!(row.shape, [0.0; 4]);
assert_eq!(row.clump, [0.0; 4]);
let blade = BladeType {
look: BladeLook::Blades,
shape: BladeShape {
tilt: 0.25,
bow: 0.5,
rounding: 0.75,
},
clumping: Clumping {
facing: 0.125,
height: 0.375,
},
..one_blade()[0]
}
.row();
assert_eq!(blade.shape, [0.25, 0.5, 0.75, 0.0]);
assert_eq!(blade.clump, [0.125, 0.375, 0.0, 0.0]);
assert_eq!(blade.flags, [LOOK_BLADES, NORMAL_GROUND, 0, 0]);
}
#[test]
fn a_field_refuses_levers_and_stacks_a_shader_could_not_draw() {
let with_style = |style: BladeStyle| {
GrassField::new(
[1, 1],
8.0,
[0.0, 0.0],
10.0,
flat_ground(8, 0.0),
flat_cover(8, 8),
vec![BladeType {
style,
..one_blade()[0]
}],
)
};
for (style, what) in [
(
BladeStyle {
occlusion_reach: 1.5,
..BladeStyle::PLAIN
},
"occlusion reach",
),
(
BladeStyle {
glow_start: 1.0,
..BladeStyle::PLAIN
},
"glow start",
),
(
BladeStyle {
patch_share: -0.1,
..BladeStyle::PLAIN
},
"patch share",
),
(
BladeStyle {
tip_glow: [0.0, f32::NAN, 0.0],
..BladeStyle::PLAIN
},
"tip glow",
),
] {
assert!(
matches!(with_style(style), Err(GrassError::Blade { row: 0, what: refused, .. }) if refused == what),
"{what} was not refused"
);
}
let field = with_style(BladeStyle::PLAIN).expect("a plain row is a real row");
assert_eq!(field.shells(), Shells::default());
for count in [0, MAX_SHELLS + 1] {
assert_eq!(
field.clone().with_shells(Shells { count, fins: true }),
Err(GrassError::Shells { count })
);
}
let stacked = field
.clone()
.with_shells(Shells {
count: MAX_SHELLS,
fins: false,
})
.expect("the widest stack is one the block holds");
assert_eq!(stacked.shells().count, MAX_SHELLS);
let with_shape = |shape: BladeShape, clumping: Clumping| {
GrassField::new(
[1, 1],
8.0,
[0.0, 0.0],
10.0,
flat_ground(8, 0.0),
flat_cover(8, 8),
vec![BladeType {
shape,
clumping,
..one_blade()[0]
}],
)
};
for (shape, clumping, what) in [
(
BladeShape {
tilt: 1.0,
..BladeShape::STRAIGHT
},
Clumping::NONE,
"tilt",
),
(
BladeShape {
bow: f32::INFINITY,
..BladeShape::STRAIGHT
},
Clumping::NONE,
"bow",
),
(
BladeShape {
rounding: -0.5,
..BladeShape::STRAIGHT
},
Clumping::NONE,
"rounding",
),
(
BladeShape::STRAIGHT,
Clumping {
facing: 1.25,
..Clumping::NONE
},
"clump facing",
),
(
BladeShape::STRAIGHT,
Clumping {
height: 1.0,
..Clumping::NONE
},
"clump height",
),
] {
assert!(
matches!(with_shape(shape, clumping), Err(GrassError::Blade { row: 0, what: refused, .. }) if refused == what),
"{what} was not refused"
);
}
assert_eq!(field.blade_lod(), BladeLod::default());
for (distance, band) in [
(0.0, 0.0),
(f32::NAN, 1.0),
(10.0, -1.0),
(10.0, 10.5),
(10.0, f32::INFINITY),
] {
assert!(
matches!(
field.clone().with_blade_lod(BladeLod { distance, band }),
Err(GrassError::BladeLod { .. })
),
"a switch at {distance} m over {band} m was not refused"
);
}
let hard = field
.with_blade_lod(BladeLod {
distance: 6.0,
band: 0.0,
})
.expect("a hard switch is a switch");
assert_eq!(hard.blade_lod().band, 0.0);
}
#[test]
fn the_stack_stands_as_tall_as_the_tallest_shell_row() {
let mut rows = one_blade();
rows.push(BladeType {
height: 0.9,
look: BladeLook::Cards,
..rows[0]
});
let cards = GrassField::new(
[1, 1],
8.0,
[0.0, 0.0],
10.0,
flat_ground(8, 0.0),
flat_cover(8, 8),
rows.clone(),
)
.expect("a real field");
assert!(!cards.draws_shells());
assert_eq!(cards.shell_stack(), 0.0);
rows[0].look = BladeLook::Shells;
let mixed = GrassField::new(
[1, 1],
8.0,
[0.0, 0.0],
10.0,
flat_ground(8, 0.0),
flat_cover(8, 8),
rows,
)
.expect("a real field");
assert!(mixed.draws_shells());
assert!(!mixed.draws_blades());
assert_eq!(mixed.shell_stack(), 0.4);
}
#[test]
fn the_buffers_are_the_size_the_shaders_index() {
let field = field();
assert_eq!(
field.cell_bytes(),
u64::from(field.slots()) * u64::from(SLOT_CAPACITY) * INSTANCE_STRIDE as u64
);
assert_eq!(field.instance_bytes(), 2 * field.cell_bytes());
assert_eq!(field.blade_bytes(), BLADE_STRIDE as u64);
}
}