Skip to content

Commit

Permalink
Optimize update flag by SkeletonModifier
Browse files Browse the repository at this point in the history
  • Loading branch information
TokageItLab committed Jun 23, 2024
1 parent 04bf7d4 commit 5774d57
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
24 changes: 17 additions & 7 deletions scene/3d/skeleton_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ void Skeleton3D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
_process_changed();
_make_dirty();
_make_modifiers_dirty();
force_update_all_dirty_bones();
#ifndef DISABLE_DEPRECATED
Expand All @@ -333,6 +334,13 @@ void Skeleton3D::_notification(int p_what) {
_process_modifiers();
}

// Abort if pose is not changed.
if (!(update_flags & UPDATE_FLAG_POSE)) {
updating = false;
update_flags = UPDATE_FLAG_NONE;
return;
}

emit_signal(SceneStringName(skeleton_updated));

// Update skins.
Expand Down Expand Up @@ -400,13 +408,13 @@ void Skeleton3D::_notification(int p_what) {
}

updating = false;
is_update_needed = false;
update_flags = UPDATE_FLAG_NONE;
} break;
case NOTIFICATION_INTERNAL_PROCESS:
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
_find_modifiers();
if (!modifiers.is_empty()) {
_update_deferred();
_update_deferred(UPDATE_FLAG_MODIFIER);
}
} break;
}
Expand Down Expand Up @@ -436,7 +444,7 @@ void Skeleton3D::_process_changed() {

void Skeleton3D::_make_modifiers_dirty() {
modifiers_dirty = true;
_update_deferred();
_update_deferred(UPDATE_FLAG_MODIFIER);
}

Transform3D Skeleton3D::get_bone_global_pose(int p_bone) const {
Expand Down Expand Up @@ -745,10 +753,12 @@ void Skeleton3D::_make_dirty() {
_update_deferred();
}

void Skeleton3D::_update_deferred() {
if (!is_update_needed && !updating && is_inside_tree()) {
is_update_needed = true;
notify_deferred_thread_group(NOTIFICATION_UPDATE_SKELETON); // It must never be called more than once in a single frame.
void Skeleton3D::_update_deferred(UpdateFlag p_update_flag) {
if (is_inside_tree()) {
if (update_flags == UPDATE_FLAG_NONE && !updating) {
notify_deferred_thread_group(NOTIFICATION_UPDATE_SKELETON); // It must never be called more than once in a single frame.
}
update_flags |= p_update_flag;
}
}

Expand Down
10 changes: 8 additions & 2 deletions scene/3d/skeleton_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,14 @@ class Skeleton3D : public Node3D {
private:
friend class SkinReference;

void _update_deferred();
bool is_update_needed = false; // Is updating reserved?
enum UpdateFlag {
UPDATE_FLAG_NONE = 1,
UPDATE_FLAG_MODIFIER = 2,
UPDATE_FLAG_POSE = 4,
};

void _update_deferred(UpdateFlag p_update_flag = UPDATE_FLAG_POSE);
uint8_t update_flags = UPDATE_FLAG_NONE;
bool updating = false; // Is updating now?

struct Bone {
Expand Down

0 comments on commit 5774d57

Please sign in to comment.