Skip to content

Commit

Permalink
Added count getter for mesher indexed materials.
Browse files Browse the repository at this point in the history
Not used yet, but may be useful to implement shader materials with
VoxelLodTerrain and blocky mesher
  • Loading branch information
Zylann committed Jun 9, 2024
1 parent a95b030 commit d8f5293
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 0 deletions.
4 changes: 4 additions & 0 deletions meshers/blocky/voxel_blocky_library_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Ref<Material> VoxelBlockyLibraryBase::get_material_by_index(unsigned int index)
return _indexed_materials[index];
}

unsigned int VoxelBlockyLibraryBase::get_material_index_count() const {
return _indexed_materials.size();
}

#ifdef TOOLS_ENABLED

void VoxelBlockyLibraryBase::get_configuration_warnings(PackedStringArray &out_warnings) const {
Expand Down
1 change: 1 addition & 0 deletions meshers/blocky/voxel_blocky_library_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class VoxelBlockyLibraryBase : public Resource {
}

Ref<Material> get_material_by_index(unsigned int index) const;
unsigned int get_material_index_count() const;

#ifdef TOOLS_ENABLED
virtual void get_configuration_warnings(PackedStringArray &out_warnings) const;
Expand Down
8 changes: 8 additions & 0 deletions meshers/blocky/voxel_mesher_blocky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,14 @@ Ref<Material> VoxelMesherBlocky::get_material_by_index(unsigned int index) const
return lib->get_material_by_index(index);
}

unsigned int VoxelMesherBlocky::get_material_index_count() const {
Ref<VoxelBlockyLibraryBase> lib = get_library();
if (lib.is_null()) {
return 0;
}
return lib->get_material_index_count();
}

#ifdef TOOLS_ENABLED

void VoxelMesherBlocky::get_configuration_warnings(PackedStringArray &out_warnings) const {
Expand Down
1 change: 1 addition & 0 deletions meshers/blocky/voxel_mesher_blocky.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class VoxelMesherBlocky : public VoxelMesher {
}

Ref<Material> get_material_by_index(unsigned int index) const override;
unsigned int get_material_index_count() const override;

// Using std::vector because they make this mesher twice as fast than Godot Vectors.
// See why: https://github.com/godotengine/godot/issues/24731
Expand Down
4 changes: 4 additions & 0 deletions meshers/cubes/voxel_mesher_cubes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,10 @@ Ref<Material> VoxelMesherCubes::get_material_by_index(unsigned int i) const {
return _materials[i];
}

unsigned int VoxelMesherCubes::get_material_index_count() const {
return _materials.size();
}

void VoxelMesherCubes::_b_set_opaque_material(Ref<Material> material) {
set_material_by_index(MATERIAL_OPAQUE, material);
}
Expand Down
1 change: 1 addition & 0 deletions meshers/cubes/voxel_mesher_cubes.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class VoxelMesherCubes : public VoxelMesher {

void set_material_by_index(Materials id, Ref<Material> material);
Ref<Material> get_material_by_index(unsigned int i) const override;
unsigned int get_material_index_count() const override;

static Ref<Mesh> generate_mesh_from_image(Ref<Image> image, float voxel_size);

Expand Down
5 changes: 5 additions & 0 deletions meshers/voxel_mesher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ Ref<Material> VoxelMesher::get_material_by_index(unsigned int i) const {
return Ref<Material>();
}

unsigned int VoxelMesher::get_material_index_count() const {
// May be implemented in some meshers
return 0;
}

bool VoxelMesher::is_mesh_empty(const StdVector<Output::Surface> &surfaces) {
if (surfaces.size() == 0) {
return true;
Expand Down
2 changes: 2 additions & 0 deletions meshers/voxel_mesher.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ class VoxelMesher : public Resource {
// index does not have a material assigned. If not provided here, a default material may be used.
// An error can be produced if the index is out of bounds.
virtual Ref<Material> get_material_by_index(unsigned int i) const;
// Get the highest+1 material index
virtual unsigned int get_material_index_count() const;

#ifdef TOOLS_ENABLED
// If the mesher has problems, messages may be returned by this method so they can be shown to the user.
Expand Down

0 comments on commit d8f5293

Please sign in to comment.