0.2.0-dev Download ZIP
82 lines ·3.0 KB Raw
| 1 | using EvilEngine.Formats; |
| 2 | using EvilEngine.Formats.Animations; |
| 3 | using EvilEngine.Host.Gait; |
| 4 | using Godot; |
| 5 | using Animation = EvilEngine.Formats.Animations.Animation; |
| 6 | using Num = System.Numerics; |
| 7 | |
| 8 | namespace EvilEngine.Shared.World; |
| 9 | |
| 10 | public sealed class GaitPlayer |
| 11 | { |
| 12 | private readonly AnimationRig _rig; |
| 13 | private readonly Skeleton3D _skeleton; |
| 14 | |
| 15 | public GaitPlayer(Skeleton3D skeleton, BodyGait gait, Func<ContentId, Animation?> layers, |
| 16 | IReadOnlyList<PoseInterp> constraints) |
| 17 | { |
| 18 | _skeleton = skeleton; |
| 19 | Gait = gait; |
| 20 | var count = skeleton.GetBoneCount(); |
| 21 | var names = new string[count]; |
| 22 | var rest = new ClipPose[count]; |
| 23 | for (var bone = 0; bone < count; bone++) |
| 24 | { |
| 25 | names[bone] = skeleton.GetBoneName(bone); |
| 26 | var pose = skeleton.GetBoneRest(bone); |
| 27 | var rotation = pose.Basis.GetRotationQuaternion(); |
| 28 | var scale = pose.Basis.Scale; |
| 29 | rest[bone] = new ClipPose(new Num.Vector3((float)pose.Origin.X, (float)pose.Origin.Y, (float)pose.Origin.Z), |
| 30 | new Num.Quaternion((float)rotation.X, (float)rotation.Y, (float)rotation.Z, (float)rotation.W), |
| 31 | new Num.Vector3((float)scale.X, (float)scale.Y, (float)scale.Z)); |
| 32 | } |
| 33 | |
| 34 | _rig = new AnimationRig(names, rest, layers) { Constraints = constraints }; |
| 35 | } |
| 36 | |
| 37 | public BodyGait Gait { get; } |
| 38 | |
| 39 | public void Step(GaitInput input, double delta, double clock) |
| 40 | { |
| 41 | Gait.Update(input, delta); |
| 42 | Pose(clock); |
| 43 | } |
| 44 | |
| 45 | public void Pose(double clock) |
| 46 | { |
| 47 | if (Gait.Current is not { } current) |
| 48 | return; |
| 49 | var fades = Gait.Fades; |
| 50 | var cycle = (float)Gait.Cycle; |
| 51 | ClipPose[] posed; |
| 52 | if (fades.Count == 0) |
| 53 | { |
| 54 | posed = _rig.Pose(current, cycle, Gait.Parameter, clock); |
| 55 | } |
| 56 | else |
| 57 | { |
| 58 | posed = _rig.Pose(fades[0].Animation, (float)fades[0].Cycle, Gait.Parameter, clock); |
| 59 | for (var i = 0; i < fades.Count; i++) |
| 60 | { |
| 61 | var next = i + 1 < fades.Count |
| 62 | ? _rig.Pose(fades[i + 1].Animation, (float)fades[i + 1].Cycle, Gait.Parameter, clock) |
| 63 | : _rig.Pose(current, cycle, Gait.Parameter, clock); |
| 64 | posed = AnimationSampler.Fade(next, posed, fades[i].Weight); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | foreach (var gesture in Gait.Gestures) |
| 69 | _rig.Lay(posed, gesture.Animation, (float)gesture.Cycle, 1, Gait.Parameter, clock); |
| 70 | _rig.Always(posed, current, cycle, Gait.Parameter, clock); |
| 71 | _rig.Constrain(posed); |
| 72 | for (var bone = 0; bone < posed.Length; bone++) |
| 73 | { |
| 74 | var pose = posed[bone]; |
| 75 | _skeleton.SetBonePosePosition(bone, |
| 76 | new Vector3(pose.Translation.X, pose.Translation.Y, pose.Translation.Z)); |
| 77 | _skeleton.SetBonePoseRotation(bone, |
| 78 | new Quaternion(pose.Rotation.X, pose.Rotation.Y, pose.Rotation.Z, pose.Rotation.W).Normalized()); |
| 79 | _skeleton.SetBonePoseScale(bone, new Vector3(pose.Scale.X, pose.Scale.Y, pose.Scale.Z)); |
| 80 | } |
| 81 | } |
| 82 | } |