Moved _bake_colors and _bake_liquid_colors into VoxelMesherDefault. Also made the other meshers inherit it.

This commit is contained in:
Relintai 2020-04-06 14:21:46 +02:00
parent c691c13277
commit b9ba0c0bf9
6 changed files with 124 additions and 122 deletions

View File

@ -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<VoxelChunk> p_chunk);

View File

@ -37,6 +37,118 @@ _FORCE_INLINE_ void VoxelMesherDefault::set_build_flags(const int flags) {
}
}
void VoxelMesherDefault::_bake_colors(Ref<VoxelChunk> 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<VoxelChunk> 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");

View File

@ -40,6 +40,9 @@ public:
~VoxelMesherDefault();
protected:
virtual void _bake_colors(Ref<VoxelChunk> p_chunk);
virtual void _bake_liquid_colors(Ref<VoxelChunk> p_chunk);
static void _bind_methods();
private:

View File

@ -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;

View File

@ -564,61 +564,6 @@ void VoxelMesher::bake_colors(Ref<VoxelChunk> chunk) {
if (has_method("_bake_colors"))
call("_bake_colors", chunk);
}
void VoxelMesher::_bake_colors(Ref<VoxelChunk> 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<VoxelChunk> chunk) {
ERR_FAIL_COND(!chunk.is_valid());
@ -626,61 +571,6 @@ void VoxelMesher::bake_liquid_colors(Ref<VoxelChunk> chunk) {
if (has_method("_bake_liquid_colors"))
call("_bake_liquid_colors", chunk);
}
void VoxelMesher::_bake_liquid_colors(Ref<VoxelChunk> 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<Vector3> VoxelMesher::build_collider() const {
PoolVector<Vector3> 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);

View File

@ -121,10 +121,7 @@ public:
void _add_mesher(const Ref<VoxelMesher> &mesher);
void bake_colors(Ref<VoxelChunk> chunk);
void _bake_colors(Ref<VoxelChunk> p_chunk);
void bake_liquid_colors(Ref<VoxelChunk> chunk);
void _bake_liquid_colors(Ref<VoxelChunk> p_chunk);
PoolVector<Vector3> build_collider() const;