Skip to content

Commit

Permalink
Preparing material nodes for geo calls
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Jun 21, 2024
1 parent fbde458 commit 2eb610d
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 127 deletions.
45 changes: 35 additions & 10 deletions shared/src/geofxnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ use theframework::prelude::*;
// return mod(p+s*.5,s)-s*0.5;
// }

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub enum GeoFXNodeExtrusion {
None,
Vertical,
Horizontal,
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub enum GeoFXNodeFacing {
NorthSouth,
WestEast,
}

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub enum GeoFXNodeRole {
Ground,
Expand Down Expand Up @@ -314,6 +327,11 @@ impl GeoFXNode {
// }

fn op_extrusion_y(p: Vec3f, d: f32, h: f32) -> f32 {
let w = Vec2f::new(d, abs(p.y) - h);
min(max(w.x, w.y), 0.0) + length(max(w, Vec2f::zero()))
}

fn op_extrusion_z(p: Vec3f, d: f32, h: f32) -> f32 {
let w = Vec2f::new(d, abs(p.z) - h);
min(max(w.x, w.y), 0.0) + length(max(w, Vec2f::zero()))
}
Expand Down Expand Up @@ -405,32 +423,39 @@ impl GeoFXNode {
return max(-plane, d);
}
BottomWall => {
let thick = coll.get_f32_default("Thickness", 0.2);
let len = coll.get_f32_default("Length", 1.0) / 2.0 + 0.1;
let height = coll.get_f32_default("Height", 1.0);

let pos = self.position();

let mut uv = vec2f(p.x, p.y);
//let mut uv = vec2f(p.x, p.y);
//return p-s*round(p/s);
//uv = uv - 2.0 * round(uv / 2.0);

let d = self.box2d(uv, vec2f(pos.x, height / 2.0), len, height);
let d = self.box2d(vec2f(p.x, p.y), vec2f(pos.x, height / 2.0), len, height);
//let d = length(vec2f(p.x, p.y) - vec2f(pos.x, height / 2.0)) - height / 2.0;

if let Some(hit) = hit {
hit.extrusion = GeoFXNodeExtrusion::Horizontal;
hit.extrusion_length = coll.get_f32_default("Thickness", 0.2);
hit.interior_distance = d;
hit.hit_point = p - vec3f(
0.0,
0.0,
pos.y.floor() + 1.0 - hit.extrusion_length.fract() / 2.0,
);
}

//pos.y = pos.y.floor() + 1.0 - thick.fract() / 2.0;

let d = op_extrusion_y(
p - vec3f(0.0, 0.0, pos.y.floor() + 1.0 - thick.fract() / 2.0),
d,
thick,
);
let plane = dot(p, vec3f(0.0, 1.0, 0.0));
return max(-plane, d);
// let d = op_extrusion_z(
// p - vec3f(0.0, 0.0, pos.y.floor() + 1.0 - thick.fract() / 2.0),
// d,
// thick,
// );
// let plane = dot(p, vec3f(0.0, 1.0, 0.0));
// return max(-plane, d);
return d;
}
BendWallNW => {
let thick = coll.get_f32_default("Thickness", 0.2);
Expand Down
11 changes: 11 additions & 0 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub mod prelude {
pub use rstar::*;
}

use geofxnode::{GeoFXNodeExtrusion, GeoFXNodeFacing};
use theframework::prelude::*;

/// Messages to the clients. The first argument is always the client id.
Expand Down Expand Up @@ -127,6 +128,11 @@ pub struct Hit {
pub key: Vec3f,
pub hash: f32,

pub extrusion: GeoFXNodeExtrusion,
pub extrusion_length: f32,

pub facing: GeoFXNodeFacing,

pub distance: f32,
pub interior_distance: f32,

Expand Down Expand Up @@ -166,6 +172,11 @@ impl Hit {
key: Vec3f::zero(),
hash: 0.0,

extrusion: GeoFXNodeExtrusion::None,
extrusion_length: 0.0,

facing: GeoFXNodeFacing::NorthSouth,

distance: f32::MAX,
interior_distance: f32::MAX,

Expand Down
43 changes: 43 additions & 0 deletions shared/src/materialfxobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,49 @@ impl MaterialFXObject {
}
}

pub fn get_distance(
&self,
time: &TheTime,
p: Vec3f,
hit: &mut Hit,
geo_obj: &GeoFXObject,
) -> (f32, usize) {
let mut d = geo_obj.distance_3d(time, p, &mut Some(hit));

match hit.extrusion {
GeoFXNodeExtrusion::Horizontal => {
fn op_extrusion_z(p: Vec3f, d: f32, h: f32) -> f32 {
let w = Vec2f::new(d, abs(p.z) - h);
min(max(w.x, w.y), 0.0) + length(max(w, Vec2f::zero()))
}

d.0 = op_extrusion_z(hit.hit_point, hit.interior_distance, hit.extrusion_length);
}
_ => {}
}

d
}

/// Calculate normal
pub fn normal(&self, time: &TheTime, p: Vec3f, hit: &mut Hit, geo_obj: &GeoFXObject) -> Vec3f {
let scale = 0.5773 * 0.0005;
let e = vec2f(1.0 * scale, -1.0 * scale);

// IQs normal function

let e1 = vec3f(e.x, e.y, e.y);
let e2 = vec3f(e.y, e.y, e.x);
let e3 = vec3f(e.y, e.x, e.y);
let e4 = vec3f(e.x, e.x, e.x);

let n = e1 * self.get_distance(time, p + e1, hit, geo_obj).0
+ e2 * self.get_distance(time, p + e2, hit, geo_obj).0
+ e3 * self.get_distance(time, p + e3, hit, geo_obj).0
+ e4 * self.get_distance(time, p + e4, hit, geo_obj).0;
normalize(n)
}

/// Computes the displacement if any
pub fn displacement(&self, hit: &mut Hit) {
for (i, node) in self.nodes.iter().enumerate() {
Expand Down
18 changes: 0 additions & 18 deletions shared/src/prerendered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ pub struct PreRendered {

#[serde(default = "default_prerendered_lights")]
pub lights: TheFlattenedMap<Vec<PreRenderedLight>>,
pub tiles_to_render: Vec<Vec2i>,

#[serde(skip)]
pub tree: RTree<PreRenderedData>,
Expand All @@ -73,7 +72,6 @@ impl PreRendered {
albedo,
sky_absorption,

tiles_to_render: Vec::new(),
tree: RTree::new(),

tile_samples: FxHashMap::default(),
Expand All @@ -88,7 +86,6 @@ impl PreRendered {

lights: TheFlattenedMap::new(0, 0),

tiles_to_render: Vec::new(),
tree: RTree::new(),

tile_samples: FxHashMap::default(),
Expand All @@ -109,20 +106,6 @@ impl PreRendered {
self.lights.clear();
}

/// Add all tiles to be rendered.
pub fn add_all_tiles(&mut self, grid_size: i32) {
let mut tiles = Vec::new();
let w = self.albedo.dim().width / grid_size;
let h = self.albedo.dim().height / grid_size;
for x in 0..w {
for y in 0..h {
let tile = Vec2i::new(x, y);
tiles.push(tile);
}
}
self.tiles_to_render = tiles;
}

/// Add the given tiles to be rendered in grid space, we map them to local space.
pub fn remove_tiles(&mut self, tiles: Vec<Vec2i>, grid_size: i32) {
for tile in tiles {
Expand All @@ -135,7 +118,6 @@ impl PreRendered {
for y in tile.y - 2..=tile.y + 2 {
for x in tile.x - 2..=tile.x + 2 {
let t = Vec2i::new(x, y);
println!("remove {:?}", t);
self.tile_samples.remove(&t);
}
}
Expand Down
4 changes: 1 addition & 3 deletions shared/src/prerenderthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,6 @@ impl PreRenderThread {
TheRGBBuffer::new(TheDim::sized(w, h)),
);

prerendered.add_all_tiles(region.grid_size);

background_pool.install(|| {
renderer.prerender_rtree(
&mut prerendered,
Expand Down Expand Up @@ -250,7 +248,7 @@ impl PreRenderThread {
prerendered_regions.insert(curr_region.id, prerendered.clone());
}
std::thread::yield_now();
std::thread::sleep(std::time::Duration::from_millis(100));
//std::thread::sleep(std::time::Duration::from_millis(10));
}

println!("Renderer thread exiting")
Expand Down
Loading

0 comments on commit 2eb610d

Please sign in to comment.