From 2fac4c9284c394db180fcab07987972c31d4cf36 Mon Sep 17 00:00:00 2001 From: Ruan Lucas <79365912+RuanLucasGD@users.noreply.github.com> Date: Tue, 18 Jul 2023 20:28:28 -0400 Subject: [PATCH] add smooth tangent on first and last keyframe --- .../CustomEditors/Dedicated/SplineEditor.cs | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/Source/Editor/CustomEditors/Dedicated/SplineEditor.cs b/Source/Editor/CustomEditors/Dedicated/SplineEditor.cs index 8f6dccd39..4ddf5c26c 100644 --- a/Source/Editor/CustomEditors/Dedicated/SplineEditor.cs +++ b/Source/Editor/CustomEditors/Dedicated/SplineEditor.cs @@ -19,7 +19,7 @@ namespace FlaxEditor.CustomEditors.Dedicated /// /// Basis for creating tangent manipulation types for bezier curves. /// - public abstract class TangentModeBase + private abstract class TangentModeBase { /// /// Called when user set selected tangent mode. @@ -60,7 +60,7 @@ namespace FlaxEditor.CustomEditors.Dedicated /// /// Edit curve options manipulate the curve as free mode /// - public sealed class FreeTangentMode : TangentModeBase + private sealed class FreeTangentMode : TangentModeBase { /// public override void OnMoveTangentIn(Spline spline, int index) { } @@ -81,7 +81,7 @@ namespace FlaxEditor.CustomEditors.Dedicated /// /// Edit curve options to set tangents to linear /// - public sealed class LinearTangentMode : TangentModeBase + private sealed class LinearTangentMode : TangentModeBase { /// public override void OnMoveTangentIn(Spline spline, int index) { } @@ -116,7 +116,7 @@ namespace FlaxEditor.CustomEditors.Dedicated /// /// Edit curve options to align tangents of selected spline /// - public sealed class AlignedTangentMode : TangentModeBase + private sealed class AlignedTangentMode : TangentModeBase { /// public override void OnSetMode(Spline spline, int index) @@ -167,18 +167,19 @@ namespace FlaxEditor.CustomEditors.Dedicated var isLastKeyframe = index >= spline.SplinePointsCount - 1; var isFirstKeyframe = index <= 0; - if (!isLastKeyframe && !isFirstKeyframe) - { - var nextKeyframe = spline.GetSplineKeyframe(++index); - var previousKeyframe = spline.GetSplineKeyframe(--index); + // force smooth it's linear point + if (tangentInSize == 0f && !isFirstKeyframe) tangentInSize = 100; + if (tangentOutSize == 0f && !isLastKeyframe) tangentOutSize = 100; - // calc form from Spline.cpp -> SetTangentsSmooth - var slop = (keyframe.Value.Translation - previousKeyframe.Value.Translation + nextKeyframe.Value.Translation - keyframe.Value.Translation).Normalized; + var nextKeyframe = !isLastKeyframe ? spline.GetSplineKeyframe(index + 1) : keyframe; + var previousKeyframe = !isFirstKeyframe ? spline.GetSplineKeyframe(index - 1) : keyframe; - keyframe.TangentIn.Translation = -slop * tangentInSize; - keyframe.TangentOut.Translation = slop * tangentOutSize; - spline.SetSplineKeyframe(index, keyframe); - } + // calc form from Spline.cpp -> SetTangentsSmooth + var slop = (keyframe.Value.Translation - previousKeyframe.Value.Translation + nextKeyframe.Value.Translation - keyframe.Value.Translation).Normalized; + + keyframe.TangentIn.Translation = -slop * tangentInSize; + keyframe.TangentOut.Translation = slop * tangentOutSize; + spline.SetSplineKeyframe(index, keyframe); } private void SetPointAligned(Spline spline, int index, bool isIn)