diff --git a/meshers/cubic_mesher/voxel_mesher_cubic.h b/meshers/cubic_mesher/voxel_mesher_cubic.h index ab4a2eb..e24382e 100644 --- a/meshers/cubic_mesher/voxel_mesher_cubic.h +++ b/meshers/cubic_mesher/voxel_mesher_cubic.h @@ -27,12 +27,12 @@ SOFTWARE. #include "core/math/vector2.h" #include "core/math/vector3.h" -#include "../voxel_mesher.h" +#include "../default/voxel_mesher_default.h" #include "voxel_cube_points.h" -class VoxelMesherCubic : public VoxelMesher { - GDCLASS(VoxelMesherCubic, VoxelMesher); +class VoxelMesherCubic : public VoxelMesherDefault { + GDCLASS(VoxelMesherCubic, VoxelMesherDefault); public: void _add_chunk(Ref p_chunk); diff --git a/meshers/default/voxel_mesher_default.cpp b/meshers/default/voxel_mesher_default.cpp index 8df18df..30a90c2 100644 --- a/meshers/default/voxel_mesher_default.cpp +++ b/meshers/default/voxel_mesher_default.cpp @@ -37,6 +37,118 @@ _FORCE_INLINE_ void VoxelMesherDefault::set_build_flags(const int flags) { } } +void VoxelMesherDefault::_bake_colors(Ref chunk) { + ERR_FAIL_COND(!chunk.is_valid()); + + if (_vertices.size() == 0) + return; + + Color base_light(_base_light_value, _base_light_value, _base_light_value); + + for (int i = 0; i < _vertices.size(); ++i) { + Vertex vertex = _vertices[i]; + Vector3 vert = vertex.vertex; + + //Is this needed? + if (vert.x < 0 || vert.y < 0 || vert.z < 0) { + 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 (chunk->validate_channel_data_position(x, y, z)) { + Color light = Color( + chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0, + chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0, + chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0); + + float ao = (chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0) * _ao_strength; + float rao = chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0; + + ao += rao; + + light.r += _base_light_value; + light.g += _base_light_value; + light.b += _base_light_value; + + light.r -= ao; + light.g -= ao; + light.b -= ao; + + light.r = CLAMP(light.r, 0, 1.0); + light.g = CLAMP(light.g, 0, 1.0); + light.b = CLAMP(light.b, 0, 1.0); + + Color c = vertex.color; + light.a = c.a; + vertex.color = light; + + _vertices.set(i, vertex); + } else { + vertex.color = base_light; + _vertices.set(i, vertex); + } + } +} + +void VoxelMesherDefault::_bake_liquid_colors(Ref chunk) { + ERR_FAIL_COND(!chunk.is_valid()); + + if (_vertices.size() == 0) + return; + + Color base_light(_base_light_value, _base_light_value, _base_light_value); + + for (int i = 0; i < _vertices.size(); ++i) { + Vertex vertex = _vertices[i]; + Vector3 vert = vertex.vertex; + + //Is this needed? + if (vert.x < 0 || vert.y < 0 || vert.z < 0) { + 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 (chunk->validate_channel_data_position(x, y, z)) { + Color light = Color( + chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0, + chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0, + chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0); + + float ao = (chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0) * _ao_strength; + float rao = chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0; + + ao += rao; + + light.r += _base_light_value; + light.g += _base_light_value; + light.b += _base_light_value; + + light.r -= ao; + light.g -= ao; + light.b -= ao; + + light.r = CLAMP(light.r, 0, 1.0); + light.g = CLAMP(light.g, 0, 1.0); + light.b = CLAMP(light.b, 0, 1.0); + + Color c = vertex.color; + light.a = c.a; + vertex.color = light; + + _vertices.set(i, vertex); + } else { + vertex.color = base_light; + _vertices.set(i, vertex); + } + } +} + VoxelMesherDefault::VoxelMesherDefault() { _build_flags = VoxelChunkDefault::BUILD_FLAG_CREATE_COLLIDER | VoxelChunkDefault::BUILD_FLAG_CREATE_LODS; @@ -47,6 +159,9 @@ VoxelMesherDefault::~VoxelMesherDefault() { } void VoxelMesherDefault::_bind_methods() { + ClassDB::bind_method(D_METHOD("_bake_colors", "chunk"), &VoxelMesherDefault::_bake_colors); + ClassDB::bind_method(D_METHOD("_bake_liquid_colors", "chunk"), &VoxelMesherDefault::_bake_liquid_colors); + ClassDB::bind_method(D_METHOD("get_build_flags"), &VoxelMesherDefault::get_build_flags); ClassDB::bind_method(D_METHOD("set_build_flags", "value"), &VoxelMesherDefault::set_build_flags); ADD_PROPERTY(PropertyInfo(Variant::INT, "build_flags", PROPERTY_HINT_FLAGS, VoxelChunkDefault::BINDING_STRING_BUILD_FLAGS), "set_build_flags", "get_build_flags"); diff --git a/meshers/default/voxel_mesher_default.h b/meshers/default/voxel_mesher_default.h index 083361a..abf7814 100644 --- a/meshers/default/voxel_mesher_default.h +++ b/meshers/default/voxel_mesher_default.h @@ -40,6 +40,9 @@ public: ~VoxelMesherDefault(); protected: + virtual void _bake_colors(Ref p_chunk); + virtual void _bake_liquid_colors(Ref p_chunk); + static void _bind_methods(); private: diff --git a/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h b/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h index 20e495c..5a6257a 100644 --- a/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h +++ b/meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.h @@ -23,7 +23,7 @@ SOFTWARE. #ifndef VOXEL_MESHER_TRANSVOXEL_H #define VOXEL_MESHER_TRANSVOXEL_H -#include "../voxel_mesher.h" +#include "../default/voxel_mesher_default.h" #include "core/reference.h" #include "transvoxel_cell_data.h" @@ -32,8 +32,8 @@ SOFTWARE. using namespace Transvoxel; -class VoxelMesherTransvoxel : public VoxelMesher { - GDCLASS(VoxelMesherTransvoxel, VoxelMesher) +class VoxelMesherTransvoxel : public VoxelMesherDefault { + GDCLASS(VoxelMesherTransvoxel, VoxelMesherDefault) public: static const String BINDING_STRING_VOXEL_ENTRY_INDICES; diff --git a/meshers/voxel_mesher.cpp b/meshers/voxel_mesher.cpp index c2db21f..8759304 100644 --- a/meshers/voxel_mesher.cpp +++ b/meshers/voxel_mesher.cpp @@ -564,61 +564,6 @@ void VoxelMesher::bake_colors(Ref chunk) { if (has_method("_bake_colors")) call("_bake_colors", chunk); } -void VoxelMesher::_bake_colors(Ref chunk) { - ERR_FAIL_COND(!chunk.is_valid()); - - if (_vertices.size() == 0) - return; - - Color base_light(_base_light_value, _base_light_value, _base_light_value); - - for (int i = 0; i < _vertices.size(); ++i) { - Vertex vertex = _vertices[i]; - Vector3 vert = vertex.vertex; - - //Is this needed? - if (vert.x < 0 || vert.y < 0 || vert.z < 0) { - 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 (chunk->validate_channel_data_position(x, y, z)) { - Color light = Color( - chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0, - chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0, - chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0); - - float ao = (chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0) * _ao_strength; - float rao = chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0; - - ao += rao; - - light.r += _base_light_value; - light.g += _base_light_value; - light.b += _base_light_value; - - light.r -= ao; - light.g -= ao; - light.b -= ao; - - light.r = CLAMP(light.r, 0, 1.0); - light.g = CLAMP(light.g, 0, 1.0); - light.b = CLAMP(light.b, 0, 1.0); - - Color c = vertex.color; - light.a = c.a; - vertex.color = light; - - _vertices.set(i, vertex); - } else { - vertex.color = base_light; - _vertices.set(i, vertex); - } - } -} void VoxelMesher::bake_liquid_colors(Ref chunk) { ERR_FAIL_COND(!chunk.is_valid()); @@ -626,61 +571,6 @@ void VoxelMesher::bake_liquid_colors(Ref chunk) { if (has_method("_bake_liquid_colors")) call("_bake_liquid_colors", chunk); } -void VoxelMesher::_bake_liquid_colors(Ref chunk) { - ERR_FAIL_COND(!chunk.is_valid()); - - if (_vertices.size() == 0) - return; - - Color base_light(_base_light_value, _base_light_value, _base_light_value); - - for (int i = 0; i < _vertices.size(); ++i) { - Vertex vertex = _vertices[i]; - Vector3 vert = vertex.vertex; - - //Is this needed? - if (vert.x < 0 || vert.y < 0 || vert.z < 0) { - 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 (chunk->validate_channel_data_position(x, y, z)) { - Color light = Color( - chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0, - chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0, - chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0); - - float ao = (chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0) * _ao_strength; - float rao = chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0; - - ao += rao; - - light.r += _base_light_value; - light.g += _base_light_value; - light.b += _base_light_value; - - light.r -= ao; - light.g -= ao; - light.b -= ao; - - light.r = CLAMP(light.r, 0, 1.0); - light.g = CLAMP(light.g, 0, 1.0); - light.b = CLAMP(light.b, 0, 1.0); - - Color c = vertex.color; - light.a = c.a; - vertex.color = light; - - _vertices.set(i, vertex); - } else { - vertex.color = base_light; - _vertices.set(i, vertex); - } - } -} PoolVector VoxelMesher::build_collider() const { PoolVector face_points; @@ -1082,10 +972,7 @@ void VoxelMesher::_bind_methods() { ClassDB::bind_method(D_METHOD("_add_mesher", "mesher"), &VoxelMesher::_add_mesher); ClassDB::bind_method(D_METHOD("bake_colors", "chunk"), &VoxelMesher::bake_colors); - ClassDB::bind_method(D_METHOD("_bake_colors", "chunk"), &VoxelMesher::_bake_colors); - ClassDB::bind_method(D_METHOD("bake_liquid_colors", "chunk"), &VoxelMesher::bake_liquid_colors); - ClassDB::bind_method(D_METHOD("_bake_liquid_colors", "chunk"), &VoxelMesher::_bake_liquid_colors); ClassDB::bind_method(D_METHOD("get_vertices"), &VoxelMesher::get_vertices); ClassDB::bind_method(D_METHOD("set_vertices", "values"), &VoxelMesher::set_vertices); diff --git a/meshers/voxel_mesher.h b/meshers/voxel_mesher.h index f882ee4..4971de1 100644 --- a/meshers/voxel_mesher.h +++ b/meshers/voxel_mesher.h @@ -121,10 +121,7 @@ public: void _add_mesher(const Ref &mesher); void bake_colors(Ref chunk); - void _bake_colors(Ref p_chunk); - void bake_liquid_colors(Ref chunk); - void _bake_liquid_colors(Ref p_chunk); PoolVector build_collider() const;