Skip to content

Runtime editor mesh resize #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions src/controls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use iced_wgpu::{
Renderer,
};
use iced_winit::{
button, mouse, Background, Button, Color, Column, Command, Container, Element, Length, Point,
Program, Radio, Rectangle, Size, Text,
button, mouse, slider, Background, Button, Color, Column, Command, Container, Element, Length, Point,
Program, Radio, Rectangle, Size, Slider, Text,
};

use std::cell::Cell;
Expand Down Expand Up @@ -38,6 +38,7 @@ pub enum Message {
SavePressed,
LoadPressed,
ColorPicked(Color),
MeshResized(u16)
}

#[derive(Default)]
Expand Down Expand Up @@ -181,6 +182,9 @@ pub struct Controls {
export_file: Cell<Option<String>>,
save_file: Cell<Option<String>>,
load_file: Cell<Option<String>>,
mesh_slider: slider::State,
mesh_size: Cell<u16>,
resize_editor_mesh: Cell<bool>,
width: u16,
}

Expand All @@ -196,6 +200,9 @@ impl Controls {
export_file: Cell::new(None),
save_file: Cell::new(None),
load_file: Cell::new(None),
mesh_slider: slider::State::default(),
mesh_size: Cell::new(32),
resize_editor_mesh: Cell::new(false),
width,
}
}
Expand Down Expand Up @@ -227,6 +234,14 @@ impl Controls {
pub fn load_path(&self) -> Option<String> {
self.load_file.take()
}

pub fn mesh_size(&self) -> u16 {
self.mesh_size.get()
}

pub fn resize_editor_mesh(&self) -> bool {
self.resize_editor_mesh.replace(false)
}
}

impl Program for Controls {
Expand All @@ -252,6 +267,10 @@ impl Program for Controls {
if file_path.is_some() { self.load_file.set( file_path ); }
},
Message::ColorPicked(color) => self.picked_color = PickedColor::new(color),
Message::MeshResized(size) => {
self.mesh_size.set(size);
self.resize_editor_mesh.set(true);
},
};

