Skip to content

Commit

Permalink
Integrated new noiselib node
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Apr 4, 2024
1 parent fb049b0 commit e79df5d
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 33 deletions.
45 changes: 42 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion StarterProject.eldiron

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions creator/src/modelfxeditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl ModelFXEditor {
material_button.set_context_menu(Some(TheContextMenu {
items: vec![
TheContextMenuItem::new("Bricks".to_string(), TheId::named("Bricks")),
TheContextMenuItem::new("Noise".to_string(), TheId::named("Noise3D")),
TheContextMenuItem::new("Material".to_string(), TheId::named("Material")),
],
..Default::default()
Expand Down
8 changes: 7 additions & 1 deletion creator/src/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,7 +665,7 @@ impl Sidebar {
rgba_view.set_mode(TheRGBAViewMode::TilePicker);
let mut c = WHITE;
c[3] = 128;
let mut buffer = TheRGBABuffer::new(TheDim::sized(275, 8 * 65));
let mut buffer = TheRGBABuffer::new(TheDim::sized(275, 5 * 65));
buffer.fill([74, 74, 74, 255]);
rgba_view.set_background([74, 74, 74, 255]);
rgba_view.set_buffer(buffer);
Expand Down Expand Up @@ -2343,6 +2343,12 @@ impl Sidebar {
list_layout.add_item(item, ctx);
}
}

MODELFXEDITOR
.lock()
.unwrap()
.redraw_modelfx_library(project, ui, ctx);

ui.select_first_list_item("Region List", ctx);
ui.select_first_list_item("Character List", ctx);
ui.select_first_list_item("Item List", ctx);
Expand Down
1 change: 1 addition & 0 deletions shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ rand = "0.8.5"
fontdue = "0.8.0"
line_drawing = "1.0.0"
indexmap = { version = "2", features = ["serde"], default-features = true }
noiselib = "0.2.3"
106 changes: 92 additions & 14 deletions shared/src/modelfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,17 @@ impl ModelFX {
}
}

/// Remove the preview of the selected node.
/// Remove the preview of the selected node and all connected nodes.
pub fn remove_current_node_preview(&mut self) {
if let Some(selected_node) = self.selected_node {
self.node_previews[selected_node] = None;

// Remove previews of downstream connected nodes
for (src_node_idx, _, dest_node_idx, _) in &self.connections {
if *src_node_idx as usize == selected_node {
self.node_previews[*dest_node_idx as usize] = None;
}
}
}
}

Expand Down Expand Up @@ -218,6 +225,10 @@ impl ModelFX {
1.5 * zoom,
);

if i >= self.node_previews.len() {
self.node_previews.resize(i + 1, None);
}

// Remove preview buffer if size has changed
if let Some(preview_buffer) = &self.node_previews[i] {
if preview_buffer.dim().width != preview_size_scaled
Expand All @@ -233,7 +244,7 @@ impl ModelFX {
preview_size_scaled,
preview_size_scaled,
));
self.render_node_preview(&mut preview_buffer, node, palette);
self.render_node_preview(&mut preview_buffer, i, palette);
self.node_previews[i] = Some(preview_buffer);
}

Expand Down Expand Up @@ -564,6 +575,16 @@ impl ModelFX {
false
}

/// Returns the connected output node for the given input node and terminal.
pub fn find_connected_output_node(&self, node: usize, terminal_index: usize) -> Option<usize> {
for (o, _, i, it) in &self.connections {
if *i == node as u16 && *it == terminal_index as u8 {
return Some(*o as usize);
}
}
None
}

/// After exiting a geometry node follow the trail of material nodes to calculate the final color.
pub fn follow_trail(
&self,
Expand All @@ -584,15 +605,36 @@ impl ModelFX {
0 => {}
1 => {
let o = connections[0].0 as usize;
if let Some(ot) = self.nodes[o].material(&connections[0].1, hit, palette) {

let mut noise = 1.0;
if let Some(noise_index) = self.find_connected_output_node(o, 1) {
if let ModelFXNode::Noise3D(_coll) = &self.nodes[noise_index] {
noise = (self.nodes[noise_index].noise(hit) + 1.0) / 2.0;
hit.uv += 7.23;
let noise2 = (self.nodes[noise_index].noise(hit) + 1.0) / 2.0;
let wobble = vec2f(noise, noise2);
hit.uv -= 7.23;
hit.uv += wobble * 0.5;
}
}

if let Some(ot) = self.nodes[o].material(&connections[0].1, hit, palette, noise) {
self.follow_trail(o, ot as usize, hit, palette);
}
}
_ => {
let index = (hit.hash * connections.len() as f32).floor() as usize;
if let Some(random_connection) = connections.get(index) {
let o = random_connection.0 as usize;
if let Some(ot) = self.nodes[o].material(&random_connection.1, hit, palette) {
let mut noise = 1.0;
if let Some(noise_index) = self.find_connected_output_node(o, 1) {
if let ModelFXNode::Noise3D(_coll) = &self.nodes[noise_index] {
noise = (self.nodes[noise_index].noise(hit) + 1.0) / 2.0;
}
}
if let Some(ot) =
self.nodes[o].material(&random_connection.1, hit, palette, noise)
{
self.follow_trail(o, ot as usize, hit, palette);
}
}
Expand Down Expand Up @@ -701,7 +743,7 @@ impl ModelFX {
pub fn render_node_preview(
&self,
buffer: &mut TheRGBABuffer,
node: &ModelFXNode,
node_index: usize,
palette: &ThePalette,
) {
let width = buffer.dim().width as usize;
Expand Down Expand Up @@ -729,7 +771,8 @@ impl ModelFX {

let mut total = Vec4f::zero();

if node.role() == ModelFXNodeRole::Geometry {
let role = self.nodes[node_index].role();
if role == ModelFXNodeRole::Geometry {
for m in 0..aa {
for n in 0..aa {
let camera_offset =
Expand All @@ -749,7 +792,7 @@ impl ModelFX {
let mut p = ray.at(t);

while t < max_t {
let d = node.distance(p);
let d = self.nodes[node_index].distance(p);
if d < 0.001 {
break;
}
Expand All @@ -760,11 +803,12 @@ impl ModelFX {
if t < max_t {
let mut hit = Hit {
uv: vec2f(xx / width as f32, yy / height as f32),
normal: self.normal_node(p, node),
normal: self.normal_node(p, &self.nodes[node_index]),
..Default::default()
};
let c =
ModelFXColor::create(node.color_index_for_hit(&mut hit).0);
let c = ModelFXColor::create(
self.nodes[node_index].color_index_for_hit(&mut hit).0,
);
color = c.color().to_vec4f();
}

Expand All @@ -777,19 +821,53 @@ impl ModelFX {
total[1] /= aa_aa;
total[2] /= aa_aa;
total[3] /= aa_aa;
} else {
} else if role == ModelFXNodeRole::Material {
// Material node

let mut noise = 1.0;
let mut wobble = Vec2f::zero();

if let Some(noise_index) = self.find_connected_output_node(node_index, 1) {
if let ModelFXNode::Noise3D(_coll) = &self.nodes[noise_index] {
let uv = vec2f(xx / width as f32, yy / height as f32);
let mut hit = Hit {
uv: uv * 2.5,
hit_point: vec3f(uv.x, 0.0, uv.y) * 2.5,
..Default::default()
};
noise = (self.nodes[noise_index].noise(&hit) + 1.0) / 2.0;
hit.uv += 7.23;
let noise2 = (self.nodes[noise_index].noise(&hit) + 1.0) / 2.0;
wobble = vec2f(noise, noise2);
}
}

let mut hit = Hit {
uv: vec2f(xx / width as f32, yy / height as f32) * 2.5,
..Default::default()
};
if let ModelFXNode::Material(_coll) = node {
node.material(&0, &mut hit, palette);
hit.uv += wobble * 0.5;
if let ModelFXNode::Material(_coll) = &self.nodes[node_index] {
self.nodes[node_index].material(&0, &mut hit, palette, noise);
total = hit.color;
} else {
let c = ModelFXColor::create(node.color_index_for_hit(&mut hit).0);
let c = ModelFXColor::create(
self.nodes[node_index].color_index_for_hit(&mut hit).0,
);
total = c.color().to_vec4f();
}
} else if role == ModelFXNodeRole::Noise {
// Noise node
let uv = vec2f(xx / width as f32, yy / height as f32);
let hit = Hit {
uv: uv * 2.5,
hit_point: vec3f(uv.x, 0.0, uv.y) * 2.5,
..Default::default()
};
if let ModelFXNode::Noise3D(_coll) = &self.nodes[node_index] {
let n = (self.nodes[node_index].noise(&hit) + 1.0) / 2.0;
total = vec4f(n, n, n, 1.0);
}
}

pixel.copy_from_slice(&TheColor::from_vec4f(total).to_u8_array());
Expand Down
Loading

0 comments on commit e79df5d

Please sign in to comment.