Skip to content

Commit

Permalink
version bump: 0.8.17; add axis alignment to spline movement tweens;
Browse files Browse the repository at this point in the history
  • Loading branch information
dyonng committed Apr 28, 2023
1 parent 1af609d commit b0d2468
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 33 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## [0.8.17] 2023.04.28

### Added
- Added additional spline alignment settings:
- can specify the axis of alignment; useful for keeping orientation, eg: 2d splines in 3d space.
- can specify a rotational offset for the new axis alignment modes.

## [0.8.16] 2023.04.17

### Fixed
Expand Down
48 changes: 24 additions & 24 deletions Runtime/Static/Transform/Tween.MoveSpline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ public static uint FromStartToEnd(
in Entity entity,
ref NativeSpline spline,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
var splineTweenInfo = new SplineTweenInfo
{
Spline = spline,
NormalizedEndPosition = 1f,
NormalizedStartPosition = 0f,
AlignRotationToSpline = alignRotation
Alignment = alignMode
};
var command = new TweenSplineMovementCommand(entity, splineTweenInfo, duration, tweenParams);
state.EntityManager.AddComponentData(entity, command);
Expand All @@ -34,29 +34,29 @@ public static uint FromStartToEnd(
in Entity entity,
ref NativeSpline spline,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
var splineTweenInfo = new SplineTweenInfo
{
Spline = spline,
NormalizedEndPosition = 1f,
NormalizedStartPosition = 0f,
AlignRotationToSpline = alignRotation
Alignment = alignMode
};
var command = new TweenSplineMovementCommand(entity, splineTweenInfo, duration, tweenParams);
ecb.AddComponent(entity, command);
return command.TweenParams.Id;
}

public static uint FromStartToEnd(ref EntityCommandBuffer.ParallelWriter ecb, in int sortKey, in Entity entity, ref NativeSpline spline, float duration, bool alignRotation = false, in TweenParams tweenParams = default)
public static uint FromStartToEnd(ref EntityCommandBuffer.ParallelWriter ecb, in int sortKey, in Entity entity, ref NativeSpline spline, float duration, TweenSplineAlignmentSettings alignMode = default, in TweenParams tweenParams = default)
{
var splineTweenInfo = new SplineTweenInfo
{
Spline = spline,
NormalizedEndPosition = 1f,
NormalizedStartPosition = 0f,
AlignRotationToSpline = alignRotation
Alignment = alignMode
};
var command = new TweenSplineMovementCommand(entity, splineTweenInfo, duration, tweenParams);
ecb.AddComponent(sortKey, entity, command);
Expand All @@ -70,15 +70,15 @@ public static uint FromTo(
float normalizedSplineStartPosition,
float normalizedSplineEndPosition,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
var splineTweenInfo = new SplineTweenInfo
{
Spline = spline,
NormalizedEndPosition = normalizedSplineStartPosition,
NormalizedStartPosition = normalizedSplineEndPosition,
AlignRotationToSpline = alignRotation
Alignment = alignMode
};
var command = new TweenSplineMovementCommand(entity, splineTweenInfo, duration, tweenParams);
state.EntityManager.AddComponentData(entity, command);
Expand All @@ -92,15 +92,15 @@ public static uint FromTo(
float normalizedSplineStartPosition,
float normalizedSplineEndPosition,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
var splineTweenInfo = new SplineTweenInfo
{
Spline = spline,
NormalizedEndPosition = normalizedSplineStartPosition,
NormalizedStartPosition = normalizedSplineEndPosition,
AlignRotationToSpline = alignRotation
Alignment = alignMode
};
var command = new TweenSplineMovementCommand(entity, splineTweenInfo, duration, tweenParams);
ecb.AddComponent(entity, command);
Expand All @@ -115,15 +115,15 @@ public static uint FromTo(
float normalizedSplineStartPosition,
float normalizedSplineEndPosition,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
var splineTweenInfo = new SplineTweenInfo
{
Spline = spline,
NormalizedEndPosition = normalizedSplineStartPosition,
NormalizedStartPosition = normalizedSplineEndPosition,
AlignRotationToSpline = alignRotation
Alignment = alignMode
};
var command = new TweenSplineMovementCommand(entity, splineTweenInfo, duration, tweenParams);
ecb.AddComponent(sortKey, entity, command);
Expand All @@ -136,10 +136,10 @@ public static uint From(
ref NativeSpline spline,
float normalizedSplineStartPosition,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
return FromTo(ref state, entity, ref spline, normalizedSplineStartPosition, 1f, duration, alignRotation, tweenParams);
return FromTo(ref state, entity, ref spline, normalizedSplineStartPosition, 1f, duration, alignMode, tweenParams);
}

public static uint From(
Expand All @@ -148,10 +148,10 @@ public static uint From(
ref NativeSpline spline,
float normalizedSplineStartPosition,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
return FromTo(ref ecb, entity, ref spline, normalizedSplineStartPosition, 1f, duration, alignRotation, tweenParams);
return FromTo(ref ecb, entity, ref spline, normalizedSplineStartPosition, 1f, duration, alignMode, tweenParams);
}

public static uint From(
Expand All @@ -161,10 +161,10 @@ public static uint From(
ref NativeSpline spline,
float normalizedSplineStartPosition,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
return FromTo(ref parallelWriter, sortKey, entity, ref spline, normalizedSplineStartPosition, 1f, duration, alignRotation, tweenParams);
return FromTo(ref parallelWriter, sortKey, entity, ref spline, normalizedSplineStartPosition, 1f, duration, alignMode, tweenParams);
}

public static uint To(
Expand All @@ -173,10 +173,10 @@ public static uint To(
ref NativeSpline spline,
float normalizedSplineEndPosition,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
return FromTo(ref state, entity, ref spline, 0f, normalizedSplineEndPosition, duration, alignRotation, tweenParams);
return FromTo(ref state, entity, ref spline, 0f, normalizedSplineEndPosition, duration, alignMode, tweenParams);
}

public static uint To(
Expand All @@ -185,10 +185,10 @@ public static uint To(
ref NativeSpline spline,
float normalizedSplineEndPosition,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
return FromTo(ref ecb, entity, ref spline, 0f, normalizedSplineEndPosition, duration, alignRotation, tweenParams);
return FromTo(ref ecb, entity, ref spline, 0f, normalizedSplineEndPosition, duration, alignMode, tweenParams);
}

public static uint To(
Expand All @@ -198,10 +198,10 @@ public static uint To(
ref NativeSpline spline,
float normalizedSplineEndPosition,
float duration,
bool alignRotation = false,
TweenSplineAlignmentSettings alignMode = default,
in TweenParams tweenParams = default)
{
return FromTo(ref parallelWriter, sortKey, entity, ref spline, 0f, normalizedSplineEndPosition, duration, alignRotation, tweenParams);
return FromTo(ref parallelWriter, sortKey, entity, ref spline, 0f, normalizedSplineEndPosition, duration, alignMode, tweenParams);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#if DOTS_TWEEN_SPLINES
using System;
using System.Runtime.InteropServices;
using Unity.Burst;
using Unity.Entities;
using UnityEngine.Splines;
Expand Down Expand Up @@ -60,7 +59,7 @@ public void Cleanup()
public struct SplineTweenInfo : IEquatable<SplineTweenInfo>, IDisposable
{
public NativeSpline Spline;
[MarshalAs(UnmanagedType.U1)] public bool AlignRotationToSpline;
public TweenSplineAlignmentSettings Alignment;
public float NormalizedStartPosition;
public float NormalizedEndPosition;

Expand All @@ -70,7 +69,7 @@ public bool Equals(SplineTweenInfo other)
&& Spline.Closed == other.Spline.Closed
&& Spline.Curves == other.Spline.Curves
&& Spline.Knots == other.Spline.Knots
&& AlignRotationToSpline == other.AlignRotationToSpline
&& Alignment == other.Alignment
&& NormalizedStartPosition.Equals(other.NormalizedStartPosition)
&& NormalizedEndPosition.Equals(other.NormalizedEndPosition);
}
Expand All @@ -82,13 +81,60 @@ public override bool Equals(object obj)

public override int GetHashCode()
{
return HashCode.Combine(Spline, AlignRotationToSpline, NormalizedStartPosition, NormalizedEndPosition);
return HashCode.Combine(Spline, Alignment, NormalizedStartPosition, NormalizedEndPosition);
}

public void Dispose()
{
Spline.Dispose();
}
}

public struct TweenSplineAlignmentSettings : IEquatable<TweenSplineAlignmentSettings>
{
private const float Deg2Rad = 0.017453292f;

public TweenSplineAlignMode Mode;
public float AngleOffsetRadians;

public TweenSplineAlignmentSettings(TweenSplineAlignMode mode, float angleOffsetRadians)
{
Mode = mode;
AngleOffsetRadians = angleOffsetRadians;
}

public TweenSplineAlignmentSettings(float angleOffsetDegrees, TweenSplineAlignMode mode)
{
Mode = mode;
AngleOffsetRadians = angleOffsetDegrees * Deg2Rad;
}

public static bool operator ==(TweenSplineAlignmentSettings a, TweenSplineAlignmentSettings b) => a.Equals(b);
public static bool operator !=(TweenSplineAlignmentSettings a, TweenSplineAlignmentSettings b) => !(a == b);

public bool Equals(TweenSplineAlignmentSettings other)
{
return Mode == other.Mode && AngleOffsetRadians.Equals(other.AngleOffsetRadians);
}

public override bool Equals(object obj)
{
return obj is TweenSplineAlignmentSettings other && Equals(other);
}

public override int GetHashCode()
{
return HashCode.Combine((int)Mode, AngleOffsetRadians);
}
}

public enum TweenSplineAlignMode
{
None = 0,
Full,
XAxis,
YAxis,
ZAxis,
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,45 @@ protected override void OnUpdate()
float splinePosition = math.lerp(tweenInfo.SplineTweenInfo.NormalizedStartPosition, tweenInfo.SplineTweenInfo.NormalizedEndPosition, tween.EasePercentage);
localTransformRef.ValueRW.Position = tweenInfo.SplineTweenInfo.Spline.EvaluatePosition(splinePosition);

if (tweenInfo.SplineTweenInfo.AlignRotationToSpline)
switch (tweenInfo.SplineTweenInfo.Alignment.Mode)
{
var forward = math.normalize(tweenInfo.SplineTweenInfo.Spline.EvaluateTangent(splinePosition));
var up = tweenInfo.SplineTweenInfo.Spline.EvaluateUpVector(splinePosition);
localTransformRef.ValueRW.Rotation = math.normalize(quaternion.LookRotation(forward, up));
case TweenSplineAlignMode.Full:
{
var forward = math.normalize(tweenInfo.SplineTweenInfo.Spline.EvaluateTangent(splinePosition));
var up = tweenInfo.SplineTweenInfo.Spline.EvaluateUpVector(splinePosition);
localTransformRef.ValueRW.Rotation = math.normalize(quaternion.LookRotationSafe(forward, up));
break;
}

case TweenSplineAlignMode.XAxis:
{
float3 tangent = math.normalize(tweenInfo.SplineTweenInfo.Spline.EvaluateTangent(splinePosition));
float angleRadians = math.atan2(tangent.y, tangent.z);
if (!float.IsNaN(angleRadians)) localTransformRef.ValueRW.Rotation = quaternion.Euler(new float3(angleRadians - tweenInfo.SplineTweenInfo.Alignment.AngleOffsetRadians, 0f, 0f));
break;
}

case TweenSplineAlignMode.YAxis:
{
float3 tangent = math.normalize(tweenInfo.SplineTweenInfo.Spline.EvaluateTangent(splinePosition));
float angleRadians = math.atan2(tangent.z, tangent.x);
if (!float.IsNaN(angleRadians)) localTransformRef.ValueRW.Rotation = quaternion.Euler(new float3(0f, angleRadians - tweenInfo.SplineTweenInfo.Alignment.AngleOffsetRadians, 0f));
break;
}

case TweenSplineAlignMode.ZAxis:
{
float3 tangent = math.normalize(tweenInfo.SplineTweenInfo.Spline.EvaluateTangent(splinePosition));
float angleRadians = math.atan2(tangent.y, tangent.x);
if (!float.IsNaN(angleRadians)) localTransformRef.ValueRW.Rotation = quaternion.Euler(new float3(0f, 0f, angleRadians - tweenInfo.SplineTweenInfo.Alignment.AngleOffsetRadians));
break;
}

case TweenSplineAlignMode.None:
default:
{
break;
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "com.dyonng.dotstween",
"version": "0.8.16",
"version": "0.8.17",
"displayName": "DOTS Tween",
"description": "Tween library for Unity ECS/DOTS.",
"unity": "2022.2",
Expand Down

0 comments on commit b0d2468

Please sign in to comment.