Skip to content

Commit

Permalink
Working on pre-rendering framework
Browse files Browse the repository at this point in the history
  • Loading branch information
markusmoenig committed May 30, 2024
1 parent 58e6d4f commit 4c15426
Show file tree
Hide file tree
Showing 12 changed files with 1,353 additions and 23 deletions.
39 changes: 39 additions & 0 deletions Cargo.lock

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

24 changes: 16 additions & 8 deletions creator/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ lazy_static! {
pub static ref TILEFXEDITOR: Mutex<TileFXEditor> = Mutex::new(TileFXEditor::new());
pub static ref MODELFXEDITOR: Mutex<ModelFXEditor> = Mutex::new(ModelFXEditor::new());
pub static ref REGIONFXEDITOR: Mutex<RegionFXEditor> = Mutex::new(RegionFXEditor::new());
pub static ref VOXELTHREAD: Mutex<VoxelThread> = Mutex::new(VoxelThread::default());
// pub static ref VOXELTHREAD: Mutex<VoxelThread> = Mutex::new(VoxelThread::default());
pub static ref PRERENDERTHREAD: Mutex<PreRenderThread> = Mutex::new(PreRenderThread::default());
pub static ref UNDOMANAGER: Mutex<UndoManager> = Mutex::new(UndoManager::default());
}

Expand Down Expand Up @@ -344,8 +345,9 @@ impl TheTrait for Editor {

self.event_receiver = Some(ui.add_state_listener("Main Receiver".into()));

// Startup the voxel render thread.
VOXELTHREAD.lock().unwrap().startup();
// Startup the prerender thread.
//VOXELTHREAD.lock().unwrap().startup();
PRERENDERTHREAD.lock().unwrap().startup();
}

/// Set the command line arguments
Expand Down Expand Up @@ -464,13 +466,14 @@ impl TheTrait for Editor {
}
}

while let Some(VoxelRenderResult::VoxelizedModel(id, key, model)) =
VOXELTHREAD.lock().unwrap().receive()
// Get prerendered results
while let Some(PreRenderResult::RenderedRegion(id, prerendered)) =
PRERENDERTHREAD.lock().unwrap().receive()
{
if let Some(region) = self.project.get_region_mut(&id) {
region.models.insert((key.x, key.y, key.z), model.clone());
region.prerendered = prerendered.clone();
}
self.server.set_voxelized_model(id, key, model);
self.server.set_prerendered(id, prerendered);
redraw = true;
}

