Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change: IMotionAdapter argument to ref #21

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace LitMotion.Adapters
{
public readonly struct FixedString32BytesMotionAdapter : IMotionAdapter<FixedString32Bytes, StringOptions>
{
public FixedString32Bytes Evaluate(in FixedString32Bytes startValue, in FixedString32Bytes endValue, in StringOptions options, in MotionEvaluationContext context)
public FixedString32Bytes Evaluate(ref FixedString32Bytes startValue, ref FixedString32Bytes endValue, ref StringOptions options, in MotionEvaluationContext context)
{
var start = startValue;
var end = endValue;
Expand All @@ -24,7 +24,7 @@ public FixedString32Bytes Evaluate(in FixedString32Bytes startValue, in FixedStr

public readonly struct FixedString64BytesMotionAdapter : IMotionAdapter<FixedString64Bytes, StringOptions>
{
public FixedString64Bytes Evaluate(in FixedString64Bytes startValue, in FixedString64Bytes endValue, in StringOptions options, in MotionEvaluationContext context)
public FixedString64Bytes Evaluate(ref FixedString64Bytes startValue, ref FixedString64Bytes endValue, ref StringOptions options, in MotionEvaluationContext context)
{
var start = startValue;
var end = endValue;
Expand All @@ -36,7 +36,7 @@ public FixedString64Bytes Evaluate(in FixedString64Bytes startValue, in FixedStr

public readonly struct FixedString128BytesMotionAdapter : IMotionAdapter<FixedString128Bytes, StringOptions>
{
public FixedString128Bytes Evaluate(in FixedString128Bytes startValue, in FixedString128Bytes endValue, in StringOptions options, in MotionEvaluationContext context)
public FixedString128Bytes Evaluate(ref FixedString128Bytes startValue, ref FixedString128Bytes endValue, ref StringOptions options, in MotionEvaluationContext context)
{
var start = startValue;
var end = endValue;
Expand All @@ -48,7 +48,7 @@ public FixedString128Bytes Evaluate(in FixedString128Bytes startValue, in FixedS

public readonly struct FixedString512BytesMotionAdapter : IMotionAdapter<FixedString512Bytes, StringOptions>
{
public FixedString512Bytes Evaluate(in FixedString512Bytes startValue, in FixedString512Bytes endValue, in StringOptions options, in MotionEvaluationContext context)
public FixedString512Bytes Evaluate(ref FixedString512Bytes startValue, ref FixedString512Bytes endValue, ref StringOptions options, in MotionEvaluationContext context)
{
var start = startValue;
var end = endValue;
Expand All @@ -60,7 +60,7 @@ public FixedString512Bytes Evaluate(in FixedString512Bytes startValue, in FixedS

public readonly struct FixedString4096BytesMotionAdapter : IMotionAdapter<FixedString4096Bytes, StringOptions>
{
public FixedString4096Bytes Evaluate(in FixedString4096Bytes startValue, in FixedString4096Bytes endValue, in StringOptions options, in MotionEvaluationContext context)
public FixedString4096Bytes Evaluate(ref FixedString4096Bytes startValue, ref FixedString4096Bytes endValue, ref StringOptions options, in MotionEvaluationContext context)
{
var start = startValue;
var end = endValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,23 @@ namespace LitMotion.Adapters
{
public readonly struct FloatMotionAdapter : IMotionAdapter<float, NoOptions>
{
public float Evaluate(in float startValue, in float endValue, in NoOptions options, in MotionEvaluationContext context)
public float Evaluate(ref float startValue, ref float endValue, ref NoOptions options, in MotionEvaluationContext context)
{
return math.lerp(startValue, endValue, context.Progress);
}
}

public readonly struct DoubleMotionAdapter : IMotionAdapter<double, NoOptions>
{
public double Evaluate(in double startValue, in double endValue, in NoOptions options, in MotionEvaluationContext context)
public double Evaluate(ref double startValue, ref double endValue, ref NoOptions options, in MotionEvaluationContext context)
{
return math.lerp(startValue, endValue, context.Progress);
}
}

public readonly struct IntMotionAdapter : IMotionAdapter<int, IntegerOptions>
{
public int Evaluate(in int startValue, in int endValue, in IntegerOptions options, in MotionEvaluationContext context)
public int Evaluate(ref int startValue, ref int endValue, ref IntegerOptions options, in MotionEvaluationContext context)
{
var value = math.lerp(startValue, endValue, context.Progress);

Expand All @@ -44,7 +44,7 @@ public int Evaluate(in int startValue, in int endValue, in IntegerOptions option
}
public readonly struct LongMotionAdapter : IMotionAdapter<long, IntegerOptions>
{
public long Evaluate(in long startValue, in long endValue, in IntegerOptions options, in MotionEvaluationContext context)
public long Evaluate(ref long startValue, ref long endValue, ref IntegerOptions options, in MotionEvaluationContext context)
{
var value = math.lerp((double)startValue, endValue, context.Progress);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace LitMotion.Adapters

public readonly struct FloatPunchMotionAdapter : IMotionAdapter<float, PunchOptions>
{
public float Evaluate(in float startValue, in float endValue, in PunchOptions options, in MotionEvaluationContext context)
public float Evaluate(ref float startValue, ref float endValue, ref PunchOptions options, in MotionEvaluationContext context)
{
VibrationHelper.EvaluateStrength(endValue, options.Frequency, options.DampingRatio, context.Progress, out var result);
return startValue + result;
Expand All @@ -22,7 +22,7 @@ public float Evaluate(in float startValue, in float endValue, in PunchOptions op

public readonly struct Vector2PunchMotionAdapter : IMotionAdapter<Vector2, PunchOptions>
{
public Vector2 Evaluate(in Vector2 startValue, in Vector2 endValue, in PunchOptions options, in MotionEvaluationContext context)
public Vector2 Evaluate(ref Vector2 startValue, ref Vector2 endValue, ref PunchOptions options, in MotionEvaluationContext context)
{
VibrationHelper.EvaluateStrength(endValue, options.Frequency, options.DampingRatio, context.Progress, out var result);
return startValue + result;
Expand All @@ -31,7 +31,7 @@ public Vector2 Evaluate(in Vector2 startValue, in Vector2 endValue, in PunchOpti

public readonly struct Vector3PunchMotionAdapter : IMotionAdapter<Vector3, PunchOptions>
{
public Vector3 Evaluate(in Vector3 startValue, in Vector3 endValue, in PunchOptions options, in MotionEvaluationContext context)
public Vector3 Evaluate(ref Vector3 startValue, ref Vector3 endValue, ref PunchOptions options, in MotionEvaluationContext context)
{
VibrationHelper.EvaluateStrength(endValue, options.Frequency, options.DampingRatio, context.Progress, out var result);
return startValue + result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace LitMotion.Adapters

public readonly struct FloatShakeMotionAdapter : IMotionAdapter<float, ShakeOptions>
{
public float Evaluate(in float startValue, in float endValue, in ShakeOptions options, in MotionEvaluationContext context)
public float Evaluate(ref float startValue, ref float endValue, ref ShakeOptions options, in MotionEvaluationContext context)
{
VibrationHelper.EvaluateStrength(endValue, options.Frequency, options.DampingRatio, context.Progress, out var s);
float multipliar;
Expand All @@ -23,7 +23,6 @@ public float Evaluate(in float startValue, in float endValue, in ShakeOptions op
}
else
{
// Currently RandomState is defensively copied and the state is not changed. This requires changing the Adapter's option argument to ref.
multipliar = options.RandomState.NextFloat(-1f, 1f);
}
return startValue + s * multipliar;
Expand All @@ -32,7 +31,7 @@ public float Evaluate(in float startValue, in float endValue, in ShakeOptions op

public readonly struct Vector2ShakeMotionAdapter : IMotionAdapter<Vector2, ShakeOptions>
{
public Vector2 Evaluate(in Vector2 startValue, in Vector2 endValue, in ShakeOptions options, in MotionEvaluationContext context)
public Vector2 Evaluate(ref Vector2 startValue, ref Vector2 endValue, ref ShakeOptions options, in MotionEvaluationContext context)
{
VibrationHelper.EvaluateStrength(endValue, options.Frequency, options.DampingRatio, context.Progress, out var s);
Vector2 multipliar;
Expand All @@ -50,7 +49,7 @@ public Vector2 Evaluate(in Vector2 startValue, in Vector2 endValue, in ShakeOpti

public readonly struct Vector3ShakeMotionAdapter : IMotionAdapter<Vector3, ShakeOptions>
{
public Vector3 Evaluate(in Vector3 startValue, in Vector3 endValue, in ShakeOptions options, in MotionEvaluationContext context)
public Vector3 Evaluate(ref Vector3 startValue, ref Vector3 endValue, ref ShakeOptions options, in MotionEvaluationContext context)
{
VibrationHelper.EvaluateStrength(endValue, options.Frequency, options.DampingRatio, context.Progress, out var s);
Vector3 multipliar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,47 @@ namespace LitMotion.Adapters
{
public readonly struct Vector2MotionAdapter : IMotionAdapter<Vector2, NoOptions>
{
public Vector2 Evaluate(in Vector2 startValue, in Vector2 endValue, in NoOptions options, in MotionEvaluationContext context)
public Vector2 Evaluate(ref Vector2 startValue, ref Vector2 endValue, ref NoOptions options, in MotionEvaluationContext context)
{
return Vector2.LerpUnclamped(startValue, endValue, context.Progress);
}
}

public readonly struct Vector3MotionAdapter : IMotionAdapter<Vector3, NoOptions>
{
public Vector3 Evaluate(in Vector3 startValue, in Vector3 endValue, in NoOptions options, in MotionEvaluationContext context)
public Vector3 Evaluate(ref Vector3 startValue, ref Vector3 endValue, ref NoOptions options, in MotionEvaluationContext context)
{
return Vector3.LerpUnclamped(startValue, endValue, context.Progress);
}
}

public readonly struct Vector4MotionAdapter : IMotionAdapter<Vector4, NoOptions>
{
public Vector4 Evaluate(in Vector4 startValue, in Vector4 endValue, in NoOptions options, in MotionEvaluationContext context)
public Vector4 Evaluate(ref Vector4 startValue, ref Vector4 endValue, ref NoOptions options, in MotionEvaluationContext context)
{
return Vector4.LerpUnclamped(startValue, endValue, context.Progress);
}
}

public readonly struct QuaternionMotionAdapter : IMotionAdapter<Quaternion, NoOptions>
{
public Quaternion Evaluate(in Quaternion startValue, in Quaternion endValue, in NoOptions options, in MotionEvaluationContext context)
public Quaternion Evaluate(ref Quaternion startValue, ref Quaternion endValue, ref NoOptions options, in MotionEvaluationContext context)
{
return Quaternion.LerpUnclamped(startValue, endValue, context.Progress);
}
}

public readonly struct ColorMotionAdapter : IMotionAdapter<Color, NoOptions>
{
public Color Evaluate(in Color startValue, in Color endValue, in NoOptions options, in MotionEvaluationContext context)
public Color Evaluate(ref Color startValue, ref Color endValue, ref NoOptions options, in MotionEvaluationContext context)
{
return Color.LerpUnclamped(startValue, endValue, context.Progress);
}
}

public readonly struct RectMotionAdapter : IMotionAdapter<Rect, NoOptions>
{
public Rect Evaluate(in Rect startValue, in Rect endValue, in NoOptions options, in MotionEvaluationContext context)
public Rect Evaluate(ref Rect startValue, ref Rect endValue, ref NoOptions options, in MotionEvaluationContext context)
{
var x = math.lerp(startValue.x, endValue.x, context.Progress);
var y = math.lerp(startValue.y, endValue.y, context.Progress);
Expand Down
2 changes: 1 addition & 1 deletion src/LitMotion/Assets/LitMotion/Runtime/IMotionAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ public interface IMotionAdapter<TValue, TOptions>
/// <param name="options">Option value to specify</param>
/// <param name="context">Animation context</param>
/// <returns>Current value</returns>
TValue Evaluate(in TValue startValue, in TValue endValue, in TOptions options, in MotionEvaluationContext context);
TValue Evaluate(ref TValue startValue, ref TValue endValue, ref TOptions options, in MotionEvaluationContext context);
}
}
14 changes: 6 additions & 8 deletions src/LitMotion/Assets/LitMotion/Runtime/Internal/MotionStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void RemoveAll(NativeList<int> indexes)
RemoveAt(entries[entryIndexes[i]].DenseIndex);
}

//Avoid Memory leak
// Avoid Memory leak
lastCallbacksSpan[tail..].Clear();
entryIndexes.Dispose();
}
Expand All @@ -240,15 +240,14 @@ public void Cancel(MotionHandle handle)
throw new ArgumentException("Motion has been destroyed or no longer exists.");
}

var motion = dataArray[denseIndex];
ref var motion = ref GetDataSpan()[denseIndex];
var version = entry.Version;
if (version <= 0 || version != handle.Version || motion.Status == MotionStatus.None)
{
throw new ArgumentException("Motion has been destroyed or no longer exists.");
}

motion.Status = MotionStatus.Canceled;
dataArray[denseIndex] = motion;

#if LITMOTION_SUPPORT_UNITASK
callbacksArray[denseIndex].UniTaskConfiguredSource?.OnMotionCanceled();
Expand All @@ -264,7 +263,7 @@ public void Complete(MotionHandle handle)
throw new ArgumentException("Motion has been destroyed or no longer exists.");
}

var motion = dataArray[denseIndex];
ref var motion = ref GetDataSpan()[denseIndex];
var version = entry.Version;
if (version <= 0 || version != handle.Version || motion.Status == MotionStatus.None)
{
Expand All @@ -286,7 +285,6 @@ public void Complete(MotionHandle handle)

// To avoid duplication of Complete processing, it is treated as canceled internally.
motion.Status = MotionStatus.Canceled;
dataArray[denseIndex] = motion;

float endProgress = motion.LoopType switch
{
Expand All @@ -296,9 +294,9 @@ public void Complete(MotionHandle handle)
_ => 1f
};
var endValue = default(TAdapter).Evaluate(
motion.StartValue,
motion.EndValue,
motion.Options,
ref motion.StartValue,
ref motion.EndValue,
ref motion.Options,
new() { Progress = EaseUtility.Evaluate(endProgress, motion.Ease) }
);

Expand Down
2 changes: 1 addition & 1 deletion src/LitMotion/Assets/LitMotion/Runtime/MotionUpdateJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void Execute([AssumeRange(0, int.MaxValue)] int index)
Progress = progress
};

Output[index] = default(TAdapter).Evaluate(ptr->StartValue, ptr->EndValue, ptr->Options, context);
Output[index] = default(TAdapter).Evaluate(ref ptr->StartValue, ref ptr->EndValue, ref ptr->Options, context);
}
else if (ptr->Status is MotionStatus.Completed or MotionStatus.Canceled)
{
Expand Down