Skip to content

Commit

Permalink
Refactored bone pose override in Skeleton3D
Browse files Browse the repository at this point in the history
  • Loading branch information
TokageItLab committed Mar 6, 2022
1 parent 7183590 commit e7326a9
Show file tree
Hide file tree
Showing 15 changed files with 172 additions and 221 deletions.
13 changes: 0 additions & 13 deletions doc/classes/BoneAttachment3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
Returns the [NodePath] to the external [Skeleton3D] node, if one has been set.
</description>
</method>
<method name="get_override_mode" qualifiers="const">
<return type="int" />
<description>
Returns the override mode for the BoneAttachment3D node.
</description>
</method>
<method name="get_override_pose" qualifiers="const">
<return type="bool" />
<description>
Expand All @@ -48,13 +42,6 @@
Sets the [NodePath] to the external skeleton that the BoneAttachment3D node should use. The external [Skeleton3D] node is only used when [code]use_external_skeleton[/code] is set to [code]true[/code].
</description>
</method>
<method name="set_override_mode">
<return type="void" />
<argument index="0" name="override_mode" type="int" />
<description>
Sets the override mode for the BoneAttachment3D node. The override mode defines which of the bone poses the BoneAttachment3D node will override.
</description>
</method>
<method name="set_override_pose">
<return type="void" />
<argument index="0" name="override_pose" type="bool" />
Expand Down
66 changes: 38 additions & 28 deletions doc/classes/Skeleton3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,23 @@
This is useful for using the bone transform in calculations with transforms from [Node3D]-based nodes.
</description>
</method>
<method name="clear_bones">
<method name="clear_bone_pose_override">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
<description>
Clear all the bones in this skeleton.
Removes the pose override on the bone at [code]bone_idx[/code].
</description>
</method>
<method name="clear_bones_global_pose_override">
<method name="clear_bone_pose_overrides">
<return type="void" />
<description>
Removes the global pose override on all bones in the skeleton.
Removes the pose override on all bones in the skeleton.
</description>
</method>
<method name="clear_bones_local_pose_override">
<method name="clear_bones">
<return type="void" />
<description>
Removes the local pose override on all bones in the skeleton.
Clear all the bones in this skeleton.
</description>
</method>
<method name="create_skin_from_rest_transforms">
Expand Down Expand Up @@ -113,13 +114,6 @@
<description>
</description>
</method>
<method name="get_bone_local_pose_override" qualifiers="const">
<return type="Transform3D" />
<argument index="0" name="bone_idx" type="int" />
<description>
Returns the local pose override transform for [code]bone_idx[/code].
</description>
</method>
<method name="get_bone_name" qualifiers="const">
<return type="String" />
<argument index="0" name="bone_idx" type="int" />
Expand All @@ -142,6 +136,20 @@
Returns the pose transform of the specified bone. Pose is applied on top of the custom pose, which is applied on top the rest pose.
</description>
</method>
<method name="get_bone_pose_no_override" qualifiers="const">
<return type="Transform3D" />
<argument index="0" name="bone_idx" type="int" />
<description>
Returns the pose without override transform for [code]bone_idx[/code].
</description>
</method>
<method name="get_bone_pose_override" qualifiers="const">
<return type="Transform3D" />
<argument index="0" name="bone_idx" type="int" />
<description>
Returns the pose override transform for [code]bone_idx[/code].
</description>
</method>
<method name="get_bone_pose_position" qualifiers="const">
<return type="Vector3" />
<argument index="0" name="bone_idx" type="int" />
Expand Down Expand Up @@ -185,7 +193,7 @@
<argument index="1" name="global_pose" type="Transform3D" />
<description>
Takes the passed-in global pose and converts it to local pose transform.
This can be used to easily convert a global pose from [method get_bone_global_pose] to a global transform in [method set_bone_local_pose_override].
This can be used to easily convert a global pose from [method get_bone_global_pose] to a global transform in [method set_bone_pose_override].
</description>
</method>
<method name="global_pose_to_world_transform">
Expand All @@ -212,6 +220,12 @@
Returns whether the bone pose for the bone at [code]bone_idx[/code] is enabled.
</description>
</method>
<method name="is_bone_pose_overridden" qualifiers="const">
<return type="bool" />
<argument index="0" name="bone_idx" type="int" />
<description>
</description>
</method>
<method name="local_pose_to_global_pose">
<return type="Transform3D" />
<argument index="0" name="bone_idx" type="int" />
Expand Down Expand Up @@ -294,25 +308,11 @@
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="pose" type="Transform3D" />
<argument index="2" name="amount" type="float" />
<argument index="3" name="persistent" type="bool" default="false" />
<description>
Sets the global pose transform, [code]pose[/code], for the bone at [code]bone_idx[/code].
[code]amount[/code] is the interpolation strength that will be used when applying the pose, and [code]persistent[/code] determines if the applied pose will remain.
[b]Note:[/b] The pose transform needs to be a global pose! Use [method world_transform_to_global_pose] to convert a world transform, like one you can get from a [Node3D], to a global pose.
</description>
</method>
<method name="set_bone_local_pose_override">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="pose" type="Transform3D" />
<argument index="2" name="amount" type="float" />
<argument index="3" name="persistent" type="bool" default="false" />
<description>
Sets the local pose transform, [code]pose[/code], for the bone at [code]bone_idx[/code].
[code]amount[/code] is the interpolation strength that will be used when applying the pose, and [code]persistent[/code] determines if the applied pose will remain.
[b]Note:[/b] The pose transform needs to be a local pose! Use [method global_pose_to_local_pose] to convert a global pose to a local pose.
</description>
</method>
<method name="set_bone_name">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
Expand All @@ -329,6 +329,16 @@
[b]Note:[/b] [code]parent_idx[/code] must be less than [code]bone_idx[/code].
</description>
</method>
<method name="set_bone_pose_override">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
<argument index="1" name="pose" type="Transform3D" />
<argument index="2" name="amount" type="float" />
<description>
Sets the local pose transform, [code]pose[/code], for the bone at [code]bone_idx[/code].
[b]Note:[/b] The pose transform needs to be a local pose! Use [method global_pose_to_local_pose] to convert a global pose to a local pose.
</description>
</method>
<method name="set_bone_pose_position">
<return type="void" />
<argument index="0" name="bone_idx" type="int" />
Expand Down
1 change: 0 additions & 1 deletion editor/plugins/skeleton_ik_3d_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ void SkeletonIK3DEditorPlugin::_play() {
skeleton_ik->start();
} else {
skeleton_ik->stop();
skeleton_ik->get_parent_skeleton()->clear_bones_global_pose_override();
}
}

