Skip to content

Commit

Permalink
Added an array mesh conversion helper method to ProceduralTreeMesh.
Browse files Browse the repository at this point in the history
  • Loading branch information
Relintai committed Sep 8, 2024
1 parent 2124f48 commit e426350
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
44 changes: 35 additions & 9 deletions modules/procedural_tree_3d/procedural_tree_mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,16 @@ Ref<Material> ProceduralTreeMesh::get_trunk_material() const {
return _surfaces[TREE_SURFACE_TRUNK].material;
}

void ProceduralTreeMesh::set_custom_aabb(const AABB &p_custom) {
custom_aabb = p_custom;
RS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
emit_changed();
}

AABB ProceduralTreeMesh::get_custom_aabb() const {
return custom_aabb;
}

Array ProceduralTreeMesh::get_mesh_arrays() const {
Array arr;

Expand All @@ -660,14 +670,29 @@ Array ProceduralTreeMesh::get_mesh_arrays() const {
return arr;
}

void ProceduralTreeMesh::set_custom_aabb(const AABB &p_custom) {
custom_aabb = p_custom;
RS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
emit_changed();
}
Ref<ArrayMesh> ProceduralTreeMesh::to_array_mesh() const {
Ref<ArrayMesh> mesh;
mesh.instance();

AABB ProceduralTreeMesh::get_custom_aabb() const {
return custom_aabb;
if (!_enable_twig_mesh && !_enable_branch_mesh) {
return mesh;
}

for (int i = 0; i < TREE_SURFACE_COUNT; ++i) {
int si = _surfaces[i].surface_index;

if (si == -1) {
continue;
}

Array arr = surface_get_arrays(i);

mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
int msi = mesh->get_surface_count() - 1;
mesh->surface_set_material(msi, surface_get_material(i));
}

return mesh;
}

ProceduralTreeMesh::ProceduralTreeMesh() {
Expand Down Expand Up @@ -818,8 +843,6 @@ void ProceduralTreeMesh::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_trunk_material", "material"), &ProceduralTreeMesh::set_trunk_material);
ClassDB::bind_method(D_METHOD("get_trunk_material"), &ProceduralTreeMesh::get_trunk_material);

ClassDB::bind_method(D_METHOD("get_mesh_arrays"), &ProceduralTreeMesh::get_mesh_arrays);

ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &ProceduralTreeMesh::set_custom_aabb);
ClassDB::bind_method(D_METHOD("get_custom_aabb"), &ProceduralTreeMesh::get_custom_aabb);

Expand All @@ -844,6 +867,9 @@ void ProceduralTreeMesh::_bind_methods() {

ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, ""), "set_custom_aabb", "get_custom_aabb");

ClassDB::bind_method(D_METHOD("get_mesh_arrays"), &ProceduralTreeMesh::get_mesh_arrays);
ClassDB::bind_method(D_METHOD("to_array_mesh"), &ProceduralTreeMesh::to_array_mesh);

BIND_ENUM_CONSTANT(TREE_SURFACE_TRUNK);
BIND_ENUM_CONSTANT(TREE_SURFACE_TWIG);
BIND_ENUM_CONSTANT(TREE_SURFACE_COUNT);
Expand Down
5 changes: 3 additions & 2 deletions modules/procedural_tree_3d/procedural_tree_mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,12 @@ class ProceduralTreeMesh : public Mesh {
void set_trunk_material(const Ref<Material> &p_material);
Ref<Material> get_trunk_material() const;

Array get_mesh_arrays() const;

void set_custom_aabb(const AABB &p_custom);
AABB get_custom_aabb() const;

Array get_mesh_arrays() const;
Ref<ArrayMesh> to_array_mesh() const;

ProceduralTreeMesh();
~ProceduralTreeMesh();

Expand Down

0 comments on commit e426350

Please sign in to comment.