Skip to content

Commit

Permalink
Enable nested VoxelModifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
sjkillen committed Sep 30, 2023
1 parent 02596c1 commit b417c7c
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions modifiers/godot/voxel_modifier_gd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace zylann::voxel::gd {

VoxelModifier::VoxelModifier() {
set_notify_local_transform(true);
set_notify_transform(true);
}

zylann::voxel::VoxelModifier *VoxelModifier::create(zylann::voxel::VoxelModifierStack &modifiers, uint32_t id) {
Expand Down Expand Up @@ -78,6 +78,17 @@ void VoxelModifier::_notification(int p_what) {
ZN_ASSERT_RETURN(parent != nullptr);
ZN_ASSERT_RETURN(_volume == nullptr);
VoxelLodTerrain *volume = Object::cast_to<VoxelLodTerrain>(parent);
// Using get_parent_node_3d when terrain is immediate parent
// would be a breaking change as it returns null if node is toplevel
if (volume == nullptr) {
Node3D *grandparent = get_parent_node_3d();
volume = Object::cast_to<VoxelLodTerrain>(grandparent);
while (grandparent != nullptr && volume == nullptr) {
grandparent = grandparent->get_parent_node_3d();
volume = Object::cast_to<VoxelLodTerrain>(grandparent);
}
}

_volume = volume;

if (_volume != nullptr) {
Expand All @@ -92,7 +103,8 @@ void VoxelModifier::_notification(int p_what) {
sdf_modifier->set_operation(to_op(_operation));
sdf_modifier->set_smoothness(_smoothness);
}

// This transform is wrong, but will be updated with NOTIFICATION_TRANSFORM_CHANGED
// After node enters the tree
modifier->set_transform(get_transform());
_modifier_id = id;
// TODO Optimize: on loading of a scene, this could be very bad for performance because there could be,
Expand All @@ -116,15 +128,16 @@ void VoxelModifier::_notification(int p_what) {
}
} break;

case Node3D::NOTIFICATION_LOCAL_TRANSFORM_CHANGED: {
case Node3D::NOTIFICATION_TRANSFORM_CHANGED: {
if (_volume != nullptr && is_inside_tree()) {
VoxelData &voxel_data = _volume->get_storage();
VoxelModifierStack &modifiers = voxel_data.get_modifiers();
zylann::voxel::VoxelModifier *modifier = modifiers.get_modifier(_modifier_id);
ZN_ASSERT_RETURN(modifier != nullptr);

const AABB prev_aabb = modifier->get_aabb();
modifier->set_transform(get_transform());
Transform3D terrain_local = _volume->get_global_transform().affine_inverse() * get_global_transform();
modifier->set_transform(terrain_local);
const AABB aabb = modifier->get_aabb();
post_edit_modifier(*_volume, prev_aabb);
post_edit_modifier(*_volume, aabb);
Expand Down

0 comments on commit b417c7c

Please sign in to comment.