Created a default version from world, and the mesher. Also added a BuildFlags enum to VoxelChunkDefault. Converted a few properties to use this.

This commit is contained in:
Relintai 2020-04-06 13:41:45 +02:00
parent d717c9886b
commit b4c61ac674
17 changed files with 489 additions and 172 deletions

10
SCsub
View File

@ -28,20 +28,24 @@ sources = [
"meshers/transvoxel_uv_mesher/transvoxel_tables.cpp",
"meshers/blocky/voxel_mesher_blocky.cpp",
"meshers/default/voxel_mesher_default.cpp",
"world/voxel_world.cpp",
"world/voxel_chunk.cpp",
"world/voxel_chunk_default.cpp",
"world/voxel_structure.cpp",
"world/block_voxel_structure.cpp",
"world/environment_data.cpp",
"world/voxel_chunk_prop_data.cpp",
"world/blocky/voxel_chunk_blocky.cpp",
"world/blocky/voxel_world_blocky.cpp",
"world/default/voxel_world_default.cpp",
"world/default/voxel_chunk_default.cpp",
"meshers/cubic_mesher/voxel_mesher_cubic.cpp",
"meshers/cubic_mesher/voxel_cube_points.cpp",
"world/blocky/voxel_chunk_blocky.cpp",
"world/blocky/voxel_world_blocky.cpp",
"level_generator/voxelman_level_generator.cpp",

View File

@ -22,14 +22,15 @@ SOFTWARE.
#include "voxel_mesher_blocky.h"
#include "../../world/voxel_chunk_default.h"
#include "../../world/default/voxel_chunk_default.h"
void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
Ref<VoxelChunkDefault> chunk = p_chunk;
ERR_FAIL_COND(!chunk.is_valid());
chunk->generate_ao();
if ((get_build_flags() & VoxelChunkDefault::BUILD_FLAG_GENERATE_AO) != 0)
chunk->generate_ao();
int x_size = chunk->get_size_x();
int y_size = chunk->get_size_y();
@ -38,6 +39,10 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
float voxel_scale = get_voxel_scale();
Color base_light(_base_light_value, _base_light_value, _base_light_value);
Color light;
bool use_lighting = (get_build_flags() & VoxelChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0;
bool use_ao = (get_build_flags() & VoxelChunkDefault::BUILD_FLAG_USE_AO) != 0;
bool use_rao = (get_build_flags() & VoxelChunkDefault::BUILD_FLAG_USE_RAO) != 0;
for (int y = 0; y < y_size; ++y) {
for (int z = 0; z < z_size; ++z) {
@ -64,20 +69,30 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
//x + 1
if (neighbours[0] == 0) {
Color light(chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
if (use_lighting) {
light = Color(chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
float ao = chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
float rao = chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
float ao = 0;
light += base_light;
light -= Color(ao, ao, ao) * _ao_strength;
if (use_ao)
ao = chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
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 (use_rao) {
float rao = chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
}
light += base_light;
if (ao > 0)
light -= Color(ao, ao, ao) * _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);
}
int vc = get_vertex_count();
add_indices(vc + 2);
@ -103,7 +118,10 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
for (int i = 0; i < 4; ++i) {
add_normal(Vector3(1, 0, 0));
add_color(light);
if (use_lighting)
add_color(light);
add_uv(uvs[i]);
add_vertex(verts[i]);
}
@ -111,20 +129,30 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
//x - 1
if (neighbours[1] == 0) {
Color light(chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
if (use_lighting) {
light = Color(chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
float ao = chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
float rao = chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
float ao = 0;
light += base_light;
light -= Color(ao, ao, ao) * _ao_strength;
if (use_ao)
ao = chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
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 (use_rao) {
float rao = chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
}
light += base_light;
if (ao > 0)
light -= Color(ao, ao, ao) * _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);
}
int vc = get_vertex_count();
add_indices(vc + 0);
@ -151,7 +179,10 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
for (int i = 0; i < 4; ++i) {
add_normal(Vector3(-1, 0, 0));
add_color(light);
if (use_lighting)
add_color(light);
add_uv(uvs[i]);
add_vertex(verts[i]);
}
@ -159,16 +190,26 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
//y + 1
if (neighbours[2] == 0) {
Color light(chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
if (use_lighting) {
light = Color(chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
float ao = chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
float rao = chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
float ao = 0;
light += base_light;
light -= Color(ao, ao, ao) * _ao_strength;
if (use_ao)
ao = chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
if (use_rao) {
float rao = chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
}
light += base_light;
if (ao > 0)
light -= Color(ao, ao, ao) * _ao_strength;
}
light.r = CLAMP(light.r, 0, 1.0);
light.g = CLAMP(light.g, 0, 1.0);
@ -198,7 +239,10 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
for (int i = 0; i < 4; ++i) {
add_normal(Vector3(0, 1, 0));
add_color(light);
if (use_lighting)
add_color(light);
add_uv(uvs[i]);
add_vertex(verts[i]);
}
@ -206,16 +250,26 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
//y - 1
if (neighbours[3] == 0) {
Color light(chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
if (use_lighting) {
light = Color(chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
float ao = chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
float rao = chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
float ao = 0;
light += base_light;
light -= Color(ao, ao, ao) * _ao_strength;
if (use_ao)
ao = chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
if (use_rao) {
float rao = chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
}
light += base_light;
if (ao > 0)
light -= Color(ao, ao, ao) * _ao_strength;
}
light.r = CLAMP(light.r, 0, 1.0);
light.g = CLAMP(light.g, 0, 1.0);
@ -246,7 +300,10 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
for (int i = 0; i < 4; ++i) {
add_normal(Vector3(0, -1, 0));
add_color(light);
if (use_lighting)
add_color(light);
add_uv(uvs[i]);
add_vertex(verts[i]);
}
@ -254,20 +311,30 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
//z + 1
if (neighbours[4] == 0) {
Color light(chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
if (use_lighting) {
light = Color(chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
float ao = chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
float rao = chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
float ao = 0;
light += base_light;
light -= Color(ao, ao, ao) * _ao_strength;
if (use_ao)
ao = chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
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 (use_rao) {
float rao = chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
}
light += base_light;
if (ao > 0)
light -= Color(ao, ao, ao) * _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);
}
int vc = get_vertex_count();
add_indices(vc + 2);
@ -293,7 +360,10 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
for (int i = 0; i < 4; ++i) {
add_normal(Vector3(0, 0, 1));
add_color(light);
if (use_lighting)
add_color(light);
add_uv(uvs[i]);
add_vertex(verts[i]);
}
@ -301,20 +371,30 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
//z - 1
if (neighbours[5] == 0) {
Color light(chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
if (use_lighting) {
light = Color(chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_R) / 255.0,
chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_G) / 255.0,
chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_LIGHT_COLOR_B) / 255.0);
float ao = chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
float rao = chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
float ao = 0;
light += base_light;
light -= Color(ao, ao, ao) * _ao_strength;
if (use_ao)
ao = chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_AO) / 255.0;
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 (use_rao) {
float rao = chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO) / 255.0;
ao += rao;
}
light += base_light;
if (ao > 0)
light -= Color(ao, ao, ao) * _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);
}
int vc = get_vertex_count();
add_indices(vc + 0);
@ -341,7 +421,10 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
for (int i = 0; i < 4; ++i) {
add_normal(Vector3(0, 0, -1));
add_color(light);
if (use_lighting)
add_color(light);
add_uv(uvs[i]);
add_vertex(verts[i]);
}
@ -352,7 +435,6 @@ void VoxelMesherBlocky::_add_chunk(Ref<VoxelChunk> p_chunk) {
}
VoxelMesherBlocky::VoxelMesherBlocky() {
_format = VisualServer::ARRAY_FORMAT_NORMAL | VisualServer::ARRAY_FORMAT_COLOR | VisualServer::ARRAY_FORMAT_TEX_UV;
}
VoxelMesherBlocky::~VoxelMesherBlocky() {

View File

@ -27,10 +27,10 @@ SOFTWARE.
#include "core/math/vector2.h"
#include "core/math/vector3.h"
#include "../voxel_mesher.h"
#include "../default/voxel_mesher_default.h"
class VoxelMesherBlocky : public VoxelMesher {
GDCLASS(VoxelMesherBlocky, VoxelMesher);
class VoxelMesherBlocky : public VoxelMesherDefault {
GDCLASS(VoxelMesherBlocky, VoxelMesherDefault);
public:
void _add_chunk(Ref<VoxelChunk> p_chunk);

View File

@ -21,8 +21,8 @@ SOFTWARE.
*/
#include "voxel_cube_points.h"
#include "../../world/default/voxel_chunk_default.h"
#include "../../world/voxel_chunk.h"
#include "../../world/voxel_chunk_default.h"
const unsigned int VoxelCubePoints::index_table[6][4] = {
{ P000, P010, P110, P100 }, //VOXEL_FACE_FRONT 0

View File

@ -22,7 +22,7 @@ SOFTWARE.
#include "voxel_mesher_cubic.h"
#include "../../world/voxel_chunk_default.h"
#include "../../world/default/voxel_chunk_default.h"
void VoxelMesherCubic::_add_chunk(Ref<VoxelChunk> p_chunk) {
Ref<VoxelChunkDefault> chunk = p_chunk;

View File

@ -0,0 +1,53 @@
/*
Copyright (c) 2019-2020 Péter Magyar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "voxel_mesher_default.h"
#include "../../world/default/voxel_chunk_default.h"
_FORCE_INLINE_ int VoxelMesherDefault::get_build_flags() const {
return _build_flags;
}
_FORCE_INLINE_ void VoxelMesherDefault::set_build_flags(const int flags) {
_build_flags = flags;
if ((_build_flags & VoxelChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0) {
_format |= VisualServer::ARRAY_FORMAT_COLOR;
} else {
_format ^= VisualServer::ARRAY_FORMAT_COLOR;
}
}
VoxelMesherDefault::VoxelMesherDefault() {
_build_flags = VoxelChunkDefault::BUILD_FLAG_CREATE_COLLIDER | VoxelChunkDefault::BUILD_FLAG_CREATE_LODS;
_format = VisualServer::ARRAY_FORMAT_NORMAL | VisualServer::ARRAY_FORMAT_TEX_UV;
}
VoxelMesherDefault::~VoxelMesherDefault() {
}
void VoxelMesherDefault::_bind_methods() {
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

@ -0,0 +1,49 @@
/*
Copyright (c) 2019-2020 Péter Magyar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef VOXEL_MESHER_DEFAULT_H
#define VOXEL_MESHER_DEFAULT_H
#include "core/color.h"
#include "core/math/vector2.h"
#include "core/math/vector3.h"
#include "../voxel_mesher.h"
class VoxelMesherDefault : public VoxelMesher {
GDCLASS(VoxelMesherDefault, VoxelMesher);
public:
int get_build_flags() const;
void set_build_flags(const int flags);
VoxelMesherDefault();
~VoxelMesherDefault();
protected:
static void _bind_methods();
private:
int _build_flags;
};
#endif

View File

@ -22,8 +22,8 @@ SOFTWARE.
#include "voxel_mesher_transvoxel.h"
#include "../../world/default/voxel_chunk_default.h"
#include "../../world/voxel_chunk.h"
#include "../../world/voxel_chunk_default.h"
#include "core/array.h"
#include "core/dictionary.h"
#include "servers/visual_server.h"

View File

@ -22,8 +22,8 @@ SOFTWARE.
#include "voxel_mesher.h"
#include "../world/default/voxel_chunk_default.h"
#include "../world/voxel_chunk.h"
#include "../world/voxel_chunk_default.h"
bool VoxelMesher::Vertex::operator==(const Vertex &p_vertex) const {

View File

@ -38,11 +38,13 @@ SOFTWARE.
#include "world/block_voxel_structure.h"
#include "world/environment_data.h"
#include "world/voxel_chunk.h"
#include "world/voxel_chunk_default.h"
#include "world/voxel_chunk_prop_data.h"
#include "world/voxel_structure.h"
#include "world/voxel_world.h"
#include "world/default/voxel_chunk_default.h"
#include "world/default/voxel_world_default.h"
#include "meshers/cubic_mesher/voxel_cube_points.h"
#include "meshers/cubic_mesher/voxel_mesher_cubic.h"
@ -73,12 +75,14 @@ void register_voxelman_types() {
ClassDB::register_class<VoxelWorld>();
ClassDB::register_class<VoxelChunk>();
ClassDB::register_class<VoxelChunkDefault>();
ClassDB::register_class<VoxelStructure>();
ClassDB::register_class<BlockVoxelStructure>();
ClassDB::register_class<EnvironmentData>();
ClassDB::register_class<VoxelChunkPropData>();
ClassDB::register_class<VoxelChunkDefault>();
ClassDB::register_class<VoxelWorldDefault>();
ClassDB::register_class<VoxelMesherCubic>();
ClassDB::register_class<VoxelCubePoints>();

View File

@ -44,6 +44,12 @@ void VoxelChunkBlocky::_create_meshers() {
mesher->set_lod_size(get_lod_size());
mesher->set_voxel_scale(get_voxel_scale());
Ref<VoxelMesherDefault> md = mesher;
if (md.is_valid()) {
md->set_build_flags(get_build_flags());
}
}
}

View File

@ -23,9 +23,7 @@ SOFTWARE.
#ifndef VOXEL_CHUNK_BLOCKY_H
#define VOXEL_CHUNK_BLOCKY_H
#include "../voxel_chunk_default.h"
class VoxelWorld;
#include "../default/voxel_chunk_default.h"
class VoxelChunkBlocky : public VoxelChunkDefault {
GDCLASS(VoxelChunkBlocky, VoxelChunkDefault);

View File

@ -23,10 +23,10 @@ SOFTWARE.
#ifndef VOXEL_WORLD_BLOCKY_H
#define VOXEL_WORLD_BLOCKY_H
#include "../voxel_world.h"
#include "../default/voxel_world_default.h"
class VoxelWorldBlocky : public VoxelWorld {
GDCLASS(VoxelWorldBlocky, VoxelWorld);
class VoxelWorldBlocky : public VoxelWorldDefault {
GDCLASS(VoxelWorldBlocky, VoxelWorldDefault);
public:
VoxelWorldBlocky();

View File

@ -22,19 +22,29 @@ SOFTWARE.
#include "voxel_chunk_default.h"
#include "voxel_world.h"
#include "../voxel_world.h"
#include "../../opensimplex/open_simplex_noise.h"
#include "../../meshers/default/voxel_mesher_default.h"
#include "../../../opensimplex/open_simplex_noise.h"
const String VoxelChunkDefault::BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE = "Normal,Process,Physics Process";
const String VoxelChunkDefault::BINDING_STRING_BUILD_FLAGS = "Use Isolevel,Use Lighting,Use AO,Use RAO,Generate AO,Generate RAO,Bake Lights,Create Collider,Create Lods";
_FORCE_INLINE_ bool VoxelChunkDefault::get_is_build_threaded() const {
return _is_build_threaded;
}
_FORCE_INLINE_ void VoxelChunkDefault::set_is_build_threaded(bool value) {
_FORCE_INLINE_ void VoxelChunkDefault::set_is_build_threaded(const bool value) {
_is_build_threaded = value;
}
_FORCE_INLINE_ int VoxelChunkDefault::get_build_flags() const {
return _build_flags;
}
_FORCE_INLINE_ void VoxelChunkDefault::set_build_flags(const int flags) {
_build_flags = flags;
}
_FORCE_INLINE_ VoxelChunkDefault::ActiveBuildPhaseType VoxelChunkDefault::get_active_build_phase_type() const {
return _active_build_phase_type;
}
@ -60,7 +70,7 @@ _FORCE_INLINE_ void VoxelChunkDefault::set_active_build_phase_type(const VoxelCh
bool VoxelChunkDefault::get_build_phase_done() const {
return _build_phase_done;
}
void VoxelChunkDefault::set_build_phase_done(bool value) {
void VoxelChunkDefault::set_build_phase_done(const bool value) {
_build_phase_done = value;
}
@ -79,39 +89,19 @@ void VoxelChunkDefault::set_lod_size(const int lod_size) {
}
}
int VoxelChunkDefault::get_current_build_phase() {
int VoxelChunkDefault::get_current_build_phase() const {
return _current_build_phase;
}
void VoxelChunkDefault::set_current_build_phase(int value) {
void VoxelChunkDefault::set_current_build_phase(const int value) {
_current_build_phase = value;
}
int VoxelChunkDefault::get_max_build_phase() {
int VoxelChunkDefault::get_max_build_phase() const {
return _max_build_phases;
}
void VoxelChunkDefault::set_max_build_phase(int value) {
void VoxelChunkDefault::set_max_build_phase(const int value) {
_max_build_phases = value;
}
bool VoxelChunkDefault::get_create_collider() const {
return _create_collider;
}
void VoxelChunkDefault::set_create_collider(bool value) {
_create_collider = value;
}
bool VoxelChunkDefault::get_bake_lights() const {
return _bake_lights;
}
void VoxelChunkDefault::set_bake_lights(bool value) {
_bake_lights = value;
}
bool VoxelChunkDefault::get_generate_lod() const {
return _generate_lod;
}
void VoxelChunkDefault::set_generate_lod(const bool value) {
_generate_lod = value;
}
int VoxelChunkDefault::get_lod_num() const {
return _lod_num;
@ -126,7 +116,7 @@ int VoxelChunkDefault::get_current_lod_level() const {
void VoxelChunkDefault::set_current_lod_level(const int value) {
_current_lod_level = value;
if (!_generate_lod)
if ((_build_flags & BUILD_FLAG_CREATE_LODS) == 0)
return;
if (_current_lod_level < 0)
@ -922,9 +912,6 @@ VoxelChunkDefault::VoxelChunkDefault() {
_state = VOXEL_CHUNK_STATE_OK;
_enabled = true;
_build_mesh = true;
_create_collider = true;
_bake_lights = true;
_current_build_phase = BUILD_PHASE_DONE;
_max_build_phases = BUILD_PHASE_MAX;
@ -956,9 +943,10 @@ VoxelChunkDefault::VoxelChunkDefault() {
_active_build_phase_type = BUILD_PHASE_TYPE_NORMAL;
_generate_lod = true;
_lod_num = 3;
_current_lod_level = 0;
_build_flags = BUILD_FLAG_CREATE_COLLIDER | BUILD_FLAG_CREATE_LODS;
}
VoxelChunkDefault::~VoxelChunkDefault() {
@ -1010,7 +998,7 @@ void VoxelChunkDefault::_build_phase(int phase) {
return;
}
case BUILD_PHASE_TERRARIN_MESH_COLLIDER: {
if (!get_create_collider()) {
if ((_build_flags & BUILD_FLAG_CREATE_COLLIDER) == 0) {
next_phase();
return;
}
@ -1102,7 +1090,7 @@ void VoxelChunkDefault::_build_phase(int phase) {
if (_library->get_material(0).is_valid())
VS::get_singleton()->mesh_surface_set_material(mesh_rid, 0, _library->get_material(0)->get_rid());
if (_generate_lod) {
if ((_build_flags & BUILD_FLAG_CREATE_LODS) != 0) {
if (_lod_num >= 1) {
//for lod 1 just remove uv2
temp_mesh_arr[VisualServer::ARRAY_TEX_UV2] = Variant();
@ -1173,9 +1161,23 @@ void VoxelChunkDefault::_build_phase(int phase) {
return;
}
case BUILD_PHASE_LIGHTS: {
clear_baked_lights();
generate_random_ao(123);
bake_lights();
bool gr = (_build_flags & BUILD_FLAG_AUTO_GENERATE_RAO) != 0;
if (!gr && (_build_flags & BUILD_FLAG_USE_LIGHTING) == 0) {
next_phase();
return;
}
bool bl = (_build_flags & BUILD_FLAG_BAKE_LIGHTS) != 0;
if (bl)
clear_baked_lights();
if (gr)
generate_random_ao(_voxel_world->get_current_seed());
if (bl)
bake_lights();
next_phase();
@ -1280,8 +1282,6 @@ void VoxelChunkDefault::_clear_baked_lights() {
}
void VoxelChunkDefault::_create_meshers() {
add_mesher(Ref<VoxelMesher>(memnew(VoxelMesherCubic())));
for (int i = 0; i < _meshers.size(); ++i) {
Ref<VoxelMesher> mesher = _meshers.get(i);
@ -1289,6 +1289,12 @@ void VoxelChunkDefault::_create_meshers() {
mesher->set_lod_size(get_lod_size());
mesher->set_voxel_scale(get_voxel_scale());
Ref<VoxelMesherDefault> md = mesher;
if (md.is_valid()) {
md->set_build_flags(get_build_flags());
}
}
}
@ -1309,6 +1315,10 @@ void VoxelChunkDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_is_build_threaded", "value"), &VoxelChunkDefault::set_is_build_threaded);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_build_threaded"), "set_is_build_threaded", "get_is_build_threaded");
ClassDB::bind_method(D_METHOD("get_build_flags"), &VoxelChunkDefault::get_build_flags);
ClassDB::bind_method(D_METHOD("set_build_flags", "value"), &VoxelChunkDefault::set_build_flags);
ADD_PROPERTY(PropertyInfo(Variant::INT, "build_flags", PROPERTY_HINT_FLAGS, BINDING_STRING_BUILD_FLAGS), "set_build_flags", "get_build_flags");
ClassDB::bind_method(D_METHOD("get_active_build_phase_type"), &VoxelChunkDefault::get_active_build_phase_type);
ClassDB::bind_method(D_METHOD("set_active_build_phase_type", "value"), &VoxelChunkDefault::set_active_build_phase_type);
ADD_PROPERTY(PropertyInfo(Variant::INT, "active_build_phase_type", PROPERTY_HINT_ENUM, BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE), "set_active_build_phase_type", "get_active_build_phase_type");
@ -1325,19 +1335,6 @@ void VoxelChunkDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_max_build_phase", "value"), &VoxelChunkDefault::set_max_build_phase);
ADD_PROPERTY(PropertyInfo(Variant::INT, "max_build_phase"), "set_max_build_phase", "get_max_build_phase");
ADD_GROUP("Meshing", "meshing");
ClassDB::bind_method(D_METHOD("meshing_get_create_collider"), &VoxelChunkDefault::get_create_collider);
ClassDB::bind_method(D_METHOD("meshing_set_create_collider", "value"), &VoxelChunkDefault::set_create_collider);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meshing_create_collider"), "meshing_set_create_collider", "meshing_get_create_collider");
ClassDB::bind_method(D_METHOD("meshing_get_bake_lights"), &VoxelChunkDefault::get_bake_lights);
ClassDB::bind_method(D_METHOD("meshing_set_bake_lights", "value"), &VoxelChunkDefault::set_bake_lights);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "meshing_bake_lights"), "meshing_set_bake_lights", "meshing_get_bake_lights");
ClassDB::bind_method(D_METHOD("get_generate_lod"), &VoxelChunkDefault::get_generate_lod);
ClassDB::bind_method(D_METHOD("set_generate_lod"), &VoxelChunkDefault::set_generate_lod);
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_lod"), "set_generate_lod", "get_generate_lod");
ClassDB::bind_method(D_METHOD("get_lod_num"), &VoxelChunkDefault::get_lod_num);
ClassDB::bind_method(D_METHOD("set_lod_num"), &VoxelChunkDefault::set_lod_num);
ADD_PROPERTY(PropertyInfo(Variant::INT, "lod_num"), "set_lod_num", "get_lod_num");
@ -1465,4 +1462,14 @@ void VoxelChunkDefault::_bind_methods() {
BIND_CONSTANT(MESH_TYPE_INDEX_MESH_INSTANCE);
BIND_CONSTANT(MESH_TYPE_INDEX_SHAPE);
BIND_CONSTANT(MESH_TYPE_INDEX_BODY);
BIND_ENUM_CONSTANT(BUILD_FLAG_USE_ISOLEVEL);
BIND_ENUM_CONSTANT(BUILD_FLAG_USE_LIGHTING);
BIND_ENUM_CONSTANT(BUILD_FLAG_USE_AO);
BIND_ENUM_CONSTANT(BUILD_FLAG_USE_RAO);
BIND_ENUM_CONSTANT(BUILD_FLAG_GENERATE_AO);
BIND_ENUM_CONSTANT(BUILD_FLAG_AUTO_GENERATE_RAO);
BIND_ENUM_CONSTANT(BUILD_FLAG_BAKE_LIGHTS);
BIND_ENUM_CONSTANT(BUILD_FLAG_CREATE_COLLIDER);
BIND_ENUM_CONSTANT(BUILD_FLAG_CREATE_LODS);
}

View File

@ -23,7 +23,7 @@ SOFTWARE.
#ifndef VOXEL_CHUNK_DEFAULT_H
#define VOXEL_CHUNK_DEFAULT_H
#include "voxel_chunk.h"
#include "../voxel_chunk.h"
#include "scene/3d/spatial.h"
@ -42,18 +42,18 @@ SOFTWARE.
#include "scene/resources/concave_polygon_shape.h"
#include "scene/resources/packed_scene.h"
#include "voxel_world.h"
#include "../voxel_world.h"
#include "../data/voxel_light.h"
#include "../../data/voxel_light.h"
#include "../meshers/cubic_mesher/voxel_mesher_cubic.h"
#include "../meshers/voxel_mesher.h"
#include "../../meshers/cubic_mesher/voxel_mesher_cubic.h"
#include "../../meshers/voxel_mesher.h"
#include "../library/voxel_surface.h"
#include "../library/voxelman_library.h"
#include "../../library/voxel_surface.h"
#include "../../library/voxelman_library.h"
#include "../../mesh_data_resource/mesh_data_resource.h"
#include "voxel_chunk_prop_data.h"
#include "../../../mesh_data_resource/mesh_data_resource.h"
#include "../voxel_chunk_prop_data.h"
class VoxelWorld;
@ -64,6 +64,7 @@ class VoxelChunkDefault : public VoxelChunk {
public:
static const String BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE;
static const String BINDING_STRING_BUILD_FLAGS;
enum {
VOXEL_CHUNK_STATE_GENERATION_QUEUED = 1,
@ -119,35 +120,41 @@ public:
MESH_TYPE_INDEX_BODY
};
enum BuildFlags {
BUILD_FLAG_USE_ISOLEVEL = 1 << 0,
BUILD_FLAG_USE_LIGHTING = 1 << 1,
BUILD_FLAG_USE_AO = 1 << 2,
BUILD_FLAG_USE_RAO = 1 << 3,
BUILD_FLAG_GENERATE_AO = 1 << 4,
BUILD_FLAG_AUTO_GENERATE_RAO = 1 << 5,
BUILD_FLAG_BAKE_LIGHTS = 1 << 6,
BUILD_FLAG_CREATE_COLLIDER = 1 << 7,
BUILD_FLAG_CREATE_LODS = 1 << 8,
};
public:
bool get_is_build_threaded() const;
void set_is_build_threaded(bool value);
void set_is_build_threaded(const bool value);
int get_build_flags() const;
void set_build_flags(const int flags);
ActiveBuildPhaseType get_active_build_phase_type() const;
void set_active_build_phase_type(const ActiveBuildPhaseType value);
bool get_build_phase_done() const;
void set_build_phase_done(bool value);
void set_build_phase_done(const bool value);
int get_lod_size() const;
void set_lod_size(int lod_size);
void set_lod_size(const int lod_size);
int get_current_build_phase();
void set_current_build_phase(int value);
int get_current_build_phase() const;
void set_current_build_phase(const int value);
int get_max_build_phase();
void set_max_build_phase(int value);
bool get_create_collider() const;
void set_create_collider(bool value);
bool get_bake_lights() const;
void set_bake_lights(bool value);
int get_max_build_phase() const;
void set_max_build_phase(const int value);
//Lod
bool get_generate_lod() const;
void set_generate_lod(const bool value);
int get_lod_num() const;
void set_lod_num(const int value);
@ -216,7 +223,7 @@ public:
void emit_build_finished();
void generate_random_ao(int seed, int octaves = 4, int period = 30, float persistence = 0.3, float scale_factor = 0.6);
void generate_random_ao(const int seed, const int octaves = 4, const int period = 30, const float persistence = 0.3, const float scale_factor = 0.6);
VoxelChunkDefault();
~VoxelChunkDefault();
@ -242,6 +249,8 @@ protected:
static void _bind_methods();
int _build_flags;
bool _is_build_threaded;
bool _abort_build;
@ -252,7 +261,6 @@ protected:
int _lod_size;
//lod
bool _generate_lod;
int _lod_num;
int _current_lod_level;
@ -262,11 +270,6 @@ protected:
//debug
ImmediateGeometry *_debug_drawer;
bool _build_mesh;
bool _create_collider;
bool _bake_lights;
bool _build_prioritized;
bool _build_phase_done;
Thread *_build_thread;
@ -280,5 +283,6 @@ protected:
VARIANT_ENUM_CAST(VoxelChunkDefault::DefaultChannels);
VARIANT_ENUM_CAST(VoxelChunkDefault::ActiveBuildPhaseType);
VARIANT_ENUM_CAST(VoxelChunkDefault::BuildFlags);
#endif

View File

@ -0,0 +1,63 @@
/*
Copyright (c) 2019-2020 Péter Magyar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "voxel_world_default.h"
#include "voxel_chunk_default.h"
_FORCE_INLINE_ int VoxelWorldDefault::get_build_flags() const {
return _build_flags;
}
_FORCE_INLINE_ void VoxelWorldDefault::set_build_flags(const int flags) {
_build_flags = flags;
}
Ref<VoxelChunk> VoxelWorldDefault::_create_chunk(int x, int y, int z, Ref<VoxelChunk> chunk) {
if (!chunk.is_valid()) {
chunk = Ref<VoxelChunk>(memnew(VoxelChunkDefault));
}
Ref<VoxelChunkDefault> vcd = chunk;
if (vcd.is_valid()) {
vcd->set_build_flags(_build_flags);
}
return VoxelWorld::_create_chunk(x, y, z, chunk);
}
VoxelWorldDefault::VoxelWorldDefault() {
_build_flags = VoxelChunkDefault::BUILD_FLAG_CREATE_COLLIDER | VoxelChunkDefault::BUILD_FLAG_CREATE_LODS;
set_data_margin_start(1);
set_data_margin_end(1);
}
VoxelWorldDefault ::~VoxelWorldDefault() {
}
void VoxelWorldDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_build_flags"), &VoxelWorldDefault::get_build_flags);
ClassDB::bind_method(D_METHOD("set_build_flags", "value"), &VoxelWorldDefault::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

@ -0,0 +1,47 @@
/*
Copyright (c) 2019-2020 Péter Magyar
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef VOXEL_WORLD_DEFAULT_H
#define VOXEL_WORLD_DEFAULT_H
#include "../voxel_world.h"
class VoxelWorldDefault : public VoxelWorld {
GDCLASS(VoxelWorldDefault, VoxelWorld);
public:
int get_build_flags() const;
void set_build_flags(const int flags);
VoxelWorldDefault();
~VoxelWorldDefault();
protected:
Ref<VoxelChunk> _create_chunk(int x, int y, int z, Ref<VoxelChunk> p_chunk);
static void _bind_methods();
private:
int _build_flags;
};
#endif