Added a blocky mesher, chunk, and world (The minecraft-style mesher). It's incomplete. Also ported the light baking step from Broken Seals, and fixed a few warnings.

This commit is contained in:
Relintai 2020-04-05 01:36:41 +02:00
parent 2d2b7c8c3e
commit dfee0a7786
14 changed files with 420 additions and 14 deletions

5
SCsub
View File

@ -27,6 +27,8 @@ sources = [
"meshers/transvoxel_uv_mesher/voxel_mesher_transvoxel.cpp",
"meshers/transvoxel_uv_mesher/transvoxel_tables.cpp",
"meshers/blocky/voxel_mesher_blocky.cpp",
"world/voxel_world.cpp",
"world/voxel_chunk.cpp",
"world/voxel_chunk_default.cpp",
@ -38,6 +40,9 @@ sources = [
"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",
"areas/world_area.cpp",

View File

@ -37,6 +37,10 @@ def get_doc_classes():
"VoxelStructure",
"BlockVoxelStructure",
"VoxelWorld",
"VoxelMesherBlocky",
"VoxelWorldBlocky",
"VoxelChunkBlocky",
]
def get_doc_path():

View File

@ -0,0 +1,119 @@
/*
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_blocky.h"
#include "../../world/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();
int x_size = chunk->get_size_x();
int y_size = chunk->get_size_y();
int z_size = chunk->get_size_z();
float voxel_size = get_lod_size();
float voxel_scale = get_voxel_scale();
Color base_light(_base_light_value, _base_light_value, _base_light_value);
for (int y = 0; y < y_size; ++y) {
for (int z = 0; z < z_size; ++z) {
for (int x = 0; x < x_size; ++x) {
uint8_t type = chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE);
if (type == 0)
continue;
Ref<VoxelSurface> surface = _library->get_voxel_surface(type);
if (!surface.is_valid())
continue;
uint8_t neighbours[] = {
chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE),
chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE),
chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE),
chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE),
chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE),
chunk->get_voxel(x, y, z - 1, VoxelChunkDefault::DEFAULT_CHANNEL_TYPE),
};
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);
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;
light += base_light;
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);
add_indices(get_vertex_count() + 2);
add_indices(get_vertex_count() + 1);
add_indices(get_vertex_count() + 0);
add_indices(get_vertex_count() + 3);
add_indices(get_vertex_count() + 2);
add_indices(get_vertex_count() + 0);
for (int i = 0; i < 4; ++i) {
add_normal(Vector3(1, 0, 0));
add_color(light);
}
add_uv(surface->transform_uv(VoxelSurface::VOXEL_SIDE_SIDE, Vector2(0, 0)));
add_uv(surface->transform_uv(VoxelSurface::VOXEL_SIDE_SIDE, Vector2(0, 1)));
add_uv(surface->transform_uv(VoxelSurface::VOXEL_SIDE_SIDE, Vector2(1, 0)));
add_uv(surface->transform_uv(VoxelSurface::VOXEL_SIDE_SIDE, Vector2(1, 1)));
add_vertex((Vector3(1, 0, 0) * voxel_size + Vector3(x, y, z)) * voxel_scale);
add_vertex((Vector3(1, 1, 0) * voxel_size + Vector3(x, y, z)) * voxel_scale);
add_vertex((Vector3(1, 1, 1) * voxel_size + Vector3(x, y, z)) * voxel_scale);
add_vertex((Vector3(1, 0, 1) * voxel_size + Vector3(x, y, z)) * voxel_scale);
}
}
}
}
}
VoxelMesherBlocky::VoxelMesherBlocky() {
_format = VisualServer::ARRAY_FORMAT_NORMAL | VisualServer::ARRAY_FORMAT_COLOR | VisualServer::ARRAY_FORMAT_TEX_UV;
}
VoxelMesherBlocky::~VoxelMesherBlocky() {
}
void VoxelMesherBlocky::_bind_methods() {
ClassDB::bind_method(D_METHOD("_add_chunk", "buffer"), &VoxelMesherBlocky::_add_chunk);
}

View File

@ -0,0 +1,45 @@
/*
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_BLOCKY_H
#define VOXEL_MESHER_BLOCKY_H
#include "core/color.h"
#include "core/math/vector2.h"
#include "core/math/vector3.h"
#include "../voxel_mesher.h"
class VoxelMesherBlocky : public VoxelMesher {
GDCLASS(VoxelMesherBlocky, VoxelMesher);
public:
void _add_chunk(Ref<VoxelChunk> p_chunk);
VoxelMesherBlocky();
~VoxelMesherBlocky();
protected:
static void _bind_methods();
};
#endif

View File

@ -52,6 +52,10 @@ SOFTWARE.
#include "world/voxel_world_editor.h"
#include "meshers/blocky/voxel_mesher_blocky.h"
#include "world/blocky/voxel_chunk_blocky.h"
#include "world/blocky/voxel_world_blocky.h"
void register_voxelman_types() {
ClassDB::register_class<VoxelMesher>();
ClassDB::register_class<VoxelMesherTransvoxel>();
@ -78,6 +82,10 @@ void register_voxelman_types() {
ClassDB::register_class<VoxelMesherCubic>();
ClassDB::register_class<VoxelCubePoints>();
ClassDB::register_class<VoxelMesherBlocky>();
ClassDB::register_class<VoxelWorldBlocky>();
ClassDB::register_class<VoxelChunkBlocky>();
ClassDB::register_class<VoxelmanLevelGenerator>();
ClassDB::register_class<WorldArea>();

View File

@ -0,0 +1,51 @@
/*
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_chunk_blocky.h"
#include "../../meshers/blocky/voxel_mesher_blocky.h"
VoxelChunkBlocky::VoxelChunkBlocky() {
}
VoxelChunkBlocky::~VoxelChunkBlocky() {
}
void VoxelChunkBlocky::_setup_channels() {
set_channel_count(MAX_DEFAULT_CHANNELS);
}
void VoxelChunkBlocky::_create_meshers() {
add_mesher(Ref<VoxelMesher>(memnew(VoxelMesherBlocky())));
for (int i = 0; i < _meshers.size(); ++i) {
Ref<VoxelMesher> mesher = _meshers.get(i);
ERR_CONTINUE(!mesher.is_valid());
mesher->set_lod_size(get_lod_size());
mesher->set_voxel_scale(get_voxel_scale());
}
}
void VoxelChunkBlocky::_bind_methods() {
}

View File

@ -0,0 +1,44 @@
/*
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_CHUNK_BLOCKY_H
#define VOXEL_CHUNK_BLOCKY_H
#include "../voxel_chunk_default.h"
class VoxelWorld;
class VoxelChunkBlocky : public VoxelChunkDefault {
GDCLASS(VoxelChunkBlocky, VoxelChunkDefault);
public:
VoxelChunkBlocky();
~VoxelChunkBlocky();
protected:
virtual void _setup_channels();
virtual void _create_meshers();
static void _bind_methods();
};
#endif

View File

@ -0,0 +1,45 @@
/*
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_blocky.h"
#include "voxel_chunk_blocky.h"
Ref<VoxelChunk> VoxelWorldBlocky::_create_chunk(int x, int y, int z, Ref<VoxelChunk> chunk) {
if (!chunk.is_valid()) {
chunk = Ref<VoxelChunk>(memnew(VoxelChunkBlocky));
}
return VoxelWorld::_create_chunk(x, y, z, chunk);
}
VoxelWorldBlocky::VoxelWorldBlocky() {
set_data_margin_start(1);
set_data_margin_end(1);
}
VoxelWorldBlocky ::~VoxelWorldBlocky() {
}
void VoxelWorldBlocky::_bind_methods() {
}

View File

@ -0,0 +1,41 @@
/*
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_BLOCKY_H
#define VOXEL_WORLD_BLOCKY_H
#include "../voxel_world.h"
class VoxelWorldBlocky : public VoxelWorld {
GDCLASS(VoxelWorldBlocky, VoxelWorld);
public:
VoxelWorldBlocky();
~VoxelWorldBlocky();
protected:
Ref<VoxelChunk> _create_chunk(int x, int y, int z, Ref<VoxelChunk> p_chunk);
static void _bind_methods();
};
#endif

View File

@ -221,7 +221,7 @@ void VoxelChunk::setup_channels() {
call("_setup_channels");
}
void VoxelChunk::set_size(uint32_t size_x, uint32_t size_y, uint32_t size_z, uint32_t margin_start, uint32_t margin_end) {
void VoxelChunk::set_size(int size_x, int size_y, int size_z, int margin_start, int margin_end) {
if (_size_x == size_x && _size_y == size_y && _size_z == size_z && _margin_start == margin_start && _margin_end == margin_end) {
return;
}
@ -248,7 +248,7 @@ void VoxelChunk::set_size(uint32_t size_x, uint32_t size_y, uint32_t size_z, uin
_margin_end = margin_end;
}
bool VoxelChunk::validate_channel_data_position(uint32_t x, uint32_t y, uint32_t z) const {
bool VoxelChunk::validate_channel_data_position(int x, int y, int z) const {
return x < _data_size_x && y < _data_size_y && z < _data_size_z;
}

View File

@ -135,9 +135,9 @@ public:
//Channels
void setup_channels();
void set_size(uint32_t size_x, uint32_t size_y, uint32_t size_z, uint32_t margin_start = 0, uint32_t margin_end = 0);
void set_size(int size_x, int size_y, int size_z, int margin_start = 0, int margin_end = 0);
bool validate_channel_data_position(uint32_t x, uint32_t y, uint32_t z) const;
bool validate_channel_data_position(int x, int y, int z) const;
uint8_t get_voxel(int p_x, int p_y, int p_z, int p_channel_index) const;
void set_voxel(uint8_t p_value, int p_x, int p_y, int p_z, int p_channel_index);
@ -232,16 +232,16 @@ protected:
int _position_y;
int _position_z;
uint32_t _size_x;
uint32_t _size_y;
uint32_t _size_z;
int _size_x;
int _size_y;
int _size_z;
uint32_t _data_size_x;
uint32_t _data_size_y;
uint32_t _data_size_z;
int _data_size_x;
int _data_size_y;
int _data_size_z;
uint32_t _margin_start;
uint32_t _margin_end;
int _margin_start;
int _margin_end;
Vector<uint8_t *> _channels;

View File

@ -24,6 +24,8 @@ SOFTWARE.
#include "voxel_world.h"
#include "../../opensimplex/open_simplex_noise.h"
const String VoxelChunkDefault::BINDING_STRING_ACTIVE_BUILD_PHASE_TYPE = "Normal,Process,Physics Process";
_FORCE_INLINE_ bool VoxelChunkDefault::get_is_build_threaded() const {
@ -330,6 +332,35 @@ void VoxelChunkDefault::emit_build_finished() {
}
}
void VoxelChunkDefault::generate_random_ao(int seed, int octaves, int period, float persistence, float scale_factor) {
Ref<OpenSimplexNoise> noise;
noise.instance();
noise->set_seed(seed);
noise->set_octaves(octaves);
noise->set_period(period);
noise->set_persistence(persistence);
for (int x = -get_margin_start(); x < _size_x + get_margin_end(); ++x) {
for (int z = -get_margin_start(); z < _size_z + get_margin_end(); ++z) {
for (int y = -get_margin_start(); y < _size_y + get_margin_end(); ++y) {
float val = noise->get_noise_3d(x + (_position_x * _size_x), y + (_position_y * _size_y), z + (_position_z * _size_z));
val *= scale_factor;
if (val > 1)
val = 1;
if (val < 0)
val = -val;
set_voxel(int(val * 255.0), x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO);
}
}
}
}
//Meshes
Dictionary VoxelChunkDefault::get_mesh_rids() {
return _rids;
@ -1141,6 +1172,15 @@ void VoxelChunkDefault::_build_phase(int phase) {
return;
}
case BUILD_PHASE_LIGHTS: {
clear_baked_lights();
generate_random_ao(123);
bake_lights();
next_phase();
return;
}
/*
case BUILD_PHASE_LIQUID: {
next_phase();
@ -1367,6 +1407,8 @@ void VoxelChunkDefault::_bind_methods() {
ClassDB::bind_method(D_METHOD("draw_debug_voxel_lights"), &VoxelChunkDefault::draw_debug_voxel_lights);
ClassDB::bind_method(D_METHOD("generate_random_ao", "seed", "octaves", "period", "persistence", "scale_factor"), &VoxelChunkDefault::generate_random_ao, DEFVAL(4), DEFVAL(30), DEFVAL(0.3), DEFVAL(0.6));
ClassDB::bind_method(D_METHOD("_setup_channels"), &VoxelChunkDefault::_setup_channels);
ClassDB::bind_method(D_METHOD("_build_phase", "phase"), &VoxelChunkDefault::_build_phase);
ClassDB::bind_method(D_METHOD("_build_phase_process", "phase"), &VoxelChunkDefault::_build_phase_process);

View File

@ -216,6 +216,8 @@ 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);
VoxelChunkDefault();
~VoxelChunkDefault();

View File

@ -116,7 +116,6 @@ public:
void clear();
Ref<VoxelChunk> create_chunk(int x, int y, int z);
Ref<VoxelChunk> _create_chunk(int x, int y, int z, Ref<VoxelChunk> p_chunk);
void generate_chunk(Ref<VoxelChunk> chunk);
@ -132,7 +131,8 @@ public:
~VoxelWorld();
protected:
void _generate_chunk(Ref<VoxelChunk> chunk);
virtual void _generate_chunk(Ref<VoxelChunk> chunk);
virtual Ref<VoxelChunk> _create_chunk(int x, int y, int z, Ref<VoxelChunk> p_chunk);
virtual void _notification(int p_what);
static void _bind_methods();