Command::none()
Expand Down Expand Up @@ -289,6 +308,12 @@ impl Program for Controls {
.push(
Button::new(&mut self.load_button, Text::new("Load from .ron"))
.on_press(Message::LoadPressed),
)
.push(
Slider::new(&mut self.mesh_slider, 32..=128, self.mesh_size.get(), move |s| {
Message::MeshResized(s)
})
.step(8),
);

Container::new(edit_bar)
Expand Down
11 changes: 9 additions & 2 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl Editor {
let mut closest_plane_name = "None";
let mut closest_plane = None;
let mut intersection_point = Vector3::new(0.0, 0.0, 0.0);
let mesh_count = DEFAULT_MESH_COUNT as f32;
let mesh_count = self.renderer.mesh_count() as f32;
if erase_box.is_none() {
for plane in [XY_PLANE, YZ_PLANE, XZ_PLANE].iter() {
if let Some(point) = self.cursor_ray.plane_intersection(plane) {
Expand Down Expand Up @@ -202,6 +202,13 @@ impl Editor {
self.state = EditorState::ChangeView;
}
}

if self.ui.controls().resize_editor_mesh() {
let new_mesh_count = self.ui.controls().mesh_size();
println!("resize_editor_mesh to {}", new_mesh_count);
self.voxel_manager.resize(new_mesh_count as usize);
self.renderer.resize_scene(new_mesh_count);
}
}

fn redraw(&mut self) {
Expand Down Expand Up @@ -363,7 +370,7 @@ impl Editor {
event_loop.run(move |event, _, control_flow| {
if let Some(fps) = fps_counter.get_fps() {
self.window
.set_title(&format!("Voxel-editor (FPS: {:?})", fps));
.set_title(&format!("Voxel-editor (FPS: {0}) {1}x{1}x{1}", fps, self.renderer.mesh_count()));
}
if let Some(file_path) = self.ui.controls().export_path() {
match self.export_vertices(file_path) {
Expand Down
48 changes: 45 additions & 3 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,9 +714,9 @@ impl Renderer {
mesh_count,
light: Light::new(
cgmath::Point3::new(
(DEFAULT_MESH_COUNT / 2) as f32,
DEFAULT_MESH_COUNT as f32 * 1.5,
DEFAULT_MESH_COUNT as f32 * 2.0,
(mesh_count / 2) as f32,
mesh_count as f32 * 1.5,
mesh_count as f32 * 2.0,
),
wgpu::Color {
r: 1.0,
Expand Down Expand Up @@ -748,6 +748,10 @@ impl Renderer {
self.render_mesh = !self.render_mesh
}

pub fn mesh_count(&self) -> u16 {
self.mesh_count
}

fn get_grid_pos(world_pos: cgmath::Vector3<f32>) -> cgmath::Vector3<f32> {
cgmath::Vector3::new(world_pos.x.floor(), world_pos.y.ceil(), world_pos.z.ceil())
}
Expand Down Expand Up @@ -911,6 +915,44 @@ impl Renderer {
);
}

pub fn resize_scene(
&mut self,
new_mesh_count: u16,
) {
self.mesh_count = new_mesh_count;
let (vertex_data, index_data) = generate_mesh_vertices(self.mesh_count);
self.mesh_pipeline.index_count = index_data.len();
let vertex_buf_mesh = self.device.create_buffer_with_data(
bytemuck::cast_slice(&vertex_data),
wgpu::BufferUsage::VERTEX | wgpu::BufferUsage::COPY_DST,
);

let index_buf_mesh = self.device.create_buffer_with_data(
bytemuck::cast_slice(&index_data),
wgpu::BufferUsage::INDEX | wgpu::BufferUsage::COPY_DST,
);
self.mesh_pipeline.vertex_buf = Rc::new(vertex_buf_mesh);
self.mesh_pipeline.index_buf = Rc::new(index_buf_mesh);
self.recreate_light(None);
}

fn recreate_light(&mut self, color: Option<wgpu::Color>) {
self.light = Light::new(
cgmath::Point3::new(
(self.mesh_count / 2) as f32,
self.mesh_count as f32 * 1.5,
self.mesh_count as f32 * 2.0,
),
wgpu::Color {
r: 1.0,
g: 1.0,
b: 1.0,
a: 1.0,
},
);
self.lights_are_dirty = true;
}

pub fn resize(&mut self, size: winit::dpi::PhysicalSize<u32>, camera: &mut CameraWrapper) {
self.sc_desc.width = size.width;
self.sc_desc.height = size.height;
Expand Down
3 changes: 2 additions & 1 deletion src/vertex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ pub fn generate_mesh_vertices(meshes: u16) -> (Vec<Vertex>, Vec<u16>) {
vertex_data.push(vertex([0.0, 0.0, mesh_count], BLUE));
index_data.push((vertex_data.len() - 1) as u16);

for i in 1..(meshes + 1) {
for i in 1..=meshes {
println!("Mesh idx {}", i);
// back
vertex_data.push(white_vertex([0.0, 0.0 + i as f32, 0.0]));
index_data.push((vertex_data.len() - 1) as u16);
Expand Down
19 changes: 19 additions & 0 deletions src/voxel_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,25 @@ impl VoxelManager {
}
}

pub fn resize(&mut self, new_extent: usize) {
let copy_extent;
if new_extent >= self.extent {
copy_extent = self.extent;
} else {
copy_extent = new_extent;
}
println!("Copy extent {}", copy_extent);
let mut voxels = vec![vec![vec![Default::default(); new_extent]; new_extent]; new_extent];
for x in 0..copy_extent {
for y in 0..copy_extent {
for z in 0..copy_extent {
voxels[x][y][z] = self.voxels[x][y][z]
}
}
}
self.voxels = voxels
}

pub fn add_box(&mut self, bbox: BoundingBox) {
let origin: Vector3<usize> = Vector3::new(
bbox.corner.x as usize,
Expand Down