Fix saving player scene changing bone transforms

main
copygirl 11 months ago
parent 8177655670
commit 7518e15f99
  1. 35
      player/AnimationController.cs
  2. 155
      player/player.tscn

@ -1,22 +1,19 @@
public partial class AnimationController : Node3D
{
[Export] public Skeleton3D Skeleton { get; set; }
[Export] public BoneAttachment3D RootBone { get; set; }
// Contains all the bones in the skeleton, keyed by name (e.g. "LowerArm_L").
Dictionary<string, BoneAttachment3D> _bones = [];
// Whether the player's body is currently turning to match up with the camera rotation.
bool _isTurning = false;
// Current amount the body is turned due to walking sideways.
float _bodyYaw = 0.0f;
bool _isTurning = false; // Whether the player's body is currently turning to match up with the camera rotation.
float _bodyYaw = 0.0f; // Current amount the body is turned due to walking sideways.
Player _player;
Transform3D _cameraDefaultTransform;
AnimationTree _animTree;
Animation _walkForwardAnim;
Animation _walkBackwardAnim;
BoneAttachment3D _rootBone;
public override void _Ready()
{
_player = GetParent<Player>();
@ -24,11 +21,17 @@ public partial class AnimationController : Node3D
_walkForwardAnim = _animTree.GetAnimation("walk_forward");
_walkBackwardAnim = _animTree.GetAnimation("walk_backward");
void AddBone(BoneAttachment3D bone)
{ bone.OverridePose = true; _bones[bone.Name] = bone; }
AddBone(RootBone);
foreach (var child in RootBone.FindChildren("*", "BoneAttachment3D"))
AddBone((BoneAttachment3D)child);
_rootBone = GetNode<BoneAttachment3D>("Root");
foreach (var child in FindChildren("*", "BoneAttachment3D")) {
var bone = (BoneAttachment3D)child;
bone.OverridePose = true;
_bones[bone.Name] = bone;
}
// We disable the AnimationTree while in the editor so our
// BoneAttackment3D nodes don't get updated, resulting in
// those changes being picked up by version control.
_animTree.Active = true;
}
public override void _Process(double delta)
@ -41,8 +44,16 @@ public partial class AnimationController : Node3D
void ResetTransforms()
{
foreach (var bone in _bones.Values)
foreach (var bone in _bones.Values) {
bone.Transform = Skeleton.GetBonePose(bone.BoneIdx);
if (bone == _rootBone) {
// This is a dirty hack that makes sure the `Root` bone has the same
// Transform as it would if it was parented to `Player/Model/Skeleton`.
var skel = Skeleton.GetParent<Node3D>();
var model = skel.GetParent<Node3D>();
bone.Transform = model.Transform * skel.Transform * bone.Transform;
}
}
if (_player.Camera.Camera is Camera3D camera)
camera.Transform = _player.Camera.DefaultTransform;
}

@ -16,7 +16,7 @@ height = 1.5
radius = 0.28
height = 1.5
[sub_resource type="Animation" id="Animation_arrr6"]
[sub_resource type="Animation" id="Animation_5wsog"]
resource_name = "idle_loop"
length = 2.5
loop_mode = 1
@ -161,7 +161,7 @@ tracks/19/interp = 1
tracks/19/loop_wrap = true
tracks/19/keys = PackedFloat32Array(0, 1, -0.707001, 4.51326e-18, -4.26954e-18, 0.707212, 0.166667, 1, -0.707898, 4.51333e-18, -4.26961e-18, 0.706315, 0.233333, 1, -0.708755, 4.5134e-18, -4.26967e-18, 0.705454, 0.3, 1, -0.709809, 4.51347e-18, -4.26974e-18, 0.704395, 0.366667, 1, -0.711039, 4.51354e-18, -4.26981e-18, 0.703153, 0.466667, 1, -0.713127, 4.51363e-18, -4.26989e-18, 0.701034, 0.5, 1, -0.713869, 4.51366e-18, -4.26991e-18, 0.700279, 0.533333, 1, -0.71463, 4.51367e-18, -4.26993e-18, 0.699502, 0.566667, 1, -0.715401, 4.51369e-18, -4.26994e-18, 0.698714, 0.6, 1, -0.716178, 4.51369e-18, -4.26995e-18, 0.697918, 0.633333, 1, -0.716958, 4.51369e-18, -4.26995e-18, 0.697116, 0.733333, 1, -0.719267, 4.51367e-18, -4.26992e-18, 0.694734, 0.8, 1, -0.720739, 4.51362e-18, -4.26988e-18, 0.693206, 0.866667, 1, -0.722115, 4.51356e-18, -4.26982e-18, 0.691773, 0.933333, 1, -0.723357, 4.51349e-18, -4.26976e-18, 0.690474, 1, 1, -0.724446, 4.51342e-18, -4.26969e-18, 0.689331, 1.06667, 1, -0.725318, 4.51335e-18, -4.26963e-18, 0.688414, 1.16667, 1, -0.726204, 4.51327e-18, -4.26955e-18, 0.687479, 1.43333, 1, -0.725318, 0, 0, 0.688414, 1.5, 1, -0.724446, 0, 0, 0.689331, 1.6, 1, -0.722753, 0, 0, 0.691106, 1.66667, 1, -0.721445, 0, 0, 0.692472, 1.73333, 1, -0.720012, 0, 0, 0.693961, 1.76667, 1, -0.719267, 0, 0, 0.694734, 1.8, 1, -0.718508, 0, 0, 0.695519, 1.83333, 1, -0.717737, 0, 0, 0.696315, 1.86667, 1, -0.716958, 0, 0, 0.697116, 1.9, 1, -0.716178, 0, 0, 0.697918, 1.93333, 1, -0.715401, 0, 0, 0.698714, 1.96667, 1, -0.71463, 0, 0, 0.699502, 2.03333, 1, -0.713127, 0, 0, 0.701034, 2.1, 1, -0.711709, 0, 0, 0.702475, 2.16667, 1, -0.710399, 0, 0, 0.703799, 2.23333, 1, -0.709259, 0, 0, 0.704948, 2.3, 1, -0.7083, 0, 0, 0.705911, 2.36667, 1, -0.707572, 0, 0, 0.706641, 2.5, 1, -0.706937, 4.51325e-18, -4.26953e-18, 0.707277)
[sub_resource type="Animation" id="Animation_juvj2"]
[sub_resource type="Animation" id="Animation_ylm5j"]
resource_name = "walk_backward_loop"
length = 0.833333
loop_mode = 1
@ -306,7 +306,7 @@ tracks/19/interp = 1
tracks/19/loop_wrap = true
tracks/19/keys = PackedFloat32Array(0, 1, 5.1721e-09, -1.73886e-07, 0.0182467, 0.999834)
[sub_resource type="Animation" id="Animation_0igae"]
[sub_resource type="Animation" id="Animation_mc0wh"]
resource_name = "walk_forward_loop"
length = 0.833333
loop_mode = 1
@ -451,7 +451,7 @@ tracks/19/interp = 1
tracks/19/loop_wrap = true
tracks/19/keys = PackedFloat32Array(0, 1, 5.1721e-09, -1.73886e-07, 0.0182467, 0.999834)
[sub_resource type="Animation" id="Animation_5quca"]
[sub_resource type="Animation" id="Animation_kreh1"]
resource_name = "walk_left_loop"
length = 0.833333
loop_mode = 1
@ -596,7 +596,7 @@ tracks/19/interp = 1
tracks/19/loop_wrap = true
tracks/19/keys = PackedFloat32Array(0, 1, 5.1721e-09, -1.73886e-07, 0.0182467, 0.999834)
[sub_resource type="Animation" id="Animation_otirq"]
[sub_resource type="Animation" id="Animation_q7kt1"]
resource_name = "walk_right_loop"
length = 0.833333
loop_mode = 1
@ -741,16 +741,16 @@ tracks/19/interp = 1
tracks/19/loop_wrap = true
tracks/19/keys = PackedFloat32Array(0, 1, 5.1721e-09, -1.73886e-07, 0.0182467, 0.999834)
[sub_resource type="AnimationLibrary" id="AnimationLibrary_4fkhn"]
[sub_resource type="AnimationLibrary" id="AnimationLibrary_bvwgj"]
_data = {
"idle": SubResource("Animation_arrr6"),
"walk_backward": SubResource("Animation_juvj2"),
"walk_forward": SubResource("Animation_0igae"),
"walk_left": SubResource("Animation_5quca"),
"walk_right": SubResource("Animation_otirq")
"idle": SubResource("Animation_5wsog"),
"walk_backward": SubResource("Animation_ylm5j"),
"walk_forward": SubResource("Animation_mc0wh"),
"walk_left": SubResource("Animation_kreh1"),
"walk_right": SubResource("Animation_q7kt1")
}
[sub_resource type="AnimationNodeAnimation" id="AnimationNodeAnimation_4v5ew"]
[sub_resource type="AnimationNodeAnimation" id="AnimationNodeAnimation_ra5r1"]
animation = &"idle"
[sub_resource type="AnimationNodeAnimation" id="AnimationNodeAnimation_cun17"]
@ -784,9 +784,9 @@ input_1/auto_advance = false
input_1/reset = true
[sub_resource type="AnimationNodeBlendTree" id="AnimationNodeBlendTree_cstk8"]
graph_offset = Vector2(0, -22)
nodes/idle/node = SubResource("AnimationNodeAnimation_4v5ew")
nodes/idle/position = Vector2(600, 60)
graph_offset = Vector2(30, 1.30002)
nodes/Animation/node = SubResource("AnimationNodeAnimation_ra5r1")
nodes/Animation/position = Vector2(600, 60)
nodes/not_moving/node = SubResource("AnimationNodeAnimation_cun17")
nodes/not_moving/position = Vector2(400, 60)
nodes/output/position = Vector2(960, 180)
@ -800,7 +800,7 @@ nodes/walk_speed/node = SubResource("AnimationNodeBlend2_krc6q")
nodes/walk_speed/position = Vector2(600, 200)
nodes/walk_state/node = SubResource("AnimationNodeTransition_nyobm")
nodes/walk_state/position = Vector2(800, 120)
node_connections = [&"output", 0, &"walk_state", &"walk_direction", 0, &"walk_forward", &"walk_direction", 1, &"walk_backward", &"walk_speed", 0, &"not_moving", &"walk_speed", 1, &"walk_direction", &"walk_state", 0, &"idle", &"walk_state", 1, &"walk_speed"]
node_connections = [&"output", 0, &"walk_state", &"walk_direction", 0, &"walk_forward", &"walk_direction", 1, &"walk_backward", &"walk_speed", 0, &"not_moving", &"walk_speed", 1, &"walk_direction", &"walk_state", 0, &"Animation", &"walk_state", 1, &"walk_speed"]
[node name="Player" type="CharacterBody3D"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.75, 0)
@ -822,110 +822,87 @@ shape = SubResource("CapsuleShape3D_l8s0f")
[node name="Model" parent="." instance=ExtResource("1_3qh37")]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, -0.75, 0)
[node name="Root" type="BoneAttachment3D" parent="Model/Skeleton" index="0"]
transform = Transform3D(-1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0.198819, 0)
[node name="MovementController" type="Node" parent="."]
script = ExtResource("2_1pst4")
[node name="CameraController" type="Node" parent="." node_paths=PackedStringArray("Camera")]
script = ExtResource("2_r3gna")
Camera = NodePath("../AnimationController/Root/LowerBody/UpperBody/Neck/Head/Camera")
[node name="AnimationController" type="Node3D" parent="." node_paths=PackedStringArray("Skeleton")]
script = ExtResource("3_5rlwc")
Skeleton = NodePath("../Model/Skeleton/Skeleton3D")
[node name="AnimationTree" type="AnimationTree" parent="AnimationController"]
active = false
root_node = NodePath("../../Model")
libraries = {
"": SubResource("AnimationLibrary_bvwgj")
}
tree_root = SubResource("AnimationNodeBlendTree_cstk8")
anim_player = NodePath("../../Model/AnimationPlayer")
parameters/walk_direction/current_state = "forward"
parameters/walk_direction/transition_request = ""
parameters/walk_direction/current_index = 0
parameters/walk_speed/blend_amount = 0.0
parameters/walk_state/current_state = "idle"
parameters/walk_state/transition_request = "idle"
parameters/walk_state/current_index = 0
[node name="Root" type="BoneAttachment3D" parent="AnimationController"]
transform = Transform3D(1, -8.74228e-08, 0, 0, 0, 1, -8.74228e-08, -1, 0, 2.18384e-09, 0.271327, 0.0249802)
bone_name = "Root"
bone_idx = 2
use_external_skeleton = true
external_skeleton = NodePath("../Skeleton3D")
external_skeleton = NodePath("../../Model/Skeleton/Skeleton3D")
[node name="LowerBody" type="BoneAttachment3D" parent="Model/Skeleton/Root"]
transform = Transform3D(-1, -1.37451e-09, 8.7412e-08, 8.71645e-08, 0.0611276, 0.99813, -6.71522e-09, 0.99813, -0.0611275, 0, 0, -0.199593)
[node name="LowerBody" type="BoneAttachment3D" parent="AnimationController/Root"]
transform = Transform3D(-1, 0, 8.74228e-08, 8.71645e-08, 0.0768132, 0.997046, -6.71523e-09, 0.997046, -0.0768132, 0, 0, -0.199593)
bone_name = "LowerBody"
bone_idx = 3
use_external_skeleton = true
external_skeleton = NodePath("../../Skeleton3D")
external_skeleton = NodePath("../../../Model/Skeleton/Skeleton3D")
[node name="UpperBody" type="BoneAttachment3D" parent="Model/Skeleton/Root/LowerBody"]
transform = Transform3D(1, -8.24329e-09, 8.70333e-08, -4.44089e-16, 0.995544, 0.0942922, -8.74228e-08, -0.0942922, 0.995544, 8.88178e-16, 0.154362, 0)
[node name="UpperBody" type="BoneAttachment3D" parent="AnimationController/Root/LowerBody"]
transform = Transform3D(1, -1.08108e-08, 8.67517e-08, 0, 0.992324, 0.123662, -8.74228e-08, -0.123662, 0.992324, 8.88178e-16, 0.154362, 0)
bone_name = "UpperBody"
bone_idx = 4
use_external_skeleton = true
external_skeleton = NodePath("../../../Skeleton3D")
external_skeleton = NodePath("../../../../Model/Skeleton/Skeleton3D")
[node name="Neck" type="BoneAttachment3D" parent="Model/Skeleton/Root/LowerBody/UpperBody"]
transform = Transform3D(1, -8.88178e-16, -7.10543e-15, 0, 0.998891, -0.0470728, 0, 0.0470728, 0.998892, -1.11022e-16, 0.251888, 5.58794e-09)
[node name="Neck" type="BoneAttachment3D" parent="AnimationController/Root/LowerBody/UpperBody"]
transform = Transform3D(1, -4.4409e-16, -7.10543e-15, 0, 0.998891, -0.0470728, 0, 0.0470728, 0.998892, -4.44089e-16, 0.251888, 1.49012e-08)
bone_name = "Neck"
bone_idx = 5
use_external_skeleton = true
external_skeleton = NodePath("../../../../Skeleton3D")
external_skeleton = NodePath("../../../../../Model/Skeleton/Skeleton3D")
[node name="Head" type="BoneAttachment3D" parent="Model/Skeleton/Root/LowerBody/UpperBody/Neck"]
transform = Transform3D(-1, 4.50711e-10, 8.74216e-08, 8.74228e-08, 0.00515562, 0.999987, -7.10543e-15, 0.999987, -0.00515556, -4.44089e-16, 0.101598, 7.45058e-09)
[node name="Head" type="BoneAttachment3D" parent="AnimationController/Root/LowerBody/UpperBody/Neck"]
transform = Transform3D(-1, 7.10543e-15, 8.74228e-08, 8.74228e-08, -7.10543e-15, 1, 0, 1, 7.10543e-15, -2.22045e-16, 0.101598, -3.72529e-09)
bone_name = "Head"
bone_idx = 6
use_external_skeleton = true
external_skeleton = NodePath("../../../../../Skeleton3D")
external_skeleton = NodePath("../../../../../../Model/Skeleton/Skeleton3D")
[node name="Camera" type="Camera3D" parent="Model/Skeleton/Root/LowerBody/UpperBody/Neck/Head"]
[node name="Camera" type="Camera3D" parent="AnimationController/Root/LowerBody/UpperBody/Neck/Head"]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, -1, 0, 1, -4.37114e-08, 0, 0.08, 0.04)
cull_mask = 1
current = true
[node name="UpperArm_L" type="BoneAttachment3D" parent="Model/Skeleton/Root/LowerBody/UpperBody"]
transform = Transform3D(-0.94861, 0.316447, -0.000603795, -0.316069, -0.947568, -0.0470641, -0.0154654, -0.0444547, 0.998892, 0.0873834, 0.213866, -0.00179176)
[node name="UpperArm_L" type="BoneAttachment3D" parent="AnimationController/Root/LowerBody/UpperBody"]
transform = Transform3D(-0.572005, 0.82025, 9.68575e-08, -0.819341, -0.571371, -0.0470728, -0.0386114, -0.026926, 0.998891, 0.0873834, 0.213866, -0.00179176)
bone_name = "UpperArm_L"
bone_idx = 7
use_external_skeleton = true
external_skeleton = NodePath("../../../../Skeleton3D")
external_skeleton = NodePath("../../../../../Model/Skeleton/Skeleton3D")
[node name="UpperArm_R" type="BoneAttachment3D" parent="Model/Skeleton/Root/LowerBody/UpperBody"]
transform = Transform3D(-0.94861, -0.316447, 0.000603795, 0.316069, -0.947568, -0.0470641, 0.0154654, -0.0444547, 0.998892, -0.0873834, 0.213866, -0.00179176)
[node name="UpperArm_R" type="BoneAttachment3D" parent="AnimationController/Root/LowerBody/UpperBody"]
transform = Transform3D(-0.572005, -0.82025, -9.68575e-08, 0.819341, -0.571371, -0.0470728, 0.0386114, -0.026926, 0.998891, -0.0873834, 0.213866, -0.00179176)
bone_name = "UpperArm_R"
bone_idx = 14
use_external_skeleton = true
external_skeleton = NodePath("../../../../Skeleton3D")
[node name="Skeleton3D" parent="Model/Skeleton" index="1"]
bones/1/rotation = Quaternion(-0.723196, 4.5135e-18, -4.26977e-18, 0.690643)
bones/2/position = Vector3(0, 0.198819, 0)
bones/3/rotation = Quaternion(2.94447e-08, 0.728398, 0.685154, 3.23062e-08)
bones/4/rotation = Quaternion(-0.0471987, 4.36627e-08, 2.06312e-09, 0.998886)
bones/6/rotation = Quaternion(3.09882e-08, 0.708927, 0.705282, 3.08288e-08)
bones/7/rotation = Quaternion(-0.00407106, -0.0231858, 0.986797, -0.160245)
bones/8/rotation = Quaternion(8.60099e-05, 0.000289714, -0.076692, 0.997055)
bones/9/rotation = Quaternion(-6.24049e-05, -0.000295211, -0.00375171, 0.999993)
bones/14/rotation = Quaternion(0.00407106, -0.0231858, 0.986797, 0.160245)
bones/15/rotation = Quaternion(8.60101e-05, -0.000289714, 0.0766921, 0.997055)
bones/16/rotation = Quaternion(-6.24048e-05, 0.000295211, 0.0037517, 0.999993)
bones/21/rotation = Quaternion(0.010666, -0.718595, 0.695263, 0.0108048)
bones/22/rotation = Quaternion(0.0583992, 0.00254482, 0.00356579, 0.998284)
bones/23/rotation = Quaternion(-0.00141986, -0.675636, 0.736986, -0.0191147)
bones/24/rotation = Quaternion(-0.010666, -0.718595, 0.695263, -0.0108048)
bones/25/rotation = Quaternion(0.0583992, -0.00254482, -0.00356579, 0.998284)
bones/26/rotation = Quaternion(0.00141986, -0.675636, 0.736986, 0.0191147)
bones/28/rotation = Quaternion(-0.723196, 4.5135e-18, -4.26977e-18, 0.690643)
[node name="MovementController" type="Node" parent="."]
script = ExtResource("2_1pst4")
[node name="CameraController" type="Node" parent="." node_paths=PackedStringArray("Camera")]
script = ExtResource("2_r3gna")
Camera = NodePath("../Model/Skeleton/Root/LowerBody/UpperBody/Neck/Head/Camera")
[node name="AnimationController" type="Node3D" parent="." node_paths=PackedStringArray("Skeleton", "RootBone")]
script = ExtResource("3_5rlwc")
Skeleton = NodePath("../Model/Skeleton/Skeleton3D")
RootBone = NodePath("../Model/Skeleton/Root")
[node name="AnimationTree" type="AnimationTree" parent="AnimationController"]
deterministic = false
root_node = NodePath("../../Model")
libraries = {
"": SubResource("AnimationLibrary_4fkhn")
}
tree_root = SubResource("AnimationNodeBlendTree_cstk8")
anim_player = NodePath("../../Model/AnimationPlayer")
parameters/walk_direction/current_state = "forward"
parameters/walk_direction/transition_request = ""
parameters/walk_direction/current_index = 0
parameters/walk_speed/blend_amount = 0.0
parameters/walk_state/current_state = "move"
parameters/walk_state/transition_request = ""
parameters/walk_state/current_index = 1
external_skeleton = NodePath("../../../../../Model/Skeleton/Skeleton3D")
[node name="PickupController" type="Node3D" parent="." node_paths=PackedStringArray("Camera")]
script = ExtResource("2_ns2pe")
Camera = NodePath("../Model/Skeleton/Root/LowerBody/UpperBody/Neck/Head/Camera")
[editable path="Model"]
Camera = NodePath("../AnimationController/Root/LowerBody/UpperBody/Neck/Head/Camera")

Loading…
Cancel
Save