2018-09-24 16:54:07 -07:00
|
|
|
#include "voxel_mesh_updater.h"
|
2019-04-28 09:58:29 -07:00
|
|
|
#include "../util/utility.h"
|
2019-05-03 16:02:10 -07:00
|
|
|
#include "voxel_lod_terrain.h"
|
2019-04-23 17:29:47 -07:00
|
|
|
#include <core/os/os.h>
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-05-03 16:02:10 -07:00
|
|
|
static void scale_mesh_data(VoxelMesher::Output &data, float factor) {
|
|
|
|
|
|
|
|
for (int i = 0; i < data.surfaces.size(); ++i) {
|
|
|
|
Array &surface = data.surfaces.write[i]; // There is COW here too but should not happen, hopefully
|
|
|
|
|
|
|
|
if (surface.empty()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
PoolVector3Array positions = surface[Mesh::ARRAY_VERTEX]; // Array of Variants here, implicit cast going on
|
|
|
|
|
|
|
|
// Now dear COW, let's make sure there is only ONE ref to that PoolVector,
|
|
|
|
// so you won't trash performance with pointless allocations
|
|
|
|
surface[Mesh::ARRAY_VERTEX] = Variant();
|
|
|
|
|
2019-05-12 08:33:25 -07:00
|
|
|
{
|
|
|
|
PoolVector3Array::Write w = positions.write();
|
|
|
|
int len = positions.size();
|
|
|
|
for (int j = 0; j < len; ++j) {
|
|
|
|
w[j] = w[j] * factor;
|
|
|
|
}
|
2019-05-03 16:02:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Thank you
|
|
|
|
surface[Mesh::ARRAY_VERTEX] = positions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
VoxelMeshUpdater::VoxelMeshUpdater(unsigned int thread_count, MeshingParams params) {
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
Ref<VoxelMesherBlocky> blocky_mesher;
|
|
|
|
Ref<VoxelMesherDMC> smooth_mesher;
|
2019-04-27 17:32:23 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
if (params.library.is_valid()) {
|
|
|
|
blocky_mesher.instance();
|
|
|
|
blocky_mesher->set_library(params.library);
|
|
|
|
blocky_mesher->set_occlusion_enabled(params.baked_ao);
|
|
|
|
blocky_mesher->set_occlusion_darkness(params.baked_ao_darkness);
|
2019-05-03 15:59:21 -07:00
|
|
|
}
|
2019-04-27 17:32:23 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
if (params.smooth_surface) {
|
|
|
|
smooth_mesher.instance();
|
|
|
|
smooth_mesher->set_geometric_error(0.05);
|
2019-05-25 07:00:22 -07:00
|
|
|
smooth_mesher->set_simplify_mode(VoxelMesherDMC::SIMPLIFY_NONE);
|
2019-05-19 10:27:49 -07:00
|
|
|
smooth_mesher->set_seam_mode(VoxelMesherDMC::SEAM_MARCHING_SQUARE_SKIRTS);
|
2019-04-27 17:32:23 -07:00
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
Processor processors[Mgr::MAX_LOD];
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
for (int i = 0; i < thread_count; ++i) {
|
|
|
|
Processor &p = processors[i];
|
|
|
|
if (i == 0) {
|
|
|
|
p.blocky_mesher = blocky_mesher;
|
|
|
|
p.smooth_mesher = smooth_mesher;
|
|
|
|
_required_padding = p.get_required_padding();
|
2019-05-12 08:19:08 -07:00
|
|
|
} else {
|
2019-05-19 10:27:49 -07:00
|
|
|
// Need to clone them because they are not thread-safe.
|
|
|
|
// Also thanks to the wonders of ref_pointer() being private we trigger extra refs/unrefs for no reason
|
|
|
|
if (blocky_mesher.is_valid()) {
|
|
|
|
p.blocky_mesher = Ref<VoxelMesher>(blocky_mesher->clone());
|
|
|
|
}
|
|
|
|
if (smooth_mesher.is_valid()) {
|
|
|
|
p.smooth_mesher = Ref<VoxelMesher>(smooth_mesher->clone());
|
|
|
|
}
|
2019-05-12 08:19:08 -07:00
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
_mgr = memnew(Mgr(thread_count, 50, processors));
|
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
VoxelMeshUpdater::~VoxelMeshUpdater() {
|
|
|
|
if (_mgr) {
|
|
|
|
memdelete(_mgr);
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
2019-05-19 10:27:49 -07:00
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
int VoxelMeshUpdater::Processor::get_required_padding() {
|
|
|
|
int padding = 0;
|
|
|
|
if (blocky_mesher.is_valid()) {
|
|
|
|
padding = max(padding, blocky_mesher->get_minimum_padding());
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
2019-05-19 10:27:49 -07:00
|
|
|
if (smooth_mesher.is_valid()) {
|
|
|
|
padding = max(padding, smooth_mesher->get_minimum_padding());
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
2019-05-19 10:27:49 -07:00
|
|
|
return padding;
|
|
|
|
}
|
2018-09-24 16:54:07 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
void VoxelMeshUpdater::Processor::process_block(const InputBlockData &input, OutputBlockData &output, Vector3i block_position, unsigned int lod) {
|
2019-05-12 08:33:25 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
const InputBlockData &block = input;
|
|
|
|
CRASH_COND(block.voxels.is_null());
|
2019-05-12 08:33:25 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
int padding = get_required_padding();
|
2019-05-12 08:33:25 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
if (blocky_mesher.is_valid()) {
|
|
|
|
blocky_mesher->build(output.blocky_surfaces, **block.voxels, padding);
|
2019-05-12 08:33:25 -07:00
|
|
|
}
|
2019-05-19 10:27:49 -07:00
|
|
|
if (smooth_mesher.is_valid()) {
|
|
|
|
smooth_mesher->build(output.smooth_surfaces, **block.voxels, padding);
|
2018-09-24 16:54:07 -07:00
|
|
|
}
|
2019-05-04 17:09:12 -07:00
|
|
|
|
2019-05-19 10:27:49 -07:00
|
|
|
if (lod > 0) {
|
|
|
|
float factor = 1 << lod;
|
|
|
|
scale_mesh_data(output.blocky_surfaces, factor);
|
|
|
|
scale_mesh_data(output.smooth_surfaces, factor);
|
|
|
|
}
|
2019-05-04 17:09:12 -07:00
|
|
|
}
|