141 lines
4.5 KiB
Rust
141 lines
4.5 KiB
Rust
//! Cloud visibility and flat/fancy cell geometry (`CloudRenderer`).
|
|
/// `LevelRenderer`: skip OFF clouds and colors with no packed alpha.
|
|
pub fn visible(mode: &str, alpha: f32) -> bool {
|
|
mode != "false" && (alpha.clamp(0.0, 1.0) * 255.0) as u8 > 0
|
|
}
|
|
|
|
/// `buildFlatCell`: bottom plane, in both windings for the culled cloud pipeline.
|
|
pub fn flat_cell(x: f32, z: f32, bottom: f32) -> [[[f32; 3]; 4]; 2] {
|
|
let p = [
|
|
[x + 12.0, bottom, z],
|
|
[x + 12.0, bottom, z + 12.0],
|
|
[x, bottom, z + 12.0],
|
|
[x, bottom, z],
|
|
];
|
|
[p, [p[3], p[2], p[1], p[0]]]
|
|
}
|
|
|
|
/// Direction order is DOWN, UP, NORTH, SOUTH, WEST, EAST; inward faces use bits 6..12.
|
|
pub fn face_mask(x: i32, z: i32, bottom: f32, empty: [bool; 4]) -> u16 {
|
|
let mut mask = 0;
|
|
if bottom <= 0.0 {
|
|
mask |= 1 << 1;
|
|
}
|
|
if bottom + 4.0 >= 0.0 {
|
|
mask |= 1;
|
|
}
|
|
for (i, visible) in [z > 0, z < 0, x > 0, x < 0].into_iter().enumerate() {
|
|
if empty[i] && visible {
|
|
mask |= 1 << (i + 2);
|
|
}
|
|
}
|
|
if x.abs() <= 1 && z.abs() <= 1 {
|
|
mask |= 63 << 6;
|
|
}
|
|
mask
|
|
}
|
|
|
|
/// Exterior faces first, then reversed interior faces, in vanilla emission order.
|
|
pub fn faces(mask: u16) -> impl Iterator<Item = (u8, bool)> {
|
|
[1, 0, 2, 3, 4, 5]
|
|
.into_iter()
|
|
.filter(move |d| mask & (1 << d) != 0)
|
|
.map(|d| (d, false))
|
|
.chain(
|
|
(0..6)
|
|
.filter(move |d| mask & (1 << (d + 6)) != 0)
|
|
.map(|d| (d, true)),
|
|
)
|
|
}
|
|
|
|
/// `CloudRenderer.prepare`: the cell radius for the Cloud Distance option
|
|
/// (in chunks), independent of the render distance.
|
|
pub fn radius_cells(cloud_range_chunks: i32) -> i32 {
|
|
((cloud_range_chunks * 16) as f32 / 12.0).ceil() as i32
|
|
}
|
|
|
|
/// CloudRenderer builds cells in expanding Manhattan rings, nearest first.
|
|
pub fn cells(radius: i32) -> impl Iterator<Item = (i32, i32)> {
|
|
(0..=2 * radius).flat_map(move |ring| {
|
|
(-ring..=ring).flat_map(move |x| {
|
|
let z = ring - x.abs();
|
|
let visible = z <= radius && x * x + z * z <= radius * radius;
|
|
(visible && z != 0)
|
|
.then_some((x, -z))
|
|
.into_iter()
|
|
.chain(visible.then_some((x, z)))
|
|
})
|
|
})
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
#[test]
|
|
fn settings_and_packed_alpha_gate_clouds() {
|
|
for mode in ["false", "fast", "true"] {
|
|
assert_eq!(visible(mode, 1.0), mode != "false");
|
|
assert!(!visible(mode, 0.0));
|
|
assert!(!visible(mode, 0.5 / 255.0));
|
|
}
|
|
assert!(visible("fast", 1.0 / 255.0));
|
|
}
|
|
|
|
#[test]
|
|
fn fast_cells_are_flat_at_the_bottom_and_visible_from_both_sides() {
|
|
for bottom in [-10.0, 0.0, 10.0] {
|
|
let [down, up] = flat_cell(-3.0, 7.0, bottom);
|
|
assert_eq!(
|
|
down,
|
|
[
|
|
[9.0, bottom, 7.0],
|
|
[9.0, bottom, 19.0],
|
|
[-3.0, bottom, 19.0],
|
|
[-3.0, bottom, 7.0],
|
|
]
|
|
);
|
|
assert_eq!(up, [down[3], down[2], down[1], down[0]]);
|
|
for (quad, sign) in [(down, -1.0), (up, 1.0)] {
|
|
let [a, b, c] = [quad[0], quad[1], quad[2]].map(glam::Vec3::from_array);
|
|
assert!((b - a).cross(c - a).y * sign > 0.0);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn cells_are_nearest_first_and_unique() {
|
|
let cells: Vec<_> = cells(2).collect();
|
|
assert_eq!(cells[0], (0, 0));
|
|
assert_eq!(cells.len(), 13);
|
|
assert!(
|
|
cells
|
|
.windows(2)
|
|
.all(|w| w[0].0.abs() + w[0].1.abs() <= w[1].0.abs() + w[1].1.abs())
|
|
);
|
|
assert_eq!(
|
|
cells.iter().collect::<std::collections::HashSet<_>>().len(),
|
|
cells.len()
|
|
);
|
|
}
|
|
#[test]
|
|
fn only_camera_facing_exterior_faces() {
|
|
assert_eq!(face_mask(2, 2, 10.0, [true; 4]), 1 | 4 | 16);
|
|
assert_eq!(face_mask(-2, -2, -5.0, [true; 4]), 2 | 8 | 32);
|
|
assert_eq!(face_mask(2, 2, 0.0, [false; 4]), 3);
|
|
}
|
|
#[test]
|
|
fn cloud_distance_sets_the_radius() {
|
|
assert_eq!(radius_cells(2), 3);
|
|
assert_eq!(radius_cells(12), 16);
|
|
assert_eq!(radius_cells(128), 171);
|
|
}
|
|
#[test]
|
|
fn connected_cells_have_no_exterior_walls() {
|
|
assert_eq!(face_mask(2, 2, 10.0, [false; 4]), 1);
|
|
}
|
|
#[test]
|
|
fn nearby_cells_keep_inward_faces_for_camera_inside_clouds() {
|
|
assert_eq!(face_mask(0, 0, 0.0, [false; 4]), 3 | (63 << 6));
|
|
}
|
|
}
|