Physics Interpolation - Fix 2D skinning

2D skinning required the interpolated skeleton base transform to be updated when using interpolation.
This commit is contained in:
lawnjelly 2024-06-18 14:22:26 +01:00 committed by Relintai
parent 9d8dfe2a31
commit 88406b7fbc
2 changed files with 101 additions and 31 deletions

View File

@ -35,6 +35,8 @@
#include "../resources/skeleton_modification_stack_2d.h"
#include "core/config/engine.h"
#include "scene/main/scene_string_names.h"
#include "core/config/engine.h"
#include "core/math/transform_interpolator.h"
#ifdef TOOLS_ENABLED
#include "editor/editor_data.h"
@ -657,40 +659,97 @@ Bone2D *Skeleton2D::get_bone(int p_idx) {
return bones[p_idx].bone;
}
void Skeleton2D::_notification(int p_what) {
if (p_what == NOTIFICATION_READY) {
if (bone_setup_dirty) {
_update_bone_setup();
}
if (transform_dirty) {
_update_transform();
}
void Skeleton2D::_update_process_mode() {
bool process = is_physics_interpolated_and_enabled() && is_visible_in_tree();
request_ready();
}
if (p_what == NOTIFICATION_TRANSFORM_CHANGED) {
RS::get_singleton()->skeleton_set_base_transform_2d(skeleton, get_global_transform());
} else if (p_what == NOTIFICATION_INTERNAL_PROCESS) {
if (modification_stack.is_valid()) {
execute_modifications(get_process_delta_time(), SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_process);
}
} else if (p_what == NOTIFICATION_INTERNAL_PHYSICS_PROCESS) {
if (modification_stack.is_valid()) {
execute_modifications(get_physics_process_delta_time(), SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_physics_process);
}
}
#ifdef TOOLS_ENABLED
else if (p_what == NOTIFICATION_DRAW) {
if (Engine::get_singleton()->is_editor_hint()) {
if (modification_stack.is_valid()) {
modification_stack->draw_editor_gizmos();
}
}
}
#endif // TOOLS_ENABLED
set_process_internal(process);
set_physics_process_internal(process);
}
void Skeleton2D::_ensure_update_interpolation_data() {
uint64_t tick = Engine::get_singleton()->get_physics_frames();
if (_interpolation_data.last_update_physics_tick != tick) {
_interpolation_data.xform_prev = _interpolation_data.xform_curr;
_interpolation_data.last_update_physics_tick = tick;
}
}
void Skeleton2D::_physics_interpolated_changed() {
_update_process_mode();
}
void Skeleton2D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_READY: {
if (bone_setup_dirty) {
_update_bone_setup();
}
if (transform_dirty) {
_update_transform();
}
request_ready();
} break;
case NOTIFICATION_ENTER_TREE: {
_update_process_mode();
if (is_physics_interpolated_and_enabled()) {
_interpolation_data.xform_curr = get_global_transform();
_interpolation_data.xform_prev = _interpolation_data.xform_curr;
}
} break;
case NOTIFICATION_TRANSFORM_CHANGED: {
if (is_physics_interpolated_and_enabled()) {
_ensure_update_interpolation_data();
if (Engine::get_singleton()->is_in_physics_frame()) {
_interpolation_data.xform_curr = get_global_transform();
}
} else {
RS::get_singleton()->skeleton_set_base_transform_2d(skeleton, get_global_transform());
}
} break;
case NOTIFICATION_RESET_PHYSICS_INTERPOLATION: {
_interpolation_data.xform_curr = get_global_transform();
_interpolation_data.xform_prev = _interpolation_data.xform_curr;
} break;
case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
if (modification_stack.is_valid()) {
execute_modifications(get_physics_process_delta_time(), SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_physics_process);
}
if (is_physics_interpolated_and_enabled()) {
_ensure_update_interpolation_data();
_interpolation_data.xform_curr = get_global_transform();
}
} break;
case NOTIFICATION_INTERNAL_PROCESS: {
if (modification_stack.is_valid()) {
execute_modifications(get_process_delta_time(), SkeletonModificationStack2D::EXECUTION_MODE::execution_mode_process);
}
if (is_physics_interpolated_and_enabled()) {
Transform2D res;
TransformInterpolator::interpolate_transform_2d(_interpolation_data.xform_prev, _interpolation_data.xform_curr, res, Engine::get_singleton()->get_physics_interpolation_fraction());
RS::get_singleton()->skeleton_set_base_transform_2d(skeleton, res);
}
} break;
case NOTIFICATION_VISIBILITY_CHANGED: {
_update_process_mode();
} break;
#ifdef TOOLS_ENABLED
case NOTIFICATION_DRAW: {
if (Engine::get_singleton()->is_editor_hint()) {
if (modification_stack.is_valid()) {
modification_stack->draw_editor_gizmos();
}
}
} break;
#endif // TOOLS_ENABLED
}
}
RID Skeleton2D::get_skeleton() const {
return skeleton;
}

View File

@ -144,6 +144,15 @@ class Skeleton2D : public Node2D {
Ref<SkeletonModificationStack2D> modification_stack;
void _update_process_mode();
void _ensure_update_interpolation_data();
struct InterpolationData {
Transform2D xform_curr;
Transform2D xform_prev;
uint32_t last_update_physics_tick = UINT32_MAX;
} _interpolation_data;
protected:
void _notification(int p_what);
static void _bind_methods();
@ -151,6 +160,8 @@ protected:
bool _get(const StringName &p_path, Variant &r_ret) const;
void _get_property_list(List<PropertyInfo> *p_list) const;
virtual void _physics_interpolated_changed();
public:
int get_bone_count() const;
Bone2D *get_bone(int p_idx);