From 9b842c159adb6d37dba3acb1e36ffc212aa1f4bf Mon Sep 17 00:00:00 2001 From: Relintai Date: Wed, 24 Jul 2019 02:14:02 +0200 Subject: [PATCH] -Moved the basic mesh color baking into the base VoxelMesher, as well as the 2 light specific parameters. -Added a binding from the AO VoxelBuffer enum value. -Renamed back draw_debug_voxels_colored to draw_debug_voxels. --- meshers/cubic_mesher/voxel_mesher_cubic.cpp | 81 +--------- meshers/cubic_mesher/voxel_mesher_cubic.h | 12 +- meshers/voxel_mesher.cpp | 170 +++++++++++++++----- meshers/voxel_mesher.h | 25 ++- world/voxel_buffer.cpp | 1 + world/voxel_chunk.cpp | 4 +- world/voxel_chunk.h | 2 +- 7 files changed, 153 insertions(+), 142 deletions(-) diff --git a/meshers/cubic_mesher/voxel_mesher_cubic.cpp b/meshers/cubic_mesher/voxel_mesher_cubic.cpp index 10a1669..e6bb1bd 100644 --- a/meshers/cubic_mesher/voxel_mesher_cubic.cpp +++ b/meshers/cubic_mesher/voxel_mesher_cubic.cpp @@ -1,19 +1,5 @@ #include "voxel_mesher_cubic.h" -float VoxelMesherCubic::get_ao_strength() const { - return _ao_strength; -} -void VoxelMesherCubic::set_ao_strength(float value) { - _ao_strength = value; -} - -float VoxelMesherCubic::get_base_light_value() const { - return _base_light_value; -} -void VoxelMesherCubic::set_base_light_value(float value) { - _base_light_value = value; -} - void VoxelMesherCubic::_add_buffer(Ref buffer) { buffer->generate_ao(); @@ -95,64 +81,8 @@ void VoxelMesherCubic::_add_buffer(Ref buffer) { } } -void VoxelMesherCubic::_bake_colors(Ref buffer) { - Color base_light(_base_light_value, _base_light_value, _base_light_value); - - ERR_FAIL_COND(_vertices.size() != _normals.size()); - - /* - if (_vertices.size() != _normals.size()) { - print_error("VoxelMesherCubic: Generating normals!"); - }*/ - - for (int i = 0; i < _vertices.size(); ++i) { - Vector3 vert = _vertices[i]; - - if (vert.x < 0 || vert.y < 0 || vert.z < 0) { - if (_colors.size() < _vertices.size()) { - _colors.push_back(base_light); - } - - continue; - } - - unsigned int x = (unsigned int)(vert.x / _voxel_scale); - unsigned int y = (unsigned int)(vert.y / _voxel_scale); - unsigned int z = (unsigned int)(vert.z / _voxel_scale); - - if (buffer->validate_pos(x, y, z)) { - int ao = buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_AO); - Color light = Color(buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_R) / 255.0, buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_G) / 255.0, buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_B) / 255.0); - Color ao_color(ao, ao, ao); - - light += base_light; - - float NdotL = CLAMP(_normals[i].dot(vert - Vector3(x, y, z)), 0, 1.0); - - light *= NdotL; - - light -= ao_color * _ao_strength; - - light.r = CLAMP(light.r, 0, 1.0); - light.g = CLAMP(light.g, 0, 1.0); - light.b = CLAMP(light.b, 0, 1.0); - - if (_colors.size() < _vertices.size()) { - _colors.push_back(light); - } else { - _colors.set(i, light); - } - } else { - if (_colors.size() < _vertices.size()) { - _colors.push_back(base_light); - } - } - } -} - VoxelMesherCubic::VoxelMesherCubic() { - _ao_strength = 0.25; - _base_light_value = 0.5; + } VoxelMesherCubic::~VoxelMesherCubic() { @@ -160,13 +90,4 @@ VoxelMesherCubic::~VoxelMesherCubic() { void VoxelMesherCubic::_bind_methods() { ClassDB::bind_method(D_METHOD("_add_buffer", "buffer"), &VoxelMesherCubic::_add_buffer); - ClassDB::bind_method(D_METHOD("_bake_colors", "buffer"), &VoxelMesherCubic::_bake_colors); - - ClassDB::bind_method(D_METHOD("get_ao_strength"), &VoxelMesherCubic::get_ao_strength); - ClassDB::bind_method(D_METHOD("set_ao_strength", "value"), &VoxelMesherCubic::set_ao_strength); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "ao_strength"), "set_ao_strength", "get_ao_strength"); - - ClassDB::bind_method(D_METHOD("get_base_light_value"), &VoxelMesherCubic::get_base_light_value); - ClassDB::bind_method(D_METHOD("set_base_light_value", "value"), &VoxelMesherCubic::set_base_light_value); - ADD_PROPERTY(PropertyInfo(Variant::REAL, "base_light_value"), "set_base_light_value", "get_base_light_value"); } diff --git a/meshers/cubic_mesher/voxel_mesher_cubic.h b/meshers/cubic_mesher/voxel_mesher_cubic.h index b904f67..b14afd4 100644 --- a/meshers/cubic_mesher/voxel_mesher_cubic.h +++ b/meshers/cubic_mesher/voxel_mesher_cubic.h @@ -13,24 +13,14 @@ class VoxelMesherCubic : public VoxelMesher { GDCLASS(VoxelMesherCubic, VoxelMesher); public: - float get_ao_strength() const; - void set_ao_strength(float value); - - float get_base_light_value() const; - void set_base_light_value(float value); - void _add_buffer(Ref buffer); - void _bake_colors(Ref buffer); - + VoxelMesherCubic(); ~VoxelMesherCubic(); protected: static void _bind_methods(); -private: - float _ao_strength; - float _base_light_value; }; #endif diff --git a/meshers/voxel_mesher.cpp b/meshers/voxel_mesher.cpp index 1fc572f..1cfdafa 100644 --- a/meshers/voxel_mesher.cpp +++ b/meshers/voxel_mesher.cpp @@ -1,35 +1,38 @@ #include "voxel_mesher.h" -VoxelMesher::VoxelMesher(Ref library) { +Ref VoxelMesher::get_library() { + return _library; +} +void VoxelMesher::set_library(Ref library) { _library = library; - - _voxel_scale = 1; - _lod_size = 1; - - _surface_tool.instance(); } -VoxelMesher::VoxelMesher() { - - _voxel_scale = 1; - _lod_size = 1; - - _surface_tool.instance(); +float VoxelMesher::get_ao_strength() const { + return _ao_strength; +} +void VoxelMesher::set_ao_strength(float value) { + _ao_strength = value; } -VoxelMesher::~VoxelMesher() { - _vertices.clear(); - _normals.clear(); - _colors.clear(); - _uvs.clear(); - _indices.clear(); - _bones.clear(); +float VoxelMesher::get_base_light_value() const { + return _base_light_value; +} +void VoxelMesher::set_base_light_value(float value) { + _base_light_value = value; +} - _surface_tool.unref(); +float VoxelMesher::get_voxel_scale() const { + return _voxel_scale; +} +void VoxelMesher::set_voxel_scale(const float voxel_scale) { + _voxel_scale = voxel_scale; +} - if (_library.is_valid()) { - _library.unref(); - } +int VoxelMesher::get_lod_size() const { + return _lod_size; +} +void VoxelMesher::set_lod_size(const int lod_size) { + _lod_size = lod_size; } void VoxelMesher::build_mesh(RID mesh) { @@ -221,18 +224,59 @@ void VoxelMesher::bake_colors(Ref voxels) { call("_bake_colors", voxels); } -float VoxelMesher::get_voxel_scale() const { - return _voxel_scale; -} -void VoxelMesher::set_voxel_scale(const float voxel_scale) { - _voxel_scale = voxel_scale; -} +void VoxelMesher::_bake_colors(Ref buffer) { + Color base_light(_base_light_value, _base_light_value, _base_light_value); -int VoxelMesher::get_lod_size() const { - return _lod_size; -} -void VoxelMesher::set_lod_size(const int lod_size) { - _lod_size = lod_size; + ERR_FAIL_COND(_vertices.size() != _normals.size()); + + /* + if (_vertices.size() != _normals.size()) { + print_error("VoxelMesherCubic: Generating normals!"); + }*/ + + for (int i = 0; i < _vertices.size(); ++i) { + Vector3 vert = _vertices[i]; + + if (vert.x < 0 || vert.y < 0 || vert.z < 0) { + if (_colors.size() < _vertices.size()) { + _colors.push_back(base_light); + } + + continue; + } + + unsigned int x = (unsigned int)(vert.x / _voxel_scale); + unsigned int y = (unsigned int)(vert.y / _voxel_scale); + unsigned int z = (unsigned int)(vert.z / _voxel_scale); + + if (buffer->validate_pos(x, y, z)) { + int ao = buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_AO); + Color light = Color(buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_R) / 255.0, buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_G) / 255.0, buffer->get_voxel(x, y, z, VoxelBuffer::CHANNEL_LIGHT_COLOR_B) / 255.0); + Color ao_color(ao, ao, ao); + + light += base_light; + + float NdotL = CLAMP(_normals[i].dot(vert - Vector3(x, y, z)), 0, 1.0); + + light *= NdotL; + + light -= ao_color * _ao_strength; + + light.r = CLAMP(light.r, 0, 1.0); + light.g = CLAMP(light.g, 0, 1.0); + light.b = CLAMP(light.b, 0, 1.0); + + if (_colors.size() < _vertices.size()) { + _colors.push_back(light); + } else { + _colors.set(i, light); + } + } else { + if (_colors.size() < _vertices.size()) { + _colors.push_back(base_light); + } + } + } } void VoxelMesher::build_collider(RID shape) const { @@ -458,15 +502,47 @@ void VoxelMesher::remove_indices(int idx) { _indices.remove(idx); } +VoxelMesher::VoxelMesher(Ref library) { + _library = library; + + _voxel_scale = 1; + _lod_size = 1; + + _surface_tool.instance(); +} + +VoxelMesher::VoxelMesher() { + + _voxel_scale = 1; + _lod_size = 1; + _ao_strength = 0.25; + _base_light_value = 0.5; + + _surface_tool.instance(); +} + +VoxelMesher::~VoxelMesher() { + _vertices.clear(); + _normals.clear(); + _colors.clear(); + _uvs.clear(); + _indices.clear(); + _bones.clear(); + + _surface_tool.unref(); + + if (_library.is_valid()) { + _library.unref(); + } +} void VoxelMesher::_bind_methods() { BIND_VMETHOD(MethodInfo("_add_buffer", PropertyInfo(Variant::OBJECT, "buffer", PROPERTY_HINT_RESOURCE_TYPE, "VoxelBuffer"))); BIND_VMETHOD(MethodInfo("_bake_colors", PropertyInfo(Variant::OBJECT, "buffer", PROPERTY_HINT_RESOURCE_TYPE, "VoxelBuffer"))); - ClassDB::bind_method(D_METHOD("add_buffer", "buffer"), &VoxelMesher::add_buffer); - ClassDB::bind_method(D_METHOD("add_mesh_data_resource", "mesh", "position", "rotation", "scale"), &VoxelMesher::add_mesh_data_resource, DEFVAL(Vector3(1.0, 1.0, 1.0)), DEFVAL(Vector3()), DEFVAL(Vector3())); - ClassDB::bind_method(D_METHOD("add_mesh_data_resource_transform", "transform"), &VoxelMesher::add_mesh_data_resource_transform); - ClassDB::bind_method(D_METHOD("bake_colors", "buffer"), &VoxelMesher::bake_colors); + ClassDB::bind_method(D_METHOD("get_library"), &VoxelMesher::get_library); + ClassDB::bind_method(D_METHOD("set_library", "value"), &VoxelMesher::set_library); + ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library"), "set_library", "get_library"); ClassDB::bind_method(D_METHOD("get_voxel_scale"), &VoxelMesher::get_voxel_scale); ClassDB::bind_method(D_METHOD("set_voxel_scale", "value"), &VoxelMesher::set_voxel_scale); @@ -476,9 +552,21 @@ void VoxelMesher::_bind_methods() { ClassDB::bind_method(D_METHOD("set_lod_size", "value"), &VoxelMesher::set_lod_size); ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_size"), "set_lod_size", "get_lod_size"); - ClassDB::bind_method(D_METHOD("get_library"), &VoxelMesher::get_library); - ClassDB::bind_method(D_METHOD("set_library", "value"), &VoxelMesher::set_library); - ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "library"), "set_library", "get_library"); + ClassDB::bind_method(D_METHOD("get_ao_strength"), &VoxelMesher::get_ao_strength); + ClassDB::bind_method(D_METHOD("set_ao_strength", "value"), &VoxelMesher::set_ao_strength); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "ao_strength"), "set_ao_strength", "get_ao_strength"); + + ClassDB::bind_method(D_METHOD("get_base_light_value"), &VoxelMesher::get_base_light_value); + ClassDB::bind_method(D_METHOD("set_base_light_value", "value"), &VoxelMesher::set_base_light_value); + ADD_PROPERTY(PropertyInfo(Variant::REAL, "base_light_value"), "set_base_light_value", "get_base_light_value"); + + + ClassDB::bind_method(D_METHOD("add_buffer", "buffer"), &VoxelMesher::add_buffer); + ClassDB::bind_method(D_METHOD("add_mesh_data_resource", "mesh", "position", "rotation", "scale"), &VoxelMesher::add_mesh_data_resource, DEFVAL(Vector3(1.0, 1.0, 1.0)), DEFVAL(Vector3()), DEFVAL(Vector3())); + ClassDB::bind_method(D_METHOD("add_mesh_data_resource_transform", "transform"), &VoxelMesher::add_mesh_data_resource_transform); + ClassDB::bind_method(D_METHOD("bake_colors", "buffer"), &VoxelMesher::bake_colors); + + ClassDB::bind_method(D_METHOD("_bake_colors", "buffer"), &VoxelMesher::_bake_colors); ClassDB::bind_method(D_METHOD("get_vertex_count"), &VoxelMesher::get_vertex_count); ClassDB::bind_method(D_METHOD("get_vertex", "idx"), &VoxelMesher::get_vertex); diff --git a/meshers/voxel_mesher.h b/meshers/voxel_mesher.h index ff6c70a..2a7db44 100644 --- a/meshers/voxel_mesher.h +++ b/meshers/voxel_mesher.h @@ -30,8 +30,20 @@ class VoxelMesher : public Reference { GDCLASS(VoxelMesher, Reference); public: - Ref get_library() { return _library; } - void set_library(Ref library) { _library = library; } + Ref get_library(); + void set_library(Ref library); + + float get_ao_strength() const; + void set_ao_strength(float value); + + float get_base_light_value() const; + void set_base_light_value(float value); + + float get_voxel_scale() const; + void set_voxel_scale(const float voxel_scale); + + int get_lod_size() const; + void set_lod_size(const int lod_size); void reset(); @@ -40,11 +52,7 @@ public: void add_mesh_data_resource_transform(Ref mesh, const Transform transform); void bake_colors(Ref voxels); - float get_voxel_scale() const; - void set_voxel_scale(const float voxel_scale); - - int get_lod_size() const; - void set_lod_size(const int lod_size); + void _bake_colors(Ref buffer); void build_collider(RID shape) const; @@ -102,6 +110,9 @@ protected: int _lod_size; Ref _surface_tool; + + float _ao_strength; + float _base_light_value; }; #endif diff --git a/world/voxel_buffer.cpp b/world/voxel_buffer.cpp index e03d841..0bdf86c 100644 --- a/world/voxel_buffer.cpp +++ b/world/voxel_buffer.cpp @@ -432,6 +432,7 @@ void VoxelBuffer::_bind_methods() { BIND_ENUM_CONSTANT(CHANNEL_LIGHT_COLOR_R); BIND_ENUM_CONSTANT(CHANNEL_LIGHT_COLOR_G); BIND_ENUM_CONSTANT(CHANNEL_LIGHT_COLOR_B); + BIND_ENUM_CONSTANT(CHANNEL_AO); BIND_ENUM_CONSTANT(CHANNEL_DATA1); BIND_ENUM_CONSTANT(CHANNEL_DATA2); BIND_ENUM_CONSTANT(MAX_CHANNELS); diff --git a/world/voxel_chunk.cpp b/world/voxel_chunk.cpp index d7c0814..352d92d 100644 --- a/world/voxel_chunk.cpp +++ b/world/voxel_chunk.cpp @@ -625,7 +625,7 @@ void VoxelChunk::draw_cross_voxels_fill(Vector3 pos, float fill) { _debug_drawer->add_vertex(pos + Vector3(0.5 * fill, 0, 0)); } -void VoxelChunk::draw_debug_voxels_colored(int max, Color color) { +void VoxelChunk::draw_debug_voxels(int max, Color color) { if (_debug_drawer == NULL) { create_debug_immediate_geometry(); } @@ -846,7 +846,7 @@ void VoxelChunk::_bind_methods() { ClassDB::bind_method(D_METHOD("draw_cross_voxels", "pos"), &VoxelChunk::draw_cross_voxels); ClassDB::bind_method(D_METHOD("draw_cross_voxels_fill", "pos", "fill"), &VoxelChunk::draw_cross_voxels_fill); - ClassDB::bind_method(D_METHOD("draw_debug_voxels_colored", "pos", "color"), &VoxelChunk::draw_debug_voxels_colored, DEFVAL(Color(1, 1, 1))); + ClassDB::bind_method(D_METHOD("draw_debug_voxels", "pos", "color"), &VoxelChunk::draw_debug_voxels, DEFVAL(Color(1, 1, 1))); ClassDB::bind_method(D_METHOD("draw_debug_voxel_lights"), &VoxelChunk::draw_debug_voxel_lights); } diff --git a/world/voxel_chunk.h b/world/voxel_chunk.h index 09b0da5..3c01812 100644 --- a/world/voxel_chunk.h +++ b/world/voxel_chunk.h @@ -133,7 +133,7 @@ public: void draw_cross_voxels(Vector3 pos); void draw_cross_voxels_fill(Vector3 pos, float fill); - void draw_debug_voxels_colored(int max, Color color = Color(1, 1, 1)); + void draw_debug_voxels(int max, Color color = Color(1, 1, 1)); void draw_debug_voxel_lights(); VoxelChunk();