Skip to content

Commit

Permalink
Various optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Jul 1, 2024
1 parent ad1327d commit b45616a
Show file tree
Hide file tree
Showing 8 changed files with 341 additions and 241 deletions.
2 changes: 1 addition & 1 deletion StarterProject.eldiron

Large diffs are not rendered by default.

458 changes: 250 additions & 208 deletions shared/src/geofxnode.rs

Large diffs are not rendered by default.

24 changes: 17 additions & 7 deletions shared/src/geofxobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub struct GeoFXObject {
pub material_id: Uuid,

pub nodes: Vec<GeoFXNode>,

pub area: Vec<Vec2i>,

pub level: i32,
Expand All @@ -33,6 +32,16 @@ impl GeoFXObject {
}
}

/// Loads the parameters of the nodes into memory for faster access.
pub fn load_parameters(&self, time: &TheTime) -> Vec<Vec<f32>> {
let mut data = vec![];

for n in &self.nodes {
data.push(n.load_parameters(time));
}
data
}

/// Returns the distance to the object nodes, the distance and the index of the closes node is returned.
pub fn distance(
&self,
Expand Down Expand Up @@ -60,12 +69,13 @@ impl GeoFXObject {
time: &TheTime,
p: Vec3f,
hit: &mut Option<&mut Hit>,
params: &[Vec<f32>],
) -> (f32, usize) {
let mut min_distance = f32::INFINITY;
let mut index = 10000;

for (i, geo) in self.nodes.iter().enumerate() {
let distance = geo.distance_3d(time, p, hit);
let distance = geo.distance_3d(time, p, hit, &params[i]);
if distance < min_distance {
min_distance = distance;
index = i;
Expand All @@ -75,7 +85,7 @@ impl GeoFXObject {
(min_distance, index)
}

pub fn normal(&self, time: &TheTime, p: Vec3f) -> Vec3f {
pub fn normal(&self, time: &TheTime, p: Vec3f, params: &[Vec<f32>]) -> Vec3f {
let scale = 0.5773 * 0.0005;
let e = vec2f(1.0 * scale, -1.0 * scale);

Expand All @@ -86,10 +96,10 @@ impl GeoFXObject {
let e3 = vec3f(e.y, e.x, e.y);
let e4 = vec3f(e.x, e.x, e.x);

let n = e1 * self.distance_3d(time, p + e1, &mut None).0
+ e2 * self.distance_3d(time, p + e2, &mut None).0
+ e3 * self.distance_3d(time, p + e3, &mut None).0
+ e4 * self.distance_3d(time, p + e4, &mut None).0;
let n = e1 * self.distance_3d(time, p + e1, &mut None, params).0
+ e2 * self.distance_3d(time, p + e2, &mut None, params).0
+ e3 * self.distance_3d(time, p + e3, &mut None, params).0
+ e4 * self.distance_3d(time, p + e4, &mut None, params).0;
normalize(n)
}

Expand Down
21 changes: 16 additions & 5 deletions shared/src/materialfxnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,17 @@ impl MaterialFXNode {
None
}
Noise2D => {
let collection = self.collection();
hit.noise_scale = collection.get_f32_default("Out Scale", 1.0);
let coll = self.collection();
hit.noise_scale = coll.get_f32_default("Out Scale", 1.0);
let scale = vec2f(
coll.get_f32_default("UV Scale X", 1.0),
coll.get_f32_default("UV Scale Y", 1.0),
);
let octaves = coll.get_i32_default("Octaves", 5);
let value = if hit.two_d {
noise2d(&collection, &hit.global_uv)
noise2d(&hit.global_uv, scale, octaves)
} else {
noise2d(&collection, &hit.uv)
noise2d(&hit.uv, scale, octaves)
};
hit.noise = Some(value);
hit.albedo = vec3f(hit.value, hit.value, hit.value);
Expand Down Expand Up @@ -592,7 +597,13 @@ impl MaterialFXNode {
Noise2D => {
let uv = Vec2f::new(xx / width as f32, yy / height as f32);

let value = noise2d(&collection, &uv);
let scale = vec2f(
collection.get_f32_default("UV Scale X", 1.0),
collection.get_f32_default("UV Scale Y", 1.0),
);
let octaves = collection.get_i32_default("Octaves", 5);

let value = noise2d(&uv, scale, octaves);
color = Vec4f::new(value, value, value, 1.0);
}
Noise3D => {
Expand Down
29 changes: 23 additions & 6 deletions shared/src/materialfxobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ impl MaterialFXObject {
p: Vec3f,
hit: &mut Hit,
geo_obj: &GeoFXObject,
geo_obj_params: &[Vec<f32>],
) -> (f32, usize) {
let mut d = geo_obj.distance_3d(time, p, &mut Some(hit));
let mut d = geo_obj.distance_3d(time, p, &mut Some(hit), geo_obj_params);
_ = self.follow_geo_trail(time, hit);

match hit.extrusion {
Expand Down Expand Up @@ -176,7 +177,14 @@ impl MaterialFXObject {
}

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

Expand All @@ -187,10 +195,19 @@ impl MaterialFXObject {
let e3 = vec3f(e.y, e.x, e.y);
let e4 = vec3f(e.x, e.x, e.x);

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

Expand Down
12 changes: 6 additions & 6 deletions shared/src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn subdivide(coll: &TheCollection, uv: Vec2f, hit: &mut Hit) -> u8 {
}
}

pub fn noise2d(coll: &TheCollection, p: &Vec2f) -> f32 {
pub fn noise2d(p: &Vec2f, scale: Vec2f, octaves: i32) -> f32 {
fn hash(p: Vec2f) -> f32 {
let mut p3 = frac(vec3f(p.x, p.y, p.x) * 0.13);
p3 += dot(p3, vec3f(p3.y, p3.z, p3.x) + 3.333);
Expand All @@ -115,11 +115,11 @@ pub fn noise2d(coll: &TheCollection, p: &Vec2f) -> f32 {
lerp(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y
}

let scale = vec2f(
coll.get_f32_default("UV Scale X", 1.0),
coll.get_f32_default("UV Scale Y", 1.0),
);
let octaves = coll.get_i32_default("Octaves", 5);
// let scale = vec2f(
// coll.get_f32_default("UV Scale X", 1.0),
// coll.get_f32_default("UV Scale Y", 1.0),
// );
// let octaves = coll.get_i32_default("Octaves", 5);

let mut x = *p * 8.0 * scale;

Expand Down
3 changes: 1 addition & 2 deletions shared/src/prerenderthread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl PreRenderThread {
// Rendering in progress ?

if in_progress {
let mut reset = false;
//let mut reset = false;

let w = (curr_region.width as f32 * curr_region.grid_size as f32) as i32;
let h = (curr_region.height as f32 * curr_region.grid_size as f32) as i32;
Expand All @@ -246,7 +246,6 @@ impl PreRenderThread {

if let Some(prerendered) = prerendered_region_data.get_mut(&curr_region.id) {
background_pool.install(|| {
println!("working");
in_progress = !renderer.prerender(
vec2i(w, h),
prerendered,
Expand Down
33 changes: 27 additions & 6 deletions shared/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ impl Renderer {
return false;
}

// println!("tiles {}", tiles.len());

let _start = self.get_time();

let width = size.x as usize;
Expand Down Expand Up @@ -745,6 +743,7 @@ impl Renderer {
for geo_id in geo_ids {
let mut h = Hit::default();
if let Some(geo_obj) = region.geometry.get(geo_id) {
let params = geo_obj.load_parameters(&settings.time);
let material = self.materials.get(&geo_obj.material_id);
let lro = ray.at(dist);

Expand All @@ -761,13 +760,20 @@ impl Renderer {

let d; // = (f32::INFINITY, 0);
if let Some(material) = material {
d = material.get_distance_3d(&settings.time, p, &mut h, geo_obj);
d = material.get_distance_3d(
&settings.time,
p,
&mut h,
geo_obj,
&params,
);
} else {
d = MaterialFXObject::default().get_distance_3d(
&settings.time,
p,
&mut h,
geo_obj,
&params,
);
}

Expand All @@ -776,14 +782,20 @@ impl Renderer {
hit.hit_point = p;

if let Some(material) = material {
hit.normal =
material.normal(&settings.time, p, &mut h, geo_obj);
hit.normal = material.normal(
&settings.time,
p,
&mut h,
geo_obj,
&params,
);
} else {
hit.normal = MaterialFXObject::default().normal(
&settings.time,
p,
&mut h,
geo_obj,
&params,
);
}

Expand All @@ -796,6 +808,7 @@ impl Renderer {
&settings.time,
p,
&mut Some(&mut hit),
&params[d.1],
);
}

Expand Down Expand Up @@ -2298,6 +2311,7 @@ impl Renderer {
for geo_id in geo_ids {
let mut h = Hit::default();
if let Some(geo_obj) = region.geometry.get(geo_id) {
let params = geo_obj.load_parameters(&settings.time);
let material = self.materials.get(&geo_obj.material_id);
let lro = ray.at(dist);

Expand All @@ -2314,13 +2328,20 @@ impl Renderer {

let d; // = (f32::INFINITY, 0);
if let Some(material) = material {
d = material.get_distance_3d(&settings.time, p, &mut h, geo_obj);
d = material.get_distance_3d(
&settings.time,
p,
&mut h,
geo_obj,
&params,
);
} else {
d = MaterialFXObject::default().get_distance_3d(
&settings.time,
p,
&mut h,
geo_obj,
&params,
);
}

Expand Down

0 comments on commit b45616a

Please sign in to comment.