RestrictionSuggestor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace SicknessReduction.Visual.Vignetting
  6. {
  7. public abstract class RestrictionSuggestor
  8. {
  9. public abstract float Suggestion { get; }
  10. protected readonly float threshold;
  11. protected readonly float minRestriction;
  12. protected readonly float maxRestriction;
  13. protected readonly float maxValue;
  14. protected RestrictionSuggestor(float threshold, float minRestriction, float maxRestriction,
  15. float maxValue)
  16. {
  17. this.threshold = threshold;
  18. this.minRestriction = minRestriction;
  19. this.maxRestriction = maxRestriction;
  20. this.maxValue = maxValue;
  21. }
  22. protected float LerpedSuggestion(float value) => Mathf.Lerp(minRestriction, maxRestriction,
  23. (value - threshold) / (maxValue - threshold));
  24. }
  25. public class ValueBasedRestrictionSuggestor : RestrictionSuggestor
  26. {
  27. public override float Suggestion
  28. {
  29. get
  30. {
  31. var abs = Mathf.Abs(Value);
  32. return abs < threshold
  33. ? 0f
  34. : LerpedSuggestion(abs);
  35. }
  36. }
  37. public float Value { set; get; } = 0f;
  38. public ValueBasedRestrictionSuggestor(float threshold, float minRestriction, float maxRestriction,
  39. float maxValue) : base(threshold, minRestriction, maxRestriction, maxValue)
  40. {
  41. }
  42. }
  43. public abstract class QueueBasedRestrictionSuggestor<T> : RestrictionSuggestor
  44. {
  45. protected Queue<Tuple<float, T>> values;
  46. protected int bufferSize;
  47. public QueueBasedRestrictionSuggestor(float threshold, float minRestriction, float maxRestriction,
  48. float maxValue, int bufferSize = 10) : base(threshold, minRestriction, maxRestriction, maxValue)
  49. {
  50. this.bufferSize = bufferSize;
  51. values = new Queue<Tuple<float, T>>(bufferSize);
  52. }
  53. public void AddValue(T value)
  54. {
  55. if (values.Count >= bufferSize)
  56. {
  57. values.Dequeue();
  58. }
  59. values.Enqueue(new Tuple<float, T>(Time.time, value));
  60. }
  61. }
  62. public class QueueBasedRestrictionSuggestorFloat : QueueBasedRestrictionSuggestor<float>
  63. {
  64. public QueueBasedRestrictionSuggestorFloat(float threshold, float minRestriction, float maxRestriction,
  65. float maxValue, int bufferSize = 10) : base(threshold, minRestriction, maxRestriction, maxValue, bufferSize)
  66. {
  67. }
  68. public override float Suggestion
  69. {
  70. get
  71. {
  72. var perSecond = new float[values.Count];
  73. var index = 0;
  74. var previousValue = values.FirstOrDefault();
  75. foreach (var value in values)
  76. {
  77. // ReSharper disable once PossibleNullReferenceException
  78. var timeDif = value.Item1 - previousValue.Item1;
  79. perSecond[index] = timeDif == 0
  80. ? 0f
  81. : Mathf.Abs(value.Item2 - previousValue.Item2) /
  82. timeDif;
  83. previousValue = value;
  84. index++;
  85. }
  86. var avg = perSecond.Length == 0 ? 0f : perSecond.Average();
  87. return avg < threshold
  88. ? 0f
  89. : LerpedSuggestion(avg);
  90. }
  91. }
  92. }
  93. public class QueueBasedRestrictionSuggestorVector3 : QueueBasedRestrictionSuggestor<Vector3>
  94. {
  95. public QueueBasedRestrictionSuggestorVector3(float threshold, float minRestriction, float maxRestriction,
  96. float maxValue, int bufferSize = 10) : base(threshold, minRestriction, maxRestriction, maxValue, bufferSize)
  97. {
  98. }
  99. public override float Suggestion
  100. {
  101. get
  102. {
  103. var perSecond = new Vector3[values.Count];
  104. var index = 0;
  105. var previousValue = values.FirstOrDefault();
  106. foreach (var value in values)
  107. {
  108. // ReSharper disable once PossibleNullReferenceException
  109. var timeDif = value.Item1 - previousValue.Item1;
  110. perSecond[index] = timeDif == 0
  111. ? Vector3.zero
  112. : Helpers.Vector3Abs(value.Item2 - previousValue.Item2) /
  113. timeDif;
  114. previousValue = value;
  115. index++;
  116. }
  117. var avg = perSecond.Aggregate(Vector3.zero, (agg, v) => agg + v) / perSecond.Length;
  118. var max = Helpers.GetMaxComponent(avg);
  119. return max < threshold
  120. ? 0f
  121. : LerpedSuggestion(max);
  122. }
  123. }
  124. }
  125. }