Skip to content

Commit

Permalink
Merge pull request #15 from Daylily-Zeleen/daylily-zeleen/fix-duplica…
Browse files Browse the repository at this point in the history
…te-issue

Reuse `twig_inst`.
  • Loading branch information
JekSun97 authored Nov 11, 2024
2 parents b5820fa + 3351fc3 commit d718544
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 51 deletions.
60 changes: 12 additions & 48 deletions src/Tree3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void Tree3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_uv_multiplier"), &Tree3D::get_uv_multiplier);

ClassDB::bind_method(D_METHOD("set_twig_enable", "enable"), &Tree3D::set_twig_enable);
ClassDB::bind_method(D_METHOD("get_twig_enable"), &Tree3D::get_twig_enable);
ClassDB::bind_method(D_METHOD("is_twig_enable"), &Tree3D::is_twig_enable);
ClassDB::bind_method(D_METHOD("set_twig_scale", "scale"), &Tree3D::set_twig_scale);
ClassDB::bind_method(D_METHOD("get_twig_scale"), &Tree3D::get_twig_scale);

Expand Down Expand Up @@ -80,7 +80,7 @@ void Tree3D::_bind_methods() {
ClassDB::add_property("Tree3D", PropertyInfo(Variant::FLOAT, "trunk_uv_multiplier", PROPERTY_HINT_RANGE, "0.001,50,0.001"), "set_uv_multiplier", "get_uv_multiplier");

ADD_GROUP("Twig", "twig_");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "twig_enable"), "set_twig_enable", "get_twig_enable");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "twig_enable"), "set_twig_enable", "is_twig_enable");
ClassDB::add_property("Tree3D", PropertyInfo(Variant::FLOAT, "twig_scale", PROPERTY_HINT_RANGE, "0,5,0.001"), "set_twig_scale", "get_twig_scale");

ADD_GROUP("Materials", "material_");
Expand All @@ -105,19 +105,6 @@ Tree3D::~Tree3D() {
void Tree3D::_process(double delta) {
}

void Tree3D::_exit_tree() {
//godot::UtilityFunctions::print("Tree3D exit");
remove_child(trunk_inst);
trunk_inst=nullptr;
if(twig_inst)
{
remove_child(twig_inst);
twig_inst=nullptr;
}

}


void Tree3D::set_seed(int seed) {
tree.mProperties.mSeed = seed;
UpdateAllMeshes();
Expand Down Expand Up @@ -170,24 +157,18 @@ float Tree3D::get_twig_scale() {
}

void Tree3D::set_twig_enable(bool value) {
ERR_FAIL_NULL(twig_inst);
twig_inst->set_visible(value);
if (value)
{
twig_inst = memnew(MeshInstance3D);
this->add_child(twig_inst);
tree.generate();
UpdateMeshTwig();
}
else
{
remove_child(twig_inst);
twig_inst=nullptr;
}
_twig_enable=value;
//twig_inst->set_visible(value);
}

bool Tree3D::get_twig_enable() {
return _twig_enable;
bool Tree3D::is_twig_enable() {
ERR_FAIL_NULL_V(twig_inst, false);
return twig_inst->is_visible();
}


Expand Down Expand Up @@ -355,25 +336,13 @@ Ref<Material> Tree3D::get_material_trunk() {
}

void Tree3D::set_material_twig(const Ref<Material> &mat) {
if (twig_inst)
{
ERR_FAIL_NULL(twig_inst);
twig_inst->set_surface_override_material(0,mat);
}
else
{
return;
}
}

Ref<Material> Tree3D::get_material_twig() {
if (twig_inst)
{
ERR_FAIL_NULL_V(twig_inst, nullptr);
return twig_inst->get_surface_override_material(0);
}
else
{
return nullptr;
}
}


Expand Down Expand Up @@ -407,8 +376,7 @@ void Tree3D::UpdateMeshTrunk()

void Tree3D::UpdateMeshTwig()
{
if (twig_inst)
{
ERR_FAIL_NULL(twig_inst);
Ref<SurfaceTool> st;
st.instantiate();
st->begin(Mesh::PRIMITIVE_TRIANGLES);
Expand All @@ -427,17 +395,13 @@ void Tree3D::UpdateMeshTwig()
}
st->optimize_indices_for_cache();
twig_inst->set_mesh(st->commit());
}
else
{
return;
}

}

void Tree3D::UpdateAllMeshes() {
tree.generate();
UpdateMeshTrunk();
if (is_twig_enable()) {
UpdateMeshTwig();
}
}

4 changes: 1 addition & 3 deletions src/Tree3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class Tree3D : public Node3D {
void UpdateMeshTrunk();
void UpdateMeshTwig();
void UpdateAllMeshes();
bool _twig_enable = true;

protected:
static void _bind_methods();
Expand All @@ -35,7 +34,6 @@ class Tree3D : public Node3D {
~Tree3D();

void _process(double delta) override;
void _exit_tree() override;

void set_seed(int seed);
int get_seed();
Expand Down Expand Up @@ -84,7 +82,7 @@ class Tree3D : public Node3D {
float get_uv_multiplier() const;

void set_twig_enable(bool value);
bool get_twig_enable();
bool is_twig_enable();
void set_twig_scale(float value);
float get_twig_scale();

Expand Down

0 comments on commit d718544

Please sign in to comment.