Skip to content

Commit

Permalink
Rudimentary node render integration
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed Mar 31, 2024
1 parent a374bcc commit 3a4c875
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 79 deletions.
38 changes: 3 additions & 35 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions creator/src/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ impl Sidebar {
palette_picker.set_color(color.clone());
redraw = true;
project.palette[palette_picker.index()] = Some(color);
server.set_palette(&project.palette);
}
}
} else if id.name == "Screen Aspect Ratio Dropdown" {
Expand Down
11 changes: 6 additions & 5 deletions creator/src/tileeditor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::editor::{
CODEEDITOR, /*MODELFXEDITOR,*/ RENDERER, RENDERMODE, SIDEBARMODE, TILEDRAWER, TILEFXEDITOR,
CODEEDITOR, MODELFXEDITOR, RENDERER, RENDERMODE, SIDEBARMODE, TILEDRAWER, TILEFXEDITOR,
};
use crate::prelude::*;

Expand Down Expand Up @@ -861,8 +861,9 @@ impl TileEditor {

if self.editor_mode == EditorMode::Model {
if let Some(region) = project.get_region_mut(&server_ctx.curr_region) {
//let timeline = MODELFXEDITOR.lock().unwrap().curr_timeline.clone();
//region.models.insert((coord.x, coord.y), timeline);
let timeline = MODELFXEDITOR.lock().unwrap().modelfx.clone();
region.models.insert((coord.x, 0, coord.y), timeline);
server.update_region(region);
RENDERER.lock().unwrap().set_region(region);
}
} else if self.editor_mode == EditorMode::Select {
Expand Down Expand Up @@ -947,8 +948,8 @@ impl TileEditor {
if let Some(tile) = region.tiles.get_mut(&(coord.x, coord.y)) {
tile.tilefx = None;
}
} else if region.models.contains_key(&(coord.x, coord.y)) {
region.models.remove(&(coord.x, coord.y));
} else if region.models.contains_key(&(coord.x, 0, coord.y)) {
region.models.remove(&(coord.x, 0, coord.y));
} else if region.tiles.contains_key(&(coord.x, coord.y)) {
region.tiles.remove(&(coord.x, coord.y));
}
Expand Down
65 changes: 33 additions & 32 deletions shared/src/modelfx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,38 @@ impl ModelFX {
}
}

pub fn render(&self, ray: &Ray, max_distance: f32, palette: &ThePalette) -> Option<Hit> {
let max_t = max_distance * 1.732;
let mut t = 0.0;

let mut hit = Hit::default();
let mut p = ray.at(t);

while t < max_t {
let d = self.distance_hit(p, &mut hit);
if d < 0.001 {
hit.distance = t;
break;
}
t += d;
p = ray.at(t);
}

if t < max_t {
hit.normal = self.normal(p);

let c = dot(hit.normal, normalize(vec3f(1.0, 2.0, 3.0))) * 0.5 + 0.5;
hit.color = vec4f(c, c, c, 1.0);

let terminal_index = self.nodes[hit.node].color_index_for_hit(&hit).1;
self.follow_trail(hit.node, terminal_index as usize, &mut hit, palette);

Some(hit)
} else {
None
}
}

pub fn render_preview(&mut self, buffer: &mut TheRGBABuffer, palette: &ThePalette) {
//}, palette: &ThePalette) {
let width = buffer.dim().width as usize;
Expand Down Expand Up @@ -594,38 +626,7 @@ impl ModelFX {
camera_offset,
);

let max_t = 3.0 * 1.732;
let mut t = 0.0;

let mut p = ray.at(t);
let mut hit = Hit::default();

while t < max_t {
let d = self.distance_hit(p, &mut hit);
if d < 0.001 {
hit.distance = t;
break;
}
t += d;
p = ray.at(t);
}

if t < max_t {
hit.normal = self.normal(p);

let c =
dot(hit.normal, normalize(vec3f(1.0, 2.0, 3.0))) * 0.5 + 0.5;
hit.color = vec4f(c, c, c, 1.0);

let terminal_index =
self.nodes[hit.node].color_index_for_hit(&hit).1;

self.follow_trail(
hit.node,
terminal_index as usize,
&mut hit,
palette,
);
if let Some(hit) = self.render(&ray, 3.0, palette) {
color = hit.color;
}

Expand Down
2 changes: 1 addition & 1 deletion shared/src/modelfxnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl ModelFXNode {
pub fn input_terminals(&self) -> Vec<ModelFXTerminal> {
match self {
Self::Material(_) => {
vec![ModelFXTerminal::new(UV, 0)]
vec![ModelFXTerminal::new(UV, 2)]
}
_ => {
vec![]
Expand Down
2 changes: 1 addition & 1 deletion shared/src/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub struct Region {

#[serde(default)]
#[serde(with = "vectorize")]
pub models: FxHashMap<(i32, i32), ModelFX>,
pub models: FxHashMap<(i32, i32, i32), ModelFX>,

#[serde(default)]
pub areas: FxHashMap<Uuid, Area>,
Expand Down
20 changes: 15 additions & 5 deletions shared/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl Renderer {
width: usize,
height: usize,
compute_delta: bool,
palette: &ThePalette,
) {
let _start = self.get_time();

Expand Down Expand Up @@ -150,6 +151,7 @@ impl Renderer {
&level,
&saturation,
max_render_distance,
palette,
));
}
});
Expand All @@ -170,6 +172,7 @@ impl Renderer {
level: &Level,
saturation: &Option<f32>,
max_render_distance: i32,
palette: &ThePalette,
) -> RGBA {
let mut color = vec4f(0.0, 0.0, 0.0, 1.0);

Expand Down Expand Up @@ -202,15 +205,22 @@ impl Renderer {
break;
}

if let Some(model) = region.models.get(&(key.x, key.z)) {
if let Some(model) = region.models.get(&(key.x, key.y, key.z)) {
let mut lro = ray.at(dist);
lro -= Vec3f::from(key);
//lro *= tile.size as f32;
lro -= rd * 0.01;

let mut r = ray.clone();
r.o = lro;

if let Some(hit_struct) = model.render(&r, 1.01, palette) {
color = hit_struct.color;
hit = true;
//normal = hit_struct.normal;
dist = hit_struct.distance;
break;
}
/*
if let Some(hit_struct) = model.hit(&r) {
if let Some(tile) = self.tiles.get((key.x, key.y, key.z)) {
if let Some(data) = self.textures.get(tile) {
Expand Down Expand Up @@ -264,7 +274,7 @@ impl Renderer {
}
}
}
}
}*/
}
// Test against world tiles
else if let Some(tile) = self.tiles.get((key.x, key.y, key.z)) {
Expand Down Expand Up @@ -917,8 +927,8 @@ impl Renderer {
break;
}

if region.models.get(&(key.x, key.z)).is_some() {
return Some(vec3i(key.x, 0, key.z));
if region.models.get(&(key.x, key.y, key.z)).is_some() {
return Some(vec3i(key.x, key.y, key.z));
}
// Test against world tiles
if self.tiles.get((key.x, key.y, key.z)).is_some() {
Expand Down
7 changes: 7 additions & 0 deletions shared/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,4 +701,11 @@ impl Server {
pub fn get_interactions(&self) -> Vec<Interaction> {
INTERACTIONS.read().unwrap().clone()
}

/// Set an updated palette
pub fn set_palette(&mut self, palette: &ThePalette) {
for instance in self.instances.values_mut() {
instance.palette = palette.clone();
}
}
}
5 changes: 5 additions & 0 deletions shared/src/server/region_instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub struct RegionInstance {

pub draw_settings: RegionDrawSettings,
time: TheTime,

pub palette: ThePalette,
}

impl Default for RegionInstance {
Expand Down Expand Up @@ -83,6 +85,7 @@ impl RegionInstance {
time: TheTime::default(),

daylight: Daylight::default(),
palette: ThePalette::default(),
}
}

Expand All @@ -105,6 +108,7 @@ impl RegionInstance {
self.redraw_ms = 1000 / project.target_fps;

self.draw_settings.delta = self.redraw_ms as f32 / self.tick_ms as f32;
self.palette = project.palette.clone();
}

/// Tick. Compute the next frame.
Expand Down Expand Up @@ -225,6 +229,7 @@ impl RegionInstance {
buffer.dim().width as usize,
buffer.dim().height as usize,
compute_delta,
&self.palette,
);
}
}
Expand Down

0 comments on commit 3a4c875

Please sign in to comment.