diff --git a/SCsub b/SCsub index c66982f..12163a5 100644 --- a/SCsub +++ b/SCsub @@ -72,7 +72,7 @@ sources = [ "thirdparty/lz4/lz4.c", - "world/default/voxel_job.cpp", + "world/jobs/voxel_job.cpp", ] if has_texture_packer: diff --git a/meshers/marching_cubes/voxel_mesher_marching_cubes.cpp b/meshers/marching_cubes/voxel_mesher_marching_cubes.cpp index bc4579a..86382c4 100644 --- a/meshers/marching_cubes/voxel_mesher_marching_cubes.cpp +++ b/meshers/marching_cubes/voxel_mesher_marching_cubes.cpp @@ -27,7 +27,7 @@ SOFTWARE. #include "core/array.h" #include "core/dictionary.h" -#include "../../world/default/voxel_job.h" +#include "../../world/jobs/voxel_job.h" #include "core/version.h" diff --git a/register_types.cpp b/register_types.cpp index 9d6bcd9..8036891 100644 --- a/register_types.cpp +++ b/register_types.cpp @@ -72,7 +72,7 @@ SOFTWARE. #include "nodes/voxelman_light.h" -#include "world/default/voxel_job.h" +#include "world/jobs/voxel_job.h" void register_voxelman_types() { ClassDB::register_class(); diff --git a/world/default/voxel_chunk_default.cpp b/world/default/voxel_chunk_default.cpp index 2454803..efa9dcd 100644 --- a/world/default/voxel_chunk_default.cpp +++ b/world/default/voxel_chunk_default.cpp @@ -32,7 +32,7 @@ SOFTWARE. #include "../voxel_world.h" #include "../../../thread_pool/thread_pool.h" -#include "voxel_job.h" +#include "../jobs/voxel_job.h" #include "voxel_world_default.h" diff --git a/world/default/voxel_job.cpp b/world/jobs/voxel_job.cpp similarity index 65% rename from world/default/voxel_job.cpp rename to world/jobs/voxel_job.cpp index 13e1a68..1fd63d5 100644 --- a/world/default/voxel_job.cpp +++ b/world/jobs/voxel_job.cpp @@ -22,7 +22,9 @@ SOFTWARE. #include "voxel_job.h" -#include "voxel_chunk_default.h" +#include "../default/voxel_chunk_default.h" + +#include "../../../opensimplex/open_simplex_noise.h" void VoxelJob::set_chunk(const Ref &chunk) { _chunk = chunk; @@ -83,6 +85,93 @@ void VoxelJob::_execute() { set_complete(true); } +//Data Management functions +void VoxelJob::generate_ao() { + ERR_FAIL_COND(!_chunk.is_valid()); + + int data_size_x = _chunk->get_data_size_x(); + int data_size_y = _chunk->get_data_size_y(); + int data_size_z = _chunk->get_data_size_z(); + + ERR_FAIL_COND(data_size_x == 0 || data_size_y == 0 || data_size_z == 0); + + int margin_start = _chunk->get_margin_start(); + int margin_end = _chunk->get_margin_end(); + + int ssize_x = _chunk->get_size_x(); + int ssize_y = _chunk->get_size_y(); + int ssize_z = _chunk->get_size_z(); + + int size_x = ssize_x + margin_end; + int size_y = ssize_y + margin_end; + int size_z = ssize_z + margin_end; + + for (int y = margin_start - 1; y < size_y - 1; ++y) { + for (int z = margin_start - 1; z < size_z - 1; ++z) { + for (int x = margin_start - 1; x < size_x - 1; ++x) { + int current = _chunk->get_voxel(x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + + int sum = _chunk->get_voxel(x + 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + sum += _chunk->get_voxel(x - 1, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + sum += _chunk->get_voxel(x, y + 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + sum += _chunk->get_voxel(x, y - 1, z, VoxelChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + sum += _chunk->get_voxel(x, y, z + 1, VoxelChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + sum += _chunk->get_voxel(x, y, z - 1, VoxelChunkDefault::DEFAULT_CHANNEL_ISOLEVEL); + + sum /= 6; + + sum -= current; + + if (sum < 0) + sum = 0; + + _chunk->set_voxel(sum, x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_AO); + } + } + } +} + +void VoxelJob::generate_random_ao(int seed, int octaves, int period, float persistence, float scale_factor) { + ERR_FAIL_COND(!_chunk.is_valid()); + + int margin_start = _chunk->get_margin_start(); + int margin_end = _chunk->get_margin_end(); + + int size_x = _chunk->get_size_x(); + int size_y = _chunk->get_size_y(); + int size_z = _chunk->get_size_z(); + + int position_x = _chunk->get_position_x(); + int position_y = _chunk->get_position_y(); + int position_z = _chunk->get_position_z(); + + Ref noise; + noise.instance(); + + noise->set_seed(seed); + noise->set_octaves(octaves); + noise->set_period(period); + noise->set_persistence(persistence); + + for (int x = -margin_start; x < size_x + margin_end; ++x) { + for (int z = -margin_start; z < size_z + margin_end; ++z) { + for (int y = -margin_start; y < size_y + 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; + + _chunk->set_voxel(int(val * 255.0), x, y, z, VoxelChunkDefault::DEFAULT_CHANNEL_RANDOM_AO); + } + } + } +} + VoxelJob::VoxelJob() { _in_tree = false; diff --git a/world/default/voxel_job.h b/world/jobs/voxel_job.h similarity index 91% rename from world/default/voxel_job.h rename to world/jobs/voxel_job.h index d2e7623..730d015 100644 --- a/world/default/voxel_job.h +++ b/world/jobs/voxel_job.h @@ -40,12 +40,21 @@ class VoxelJob : public Resource { #endif public: - void set_chunk(const Ref &chunk); + enum { + RESULT_TYPE_FLAG_MESH = 1 << 0, + RESULT_TYPE_FLAG_COLLIDER = 1 << 1, + }; +public: + void set_chunk(const Ref &chunk); void chunk_exit_tree(); + void finalize_build(); void _execute(); + void generate_ao(); + void generate_random_ao(int seed, int octaves, int period, float persistence, float scale_factor); + VoxelJob(); ~VoxelJob();