VibrationMotion.cs
Runtime/Adapters/Vibration/VibrationMotion.cs
1 using System;
2 using MeatMotion.Adapters;
3 using Unity.Mathematics;
4 using UnityEngine;
5
6 namespace MeatMotion
7 {
8 public enum ShakeMode : byte { Random, PerlinNoise }
9
10 [Serializable]
11 public struct PunchOptions : IMotionOptions, IEquatable<PunchOptions>
12 {
13 public int Frequency;
14 public float DampingRatio;
15 public static PunchOptions Default => new PunchOptions { Frequency = 10, DampingRatio = 1 };
16 public readonly bool Equals(PunchOptions other) => Frequency == other.Frequency && DampingRatio == other.DampingRatio;
17 public override readonly bool Equals(object obj) => obj is PunchOptions other && Equals(other);
18 public override readonly int GetHashCode() => HashCode.Combine(Frequency, DampingRatio);
19 }
20
21 [Serializable]
22 public struct ShakeOptions : IMotionOptions, IEquatable<ShakeOptions>
23 {
24 public int Frequency;
25 public float DampingRatio;
26 public uint RandomSeed;
27 public ShakeMode ShakeMode;
28 public static ShakeOptions Default => new ShakeOptions { Frequency = 10, DampingRatio = 1, ShakeMode = ShakeMode.PerlinNoise };
29 public readonly bool Equals(ShakeOptions other) =>
30 ShakeMode == other.ShakeMode && Frequency == other.Frequency &&
31 DampingRatio == other.DampingRatio && RandomSeed == other.RandomSeed;
32 public override readonly bool Equals(object obj) => obj is ShakeOptions other && Equals(other);
33 public override readonly int GetHashCode() => HashCode.Combine(ShakeMode, Frequency, DampingRatio, RandomSeed);
34 }
35
36 public static partial class MMotion
37 {
38 public static class Punch
39 {
40 public static MotionBuilder<float, PunchOptions, FloatPunchMotionAdapter> Create(float startValue, float strength, float duration)
41 => MMotion.Create<float, PunchOptions, FloatPunchMotionAdapter>(startValue, strength, duration).WithOptions(PunchOptions.Default);
42
43 public static MotionBuilder<Vector2, PunchOptions, Vector2PunchMotionAdapter> Create(Vector2 startValue, Vector2 strength, float duration)
44 => MMotion.Create<Vector2, PunchOptions, Vector2PunchMotionAdapter>(startValue, strength, duration).WithOptions(PunchOptions.Default);
45
46 public static MotionBuilder<Vector3, PunchOptions, Vector3PunchMotionAdapter> Create(Vector3 startValue, Vector3 strength, float duration)
47 => MMotion.Create<Vector3, PunchOptions, Vector3PunchMotionAdapter>(startValue, strength, duration).WithOptions(PunchOptions.Default);
48 }
49
50 public static class Shake
51 {
52 public static MotionBuilder<float, ShakeOptions, FloatShakeMotionAdapter> Create(float startValue, float strength, float duration)
53 => MMotion.Create<float, ShakeOptions, FloatShakeMotionAdapter>(startValue, strength, duration).WithOptions(ShakeOptions.Default);
54
55 public static MotionBuilder<Vector2, ShakeOptions, Vector2ShakeMotionAdapter> Create(Vector2 startValue, Vector2 strength, float duration)
56 => MMotion.Create<Vector2, ShakeOptions, Vector2ShakeMotionAdapter>(startValue, strength, duration).WithOptions(ShakeOptions.Default);
57
58 public static MotionBuilder<Vector3, ShakeOptions, Vector3ShakeMotionAdapter> Create(Vector3 startValue, Vector3 strength, float duration)
59 => MMotion.Create<Vector3, ShakeOptions, Vector3ShakeMotionAdapter>(startValue, strength, duration).WithOptions(ShakeOptions.Default);
60 }
61 }
62
63 public static partial class MotionBuilderExtensions
64 {
65 public static MotionBuilder<TValue, PunchOptions, TAdapter> WithFrequency<TValue, TAdapter>(
66 this MotionBuilder<TValue, PunchOptions, TAdapter> builder, int frequency)
67 where TValue : unmanaged
68 where TAdapter : unmanaged, IMotionAdapter<TValue, PunchOptions>
69 {
70 var options = builder.Options;
71 options.Frequency = frequency;
72 return builder.WithOptions(options);
73 }
74
75 public static MotionBuilder<TValue, PunchOptions, TAdapter> WithDampingRatio<TValue, TAdapter>(
76 this MotionBuilder<TValue, PunchOptions, TAdapter> builder, float dampingRatio)
77 where TValue : unmanaged
78 where TAdapter : unmanaged, IMotionAdapter<TValue, PunchOptions>
79 {
80 var options = builder.Options;
81 options.DampingRatio = dampingRatio;
82 return builder.WithOptions(options);
83 }
84
85 public static MotionBuilder<TValue, ShakeOptions, TAdapter> WithFrequency<TValue, TAdapter>(
86 this MotionBuilder<TValue, ShakeOptions, TAdapter> builder, int frequency)
87 where TValue : unmanaged
88 where TAdapter : unmanaged, IMotionAdapter<TValue, ShakeOptions>
89 {
90 var options = builder.Options;
91 options.Frequency = frequency;
92 return builder.WithOptions(options);
93 }
94
95 public static MotionBuilder<TValue, ShakeOptions, TAdapter> WithDampingRatio<TValue, TAdapter>(
96 this MotionBuilder<TValue, ShakeOptions, TAdapter> builder, float dampingRatio)
97 where TValue : unmanaged
98 where TAdapter : unmanaged, IMotionAdapter<TValue, ShakeOptions>
99 {
100 var options = builder.Options;
101 options.DampingRatio = dampingRatio;
102 return builder.WithOptions(options);
103 }
104
105 public static MotionBuilder<TValue, ShakeOptions, TAdapter> WithRandomSeed<TValue, TAdapter>(
106 this MotionBuilder<TValue, ShakeOptions, TAdapter> builder, uint seed)
107 where TValue : unmanaged
108 where TAdapter : unmanaged, IMotionAdapter<TValue, ShakeOptions>
109 {
110 var options = builder.Options;
111 options.RandomSeed = seed;
112 return builder.WithOptions(options);
113 }
114
115 public static MotionBuilder<TValue, ShakeOptions, TAdapter> WithShakeMode<TValue, TAdapter>(
116 this MotionBuilder<TValue, ShakeOptions, TAdapter> builder, ShakeMode shakeMode)
117 where TValue : unmanaged
118 where TAdapter : unmanaged, IMotionAdapter<TValue, ShakeOptions>
119 {
120 var options = builder.Options;
121 options.ShakeMode = shakeMode;
122 return builder.WithOptions(options);
123 }
124 }
125 }
126
127 namespace MeatMotion.Adapters
128 {
129 internal interface IVibrationAdapter { }
130
131 internal static class Vibration
132 {
133 internal static float Amplitude(float progress, int frequency, float damping)
134 {
135 if (progress == 0f || progress == 1f) return 0f;
136 float phase = math.PI * (frequency * progress - 0.5f);
137 return math.cos(phase) * math.exp(-(frequency * damping / (2f * math.PI)) * progress);
138 }
139
140 internal static float3 Noise(in ShakeOptions options, in MotionEvaluationContext context, int axes)
141 {
142 if (options.ShakeMode == ShakeMode.Random)
143 {
144 return RandomNoise(SeedHash(options.RandomSeed), context.Time);
145 }
146 float phase = context.Progress * options.Frequency;
147 var sample = new float3();
148 for (int axis = 0; axis < axes; axis++)
149 {
150 sample[axis] = noise.cnoise(new float2(phase, Offset(options.RandomSeed, axis)));
151 }
152 return sample;
153 }
154
155 internal static float Offset(uint seed, int axis) => (math.hash(new uint2(seed, (uint)axis)) & 0xffffu) * (1f / 256f);
156
157 internal static Hash128 SeedHash(uint seed)
158 {
159 var hash = new Hash128();
160 hash.Append(ref seed);
161 return hash;
162 }
163
164 internal static float3 RandomNoise(Hash128 hash, double time)
165 {
166 hash.Append(ref time);
167 return new Unity.Mathematics.Random((uint)hash.GetHashCode()).NextFloat3(-1f, 1f);
168 }
169 }
170
171 public readonly struct FloatPunchMotionAdapter : IMotionAdapter<float, PunchOptions>, IProgressOnlyAdapter, IVibrationAdapter
172 {
173 public float Evaluate(ref float startValue, ref float endValue, ref PunchOptions options, in MotionEvaluationContext context)
174 {
175 float amplitude = Vibration.Amplitude(context.Progress, options.Frequency, options.DampingRatio);
176 return startValue + endValue * amplitude;
177 }
178 }
179
180 public readonly struct Vector2PunchMotionAdapter : IMotionAdapter<Vector2, PunchOptions>, IProgressOnlyAdapter, IVibrationAdapter
181 {
182 public Vector2 Evaluate(ref Vector2 startValue, ref Vector2 endValue, ref PunchOptions options, in MotionEvaluationContext context)
183 {
184 float amplitude = Vibration.Amplitude(context.Progress, options.Frequency, options.DampingRatio);
185 return startValue + endValue * amplitude;
186 }
187 }
188
189 public readonly struct Vector3PunchMotionAdapter : IMotionAdapter<Vector3, PunchOptions>, IProgressOnlyAdapter, IVibrationAdapter
190 {
191 public Vector3 Evaluate(ref Vector3 startValue, ref Vector3 endValue, ref PunchOptions options, in MotionEvaluationContext context)
192 {
193 float amplitude = Vibration.Amplitude(context.Progress, options.Frequency, options.DampingRatio);
194 return startValue + endValue * amplitude;
195 }
196 }
197
198 public readonly struct FloatShakeMotionAdapter : IMotionAdapter<float, ShakeOptions>, IVibrationAdapter
199 {
200 public float Evaluate(ref float startValue, ref float endValue, ref ShakeOptions options, in MotionEvaluationContext context)
201 {
202 float amplitude = Vibration.Amplitude(context.Progress, options.Frequency, options.DampingRatio);
203 if (amplitude == 0) return startValue;
204 var noise = Vibration.Noise(in options, in context, 1);
205 return startValue + endValue * noise.x * amplitude;
206 }
207 }
208
209 public readonly struct Vector2ShakeMotionAdapter : IMotionAdapter<Vector2, ShakeOptions>, IVibrationAdapter
210 {
211 public Vector2 Evaluate(ref Vector2 startValue, ref Vector2 endValue, ref ShakeOptions options, in MotionEvaluationContext context)
212 {
213 float amplitude = Vibration.Amplitude(context.Progress, options.Frequency, options.DampingRatio);
214 if (amplitude == 0) return startValue;
215 var noise = Vibration.Noise(in options, in context, 2);
216 return startValue + Vector2.Scale(endValue, new Vector2(noise.x, noise.y)) * amplitude;
217 }
218 }
219
220 public readonly struct Vector3ShakeMotionAdapter : IMotionAdapter<Vector3, ShakeOptions>, IVibrationAdapter
221 {
222 public Vector3 Evaluate(ref Vector3 startValue, ref Vector3 endValue, ref ShakeOptions options, in MotionEvaluationContext context)
223 {
224 float amplitude = Vibration.Amplitude(context.Progress, options.Frequency, options.DampingRatio);
225 if (amplitude == 0) return startValue;
226 var noise = Vibration.Noise(in options, in context, 3);
227 return startValue + Vector3.Scale(endValue, new Vector3(noise.x, noise.y, noise.z)) * amplitude;
228 }
229 }
230 }