Expand Down Expand Up @@ -1040,8 +1043,13 @@ impl TheTrait for Editor {
// r.models.clear();
// }

PRERENDERTHREAD
.lock()
.unwrap()
.set_textures(self.project.extract_tiles());

for region in &mut self.project.regions {
VOXELTHREAD.lock().unwrap().voxelize_region_models(
PRERENDERTHREAD.lock().unwrap().render_region(
region.clone(),
self.project.palette.clone(),
);
Expand Down
8 changes: 7 additions & 1 deletion creator/src/modelfxeditor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,13 @@ impl ModelFXEditor {
self.render_preview(ui, &project.palette);
} else if id.name.starts_with(":MODELFX:") {
if let Some(name) = id.name.strip_prefix(":MODELFX: ") {
let value = value.clone();
let mut value = value.clone();

if let TheValue::Text(_) = &value {
if let Some(v) = value.to_f32() {
value = TheValue::Float(v);
}
}

if !self.material_mode {
if let Some(curr_geo_node) = server_ctx.curr_geo_node {
Expand Down
8 changes: 4 additions & 4 deletions creator/src/sidebar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::editor::{
CODEEDITOR, MODELFXEDITOR, REGIONFXEDITOR, RENDERER, SIDEBARMODE, TILEDRAWER, TILEMAPEDITOR,
VOXELTHREAD,
CODEEDITOR, MODELFXEDITOR, PRERENDERTHREAD, REGIONFXEDITOR, RENDERER, SIDEBARMODE, TILEDRAWER,
TILEMAPEDITOR,
};
use crate::prelude::*;

Expand Down Expand Up @@ -1088,10 +1088,10 @@ impl Sidebar {
let palette = project.palette.clone();
if let Some(region) = project.get_region_mut(&server_ctx.curr_region) {
region.grid_size = v;
VOXELTHREAD
PRERENDERTHREAD
.lock()
.unwrap()
.voxelize_region_models(region.clone(), palette);
.render_region(region.clone(), palette);
server.update_region(region);

if let Some(rgba_layout) = ui.get_rgba_layout("Region Editor") {
Expand Down
25 changes: 25 additions & 0 deletions shared/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,31 @@ impl Camera {
Ray::new(out_origin, normalize(-w))
}

pub fn create_ortho_ray2(&self, uv: Vec2f, screen: Vec2f, tiles: Vec2f, offset: Vec2f) -> Ray {
let pixel_size = Vec2f::new(1.0 / (screen.x * tiles.x), 1.0 / (screen.y * tiles.y));

let cam_origin = self.origin;
let cam_look_at = self.center;

let half_width = tiles.x;
let half_height = tiles.y;

let up_vector = Vec3f::new(0.0, 1.0, 0.0);

let w = normalize(cam_origin - cam_look_at);
let u = cross(up_vector, w);
let v = cross(w, u);

let horizontal = u * half_width * 2.0;
let vertical = v * half_height * 2.0;

let mut out_origin = cam_origin;
out_origin += horizontal * (pixel_size.x * offset.x + uv.x - 0.5);
out_origin += vertical * (pixel_size.y * offset.y + uv.y - 0.5);

Ray::new(out_origin, normalize(-w))
}

pub fn create_ortho_ray_prerendered(&self, uv: Vec2f, prerender: &PrerenderedCamera) -> Ray {
let cam_origin = self.origin;

Expand Down
9 changes: 7 additions & 2 deletions shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ pub mod modelfxnode;
pub mod modelfxstore;
pub mod modelfxterminal;
pub mod patterns;
pub mod prerendered;
pub mod prerenderthread;
pub mod project;
pub mod region;
pub mod regionfx;
Expand All @@ -27,7 +29,7 @@ pub mod tiledrawer;
pub mod tilefx;
pub mod tilemap;
pub mod update;
pub mod voxelthread;
// pub mod voxelthread;
pub mod widget;

pub mod prelude {
Expand All @@ -49,6 +51,8 @@ pub mod prelude {
pub use crate::modelfxstore::ModelFXStore;
pub use crate::modelfxterminal::*;
pub use crate::patterns::*;
pub use crate::prerendered::*;
pub use crate::prerenderthread::*;
pub use crate::project::{MapMode, Project};
pub use crate::region::{CameraMode, CameraType, Layer2DRole, Region, RegionTile};
pub use crate::regionfx::*;
Expand All @@ -63,11 +67,12 @@ pub mod prelude {
pub use crate::tilefx::TileFX;
pub use crate::tilemap::{Tile, TileRole, Tilemap};
pub use crate::update::*;
pub use crate::voxelthread::*;
// pub use crate::voxelthread::*;
pub use crate::widget::*;
pub use crate::ServerMessage;
pub use crate::{do_intersect, Hit, HitFace, Ray, Voxel, AABB2D};
pub use rand::prelude::*;
pub use rstar::*;
}

use theframework::prelude::*;
Expand Down
54 changes: 54 additions & 0 deletions shared/src/prerendered.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//use crate::prelude::*;
use rstar::{PointDistance, RTree, RTreeObject, AABB};
use theframework::prelude::*;

#[derive(Serialize, Deserialize, PartialEq, Clone, Debug)]
pub struct PreRenderedData {
pub location: (f32, f32),
pub pixel_location: (i32, i32),
}

impl PointDistance for PreRenderedData {
// Calculate the squared distance to a point
fn distance_2(&self, point: &[f32; 2]) -> f32 {
let dx = self.location.0 - point[0];
let dy = self.location.1 - point[1];
dx * dx + dy * dy
}

// This optional method improves performance by eliminating objects quickly from consideration
fn contains_point(&self, point: &[f32; 2]) -> bool {
self.location.0 == point[0] && self.location.1 == point[1]
}
}

impl RTreeObject for PreRenderedData {
type Envelope = AABB<[f32; 2]>;

fn envelope(&self) -> Self::Envelope {
AABB::from_point([self.location.0, self.location.1])
}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct PreRendered {
pub albedo: TheRGBABuffer,
pub color: FxHashMap<(i32, i32), RGBA>,
pub tree: RTree<PreRenderedData>,
}

impl Default for PreRendered {
fn default() -> Self {
Self::new()
}
}

impl PreRendered {
pub fn new() -> Self {
Self {
albedo: TheRGBABuffer::default(),
color: FxHashMap::default(),
tree: RTree::new(),
}
}
}
Loading

0 comments on commit 4c15426

Please sign in to comment.