Skip to content

Commit 09c7214

Browse files
committed
Merge pull request godotengine#106846 from TokageItLab/mod-target
Implement `ModifierBoneTarget3D` which can be target of the other `SkeletonModifier3D`s
2 parents 36f08ae + aa2c3da commit 09c7214

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<class name="ModifierBoneTarget3D" inherits="SkeletonModifier3D" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3+
<brief_description>
4+
А node that dynamically copies the 3D transform of a bone in its parent [Skeleton3D].
5+
</brief_description>
6+
<description>
7+
This node selects a bone in a [Skeleton3D] and attaches to it. This means that the [ModifierBoneTarget3D] node will dynamically copy the 3D transform of the selected bone.
8+
The functionality is similar to [BoneAttachment3D], but this node adopts the [SkeletonModifier3D] cycle and is intended to be used as another [SkeletonModifier3D]'s target.
9+
</description>
10+
<tutorials>
11+
</tutorials>
12+
<members>
13+
<member name="bone" type="int" setter="set_bone" getter="get_bone" default="-1">
14+
The index of the attached bone.
15+
</member>
16+
<member name="bone_name" type="String" setter="set_bone_name" getter="get_bone_name" default="&quot;&quot;">
17+
The name of the attached bone.
18+
</member>
19+
</members>
20+
</class>
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**************************************************************************/
2+
/* modifier_bone_target_3d.cpp */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#include "modifier_bone_target_3d.h"
32+
33+
void ModifierBoneTarget3D::_validate_bone_names() {
34+
// Prior bone name.
35+
if (!bone_name.is_empty()) {
36+
set_bone_name(bone_name);
37+
} else if (bone != -1) {
38+
set_bone(bone);
39+
}
40+
}
41+
42+
void ModifierBoneTarget3D::set_bone_name(const String &p_bone_name) {
43+
bone_name = p_bone_name;
44+
Skeleton3D *sk = get_skeleton();
45+
if (sk) {
46+
set_bone(sk->find_bone(bone_name));
47+
}
48+
}
49+
50+
String ModifierBoneTarget3D::get_bone_name() const {
51+
return bone_name;
52+
}
53+
54+
void ModifierBoneTarget3D::set_bone(int p_bone) {
55+
bone = p_bone;
56+
Skeleton3D *sk = get_skeleton();
57+
if (sk) {
58+
if (bone <= -1 || bone >= sk->get_bone_count()) {
59+
WARN_PRINT("Bone index out of range!");
60+
bone = -1;
61+
} else {
62+
bone_name = sk->get_bone_name(bone);
63+
}
64+
}
65+
}
66+
67+
int ModifierBoneTarget3D::get_bone() const {
68+
return bone;
69+
}
70+
71+
void ModifierBoneTarget3D::_validate_property(PropertyInfo &p_property) const {
72+
if (p_property.name == "influence") {
73+
p_property.usage = PROPERTY_USAGE_READ_ONLY;
74+
}
75+
}
76+
77+
void ModifierBoneTarget3D::_bind_methods() {
78+
ClassDB::bind_method(D_METHOD("set_bone_name", "bone_name"), &ModifierBoneTarget3D::set_bone_name);
79+
ClassDB::bind_method(D_METHOD("get_bone_name"), &ModifierBoneTarget3D::get_bone_name);
80+
ClassDB::bind_method(D_METHOD("set_bone", "bone"), &ModifierBoneTarget3D::set_bone);
81+
ClassDB::bind_method(D_METHOD("get_bone"), &ModifierBoneTarget3D::get_bone);
82+
83+
ADD_PROPERTY(PropertyInfo(Variant::STRING, "bone_name", PROPERTY_HINT_ENUM_SUGGESTION, ""), "set_bone_name", "get_bone_name");
84+
ADD_PROPERTY(PropertyInfo(Variant::INT, "bone", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_bone", "get_bone");
85+
}
86+
87+
void ModifierBoneTarget3D::_process_modification(double p_delta) {
88+
if (!is_inside_tree()) {
89+
return;
90+
}
91+
92+
Skeleton3D *skeleton = get_skeleton();
93+
if (!skeleton || bone < 0 || bone >= skeleton->get_bone_count()) {
94+
return;
95+
}
96+
97+
set_transform(skeleton->get_bone_global_pose(bone));
98+
}

scene/3d/modifier_bone_target_3d.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/**************************************************************************/
2+
/* modifier_bone_target_3d.h */
3+
/**************************************************************************/
4+
/* This file is part of: */
5+
/* GODOT ENGINE */
6+
/* https://godotengine.org */
7+
/**************************************************************************/
8+
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9+
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10+
/* */
11+
/* Permission is hereby granted, free of charge, to any person obtaining */
12+
/* a copy of this software and associated documentation files (the */
13+
/* "Software"), to deal in the Software without restriction, including */
14+
/* without limitation the rights to use, copy, modify, merge, publish, */
15+
/* distribute, sublicense, and/or sell copies of the Software, and to */
16+
/* permit persons to whom the Software is furnished to do so, subject to */
17+
/* the following conditions: */
18+
/* */
19+
/* The above copyright notice and this permission notice shall be */
20+
/* included in all copies or substantial portions of the Software. */
21+
/* */
22+
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23+
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24+
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25+
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26+
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27+
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28+
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29+
/**************************************************************************/
30+
31+
#pragma once
32+
33+
#include "scene/3d/skeleton_modifier_3d.h"
34+
35+
class ModifierBoneTarget3D : public SkeletonModifier3D {
36+
GDCLASS(ModifierBoneTarget3D, SkeletonModifier3D);
37+
38+
String bone_name;
39+
int bone = -1;
40+
41+
protected:
42+
void _validate_property(PropertyInfo &p_property) const;
43+
virtual void _validate_bone_names() override;
44+
static void _bind_methods();
45+
virtual void _process_modification(double p_delta) override;
46+
47+
public:
48+
void set_bone_name(const String &p_bone_name);
49+
String get_bone_name() const;
50+
void set_bone(int p_bone);
51+
int get_bone() const;
52+
};

scene/register_scene_types.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
#include "scene/3d/look_at_modifier_3d.h"
236236
#include "scene/3d/marker_3d.h"
237237
#include "scene/3d/mesh_instance_3d.h"
238+
#include "scene/3d/modifier_bone_target_3d.h"
238239
#include "scene/3d/multimesh_instance_3d.h"
239240
#include "scene/3d/node_3d.h"
240241
#include "scene/3d/occluder_instance_3d.h"
@@ -646,6 +647,7 @@ void register_scene_types() {
646647
GDREGISTER_CLASS(GPUParticlesAttractorVectorField3D);
647648
GDREGISTER_CLASS(CPUParticles3D);
648649
GDREGISTER_CLASS(Marker3D);
650+
GDREGISTER_CLASS(ModifierBoneTarget3D);
649651
GDREGISTER_CLASS(RootMotionView);
650652
GDREGISTER_VIRTUAL_CLASS(SkeletonModifier3D);
651653
GDREGISTER_CLASS(RetargetModifier3D);

0 commit comments

Comments
 (0)