Ported from Godot4: Implement global rest - TokageItLab

4d4ffa3a2c
This commit is contained in:
Relintai 2022-08-11 20:10:26 +02:00
parent da62c587e8
commit 5e3c3ff4d0
4 changed files with 45 additions and 25 deletions

View File

@ -111,13 +111,21 @@
</description>
</method>
<method name="get_bone_global_pose_override" qualifiers="const">
<return type="Transform3D" />
<return type="Transform" />
<argument index="0" name="bone_idx" type="int" />
<description>
Returns the global pose override transform for [code]bone_idx[/code].
</description>
</method>
<method name="get_bone_global_rest" qualifiers="const">
<return type="Transform" />
<argument index="0" name="bone_idx" type="int" />
<description>
Returns the global rest transform for [code]bone_idx[/code].
</description>
</method>
<method name="get_bone_local_pose_override" qualifiers="const">
<return type="Transform3D" />
<return type="Transform" />
<argument index="0" name="bone_idx" type="int" />
<description>
Returns the local pose override transform for [code]bone_idx[/code].

View File

@ -1460,9 +1460,6 @@ void SkeletonGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
surface_tool->set_material(unselected_mat);
}
Vector<Transform> grests;
grests.resize(skeleton->get_bone_count());
LocalVector<int> bones;
LocalVector<float> weights;
bones.resize(4);
@ -1486,11 +1483,6 @@ void SkeletonGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
child_bones_vector = skeleton->get_bone_children(current_bone_idx);
int child_bones_size = child_bones_vector.size();
// You have children but no parent, then you must be a root/parentless bone.
if (skeleton->get_bone_parent(current_bone_idx) < 0) {
grests.write[current_bone_idx] = skeleton->get_bone_rest(current_bone_idx);
}
for (int i = 0; i < child_bones_size; i++) {
// Something wrong.
if (child_bones_vector[i] < 0) {
@ -1499,10 +1491,8 @@ void SkeletonGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
int child_bone_idx = child_bones_vector[i];
grests.write[child_bone_idx] = grests[current_bone_idx] * skeleton->get_bone_rest(child_bone_idx);
Vector3 v0 = grests[current_bone_idx].origin;
Vector3 v1 = grests[child_bone_idx].origin;
Vector3 v0 = skeleton->get_bone_global_rest(current_bone_idx).origin;
Vector3 v1 = skeleton->get_bone_global_rest(child_bone_idx).origin;
Vector3 d = (v1 - v0).normalized();
real_t dist = v0.distance_to(v1);
@ -1510,7 +1500,7 @@ void SkeletonGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
int closest = -1;
real_t closest_d = 0.0;
for (int j = 0; j < 3; j++) {
real_t dp = Math::abs(grests[current_bone_idx].basis[j].normalized().dot(d));
real_t dp = Math::abs(skeleton->get_bone_global_rest(current_bone_idx).basis[j].normalized().dot(d));
if (j == 0 || dp > closest_d) {
closest = j;
}
@ -1537,7 +1527,7 @@ void SkeletonGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
for (int j = 0; j < 3; j++) {
Vector3 axis;
if (first == Vector3()) {
axis = d.cross(d.cross(grests[current_bone_idx].basis[j])).normalized();
axis = d.cross(d.cross(skeleton->get_bone_global_rest(current_bone_idx).basis[j])).normalized();
first = axis;
} else {
axis = d.cross(first).normalized();
@ -1592,7 +1582,7 @@ void SkeletonGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
surface_tool->add_vertex(v0);
surface_tool->add_bones(bones);
surface_tool->add_weights(weights);
surface_tool->add_vertex(v0 + (grests[current_bone_idx].basis.inverse())[j].normalized() * dist * bone_axis_length);
surface_tool->add_vertex(v0 + (skeleton->get_bone_global_rest(current_bone_idx).basis.inverse())[j].normalized() * dist * bone_axis_length);
if (j == closest) {
continue;
@ -1609,7 +1599,7 @@ void SkeletonGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
surface_tool->add_vertex(v1);
surface_tool->add_bones(bones);
surface_tool->add_weights(weights);
surface_tool->add_vertex(v1 + (grests[child_bone_idx].basis.inverse())[j].normalized() * dist * bone_axis_length);
surface_tool->add_vertex(v1 + (skeleton->get_bone_global_rest(child_bone_idx).basis.inverse())[j].normalized() * dist * bone_axis_length);
if (j == closest) {
continue;

View File

@ -524,6 +524,7 @@ void Skeleton::add_bone(const String &p_name) {
bones.push_back(b);
process_order_dirty = true;
version++;
rest_dirty = true;
_make_dirty();
update_gizmos();
}
@ -582,7 +583,7 @@ void Skeleton::set_bone_parent(int p_bone, int p_parent) {
bones.write[p_bone].parent = p_parent;
process_order_dirty = true;
version++;
rest_dirty = true;
_make_dirty();
}
@ -601,7 +602,7 @@ void Skeleton::unparent_bone_and_rest(int p_bone) {
bones.write[p_bone].parent = -1;
process_order_dirty = true;
version++;
rest_dirty = true;
_make_dirty();
}
@ -625,7 +626,7 @@ void Skeleton::set_bone_children(int p_bone, Vector<int> p_children) {
bones.write[p_bone].child_bones = p_children;
process_order_dirty = true;
version++;
rest_dirty = true;
_make_dirty();
}
@ -636,7 +637,7 @@ void Skeleton::add_bone_child(int p_bone, int p_child) {
bones.write[p_bone].child_bones.push_back(p_child);
process_order_dirty = true;
version++;
rest_dirty = true;
_make_dirty();
}
@ -653,7 +654,7 @@ void Skeleton::remove_bone_child(int p_bone, int p_child) {
}
process_order_dirty = true;
version++;
rest_dirty = true;
_make_dirty();
}
@ -667,6 +668,7 @@ void Skeleton::set_bone_rest(int p_bone, const Transform &p_rest) {
ERR_FAIL_INDEX(p_bone, bone_size);
bones.write[p_bone].rest = p_rest;
rest_dirty = true;
_make_dirty();
}
Transform Skeleton::get_bone_rest(int p_bone) const {
@ -676,6 +678,17 @@ Transform Skeleton::get_bone_rest(int p_bone) const {
return bones[p_bone].rest;
}
Transform Skeleton::get_bone_global_rest(int p_bone) const {
const int bone_size = bones.size();
ERR_FAIL_INDEX_V(p_bone, bone_size, Transform());
if (rest_dirty) {
const_cast<Skeleton *>(this)->notification(NOTIFICATION_UPDATE_SKELETON);
}
return bones[p_bone].global_rest;
}
void Skeleton::set_bone_enabled(int p_bone, bool p_enabled) {
const int bone_size = bones.size();
ERR_FAIL_INDEX(p_bone, bone_size);
@ -1105,6 +1118,10 @@ void Skeleton::force_update_bone_children_transforms(int p_bone_idx) {
}
}
if (rest_dirty) {
b.global_rest = b.parent >= 0 ? bonesptr[b.parent].global_rest * b.rest : b.rest;
}
if (b.local_pose_override_amount >= CMP_EPSILON) {
Transform override_local_pose;
if (b.parent >= 0) {
@ -1134,6 +1151,8 @@ void Skeleton::force_update_bone_children_transforms(int p_bone_idx) {
emit_signal(SceneStringNames::get_singleton()->bone_pose_changed, current_bone_idx);
}
rest_dirty = false;
}
Transform Skeleton::global_pose_to_world_transform(Transform p_global_pose) {
@ -1251,6 +1270,7 @@ void Skeleton::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_bone_rest", "bone_idx"), &Skeleton::get_bone_rest);
ClassDB::bind_method(D_METHOD("set_bone_rest", "bone_idx", "rest"), &Skeleton::set_bone_rest);
ClassDB::bind_method(D_METHOD("get_bone_global_rest", "bone_idx"), &Skeleton::get_bone_global_rest);
ClassDB::bind_method(D_METHOD("create_skin_from_rest_transforms"), &Skeleton::create_skin_from_rest_transforms);
@ -1336,6 +1356,7 @@ Skeleton::Skeleton() {
version = 1;
process_order_dirty = true;
show_rest_only = false;
rest_dirty = false;
}
Skeleton::~Skeleton() {

View File

@ -84,8 +84,7 @@ private:
int parent;
Transform rest;
//Transform pose;
Transform global_rest;
_FORCE_INLINE_ void update_pose_cache() {
if (pose_cache_dirty) {
@ -151,6 +150,7 @@ private:
void _make_dirty();
bool dirty;
bool rest_dirty;
bool show_rest_only;
@ -210,6 +210,7 @@ public:
void set_bone_rest(int p_bone, const Transform &p_rest);
Transform get_bone_rest(int p_bone) const;
Transform get_bone_global_rest(int p_bone) const;
Transform get_bone_global_pose(int p_bone) const;
Transform get_bone_global_pose_no_override(int p_bone) const;