Skip to content
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

Greedy meshing #12

Draft
wants to merge 2 commits into
base: chunkify-and-winter-cleanup
Choose a base branch
from
Draft
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
42 changes: 42 additions & 0 deletions gaiku-3d/examples/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
use std::time::Instant;

use gaiku_3d::common::{Baker, Chunk, FileFormat};

mod exporter;

pub use self::exporter::export;

pub fn bake<T>(name: &str, chunks: Vec<Chunk>, now: Instant, suffix: &str)
where
T: Baker,
{
let mut meshes = vec![];

let reader_elapsed = now.elapsed().as_micros();
let now = Instant::now();

for chunk in chunks.iter() {
let mesh = T::bake(chunk);
if let Some(mesh) = mesh {
meshes.push((mesh, chunk.position()));
}
}

let baker_elapsed = now.elapsed().as_micros();

export(meshes, &format!("{}_{}", name, suffix));

println!(
"\t<<{}>>\n\t\tChunk count: {}\n\t\tReader: {} micros\n\t\tBaker: {} micros",
name,
chunks.len(),
reader_elapsed,
baker_elapsed,
);
}

pub fn read<F, B>(name: &str, suffix: &str)
where
F: FileFormat,
B: Baker,
{
let file = format!("{}/examples/assets/{}", env!("CARGO_MANIFEST_DIR"), name);
bake::<B>(name, F::read(&file), Instant::now(), suffix);
}
55 changes: 6 additions & 49 deletions gaiku-3d/examples/heightmap.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,11 @@
use std::time::Instant;

use gaiku_3d::{
bakers::HeightMapBaker,
common::{Baker, FileFormat},
formats::PNGReader,
};
use gaiku_3d::{bakers::HeightMapBaker, formats::PNGReader};

mod common;

use crate::common::export;

fn read(name: &str) -> std::io::Result<()> {
let now = Instant::now();
let file = format!(
"{}/examples/assets/{}.png",
env!("CARGO_MANIFEST_DIR"),
name
);
let chunks = PNGReader::read(&file);
let mut meshes = vec![];

let reader_elapsed = now.elapsed().as_secs();
let now = Instant::now();

for chunk in chunks.iter() {
let mesh = HeightMapBaker::bake(chunk);
if let Some(mesh) = mesh {
meshes.push((mesh, chunk.position()));
}
}

let baker_elapsed = now.elapsed().as_secs();
let now = Instant::now();

export(meshes, &format!("{}_png", name));

println!(
"<<{}>> Chunks: {} Reader: {} Baker: {} secs Export: {} secs",
name,
chunks.len(),
reader_elapsed,
baker_elapsed,
now.elapsed().as_secs()
);

Ok(())
}

