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

Calculate a random number from the hash of the time and seed #164

Merged
merged 2 commits into from
Dec 7, 2024
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 @@ -18,8 +18,7 @@ public FixedString32Bytes Evaluate(ref FixedString32Bytes startValue, ref FixedS
var start = startValue;
var end = endValue;
var customScrambleChars = options.CustomScrambleChars;
ref var randomState = ref options.RandomState;
if (randomState.state == 0) randomState = ref SharedRandom.Random;
var randomState = RandomHelper.Create(options.RandomSeed, context.Time);
FixedStringHelper.Interpolate(ref start, ref end, context.Progress, options.ScrambleMode, options.RichTextEnabled, ref randomState, ref customScrambleChars, out var result);
return result;
}
Expand All @@ -32,8 +31,7 @@ public FixedString64Bytes Evaluate(ref FixedString64Bytes startValue, ref FixedS
var start = startValue;
var end = endValue;
var customScrambleChars = options.CustomScrambleChars;
ref var randomState = ref options.RandomState;
if (randomState.state == 0) randomState = ref SharedRandom.Random;
var randomState = RandomHelper.Create(options.RandomSeed, context.Time);
FixedStringHelper.Interpolate(ref start, ref end, context.Progress, options.ScrambleMode, options.RichTextEnabled, ref randomState, ref customScrambleChars, out var result);
return result;
}
Expand All @@ -46,8 +44,7 @@ public FixedString128Bytes Evaluate(ref FixedString128Bytes startValue, ref Fixe
var start = startValue;
var end = endValue;
var customScrambleChars = options.CustomScrambleChars;
ref var randomState = ref options.RandomState;
if (randomState.state == 0) randomState = ref SharedRandom.Random;
var randomState = RandomHelper.Create(options.RandomSeed, context.Time);
FixedStringHelper.Interpolate(ref start, ref end, context.Progress, options.ScrambleMode, options.RichTextEnabled, ref randomState, ref customScrambleChars, out var result);
return result;
}
Expand All @@ -60,8 +57,7 @@ public FixedString512Bytes Evaluate(ref FixedString512Bytes startValue, ref Fixe
var start = startValue;
var end = endValue;
var customScrambleChars = options.CustomScrambleChars;
ref var randomState = ref options.RandomState;
if (randomState.state == 0) randomState = ref SharedRandom.Random;
var randomState = RandomHelper.Create(options.RandomSeed, context.Time);
FixedStringHelper.Interpolate(ref start, ref end, context.Progress, options.ScrambleMode, options.RichTextEnabled, ref randomState, ref customScrambleChars, out var result);
return result;
}
Expand All @@ -74,8 +70,7 @@ public FixedString4096Bytes Evaluate(ref FixedString4096Bytes startValue, ref Fi
var start = startValue;
var end = endValue;
var customScrambleChars = options.CustomScrambleChars;
ref var randomState = ref options.RandomState;
if (randomState.state == 0) randomState = ref SharedRandom.Random;
var randomState = RandomHelper.Create(options.RandomSeed, context.Time);
FixedStringHelper.Interpolate(ref start, ref end, context.Progress, options.ScrambleMode, options.RichTextEnabled, ref randomState, ref customScrambleChars, out var result);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using UnityEngine;
using LitMotion;
using LitMotion.Adapters;
using Unity.Mathematics;

[assembly: RegisterGenericJobType(typeof(MotionUpdateJob<float, ShakeOptions, FloatShakeMotionAdapter>))]
[assembly: RegisterGenericJobType(typeof(MotionUpdateJob<Vector2, ShakeOptions, Vector2ShakeMotionAdapter>))]
Expand All @@ -16,15 +17,7 @@ namespace LitMotion.Adapters
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;
if (options.RandomState.state == 0)
{
multipliar = SharedRandom.Random.NextFloat(-1f, 1f);
}
else
{
multipliar = options.RandomState.NextFloat(-1f, 1f);
}
var multipliar = RandomHelper.NextFloat(options.RandomSeed, context.Time, -1f, 1f);
return startValue + s * multipliar;
}
}
Expand All @@ -34,15 +27,7 @@ public float Evaluate(ref float startValue, ref float endValue, ref ShakeOptions
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;
if (options.RandomState.state == 0)
{
multipliar = new Vector2(SharedRandom.Random.NextFloat(-1f, 1f), SharedRandom.Random.NextFloat(-1f, 1f));
}
else
{
multipliar = new Vector2(options.RandomState.NextFloat(-1f, 1f), options.RandomState.NextFloat(-1f, 1f));
}
var multipliar = RandomHelper.NextFloat2(options.RandomSeed, context.Time, new float2(-1f, -1f), new float2(1f, 1f));
return startValue + new Vector2(s.x * multipliar.x, s.y * multipliar.y);
}
}
Expand All @@ -52,15 +37,7 @@ public Vector2 Evaluate(ref Vector2 startValue, ref Vector2 endValue, ref ShakeO
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;
if (options.RandomState.state == 0)
{
multipliar = new Vector3(SharedRandom.Random.NextFloat(-1f, 1f), SharedRandom.Random.NextFloat(-1f, 1f), SharedRandom.Random.NextFloat(-1f, 1f));
}
else
{
multipliar = new Vector3(options.RandomState.NextFloat(-1f, 1f), options.RandomState.NextFloat(-1f, 1f), options.RandomState.NextFloat(-1f, 1f));
}
var multipliar = RandomHelper.NextFloat3(options.RandomSeed, context.Time, new float3(-1f, -1f, -1f), new float3(1f, 1f, 1f));
return startValue + new Vector3(s.x * multipliar.x, s.y * multipliar.y, s.z * multipliar.z);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public static void Update<TValue, TOptions, TAdapter>(MotionData<TValue, TOption

var context = new MotionEvaluationContext()
{
Progress = progress
Progress = progress,
Time = time,
};

result = default(TAdapter).Evaluate(ref ptr->StartValue, ref ptr->EndValue, ref ptr->Options, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@ public unsafe MotionHandle Create(ref MotionBuilder<TValue, TOptions, TAdapter>
{
Ease.CustomAnimationCurve => buffer.AnimationCurve.Evaluate(0f),
_ => EaseUtility.Evaluate(0f, dataRef.Core.Ease)
}
},
Time = dataRef.Core.Time,
}
));
}
Expand Down Expand Up @@ -327,7 +328,11 @@ int TryCompleteCore(MotionHandle handle)
ref unmanagedData.StartValue,
ref unmanagedData.EndValue,
ref unmanagedData.Options,
new() { Progress = easedEndProgress }
new()
{
Progress = easedEndProgress,
Time = unmanagedData.Core.Time,
}
);

