From 95df18c027e3b57bbfc9aa2c87cd6dff4ec88545 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 17 Apr 2021 12:25:38 +0200 Subject: [PATCH] Added a lod index parameter to the terrarin mesher. --- meshers/blocky/terra_mesher_blocky.cpp | 38 ++++++++++++++++++-------- meshers/blocky/terra_mesher_blocky.h | 4 +++ 2 files changed, 30 insertions(+), 12 deletions(-) diff --git a/meshers/blocky/terra_mesher_blocky.cpp b/meshers/blocky/terra_mesher_blocky.cpp index 9f9a3d7..c5e66a0 100644 --- a/meshers/blocky/terra_mesher_blocky.cpp +++ b/meshers/blocky/terra_mesher_blocky.cpp @@ -31,6 +31,13 @@ void TerraMesherBlocky::set_always_add_colors(const bool value) { _always_add_colors = value; } +int TerraMesherBlocky::get_lod_index() const { + return _lod_index; +} +void TerraMesherBlocky::set_lod_index(const int value) { + _lod_index = value; +} + void TerraMesherBlocky::_add_chunk(Ref p_chunk) { Ref chunk = p_chunk; @@ -81,15 +88,20 @@ void TerraMesherBlocky::_add_chunk(Ref p_chunk) { channel_rao = chunk->channel_get(TerraChunkDefault::DEFAULT_CHANNEL_RANDOM_AO); } - int lod_index = 1; - for (int z = chunk->get_margin_start(); z < z_size + chunk->get_margin_end(); z += lod_index) { - for (int x = chunk->get_margin_start(); x < x_size + chunk->get_margin_end(); x += lod_index) { + int lod_skip = 1; + + if (_lod_index > 0) { + lod_skip = _lod_index * 2; + } + + for (int z = chunk->get_margin_start(); z < z_size + chunk->get_margin_end(); z += lod_skip) { + for (int x = chunk->get_margin_start(); x < x_size + chunk->get_margin_end(); x += lod_skip) { int indexes[4] = { - chunk->get_data_index(x + lod_index, z), + chunk->get_data_index(x + lod_skip, z), chunk->get_data_index(x, z), - chunk->get_data_index(x, z + lod_index), - chunk->get_data_index(x + lod_index, z + lod_index) + chunk->get_data_index(x, z + lod_skip), + chunk->get_data_index(x + lod_skip, z + lod_skip) }; uint8_t type = channel_type[indexes[0]]; @@ -153,16 +165,13 @@ void TerraMesherBlocky::_add_chunk(Ref p_chunk) { surface->transform_uv_scaled(TerraSurface::TERRA_SIDE_TOP, Vector2(1, 1), x % get_texture_scale(), z % get_texture_scale(), get_texture_scale()) }; - - Vector3 verts[] = { - Vector3(x + lod_index, isolevels[0] / 255.0 * world_height, z) * voxel_scale, + Vector3(x + lod_skip, isolevels[0] / 255.0 * world_height, z) * voxel_scale, Vector3(x, isolevels[1] / 255.0 * world_height, z) * voxel_scale, - Vector3(x, isolevels[2] / 255.0 * world_height, z + lod_index) * voxel_scale, - Vector3(x + lod_index, isolevels[3] / 255.0 * world_height, z + lod_index) * voxel_scale + Vector3(x, isolevels[2] / 255.0 * world_height, z + lod_skip) * voxel_scale, + Vector3(x + lod_skip, isolevels[3] / 255.0 * world_height, z + lod_skip) * voxel_scale }; - Vector3 normals[] = { (verts[0] - verts[1]).cross(verts[0] - verts[2]).normalized(), (verts[0] - verts[1]).cross(verts[1] - verts[2]).normalized(), @@ -185,6 +194,7 @@ void TerraMesherBlocky::_add_chunk(Ref p_chunk) { TerraMesherBlocky::TerraMesherBlocky() { _always_add_colors = false; + _lod_index = 0; } TerraMesherBlocky::~TerraMesherBlocky() { @@ -196,4 +206,8 @@ void TerraMesherBlocky::_bind_methods() { ClassDB::bind_method(D_METHOD("get_always_add_colors"), &TerraMesherBlocky::get_always_add_colors); ClassDB::bind_method(D_METHOD("set_always_add_colors", "value"), &TerraMesherBlocky::set_always_add_colors); ADD_PROPERTY(PropertyInfo(Variant::INT, "always_add_colors"), "set_always_add_colors", "get_always_add_colors"); + + ClassDB::bind_method(D_METHOD("get_lod_index"), &TerraMesherBlocky::get_lod_index); + ClassDB::bind_method(D_METHOD("set_lod_index", "value"), &TerraMesherBlocky::set_lod_index); + ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_index"), "set_lod_index", "get_lod_index"); } diff --git a/meshers/blocky/terra_mesher_blocky.h b/meshers/blocky/terra_mesher_blocky.h index 54f5624..7dac952 100644 --- a/meshers/blocky/terra_mesher_blocky.h +++ b/meshers/blocky/terra_mesher_blocky.h @@ -43,6 +43,9 @@ public: bool get_always_add_colors() const; void set_always_add_colors(const bool value); + int get_lod_index() const; + void set_lod_index(const int value); + void _add_chunk(Ref p_chunk); TerraMesherBlocky(); @@ -53,6 +56,7 @@ protected: private: bool _always_add_colors; + int _lod_index; }; #endif