Began adding building skeleton structure

This commit is contained in:
Joshua Barretto 2020-04-08 20:04:49 +01:00
parent dde0319293
commit 4830757bc9
4 changed files with 68 additions and 1 deletions

View File

@ -25,7 +25,7 @@ vec3 illuminate(vec3 color, vec3 light, vec3 diffuse, vec3 ambience) {
}
float attenuation_strength(vec3 rpos) {
return 1.0 / pow(rpos.x * rpos.x + rpos.y * rpos.y + rpos.z * rpos.z, 0.8);
return 0.3 / pow(rpos.x * rpos.x + rpos.y * rpos.y + rpos.z * rpos.z, 0.5);
}
vec3 light_at(vec3 wpos, vec3 wnorm) {

View File

@ -0,0 +1 @@
mod skeleton;

View File

@ -0,0 +1,64 @@
use vek::*;
#[derive(Copy, Clone)]
pub enum Ori {
East,
North,
}
impl Ori {
pub fn flip(self) -> Self {
match self {
Ori::East => Ori::North,
Ori::North => Ori::East,
}
}
pub fn dir(self) -> Vec2<i32> {
match self {
Ori::East => Vec2::unit_x(),
Ori::North => Vec2::unit_y(),
}
}
}
pub struct Branch {
len: i32,
locus: i32,
children: Vec<(i32, Branch)>,
}
impl Branch {
fn for_each<'a>(&'a self, node: Vec2<i32>, ori: Ori, f: &mut impl FnMut(Vec2<i32>, Ori, &'a Branch)) {
f(node, ori, self);
for (offset, child) in &self.children {
child.for_each(node + ori.dir() * *offset, ori.flip(), f);
}
}
}
pub struct Skeleton {
offset: i32,
ori: Ori,
root: Branch,
}
impl Skeleton {
pub fn for_each<'a>(&'a self, mut f: impl FnMut(Vec2<i32>, Ori, &'a Branch)) {
self.root.for_each(self.ori.dir() * self.offset, self.ori, &mut f);
}
pub fn closest(&self, pos: Vec2<i32>) -> (i32, &Branch) {
let mut min = None;
self.for_each(|node, ori, branch| {
let bounds = Aabr::new_empty(node - ori.flip().dir() * branch.locus)
.expanded_to_contain_point(node + ori.dir() * branch.len + ori.flip().dir() * branch.locus);
let projected = pos.map2(bounds.min.zip(bounds.max), |e, (min, max)| Clamp::clamp(e, min, max));
let dist = (projected - pos).map(|e| e.abs()).reduce_max();
if min.map(|(min_dist, _)| dist < min_dist).unwrap_or(true) {
min = Some((dist, branch));
}
});
min.unwrap()
}
}

View File

@ -1,3 +1,5 @@
mod building;
use crate::{
column::ColumnSample,
sim::{SimChunk, WorldSim},