-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
323 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "voxel_mesher_context.h" | ||
#include "../../storage/voxel_buffer.h" | ||
#include "../voxel_mesher.h" | ||
|
||
namespace zylann::voxel { | ||
|
||
int VoxelMesherContext::get_voxel(Vector3i pos, const int channel) const { | ||
ZN_ASSERT_RETURN_V(channel >= 0 && channel < static_cast<int>(VoxelBuffer::MAX_CHANNELS)); | ||
return input->voxels.get_voxel(pos, channel); | ||
} | ||
|
||
float VoxelMesherContext::get_voxel_f(Vector3i pos, const int channel) const { | ||
ZN_ASSERT_RETURN_V(channel >= 0 && channel < static_cast<int>(VoxelBuffer::MAX_CHANNELS)); | ||
return input->voxels.get_voxel_f(pos, channel); | ||
} | ||
|
||
Vector3i VoxelMesherContext::get_voxel_resolution_with_padding() const { | ||
return input->voxels.get_size(); | ||
} | ||
|
||
int VoxelMesherContext::get_lod_index() const { | ||
return input->lod_index; | ||
} | ||
|
||
void VoxelMesherContext::add_surface_from_arrays(Array p_arrays, const int p_material_index) { | ||
ZN_ASSERT_RETURN(p_material_index >= 0 && p_material_index < 256); | ||
const uint16_t material_index = static_cast<uint16_t>(p_material_index); | ||
output->surfaces.push_back(VoxelMesherOutput::Surface{ p_arrays, material_index }); | ||
} | ||
|
||
// This is not straightforward at the moment, lots of details can go wrong | ||
// void VoxelMesherContext::add_mesher(Ref<VoxelMesher> mesher) { | ||
// ZN_ASSERT_RETURN(mesher.is_valid()); | ||
// mesher->build(*output, *input); | ||
// } | ||
|
||
void VoxelMesherContext::_bind_methods() { | ||
using Self = VoxelMesherContext; | ||
|
||
ClassDB::bind_method(D_METHOD("get_voxel", "position", "channel"), &Self::get_voxel); | ||
ClassDB::bind_method(D_METHOD("get_voxel_f", "position", "channel"), &Self::get_voxel_f); | ||
ClassDB::bind_method(D_METHOD("get_voxel_resolution_with_padding"), &Self::get_voxel_resolution_with_padding); | ||
ClassDB::bind_method(D_METHOD("get_lod_index"), &Self::get_lod_index); | ||
ClassDB::bind_method( | ||
D_METHOD("add_surface_from_arrays", "arrays", "material_index"), &Self::add_surface_from_arrays | ||
); | ||
} | ||
|
||
} // namespace zylann::voxel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#ifndef VOXEL_MESHER_CONTEXT_H | ||
#define VOXEL_MESHER_CONTEXT_H | ||
|
||
#include "../../util/godot/classes/object.h" | ||
#include "../voxel_mesher_input.h" | ||
#include "../voxel_mesher_output.h" | ||
|
||
namespace zylann::voxel { | ||
|
||
// Data and functions provided to script when meshing one chunk of voxels. | ||
// Must not be created or stored by scripts. | ||
class VoxelMesherContext : public Object { | ||
GDCLASS(VoxelMesherContext, Object) | ||
public: | ||
const VoxelMesherInput *input = nullptr; | ||
VoxelMesherOutput *output = nullptr; | ||
|
||
// TODO Maybe we need a VoxelBufferReadOnly? | ||
// Ref<VoxelBuffer> voxels_wrapper; | ||
// Ref<VoxelBuffer> get_voxels(); | ||
|
||
// Voxels access (read-only) | ||
int get_voxel(Vector3i pos, const int channel) const; | ||
float get_voxel_f(Vector3i pos, const int channel) const; | ||
Vector3i get_voxel_resolution_with_padding() const; | ||
int get_lod_index() const; | ||
|
||
// Output | ||
void add_surface_from_arrays(Array p_arrays, const int p_material_index); | ||
// void add_mesher(Ref<VoxelMesher> mesher); | ||
|
||
private: | ||
static void _bind_methods(); | ||
}; | ||
|
||
} // namespace zylann::voxel | ||
|
||
#endif // VOXEL_MESHER_CONTEXT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#include "voxel_mesher_script.h" | ||
#include "../../storage/voxel_buffer_gd.h" | ||
#include "../../util/profiling.h" | ||
|
||
namespace zylann::voxel { | ||
|
||
VoxelMesherScript::VoxelMesherScript() { | ||
// Default padding | ||
set_padding(1, 1); | ||
} | ||
|
||
VoxelMesherScript::~VoxelMesherScript() { | ||
for (VoxelMesherContext *context : _context_pool) { | ||
memdelete(context); | ||
} | ||
} | ||
|
||
void VoxelMesherScript::build(VoxelMesherOutput &output, const VoxelMesherInput &input) { | ||
ZN_PROFILE_SCOPE(); | ||
|
||
VoxelMesherContext *context = get_or_create_context(); | ||
|
||
context->input = &input; | ||
|
||
if (!GDVIRTUAL_CALL(_build, context)) { | ||
WARN_PRINT_ONCE("VoxelMesherScript::_build is unimplemented!"); | ||
} | ||
|
||
return_context(context); | ||
} | ||
|
||
Ref<Material> VoxelMesherScript::get_material_by_index(unsigned int i) const { | ||
Ref<Material> material; | ||
if (!GDVIRTUAL_CALL(_get_material_by_index, i, material)) { | ||
WARN_PRINT_ONCE("VoxelMesherScript::_get_material_by_index is unimplemented!"); | ||
} | ||
return material; | ||
} | ||
|
||
unsigned int VoxelMesherScript::get_material_index_count() const { | ||
int count = 0; | ||
if (!GDVIRTUAL_CALL(_get_material_index_count, count)) { | ||
WARN_PRINT_ONCE("VoxelMesherScript::_get_material_index_count is unimplemented!"); | ||
} | ||
ZN_ASSERT_RETURN_V(count >= 0, 0); | ||
return count; | ||
} | ||
|
||
VoxelMesherContext *VoxelMesherScript::get_or_create_context() { | ||
{ | ||
MutexLock(_context_pool_mutex); | ||
if (_context_pool.size() != 0) { | ||
VoxelMesherContext *context = _context_pool.back(); | ||
_context_pool.pop_back(); | ||
return context; | ||
} | ||
} | ||
return memnew(VoxelMesherContext); | ||
} | ||
|
||
void VoxelMesherScript::return_context(VoxelMesherContext *context) { | ||
MutexLock mlock(_context_pool_mutex); | ||
_context_pool.push_back(context); | ||
} | ||
|
||
void VoxelMesherScript::_b_set_padding(const int p_negative_padding, const int p_positive_padding) { | ||
// Check for reasonable padding | ||
ZN_ASSERT_RETURN(p_negative_padding >= 0 && p_negative_padding <= 2); | ||
ZN_ASSERT_RETURN(p_positive_padding >= 0 && p_positive_padding <= 2); | ||
set_padding(p_negative_padding, p_positive_padding); | ||
} | ||
|
||
void VoxelMesherScript::_bind_methods() { | ||
GDVIRTUAL_BIND(_build, "context"); | ||
GDVIRTUAL_BIND(_get_material_by_index, "index"); | ||
GDVIRTUAL_BIND(_get_material_index_count); | ||
} | ||
|
||
} // namespace zylann::voxel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#ifndef VOXEL_MESHER_SCRIPT_H | ||
#define VOXEL_MESHER_SCRIPT_H | ||
|
||
#include "../../util/containers/std_vector.h" | ||
#include "../../util/godot/core/gdvirtual.h" | ||
#include "../../util/thread/mutex.h" | ||
#include "../voxel_mesher.h" | ||
// GDVIRTUAL requires complete definition of the class | ||
#include "voxel_mesher_context.h" | ||
|
||
namespace zylann::voxel { | ||
|
||
// Allows to implement an entirely custom mesher using the scripting API. | ||
// With GDScript it can be used for prototyping, and faster languages like C# could be used for viable performance. | ||
// If you are creating a custom mesher with C++ using direct access to the module, prefer using VoxelMesher. | ||
class VoxelMesherScript : public VoxelMesher { | ||
GDCLASS(VoxelMesherScript, VoxelMesher) | ||
public: | ||
VoxelMesherScript(); | ||
~VoxelMesherScript(); | ||
|
||
void build(VoxelMesherOutput &output, const VoxelMesherInput &input) override; | ||
Ref<Material> get_material_by_index(unsigned int i) const override; | ||
unsigned int get_material_index_count() const override; | ||
|
||
protected: | ||
GDVIRTUAL1C(_build, VoxelMesherContext *) | ||
GDVIRTUAL1RC(Ref<Material>, _get_material_by_index, int) | ||
GDVIRTUAL0RC(int, _get_material_index_count) | ||
|
||
private: | ||
VoxelMesherContext *get_or_create_context(); | ||
void return_context(VoxelMesherContext *context); | ||
|
||
void _b_set_padding(const int p_negative_padding, const int p_positive_padding); | ||
|
||
static void _bind_methods(); | ||
|
||
StdVector<VoxelMesherContext *> _context_pool; | ||
Mutex _context_pool_mutex; | ||
}; | ||
|
||
} // namespace zylann::voxel | ||
|
||
#endif // VOXEL_MESHER_SCRIPT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.