Expand Down
42 changes: 2 additions & 40 deletions scene/3d/bone_attachment_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ void BoneAttachment3D::_validate_property(PropertyInfo &property) const {
bool BoneAttachment3D::_set(const StringName &p_path, const Variant &p_value) {
if (p_path == SNAME("override_pose")) {
set_override_pose(p_value);
} else if (p_path == SNAME("override_mode")) {
set_override_mode(p_value);
} else if (p_path == SNAME("use_external_skeleton")) {
set_use_external_skeleton(p_value);
} else if (p_path == SNAME("external_skeleton")) {
Expand All @@ -79,8 +77,6 @@ bool BoneAttachment3D::_set(const StringName &p_path, const Variant &p_value) {
bool BoneAttachment3D::_get(const StringName &p_path, Variant &r_ret) const {
if (p_path == SNAME("override_pose")) {
r_ret = get_override_pose();
} else if (p_path == SNAME("override_mode")) {
r_ret = get_override_mode();
} else if (p_path == SNAME("use_external_skeleton")) {
r_ret = get_use_external_skeleton();
} else if (p_path == SNAME("external_skeleton")) {
Expand All @@ -92,9 +88,6 @@ bool BoneAttachment3D::_get(const StringName &p_path, Variant &r_ret) const {

void BoneAttachment3D::_get_property_list(List<PropertyInfo> *p_list) const {
p_list->push_back(PropertyInfo(Variant::BOOL, "override_pose", PROPERTY_HINT_NONE, ""));
if (override_pose) {
p_list->push_back(PropertyInfo(Variant::INT, "override_mode", PROPERTY_HINT_ENUM, "Global Pose Override, Local Pose Override, Custom Pose"));
}

p_list->push_back(PropertyInfo(Variant::BOOL, "use_external_skeleton", PROPERTY_HINT_NONE, ""));
if (use_external_skeleton) {
Expand Down Expand Up @@ -213,11 +206,7 @@ void BoneAttachment3D::_transform_changed() {
our_trans = sk->world_transform_to_global_pose(get_global_transform());
}

if (override_mode == OVERRIDE_MODES::MODE_GLOBAL_POSE) {
sk->set_bone_global_pose_override(bone_idx, our_trans, 1.0, true);
} else if (override_mode == OVERRIDE_MODES::MODE_LOCAL_POSE) {
sk->set_bone_local_pose_override(bone_idx, sk->global_pose_to_local_pose(bone_idx, our_trans), 1.0, true);
}
sk->set_bone_global_pose_override(bone_idx, our_trans, 1);
}
}

Expand Down Expand Up @@ -269,11 +258,7 @@ void BoneAttachment3D::set_override_pose(bool p_override) {
if (!override_pose) {
Skeleton3D *sk = _get_skeleton3d();
if (sk) {
if (override_mode == OVERRIDE_MODES::MODE_GLOBAL_POSE) {
sk->set_bone_global_pose_override(bone_idx, Transform3D(), 0.0, false);
} else if (override_mode == OVERRIDE_MODES::MODE_LOCAL_POSE) {
sk->set_bone_local_pose_override(bone_idx, Transform3D(), 0.0, false);
}
sk->clear_bone_pose_override(bone_idx);
}
_transform_changed();
}
Expand All @@ -284,27 +269,6 @@ bool BoneAttachment3D::get_override_pose() const {
return override_pose;
}

void BoneAttachment3D::set_override_mode(int p_mode) {
if (override_pose) {
Skeleton3D *sk = _get_skeleton3d();
if (sk) {
if (override_mode == OVERRIDE_MODES::MODE_GLOBAL_POSE) {
sk->set_bone_global_pose_override(bone_idx, Transform3D(), 0.0, false);
} else if (override_mode == OVERRIDE_MODES::MODE_LOCAL_POSE) {
sk->set_bone_local_pose_override(bone_idx, Transform3D(), 0.0, false);
}
}
override_mode = p_mode;
_transform_changed();
return;
}
override_mode = p_mode;
}

int BoneAttachment3D::get_override_mode() const {
return override_mode;
}

void BoneAttachment3D::set_use_external_skeleton(bool p_use_external) {
use_external_skeleton = p_use_external;

Expand Down Expand Up @@ -391,8 +355,6 @@ void BoneAttachment3D::_bind_methods() {

ClassDB::bind_method(D_METHOD("set_override_pose", "override_pose"), &BoneAttachment3D::set_override_pose);
ClassDB::bind_method(D_METHOD("get_override_pose"), &BoneAttachment3D::get_override_pose);
ClassDB::bind_method(D_METHOD("set_override_mode", "override_mode"), &BoneAttachment3D::set_override_mode);
ClassDB::bind_method(D_METHOD("get_override_mode"), &BoneAttachment3D::get_override_mode);

ClassDB::bind_method(D_METHOD("set_use_external_skeleton", "use_external_skeleton"), &BoneAttachment3D::set_use_external_skeleton);
ClassDB::bind_method(D_METHOD("get_use_external_skeleton"), &BoneAttachment3D::get_use_external_skeleton);
Expand Down
8 changes: 0 additions & 8 deletions scene/3d/bone_attachment_3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,8 @@ class BoneAttachment3D : public Node3D {
int bone_idx = -1;

bool override_pose = false;
int override_mode = 0;
bool _override_dirty = false;

enum OVERRIDE_MODES {
MODE_GLOBAL_POSE,
MODE_LOCAL_POSE,
};

bool use_external_skeleton = false;
NodePath external_skeleton_node;
ObjectID external_skeleton_node_cache;
Expand Down Expand Up @@ -80,8 +74,6 @@ class BoneAttachment3D : public Node3D {

void set_override_pose(bool p_override);
bool get_override_pose() const;
void set_override_mode(int p_mode);
int get_override_mode() const;

void set_use_external_skeleton(bool p_external_skeleton);
bool get_use_external_skeleton() const;
Expand Down
4 changes: 2 additions & 2 deletions scene/3d/physics_body_3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2915,7 +2915,7 @@ void PhysicalBone3D::_body_state_changed(PhysicsDirectBodyState3D *p_state) {
// Update skeleton
if (parent_skeleton) {
if (-1 != bone_id) {
parent_skeleton->set_bone_global_pose_override(bone_id, parent_skeleton->get_global_transform().affine_inverse() * (global_transform * body_offset_inverse), 1.0, true);
parent_skeleton->set_bone_global_pose_override(bone_id, parent_skeleton->get_global_transform().affine_inverse() * (global_transform * body_offset_inverse), 1);
}
}
}
Expand Down Expand Up @@ -3429,7 +3429,7 @@ void PhysicalBone3D::_stop_physics_simulation() {
}
if (_internal_simulate_physics) {
PhysicsServer3D::get_singleton()->body_set_state_sync_callback(get_rid(), nullptr, nullptr);
parent_skeleton->set_bone_global_pose_override(bone_id, Transform3D(), 0.0, false);
parent_skeleton->clear_bone_pose_override(bone_id);
set_as_top_level(false);
_internal_simulate_physics = false;
}
Expand Down
Loading

0 comments on commit e7326a9

Please sign in to comment.