|
@@ -5,18 +5,81 @@ using UnityEngine;
|
|
|
|
|
|
namespace SicknessReduction.Visual.Vignetting
|
|
|
{
|
|
|
- public interface RestrictionSuggestor
|
|
|
+ public abstract class RestrictionSuggestor
|
|
|
{
|
|
|
- float Suggestion { get; }
|
|
|
+ public abstract float Suggestion { get; }
|
|
|
+
|
|
|
+ protected readonly float threshold;
|
|
|
+ protected readonly float minRestriction;
|
|
|
+ protected readonly float maxRestriction;
|
|
|
+ protected readonly float maxValue;
|
|
|
+
|
|
|
+ protected RestrictionSuggestor(float threshold, float minRestriction, float maxRestriction,
|
|
|
+ float maxValue)
|
|
|
+ {
|
|
|
+ this.threshold = threshold;
|
|
|
+ this.minRestriction = minRestriction;
|
|
|
+ this.maxRestriction = maxRestriction;
|
|
|
+ this.maxValue = maxValue;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected float LerpedSuggestion(float value) => Mathf.Lerp(minRestriction, maxRestriction,
|
|
|
+ (value - threshold) / (maxValue - threshold));
|
|
|
+ }
|
|
|
+
|
|
|
+ public class ValueBasedRestrictionSuggestor : RestrictionSuggestor
|
|
|
+ {
|
|
|
+ public override float Suggestion
|
|
|
+ {
|
|
|
+ get
|
|
|
+ {
|
|
|
+ var abs = Mathf.Abs(Value);
|
|
|
+ return abs < threshold
|
|
|
+ ? 0f
|
|
|
+ : LerpedSuggestion(abs);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public float Value { set; get; } = 0f;
|
|
|
+
|
|
|
+
|
|
|
+ public ValueBasedRestrictionSuggestor(float threshold, float minRestriction, float maxRestriction,
|
|
|
+ float maxValue) : base(threshold, minRestriction, maxRestriction, maxValue)
|
|
|
+ {
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- public class QueueBasedRestrictionSuggestor : RestrictionSuggestor
|
|
|
+ public abstract class QueueBasedRestrictionSuggestor<T> : RestrictionSuggestor
|
|
|
{
|
|
|
- private Queue<Tuple<float, float>> values;
|
|
|
- private int bufferSize;
|
|
|
- private float threshold;
|
|
|
+ protected Queue<Tuple<float, T>> values;
|
|
|
+ protected int bufferSize;
|
|
|
+
|
|
|
+ public QueueBasedRestrictionSuggestor(float threshold, float minRestriction, float maxRestriction,
|
|
|
+ float maxValue, int bufferSize = 10) : base(threshold, minRestriction, maxRestriction, maxValue)
|
|
|
+ {
|
|
|
+ this.bufferSize = bufferSize;
|
|
|
+ values = new Queue<Tuple<float, T>>(bufferSize);
|
|
|
+ }
|
|
|
|
|
|
- public float Suggestion
|
|
|
+ public void AddValue(T value)
|
|
|
+ {
|
|
|
+ if (values.Count >= bufferSize)
|
|
|
+ {
|
|
|
+ values.Dequeue();
|
|
|
+ }
|
|
|
+
|
|
|
+ values.Enqueue(new Tuple<float, T>(Time.time, value));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public class QueueBasedRestrictionSuggestorFloat : QueueBasedRestrictionSuggestor<float>
|
|
|
+ {
|
|
|
+ public QueueBasedRestrictionSuggestorFloat(float threshold, float minRestriction, float maxRestriction,
|
|
|
+ float maxValue, int bufferSize = 10) : base(threshold, minRestriction, maxRestriction, maxValue, bufferSize)
|
|
|
+ {
|
|
|
+ }
|
|
|
+
|
|
|
+ public override float Suggestion
|
|
|
{
|
|
|
get
|
|
|
{
|
|
@@ -26,36 +89,55 @@ namespace SicknessReduction.Visual.Vignetting
|
|
|
foreach (var value in values)
|
|
|
{
|
|
|
|
|
|
- perSecond[index] = Mathf.Abs(value.Item2 - previousValue.Item2) /
|
|
|
- (value.Item1 - previousValue.Item1);
|
|
|
+ var timeDif = value.Item1 - previousValue.Item1;
|
|
|
+ perSecond[index] = timeDif == 0
|
|
|
+ ? 0f
|
|
|
+ : Mathf.Abs(value.Item2 - previousValue.Item2) /
|
|
|
+ timeDif;
|
|
|
+ previousValue = value;
|
|
|
index++;
|
|
|
}
|
|
|
|
|
|
- var avg = perSecond.Average();
|
|
|
- if (avg >= threshold)
|
|
|
- {
|
|
|
- return .5f;
|
|
|
- }
|
|
|
-
|
|
|
- return 0f;
|
|
|
+ var avg = perSecond.Length == 0 ? 0f : perSecond.Average();
|
|
|
+ return avg < threshold
|
|
|
+ ? 0f
|
|
|
+ : LerpedSuggestion(avg);
|
|
|
}
|
|
|
}
|
|
|
+ }
|
|
|
|
|
|
- public QueueBasedRestrictionSuggestor(float threshold, int bufferSize = 10)
|
|
|
+ public class QueueBasedRestrictionSuggestorVector3 : QueueBasedRestrictionSuggestor<Vector3>
|
|
|
+ {
|
|
|
+ public QueueBasedRestrictionSuggestorVector3(float threshold, float minRestriction, float maxRestriction,
|
|
|
+ float maxValue, int bufferSize = 10) : base(threshold, minRestriction, maxRestriction, maxValue, bufferSize)
|
|
|
{
|
|
|
- this.bufferSize = bufferSize;
|
|
|
- this.threshold = threshold;
|
|
|
- values = new Queue<Tuple<float, float>>(bufferSize);
|
|
|
}
|
|
|
|
|
|
- public void AddValue(float value)
|
|
|
+ public override float Suggestion
|
|
|
{
|
|
|
- if (values.Count >= bufferSize)
|
|
|
+ get
|
|
|
{
|
|
|
- values.Dequeue();
|
|
|
- }
|
|
|
+ var perSecond = new Vector3[values.Count];
|
|
|
+ var index = 0;
|
|
|
+ var previousValue = values.FirstOrDefault();
|
|
|
+ foreach (var value in values)
|
|
|
+ {
|
|
|
+
|
|
|
+ var timeDif = value.Item1 - previousValue.Item1;
|
|
|
+ perSecond[index] = timeDif == 0
|
|
|
+ ? Vector3.zero
|
|
|
+ : Helpers.Vector3Abs(value.Item2 - previousValue.Item2) /
|
|
|
+ timeDif;
|
|
|
+ previousValue = value;
|
|
|
+ index++;
|
|
|
+ }
|
|
|
|
|
|
- values.Enqueue(new Tuple<float, float>(Time.time, value));
|
|
|
+ var avg = perSecond.Aggregate(Vector3.zero, (agg, v) => agg + v) / perSecond.Length;
|
|
|
+ var max = Helpers.GetMaxComponent(avg);
|
|
|
+ return max < threshold
|
|
|
+ ? 0f
|
|
|
+ : LerpedSuggestion(max);
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|