Skip to content

Commit

Permalink
Add Easing to CinemachineSplineRoll
Browse files Browse the repository at this point in the history
  • Loading branch information
glabute committed Jul 10, 2024
1 parent e5a39a9 commit e875ea0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 3 deletions.
1 change: 1 addition & 0 deletions com.unity.cinemachine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

### Added
- Added CinemachineVirtualCameraBaseEditor, to make it easier to make conformant inspectors for custom virtual cameras and virtual camera managers.
- Added Easing option to CinemachineSplineRoll.


## [3.1.1] - 2024-06-15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ If you add this behavior to the Spline itself, then any [Cinemachine Camera](Cin
| Property | Field | Description |
| --- | --- | --- |
| __Index Unit__ | | Defines how to interpret the _Index_ field for each data point. _Knot_ is the recommended value because it remains robust if the spline points change. |
| __Easing__ | | When enabled, roll value will ease in and out of the data point values. Otherwise, interpolation is linear. |
| __Data Points__ | | The list of Roll points on the spline. At these postions on the spline, it will take on the specified roll value. |
| | _Index_ | The position on the Spline where it should assume the specified roll. The value is interpreted according to the _Index Unit_ setting. |
| | _Roll_ | The roll value for the spline. This is specified in degrees, and the axis of rotation is the spline tangent at that point. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public override VisualElement CreateInspectorGUI()

var rollProp = serializedObject.FindProperty(() => splineData.Roll);
ux.Add(SplineDataInspectorUtility.CreatePathUnitField(rollProp, () => splineData == null ? null : splineData.SplineContainer));
ux.Add(new PropertyField(serializedObject.FindProperty(() => splineData.Easing)));

ux.AddHeader("Data Points");
var list = ux.AddChild(SplineDataInspectorUtility.CreateDataListField(splineData.Roll, rollProp, () => splineData?.SplineContainer));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,38 @@ public struct RollData
public static implicit operator RollData(float roll) => new () { Value = roll };
}

/// <summary>Interpolator for the RollData</summary>
/// <summary>
/// When enabled, roll eases into and out of the data point values
/// </summary>
[Tooltip("When enabled, roll eases into and out of the data point values.")]
public bool Easing = true;

/// <summary>
/// Get the appropriate interpolator for the RollData, depending on the Easing setting
/// </summary>
/// <returns>The appropriate interpolator for the RollData, depending on the Easing setting.</returns>
public IInterpolator<RollData> GetInterpolator() => Easing ? new LerpRollDataWithEasing() : new LerpRollData();

/// <summary>Interpolator for the RollData, with no easing between data points.</summary>
public struct LerpRollData : IInterpolator<RollData>
{
/// <inheritdoc/>
public RollData Interpolate(RollData a, RollData b, float t) => new() { Value = Mathf.Lerp(a.Value, b.Value, t) };
}

/// <summary>Interpolator for the RollData, with easing between data points</summary>
public struct LerpRollDataWithEasing : IInterpolator<RollData>
{
/// <inheritdoc/>
public RollData Interpolate(RollData a, RollData b, float t)
{
var t2 = t * t;
var d = 1f - t;
t = 3f * d * t2 + t * t2;
return new() { Value = Mathf.Lerp(a.Value, b.Value, t) };
}
}

/// <summary>
/// Roll (in degrees) around the forward direction for specific location on the track.
/// When placed on a SplineContainer, this is going to be a global override that affects all vcams using the Spline.
Expand Down Expand Up @@ -78,6 +103,12 @@ internal ISplineContainer SplineContainer
}
}
#endif
void Reset()
{
Roll.Clear();
Easing = true;
}

void OnEnable() {} // Needed so we can disable it in the editor

/// <summary>Cache for clients that use CinemachineSplineRoll</summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,7 @@ static class SplineContainerExtensions
// Apply extra roll
if (roll != null && roll.enabled)
{
float rollValue = roll.Roll.Evaluate(spline, tNormalized,
PathIndexUnit.Normalized, new CinemachineSplineRoll.LerpRollData());
float rollValue = roll.Roll.Evaluate(spline, tNormalized, PathIndexUnit.Normalized, roll.GetInterpolator());
rotation = Quaternion.AngleAxis(-rollValue, fwd) * rotation;
}
return true;
Expand Down

0 comments on commit e875ea0

Please sign in to comment.