From 71de54d9a07f3b40d8aa98395bfbc493505ed88c Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 8 Jun 2019 19:47:17 +0200 Subject: [PATCH] Now voxelman can actually make meshes. --- meshers/voxel_mesher.h | 6 ++--- meshers/voxel_mesher_transvoxel.cpp | 41 ++++++++++++++++++++++------- meshers/voxel_mesher_transvoxel.h | 8 +++--- world/voxel_chunk.cpp | 16 +++++++++-- world/voxel_chunk.h | 2 ++ 5 files changed, 55 insertions(+), 18 deletions(-) diff --git a/meshers/voxel_mesher.h b/meshers/voxel_mesher.h index 39b10dd..b80a435 100644 --- a/meshers/voxel_mesher.h +++ b/meshers/voxel_mesher.h @@ -1,7 +1,7 @@ #ifndef VOXEL_TOOLS_H #define VOXEL_TOOLS_H -#include "core/resource.h" +#include "core/reference.h" #include "core/color.h" #include "core/math/vector2.h" #include "core/math/vector3.h" @@ -25,8 +25,8 @@ const double PI = 3.141592653589793238463; class VoxelmanLibrary; class Voxel; -class VoxelMesher : public Resource { - GDCLASS(VoxelMesher, Resource); +class VoxelMesher : public Reference { + GDCLASS(VoxelMesher, Reference); public: Ref get_library() { return _library; } diff --git a/meshers/voxel_mesher_transvoxel.cpp b/meshers/voxel_mesher_transvoxel.cpp index 314b738..222c227 100644 --- a/meshers/voxel_mesher_transvoxel.cpp +++ b/meshers/voxel_mesher_transvoxel.cpp @@ -58,6 +58,11 @@ void TransvoxelTransitionCellData::_bind_methods() { } +Vector3 VoxelMesherTransvoxel::corner_id_to_vertex(int corner_id) const { + ERR_FAIL_COND_V(corner_id < 0 || corner_id > 8, Vector3()); + + return transvoxel_vertices[corner_id]; +} int VoxelMesherTransvoxel::get_regular_cell_class(int index) const { @@ -85,10 +90,16 @@ int VoxelMesherTransvoxel::get_regular_vertex_data_second_vertex(int index1, int return vert2; } -Vector3 VoxelMesherTransvoxel::get_regular_vertex_start_position(int index1, int index2) const { - int vert1 = regularVertexData[index1][index2] & 0x000F; +Vector3 VoxelMesherTransvoxel::get_regular_vertex_first_position(int index1, int index2) const { + int vert = regularVertexData[index1][index2] & 0x000F; - return transvoxel_vertices[vert1]; + return transvoxel_vertices[vert]; +} + +Vector3 VoxelMesherTransvoxel::get_regular_vertex_second_position(int index1, int index2) const { + int vert = (regularVertexData[index1][index2] & 0x00F0) >> 4; + + return transvoxel_vertices[vert]; } Vector3 VoxelMesherTransvoxel::get_regular_vertex_direction(int index1, int index2) const { @@ -130,15 +141,21 @@ int VoxelMesherTransvoxel::get_transition_vertex_data_second_vertex(int index1, return static_cast(vert); } -Vector3 VoxelMesherTransvoxel::get_transition_vertex_start_position(int index1, int index2) const { - int vert1 = regularVertexData[index1][index2] & 0x000F; +Vector3 VoxelMesherTransvoxel::get_transition_vertex_first_position(int index1, int index2) const { + int vert = transitionVertexData[index1][index2] & 0x000F; - return transvoxel_vertices[vert1]; + return transvoxel_vertices[vert]; +} + +Vector3 VoxelMesherTransvoxel::get_transition_vertex_second_position(int index1, int index2) const { + int vert = (transitionVertexData[index1][index2] & 0x00F0) >> 4; + + return transvoxel_vertices[vert]; } Vector3 VoxelMesherTransvoxel::get_transition_vertex_direction(int index1, int index2) const { - int vert1 = regularVertexData[index1][index2] & 0x000F; - int vert2 = (regularVertexData[index1][index2] & 0x00F0) >> 4; + int vert1 = transitionVertexData[index1][index2] & 0x000F; + int vert2 = (transitionVertexData[index1][index2] & 0x00F0) >> 4; return transvoxel_vertices[vert2] - transvoxel_vertices[vert1]; } @@ -160,12 +177,15 @@ VoxelMesherTransvoxel::~VoxelMesherTransvoxel() { } void VoxelMesherTransvoxel::_bind_methods() { + ClassDB::bind_method(D_METHOD("corner_id_to_vertex", "index1"), &VoxelMesherTransvoxel::corner_id_to_vertex); + ClassDB::bind_method(D_METHOD("get_regular_cell_class", "index"), &VoxelMesherTransvoxel::get_regular_cell_class); ClassDB::bind_method(D_METHOD("get_regular_cell_data", "index"), &VoxelMesherTransvoxel::get_regular_cell_data); ClassDB::bind_method(D_METHOD("get_regular_vertex_data", "index1", "index2"), &VoxelMesherTransvoxel::get_regular_vertex_data); ClassDB::bind_method(D_METHOD("get_regular_vertex_data_first_vertex", "index1", "index2"), &VoxelMesherTransvoxel::get_regular_vertex_data_first_vertex); ClassDB::bind_method(D_METHOD("get_regular_vertex_data_second_vertex", "index1", "index2"), &VoxelMesherTransvoxel::get_regular_vertex_data_second_vertex); - ClassDB::bind_method(D_METHOD("get_regular_vertex_start_position", "index1", "index2"), &VoxelMesherTransvoxel::get_regular_vertex_start_position); + ClassDB::bind_method(D_METHOD("get_regular_vertex_first_position", "index1", "index2"), &VoxelMesherTransvoxel::get_regular_vertex_first_position); + ClassDB::bind_method(D_METHOD("get_regular_vertex_second_position", "index1", "index2"), &VoxelMesherTransvoxel::get_regular_vertex_second_position); ClassDB::bind_method(D_METHOD("get_regular_vertex_direction", "index1", "index2"), &VoxelMesherTransvoxel::get_regular_vertex_direction); ClassDB::bind_method(D_METHOD("get_transition_cell_class", "index"), &VoxelMesherTransvoxel::get_transition_cell_class); @@ -174,7 +194,8 @@ void VoxelMesherTransvoxel::_bind_methods() { ClassDB::bind_method(D_METHOD("get_transition_vertex_data", "index1", "index2"), &VoxelMesherTransvoxel::get_transition_vertex_data); ClassDB::bind_method(D_METHOD("get_transition_vertex_data_first_vertex", "index1", "index2"), &VoxelMesherTransvoxel::get_transition_vertex_data_first_vertex); ClassDB::bind_method(D_METHOD("get_transition_vertex_data_second_vertex", "index1", "index2"), &VoxelMesherTransvoxel::get_transition_vertex_data_second_vertex); - ClassDB::bind_method(D_METHOD("get_transition_vertex_start_position", "index1", "index2"), &VoxelMesherTransvoxel::get_transition_vertex_start_position); + ClassDB::bind_method(D_METHOD("get_transition_vertex_first_position", "index1", "index2"), &VoxelMesherTransvoxel::get_transition_vertex_first_position); + ClassDB::bind_method(D_METHOD("get_transition_vertex_second_position", "index1", "index2"), &VoxelMesherTransvoxel::get_transition_vertex_second_position); ClassDB::bind_method(D_METHOD("get_transition_vertex_direction", "index1", "index2"), &VoxelMesherTransvoxel::get_transition_vertex_direction); BIND_ENUM_CONSTANT(VOXEL_ENTRY_INDEX_000); diff --git a/meshers/voxel_mesher_transvoxel.h b/meshers/voxel_mesher_transvoxel.h index 457c3b6..c4a72bb 100644 --- a/meshers/voxel_mesher_transvoxel.h +++ b/meshers/voxel_mesher_transvoxel.h @@ -81,6 +81,7 @@ public: VOXEL_ENTRY_MASK_111 = 1 << 7, }; + Vector3 corner_id_to_vertex(int corner_id) const; int get_regular_cell_class(int index) const; @@ -88,10 +89,10 @@ public: int get_regular_vertex_data(int index10, int index2) const; int get_regular_vertex_data_first_vertex(int index1, int index2) const; int get_regular_vertex_data_second_vertex(int index1, int index2) const; - Vector3 get_regular_vertex_start_position(int index1, int index2) const; + Vector3 get_regular_vertex_first_position(int index1, int index2) const; + Vector3 get_regular_vertex_second_position(int index1, int index2) const; Vector3 get_regular_vertex_direction(int index1, int index2) const; - int get_transition_cell_class(int index) const; Ref get_transition_cell_data(int index) const; @@ -99,7 +100,8 @@ public: int get_transition_vertex_data(int index1, int index2) const; int get_transition_vertex_data_first_vertex(int index1, int index2) const; int get_transition_vertex_data_second_vertex(int index1, int index2) const; - Vector3 get_transition_vertex_start_position(int index1, int index2) const; + Vector3 get_transition_vertex_first_position(int index1, int index2) const; + Vector3 get_transition_vertex_second_position(int index1, int index2) const; Vector3 get_transition_vertex_direction(int index1, int index2) const; VoxelMesherTransvoxel(); diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index c35d51d..c8b1550 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -73,7 +73,12 @@ void VoxelChunk::clear() { void VoxelChunk::build() { ERR_FAIL_COND(!_library.is_valid()); - ERR_FAIL_COND(!_mesher.is_valid()); + + if (!_mesher.is_valid()) { + call("_create_mesher"); + + ERR_FAIL_COND(!_mesher.is_valid()); + } _mesher->set_library(_library); @@ -100,6 +105,10 @@ void VoxelChunk::build() { } } +void VoxelChunk::_create_mesher() { + _mesher = Ref(memnew(VoxelMesher())); +} + void VoxelChunk::finalize_mesh() { _mesher->set_library(_library); @@ -229,7 +238,7 @@ void VoxelChunk::draw_debug_voxels(int max, Color color) { continue; } - draw_cross_voxels(Vector3(x + 0.5, y + 0.5, z + 0.5), _buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_ISOLEVEL) / 255.0); + draw_cross_voxels(Vector3(x, y, z), _buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_ISOLEVEL) / 255.0); ++a; @@ -289,6 +298,9 @@ void VoxelChunk::draw_debug_voxel_lights(int max, bool localPosition) { void VoxelChunk::_bind_methods() { BIND_VMETHOD(MethodInfo("_create_mesh")); + BIND_VMETHOD(MethodInfo("_create_mesher")); + + ClassDB::bind_method(D_METHOD("_create_mesher"), &VoxelChunk::_create_mesher); ClassDB::bind_method(D_METHOD("get_library_path"), &VoxelChunk::get_library_path); ClassDB::bind_method(D_METHOD("set_library_path", "value"), &VoxelChunk::set_library_path); diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index 9930c33..b0015c2 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -53,6 +53,8 @@ public: Ref get_buffer() const; + void _create_mesher(); + void finalize_mesh(); void clear();