Ported the helper methods for the terrain job from Terraman. Did not touch the ectual meshing code yet,

This commit is contained in:
Relintai 2022-02-09 00:05:16 +01:00
parent 9940dedb88
commit 53f3ff60c1
2 changed files with 307 additions and 6 deletions

View File

@ -22,13 +22,14 @@ SOFTWARE.
#include "voxel_terrain_job.h"
#include "../../library/voxel_surface.h"
#include "../../library/voxel_library.h"
#include "../../library/voxel_surface.h"
#include "../../meshers/default/voxel_mesher_default.h"
#include "../../meshers/voxel_mesher.h"
#include "../default/voxel_chunk_default.h"
#include "../../library/voxel_material_cache.h"
#ifdef MESH_UTILS_PRESENT
#include "../../../mesh_utils/fast_quadratic_mesh_simplifier.h"
@ -78,6 +79,28 @@ int VoxelTerrainJob::get_liquid_mesher_count() const {
return _liquid_meshers.size();
}
Ref<VoxelMesherJobStep> VoxelTerrainJob::get_jobs_step(int index) const {
ERR_FAIL_INDEX_V(index, _job_steps.size(), Ref<VoxelMesherJobStep>());
return _job_steps.get(index);
}
void VoxelTerrainJob::set_jobs_step(int index, const Ref<VoxelMesherJobStep> &step) {
ERR_FAIL_INDEX(index, _job_steps.size());
_job_steps.set(index, step);
}
void VoxelTerrainJob::remove_jobs_step(const int index) {
ERR_FAIL_INDEX(index, _job_steps.size());
_job_steps.remove(index);
}
void VoxelTerrainJob::add_jobs_step(const Ref<VoxelMesherJobStep> &step) {
_job_steps.push_back(step);
}
int VoxelTerrainJob::get_jobs_step_count() const {
return _job_steps.size();
}
void VoxelTerrainJob::phase_setup() {
for (int i = 0; i < _meshers.size(); ++i) {
Ref<VoxelMesher> mesher = _meshers.get(i);
@ -100,6 +123,45 @@ void VoxelTerrainJob::phase_setup() {
next_phase();
}
void VoxelTerrainJob::phase_library_setup() {
if (should_return()) {
return;
}
Ref<VoxelLibrary> lib = _chunk->get_library();
if (!lib.is_valid()) {
next_phase();
return;
}
if (lib->supports_caching()) {
if (!_chunk->material_cache_key_has()) {
lib->material_cache_get_key(_chunk);
} else {
Ref<VoxelMaterialCache> cache = lib->material_cache_get(_chunk->material_cache_key_get());
if (!cache.is_valid()) {
next_phase();
return;
}
//Note: without threadpool and threading none of this can happen, as cache will get initialized the first time a thread requests it!
while (!cache->get_initialized()) {
//Means it's currently merging the atlases on a different thread.
//Let's just wait
OS::get_singleton()->delay_usec(100);
}
}
}
next_phase();
if (should_return()) {
return;
}
}
void VoxelTerrainJob::phase_terrain_mesh_setup() {
int starti = 0;
@ -586,14 +648,16 @@ void VoxelTerrainJob::_execute_phase() {
if (_phase == 0) {
phase_setup();
} else if (_phase == 1) {
phase_terrain_mesh_setup();
phase_library_setup();
} else if (_phase == 2) {
phase_terrain_mesh_setup();
} else if (_phase == 3) {
phase_collider();
} else if (_phase == 4) {
phase_terrain_mesh();
} else if (_phase == 5) {
phase_terrain_mesh();
} else if (_phase == 6) {
phase_finalize();
} else if (_phase > 5) {
} else if (_phase > 6) {
set_complete(true); //So threadpool knows it's done
next_job();
ERR_FAIL_MSG("VoxelTerrainJob: _phase is too high!");
@ -606,6 +670,9 @@ void VoxelTerrainJob::_reset() {
_build_done = false;
_phase = 0;
_current_job_step = 0;
_current_mesh = 0;
for (int i = 0; i < _meshers.size(); ++i) {
Ref<VoxelMesher> mesher = _meshers.get(i);
@ -638,11 +705,219 @@ void VoxelTerrainJob::_reset() {
}
void VoxelTerrainJob::_physics_process(float delta) {
if (_phase == 3)
if (_phase == 4)
phase_physics_process();
}
void VoxelTerrainJob::step_type_normal() {
Ref<VoxelChunkDefault> chunk = _chunk;
Ref<VoxelMesher> mesher;
for (int i = 0; i < _meshers.size(); ++i) {
mesher = _meshers.get(i);
if (mesher.is_valid()) {
break;
}
}
//TODO make this automatic in build_mesh
if ((chunk->get_build_flags() & VoxelChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0) {
mesher->bake_colors(_chunk);
}
temp_mesh_arr = mesher->build_mesh();
RID mesh_rid = chunk->mesh_rid_get_index(VoxelChunkDefault::MESH_INDEX_TERRAIN, VoxelChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
Ref<Material> lmat;
if (chunk->material_cache_key_has()) {
lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh);
} else {
lmat = chunk->get_library()->material_lod_get(_current_mesh);
}
if (lmat.is_valid()) {
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
}
++_current_mesh;
}
void VoxelTerrainJob::step_type_normal_lod() {
Ref<VoxelMesherJobStep> step = _job_steps[_current_job_step];
ERR_FAIL_COND(!step.is_valid());
Ref<VoxelChunkDefault> chunk = _chunk;
Ref<VoxelMesher> mesher;
for (int i = 0; i < _meshers.size(); ++i) {
mesher = _meshers.get(i);
if (mesher.is_valid()) {
break;
}
}
mesher->set_lod_index(step->get_lod_index());
mesher->reset();
mesher->add_chunk(_chunk);
//TODO make this automatic in build_mesh
if ((chunk->get_build_flags() & VoxelChunkDefault::BUILD_FLAG_USE_LIGHTING) != 0) {
mesher->bake_colors(_chunk);
}
temp_mesh_arr = mesher->build_mesh();
RID mesh_rid = chunk->mesh_rid_get_index(VoxelChunkDefault::MESH_INDEX_TERRAIN, VoxelChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
VS::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
Ref<Material> lmat;
if (chunk->material_cache_key_has()) {
lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh);
} else {
lmat = chunk->get_library()->material_lod_get(_current_mesh);
}
if (lmat.is_valid()) {
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
}
++_current_mesh;
}
void VoxelTerrainJob::step_type_drop_uv2() {
Ref<VoxelChunkDefault> chunk = _chunk;
RID mesh_rid = chunk->mesh_rid_get_index(VoxelChunkDefault::MESH_INDEX_TERRAIN, VoxelChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
temp_mesh_arr[VisualServer::ARRAY_TEX_UV2] = Variant();
VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
Ref<Material> lmat;
if (chunk->material_cache_key_has()) {
lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh);
} else {
lmat = chunk->get_library()->material_lod_get(_current_mesh);
}
if (lmat.is_valid()) {
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
}
++_current_mesh;
}
void VoxelTerrainJob::step_type_merge_verts() {
Array temp_mesh_arr2 = merge_mesh_array(temp_mesh_arr);
temp_mesh_arr = temp_mesh_arr2;
Ref<VoxelChunkDefault> chunk = _chunk;
RID mesh_rid = chunk->mesh_rid_get_index(VoxelChunkDefault::MESH_INDEX_TERRAIN, VoxelChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
Ref<Material> lmat;
if (chunk->material_cache_key_has()) {
lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh);
} else {
lmat = chunk->get_library()->material_lod_get(_current_mesh);
}
if (lmat.is_valid()) {
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
}
++_current_mesh;
}
void VoxelTerrainJob::step_type_bake_texture() {
Ref<VoxelChunkDefault> chunk = _chunk;
Ref<ShaderMaterial> mat = chunk->get_library()->material_lod_get(0);
Ref<SpatialMaterial> spmat = chunk->get_library()->material_lod_get(0);
Ref<Texture> tex;
if (mat.is_valid()) {
tex = mat->get_shader_param("texture_albedo");
} else if (spmat.is_valid()) {
tex = spmat->get_texture(SpatialMaterial::TEXTURE_ALBEDO);
}
if (tex.is_valid()) {
temp_mesh_arr = bake_mesh_array_uv(temp_mesh_arr, tex);
temp_mesh_arr[VisualServer::ARRAY_TEX_UV] = Variant();
RID mesh_rid = chunk->mesh_rid_get_index(VoxelChunkDefault::MESH_INDEX_TERRAIN, VoxelChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
Ref<Material> lmat;
if (chunk->material_cache_key_has()) {
lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh);
} else {
lmat = chunk->get_library()->material_lod_get(_current_mesh);
}
if (lmat.is_valid()) {
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
}
}
++_current_mesh;
}
void VoxelTerrainJob::step_type_simplify_mesh() {
#ifdef MESH_UTILS_PRESENT
Ref<VoxelChunkDefault> chunk = _chunk;
Ref<VoxelMesherJobStep> step = _job_steps[_current_job_step];
ERR_FAIL_COND(!step.is_valid());
Ref<FastQuadraticMeshSimplifier> fqms = step->get_fqms();
ERR_FAIL_COND(!fqms.is_valid());
fqms->initialize(temp_mesh_arr);
for (int i = 0; i < step->get_simplification_steps(); ++i) {
fqms->simplify_mesh(temp_mesh_arr.size() * step->get_simplification_step_ratio(), step->get_simplification_agressiveness());
temp_mesh_arr = fqms->get_arrays();
RID mesh_rid = chunk->mesh_rid_get_index(VoxelChunkDefault::MESH_INDEX_TERRAIN, VoxelChunkDefault::MESH_TYPE_INDEX_MESH, _current_mesh);
VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh_rid, VisualServer::PRIMITIVE_TRIANGLES, temp_mesh_arr);
Ref<Material> lmat;
if (chunk->material_cache_key_has()) {
lmat = chunk->get_library()->material_cache_get(_chunk->material_cache_key_get())->material_lod_get(_current_mesh);
} else {
lmat = chunk->get_library()->material_lod_get(_current_mesh);
}
if (lmat.is_valid()) {
VisualServer::get_singleton()->mesh_surface_set_material(mesh_rid, 0, lmat->get_rid());
}
++_current_mesh;
}
#endif
}
VoxelTerrainJob::VoxelTerrainJob() {
_current_job_step = 0;
_current_mesh = 0;
}
VoxelTerrainJob::~VoxelTerrainJob() {
@ -663,5 +938,11 @@ void VoxelTerrainJob::_bind_methods() {
ClassDB::bind_method(D_METHOD("add_liquid_mesher", "mesher"), &VoxelTerrainJob::add_liquid_mesher);
ClassDB::bind_method(D_METHOD("get_liquid_mesher_count"), &VoxelTerrainJob::get_liquid_mesher_count);
ClassDB::bind_method(D_METHOD("get_jobs_step", "index"), &VoxelTerrainJob::get_jobs_step);
ClassDB::bind_method(D_METHOD("set_jobs_step", "index", "mesher"), &VoxelTerrainJob::set_jobs_step);
ClassDB::bind_method(D_METHOD("remove_jobs_step", "index"), &VoxelTerrainJob::remove_jobs_step);
ClassDB::bind_method(D_METHOD("add_jobs_step", "mesher"), &VoxelTerrainJob::add_jobs_step);
ClassDB::bind_method(D_METHOD("get_jobs_step_count"), &VoxelTerrainJob::get_jobs_step_count);
ClassDB::bind_method(D_METHOD("_physics_process", "delta"), &VoxelTerrainJob::_physics_process);
}

View File

@ -27,6 +27,8 @@ SOFTWARE.
#include "../../defines.h"
#include "voxel_mesher_job_step.h"
#include pool_vector_h
include_pool_vector
@ -51,7 +53,14 @@ public:
void add_liquid_mesher(const Ref<VoxelMesher> &mesher);
int get_liquid_mesher_count() const;
Ref<VoxelMesherJobStep> get_jobs_step(const int index) const;
void set_jobs_step(const int index, const Ref<VoxelMesherJobStep> &step);
void remove_jobs_step(const int index);
void add_jobs_step(const Ref<VoxelMesherJobStep> &step);
int get_jobs_step_count() const;
void phase_setup();
void phase_library_setup();
void phase_terrain_mesh_setup();
void phase_collider();
void phase_physics_proces();
@ -63,6 +72,13 @@ public:
void _reset();
void _physics_process(float delta);
void step_type_normal();
void step_type_normal_lod();
void step_type_drop_uv2();
void step_type_merge_verts();
void step_type_bake_texture();
void step_type_simplify_mesh();
VoxelTerrainJob();
~VoxelTerrainJob();
@ -72,6 +88,10 @@ protected:
Vector<Ref<VoxelMesher>> _meshers;
Vector<Ref<VoxelMesher>> _liquid_meshers;
Vector<Ref<VoxelMesherJobStep>> _job_steps;
int _current_job_step;
int _current_mesh;
PoolVector<Vector3> temp_arr_collider;
PoolVector<Vector3> temp_arr_collider_liquid;
Array temp_mesh_arr;