Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
TokageItLab committed Sep 29, 2024
1 parent 506d6e4 commit 314635f
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 1 deletion.
19 changes: 19 additions & 0 deletions doc/classes/RetargetModifier3D.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="RetargetModifier3D" inherits="SkeletonModifier3D" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
<brief_description>
</brief_description>
<description>
</description>
<tutorials>
</tutorials>
<members>
<member name="position_enabled" type="bool" setter="set_position_enabled" getter="is_position_enabled" default="false">
</member>
<member name="profile" type="SkeletonProfile" setter="set_profile" getter="get_profile">
</member>
<member name="rotation_enabled" type="bool" setter="set_rotation_enabled" getter="is_rotation_enabled" default="true">
</member>
<member name="scale_enabled" type="bool" setter="set_scale_enabled" getter="is_scale_enabled" default="false">
</member>
</members>
</class>
4 changes: 4 additions & 0 deletions doc/classes/Skeleton3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@
[b]Note:[/b] During the update process, this signal is not fired, so modification by [SkeletonModifier3D] is not detected.
</description>
</signal>
<signal name="rest_updated">
<description>
</description>
</signal>
<signal name="show_rest_only_changed">
<description>
Emitted when the value of [member show_rest_only] changes.
Expand Down
325 changes: 325 additions & 0 deletions scene/3d/retarget_modifier_3d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,325 @@
/**************************************************************************/
/* retarget_modifier_3d.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#include "retarget_modifier_3d.h"

PackedStringArray RetargetModifier3D::get_configuration_warnings() const {
PackedStringArray warnings = SkeletonModifier3D::get_configuration_warnings();
if (child_skeletons.is_empty()) {
warnings.push_back(RTR("There is no child Skeleton3D!"));
}
return warnings;
}

/// Caching

void RetargetModifier3D::_profile_changed(Ref<SkeletonProfile> p_old, Ref<SkeletonProfile> p_new) {
if (p_old.is_valid() && p_old->is_connected(SNAME("profile_updated"), callable_mp(this, &RetargetModifier3D::_profile_changed))) {
p_old->disconnect(SNAME("profile_updated"), callable_mp(this, &RetargetModifier3D::cache_all_rests));
}
profile = p_new;
if (p_new.is_valid() && !p_new->is_connected(SNAME("profile_updated"), callable_mp(this, &RetargetModifier3D::_profile_changed))) {
p_new->connect(SNAME("profile_updated"), callable_mp(this, &RetargetModifier3D::cache_all_rests));
}
cache_all_rests();
}

void RetargetModifier3D::_skeleton_changed(Skeleton3D *p_old, Skeleton3D *p_new) {
if (p_old && p_old->is_connected(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::cache_self_rests))) {
p_old->disconnect(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::cache_self_rests));
}
if (p_new && !p_new->is_connected(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::cache_self_rests))) {
p_new->connect(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::cache_self_rests));
}
cache_self_rests();
}

void RetargetModifier3D::cache_all_rests() {
cache_self_rests();
cache_child_rests();
}

void RetargetModifier3D::cache_child_rests() {
for (int i = 0; i < child_skeletons.size(); i++) {
_update_child_skeleton_rests(i);
}
};

void RetargetModifier3D::cache_self_rests() {
humanoid_bone_rests.clear();
Skeleton3D *skeleton = get_skeleton();
if (!skeleton) {
return;
}
humanoid_bone_rests = cache_bone_rests(skeleton);
for (int i = 0; i < humanoid_bone_rests.size(); i++) {
humanoid_bone_rests.write[i].cached_basis = humanoid_bone_rests[i].cached_basis.inverse();
}
}

Vector<RetargetModifier3D::RetargetBoneInfo> RetargetModifier3D::cache_bone_rests(Skeleton3D *p_skeleton) {
Vector<RetargetBoneInfo> bone_rests;
if (profile.is_null()) {
return bone_rests;
}
PackedStringArray bone_names = profile->get_bone_names();
for (const String &E : bone_names) {
int bone_id = p_skeleton->find_bone(E);
RetargetBoneInfo rbi;
rbi.bone_id = bone_id;
if (bone_id >= 0) {
Transform3D global_rest = Transform3D();
int bone_parent = p_skeleton->get_bone_parent(bone_id);
if (bone_parent >= 0) {
global_rest = p_skeleton->get_bone_global_rest(bone_parent);
}
rbi.parent_global_rest = global_rest;
rbi.cached_basis = global_rest.basis * p_skeleton->get_bone_rest(bone_id).basis;
}
bone_rests.push_back(rbi);
}
return bone_rests;
}

void RetargetModifier3D::_update_child_skeleton_rests(int p_child_skeleton_idx) {
ERR_FAIL_INDEX(p_child_skeleton_idx, child_skeletons.size());
Skeleton3D *c = Object::cast_to<Skeleton3D>(ObjectDB::get_instance(child_skeletons[p_child_skeleton_idx].skeleton_id));
if (!c) {
return;
}
child_skeletons.write[p_child_skeleton_idx].humanoid_bone_rests = cache_bone_rests(c);
}

void RetargetModifier3D::_update_child_skeletons() {
_reset_child_skeletons();

for (int i = 0; i < get_child_count(); i++) {
RetargetInfo ri;
Skeleton3D *c = Object::cast_to<Skeleton3D>(get_child(i));
if (c) {
int id = child_skeletons.size();
ri.skeleton_id = c->get_instance_id();
child_skeletons.push_back(ri);
c->connect(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::_update_child_skeleton_rests).bind(id));
}
}

cache_child_rests();
update_configuration_warnings();
}

void RetargetModifier3D::_reset_child_skeleton_poses() {
for (const RetargetInfo &E : child_skeletons) {
Skeleton3D *c = Object::cast_to<Skeleton3D>(ObjectDB::get_instance(E.skeleton_id));
if (!c) {
continue;
}
if (c->is_connected(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::_update_child_skeleton_rests))) {
c->disconnect(SNAME("rest_updated"), callable_mp(this, &RetargetModifier3D::_update_child_skeleton_rests));
}
for (const RetargetBoneInfo &F : E.humanoid_bone_rests) {
if (F.bone_id < 0) {
continue;
}
c->reset_bone_pose(F.bone_id);
}
}
}

void RetargetModifier3D::_reset_child_skeletons() {
_reset_child_skeleton_poses();
child_skeletons.clear();
}

/// Main algorythm

Transform3D RetargetModifier3D::extract_global_transform(Transform3D p_pose, Transform3D p_parent_global_rest, Basis p_cached_basis, Vector3 p_rest_origin) {
Transform3D tr;
tr.basis = p_parent_global_rest.basis * p_pose.basis * p_cached_basis;
tr.origin = p_parent_global_rest.basis.xform(p_pose.origin - p_rest_origin);
return tr;
}

/// Genaral functions

void RetargetModifier3D::add_child_notify(Node *p_child) {
if (Object::cast_to<Skeleton3D>(p_child)) {
_update_child_skeletons();
}
}

void RetargetModifier3D::move_child_notify(Node *p_child) {
if (Object::cast_to<Skeleton3D>(p_child)) {
_update_child_skeletons();
}
}

void RetargetModifier3D::remove_child_notify(Node *p_child) {
if (Object::cast_to<Skeleton3D>(p_child)) {
_update_child_skeletons();
}
}

void RetargetModifier3D::_bind_methods() {
ClassDB::bind_method(D_METHOD("set_profile", "profile"), &RetargetModifier3D::set_profile);
ClassDB::bind_method(D_METHOD("get_profile"), &RetargetModifier3D::get_profile);
ClassDB::bind_method(D_METHOD("set_position_enabled", "enabled"), &RetargetModifier3D::set_position_enabled);
ClassDB::bind_method(D_METHOD("is_position_enabled"), &RetargetModifier3D::is_position_enabled);
ClassDB::bind_method(D_METHOD("set_rotation_enabled", "enabled"), &RetargetModifier3D::set_rotation_enabled);
ClassDB::bind_method(D_METHOD("is_rotation_enabled"), &RetargetModifier3D::is_rotation_enabled);
ClassDB::bind_method(D_METHOD("set_scale_enabled", "enabled"), &RetargetModifier3D::set_scale_enabled);
ClassDB::bind_method(D_METHOD("is_scale_enabled"), &RetargetModifier3D::is_scale_enabled);

ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "profile", PROPERTY_HINT_RESOURCE_TYPE, "SkeletonProfile"), "set_profile", "get_profile");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "position_enabled"), "set_position_enabled", "is_position_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "rotation_enabled"), "set_rotation_enabled", "is_rotation_enabled");
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "scale_enabled"), "set_scale_enabled", "is_scale_enabled");
}

void RetargetModifier3D::_process_modification() {
Skeleton3D *source_skeleton = get_skeleton();
if (!source_skeleton) {
return;
}

LocalVector<Transform3D> source_poses;
if (influence < 1.0) {
for (int i = 0; i < humanoid_bone_rests.size(); i++) {
int source_bone_id = humanoid_bone_rests[i].bone_id;
source_poses.push_back(source_bone_id < 0 ? Transform3D() : source_skeleton->get_bone_rest(source_bone_id).interpolate_with(source_skeleton->get_bone_pose(source_bone_id), influence));
}
} else {
for (int i = 0; i < humanoid_bone_rests.size(); i++) {
int source_bone_id = humanoid_bone_rests[i].bone_id;
source_poses.push_back(source_bone_id < 0 ? Transform3D() : source_skeleton->get_bone_pose(source_bone_id));
}
}

for (const RetargetInfo &E : child_skeletons) {
Skeleton3D *target_skeleton = Object::cast_to<Skeleton3D>(ObjectDB::get_instance(E.skeleton_id));
if (!target_skeleton) {
continue;
}
for (int i = 0; i < humanoid_bone_rests.size(); i++) {
int target_bone_id = E.humanoid_bone_rests[i].bone_id;
if (target_bone_id < 0) {
continue;
}

int source_bone_id = humanoid_bone_rests[i].bone_id;
if (source_bone_id < 0) {
continue;
}

Transform3D extracted_transform = extract_global_transform(
source_poses[i],
humanoid_bone_rests[i].parent_global_rest,
humanoid_bone_rests[i].cached_basis,
source_skeleton->get_bone_rest(source_bone_id).origin);
Quaternion target_parent_global_rest_q = E.humanoid_bone_rests[i].parent_global_rest.basis.get_rotation_quaternion();

Transform3D retarget_pose;

if (enable_position) {
retarget_pose.origin = target_parent_global_rest_q.xform_inv(extracted_transform.origin) + target_skeleton->get_bone_rest(target_bone_id).origin;
// TODO: apply motion scale of Skeleton.
}
if (enable_rotation) {
retarget_pose.basis = Basis(target_parent_global_rest_q.inverse() * extracted_transform.basis.get_rotation_quaternion() * E.humanoid_bone_rests[i].cached_basis.get_rotation_quaternion());
}
if (enable_scale) {
retarget_pose.scale((E.humanoid_bone_rests[i].parent_global_rest.basis.inverse() * Basis().scaled(extracted_transform.basis.get_scale()) * E.humanoid_bone_rests[i].cached_basis).get_scale());
}

target_skeleton->set_bone_pose(target_bone_id, retarget_pose);
}
}
}

void RetargetModifier3D::set_profile(Ref<SkeletonProfile> p_profile) {
if (profile == p_profile) {
return;
}
_profile_changed(profile, p_profile);
}

Ref<SkeletonProfile> RetargetModifier3D::get_profile() const {
return profile;
}

void RetargetModifier3D::set_position_enabled(bool p_enabled) {
if (enable_position != p_enabled) {
_reset_child_skeleton_poses();
}
enable_position = p_enabled;
}

bool RetargetModifier3D::is_position_enabled() const {
return enable_position;
}

void RetargetModifier3D::set_rotation_enabled(bool p_enabled) {
if (enable_rotation != p_enabled) {
_reset_child_skeleton_poses();
}
enable_rotation = p_enabled;
}

bool RetargetModifier3D::is_rotation_enabled() const {
return enable_rotation;
}

void RetargetModifier3D::set_scale_enabled(bool p_enabled) {
if (enable_scale != p_enabled) {
_reset_child_skeleton_poses();
}
enable_scale = p_enabled;
}

bool RetargetModifier3D::is_scale_enabled() const {
return enable_scale;
}

void RetargetModifier3D::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_ENTER_TREE: {
_update_child_skeletons();
} break;
case NOTIFICATION_EXIT_TREE: {
_reset_child_skeletons();
} break;
}
}

RetargetModifier3D::RetargetModifier3D() {
}

RetargetModifier3D::~RetargetModifier3D() {
}
Loading

0 comments on commit 314635f

Please sign in to comment.