fn main() -> std::io::Result<()> {
let _ = read("heightmap");
use common::read;

Ok(())
fn main() {
println!("\nHeightmap");
println!("---------------------------");
read::<PNGReader, HeightMapBaker>("heightmap.png", "hm");
}
59 changes: 8 additions & 51 deletions gaiku-3d/examples/marching_cubes.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,13 @@
use std::time::Instant;

use gaiku_3d::{
bakers::MarchingCubesBaker,
common::{Baker, FileFormat},
formats::GoxReader,
};
use gaiku_3d::{bakers::MarchingCubesBaker, formats::GoxReader};

mod common;

use crate::common::export;

fn read(name: &str) -> std::io::Result<()> {
let now = Instant::now();
let file = format!(
"{}/examples/assets/{}.gox",
env!("CARGO_MANIFEST_DIR"),
name
);
let chunks = GoxReader::read(&file);
let mut meshes = vec![];

let reader_elapsed = now.elapsed().as_secs();
let now = Instant::now();

for chunk in chunks.iter() {
let mesh = MarchingCubesBaker::bake(chunk);
if let Some(mesh) = mesh {
meshes.push((mesh, chunk.position()));
}
}

let baker_elapsed = now.elapsed().as_secs();
let now = Instant::now();

export(meshes, &format!("{}_mc", name));

println!(
"<<{}>> Chunks: {} Reader: {} Baker: {} secs Export: {} secs",
name,
chunks.len(),
reader_elapsed,
baker_elapsed,
now.elapsed().as_secs()
);

Ok(())
}

fn main() -> std::io::Result<()> {
let _ = read("small_tree");
let _ = read("terrain");
let _ = read("planet");
use common::read;

Ok(())
fn main() {
println!("\nMarching cubes");
println!("---------------------------");
read::<GoxReader, MarchingCubesBaker>("small_tree.gox", "mc");
read::<GoxReader, MarchingCubesBaker>("terrain.gox", "mc");
read::<GoxReader, MarchingCubesBaker>("planet.gox", "mc");
}
83 changes: 44 additions & 39 deletions gaiku-3d/examples/voxel.rs
Original file line number Diff line number Diff line change
@@ -1,56 +1,61 @@
use std::time::Instant;

use gaiku_3d::{
bakers::VoxelBaker,
common::{Baker, FileFormat},
bakers::voxel::{GreedyMeshingBaker, LinearBaker, PyramidBaker},
common::{Baker, Chunk, Chunkify},
formats::GoxReader,
};

mod common;

use crate::common::export;

fn read(name: &str) -> std::io::Result<()> {
let now = Instant::now();
let file = format!(
"{}/examples/assets/{}.gox",
env!("CARGO_MANIFEST_DIR"),
name
);
let chunks = GoxReader::read(&file);
let mut meshes = vec![];

let reader_elapsed = now.elapsed().as_secs();
let now = Instant::now();

for chunk in chunks.iter() {
let mesh = VoxelBaker::bake(chunk);
if let Some(mesh) = mesh {
meshes.push((mesh, chunk.position()));
use common::{bake, read};

fn run<T>(algorithm: &str, suffix: &str)
where
T: Baker,
{
println!("\n{}", algorithm);
println!("---------------------------");
/*
read::<GoxReader, T>("small_tree.gox", suffix);
read::<GoxReader, T>("terrain.gox", suffix);
read::<GoxReader, T>("planet.gox", suffix);
*/
// Cube
let mut chunk = Chunk::new([0.0, 0.0, 0.0], 16, 16, 16);

for x in 0..chunk.width() {
for y in 0..chunk.width() {
for z in 0..chunk.width() {
chunk.set(x, y, z, 255);
}
}
}

let baker_elapsed = now.elapsed().as_secs();
let now = Instant::now();
bake::<T>("cube", vec![chunk], Instant::now(), suffix);

export(meshes, &format!("{}_vx", name));
// Custom chunk
let mut chunk = Chunk::new([0.0, 0.0, 0.0], 16, 16, 16);

println!(
"<<{}>> Chunks: {} Reader: {} Baker: {} secs Export: {} secs",
name,
chunks.len(),
reader_elapsed,
baker_elapsed,
now.elapsed().as_secs()
);
for x in 0..chunk.width() {
for y in 0..chunk.width() {
for z in 0..chunk.width() {
chunk.set(x, y, z, 255);
}
}
}

Ok(())
}
chunk.set(2, 0, 0, 0);
chunk.set(0, 1, 0, 0);
chunk.set(3, 1, 0, 0);
chunk.set(4, 1, 0, 0);
chunk.set(1, 3, 0, 0);

fn main() -> std::io::Result<()> {
let _ = read("small_tree");
let _ = read("terrain");
let _ = read("planet");
bake::<T>("custom chunk", vec![chunk], Instant::now(), suffix);
}

Ok(())
fn main() {
//run::<LinearBaker>("Linear", "vx_l");
run::<GreedyMeshingBaker>("Greedy meshing", "vx_gm");
//run::<PyramidBaker>("Pyramid", "vx_p");
}
4 changes: 2 additions & 2 deletions gaiku-3d/src/bakers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod heightmap;
mod marching_cubes;
mod voxel;
pub mod voxel;

pub use self::{heightmap::HeightMapBaker, marching_cubes::MarchingCubesBaker, voxel::VoxelBaker};
pub use self::{heightmap::HeightMapBaker, marching_cubes::MarchingCubesBaker};
Loading