managedData.UpdateUnsafe(endValue);
Expand Down
36 changes: 36 additions & 0 deletions src/LitMotion/Assets/LitMotion/Runtime/Internal/RandomHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Unity.Mathematics;
using UnityEngine;

namespace LitMotion
{
internal static class RandomHelper
{
public static float NextFloat(uint seed, double time, float min, float max)
{
return Create(seed, time).NextFloat(min, max);
}

public static float2 NextFloat2(uint seed, double time, in float2 min, in float2 max)
{
return Create(seed, time).NextFloat2(min, max);
}

public static float3 NextFloat3(uint seed, double time, in float3 min, in float3 max)
{
return Create(seed, time).NextFloat3(min, max);
}

static uint GetHash(uint seed, double time)
{
var hash = new Hash128();
hash.Append(ref seed);
hash.Append(ref time);
return (uint)hash.GetHashCode();
}

public static Unity.Mathematics.Random Create(uint seed, double time)
{
return new Unity.Mathematics.Random(GetHash(seed, time));
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 0 additions & 20 deletions src/LitMotion/Assets/LitMotion/Runtime/Internal/SharedRandom.cs

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ public static MotionBuilder<TValue, ShakeOptions, TAdapter> WithRandomSeed<TValu
where TAdapter : unmanaged, IMotionAdapter<TValue, ShakeOptions>
{
var options = builder.buffer.Options;
options.RandomState = new Unity.Mathematics.Random(seed);
options.RandomSeed = seed;
builder.buffer.Options = options;
return builder;
}
Expand Down Expand Up @@ -163,7 +163,7 @@ public static MotionBuilder<TValue, StringOptions, TAdapter> WithRandomSeed<TVal
where TAdapter : unmanaged, IMotionAdapter<TValue, StringOptions>
{
var options = builder.buffer.Options;
options.RandomState = new Unity.Mathematics.Random(seed);
options.RandomSeed = seed;
builder.buffer.Options = options;
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ public struct MotionEvaluationContext
/// Progress (0-1)
/// </summary>
public float Progress;

/// <summary>
/// Current motion time
/// </summary>
public double Time;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public struct ShakeOptions : IEquatable<ShakeOptions>, IMotionOptions
{
public int Frequency;
public float DampingRatio;
public Unity.Mathematics.Random RandomState;
public uint RandomSeed;

public static ShakeOptions Default
{
Expand All @@ -28,7 +28,7 @@ public readonly bool Equals(ShakeOptions other)
{
return other.Frequency == Frequency &&
other.DampingRatio == DampingRatio &&
other.RandomState.state == RandomState.state;
other.RandomSeed == RandomSeed;
}

public override readonly bool Equals(object obj)
Expand All @@ -39,7 +39,7 @@ public override readonly bool Equals(object obj)

public override readonly int GetHashCode()
{
return HashCode.Combine(Frequency, DampingRatio, RandomState);
return HashCode.Combine(Frequency, DampingRatio, RandomSeed);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public struct StringOptions : IMotionOptions, IEquatable<StringOptions>
public ScrambleMode ScrambleMode;
public bool RichTextEnabled;
public FixedString64Bytes CustomScrambleChars;
public Unity.Mathematics.Random RandomState;
public uint RandomSeed;

public readonly bool Equals(StringOptions other)
{
return other.ScrambleMode == ScrambleMode &&
other.RichTextEnabled == RichTextEnabled &&
other.CustomScrambleChars == CustomScrambleChars &&
other.RandomState.state == RandomState.state;
other.RandomSeed == RandomSeed;
}

public override readonly bool Equals(object obj)
Expand All @@ -61,7 +61,7 @@ public override readonly bool Equals(object obj)

public override readonly int GetHashCode()
{
return HashCode.Combine(ScrambleMode, RichTextEnabled, CustomScrambleChars, RandomState);
return HashCode.Combine(ScrambleMode, RichTextEnabled, CustomScrambleChars, RandomSeed);
}
}
}