Moved VoxelJob to a new folder. Also added generate_ao and generate_random_ao from voxel chunk into it.

This commit is contained in:
Relintai 2020-10-01 20:18:43 +02:00
parent 6f8e54879b
commit 71f69666cd
6 changed files with 104 additions and 6 deletions

2
SCsub
View File

@ -72,7 +72,7 @@ sources = [
"thirdparty/lz4/lz4.c",
"world/default/voxel_job.cpp",
"world/jobs/voxel_job.cpp",
]
if has_texture_packer:

View File

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

View File

@ -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<VoxelMesher>();

View File

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

View File

@ -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<VoxelChunkDefault> &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<OpenSimplexNoise> 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;

View File

@ -40,12 +40,21 @@ class VoxelJob : public Resource {
#endif
public:
void set_chunk(const Ref<VoxelChunkDefault> &chunk);
enum {
RESULT_TYPE_FLAG_MESH = 1 << 0,
RESULT_TYPE_FLAG_COLLIDER = 1 << 1,
};
public:
void set_chunk(const Ref<VoxelChunkDefault> &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();