|
3 | 3 |
|
4 | 4 | namespace TNRD.Zeepkist.GTR.Ghosting.Ghosts; |
5 | 5 |
|
| 6 | +internal readonly struct GhostFrameSample |
| 7 | +{ |
| 8 | + internal GhostFrameSample(int currentIndex, int nextIndex, float interpolation) |
| 9 | + { |
| 10 | + CurrentIndex = currentIndex; |
| 11 | + NextIndex = nextIndex; |
| 12 | + Interpolation = interpolation; |
| 13 | + } |
| 14 | + |
| 15 | + internal int CurrentIndex { get; } |
| 16 | + internal int NextIndex { get; } |
| 17 | + internal float Interpolation { get; } |
| 18 | +} |
| 19 | + |
6 | 20 | internal static class GhostFrameSearch |
7 | 21 | { |
| 22 | + internal static bool TryGetFrameSample( |
| 23 | + int frameCount, |
| 24 | + float time, |
| 25 | + Func<int, float> getTimeAtIndex, |
| 26 | + out GhostFrameSample sample) |
| 27 | + { |
| 28 | + sample = default; |
| 29 | + if (frameCount <= 0) |
| 30 | + return false; |
| 31 | + |
| 32 | + if (frameCount == 1 || time < getTimeAtIndex(0)) |
| 33 | + { |
| 34 | + sample = new GhostFrameSample(0, 0, 0f); |
| 35 | + return true; |
| 36 | + } |
| 37 | + |
| 38 | + int currentIndex = FindLastFrameIndexAtOrBeforeTime(frameCount, time, getTimeAtIndex); |
| 39 | + if (currentIndex < 0) |
| 40 | + { |
| 41 | + sample = new GhostFrameSample(0, 0, 0f); |
| 42 | + return true; |
| 43 | + } |
| 44 | + |
| 45 | + if (currentIndex >= frameCount - 1) |
| 46 | + { |
| 47 | + int lastIndex = frameCount - 1; |
| 48 | + sample = new GhostFrameSample(lastIndex, lastIndex, 0f); |
| 49 | + return true; |
| 50 | + } |
| 51 | + |
| 52 | + int nextIndex = currentIndex + 1; |
| 53 | + float currentTime = getTimeAtIndex(currentIndex); |
| 54 | + float nextTime = getTimeAtIndex(nextIndex); |
| 55 | + float interpolation = nextTime > currentTime |
| 56 | + ? (time - currentTime) / (nextTime - currentTime) |
| 57 | + : 0f; |
| 58 | + if (interpolation < 0f) |
| 59 | + interpolation = 0f; |
| 60 | + else if (interpolation > 1f) |
| 61 | + interpolation = 1f; |
| 62 | + |
| 63 | + sample = new GhostFrameSample(currentIndex, nextIndex, interpolation); |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + internal static bool TryGetFrameSample( |
| 68 | + int frameCount, |
| 69 | + float time, |
| 70 | + int currentIndexHint, |
| 71 | + Func<int, float> getTimeAtIndex, |
| 72 | + out GhostFrameSample sample) |
| 73 | + { |
| 74 | + sample = default; |
| 75 | + if (frameCount <= 0) |
| 76 | + return false; |
| 77 | + |
| 78 | + if (currentIndexHint < 0 || currentIndexHint >= frameCount) |
| 79 | + return TryGetFrameSample(frameCount, time, getTimeAtIndex, out sample); |
| 80 | + |
| 81 | + float currentTime = getTimeAtIndex(currentIndexHint); |
| 82 | + if (time < currentTime) |
| 83 | + { |
| 84 | + if (currentIndexHint == 0) |
| 85 | + { |
| 86 | + sample = new GhostFrameSample(0, 0, 0f); |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + return TryGetFrameSample(frameCount, time, getTimeAtIndex, out sample); |
| 91 | + } |
| 92 | + |
| 93 | + int currentIndex = currentIndexHint; |
| 94 | + while (currentIndex < frameCount - 1) |
| 95 | + { |
| 96 | + int nextIndex = currentIndex + 1; |
| 97 | + float nextTime = getTimeAtIndex(nextIndex); |
| 98 | + if (nextTime > time) |
| 99 | + { |
| 100 | + float interpolation = nextTime > currentTime |
| 101 | + ? (time - currentTime) / (nextTime - currentTime) |
| 102 | + : 0f; |
| 103 | + if (interpolation < 0f) |
| 104 | + interpolation = 0f; |
| 105 | + else if (interpolation > 1f) |
| 106 | + interpolation = 1f; |
| 107 | + |
| 108 | + sample = new GhostFrameSample(currentIndex, nextIndex, interpolation); |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + currentIndex++; |
| 113 | + currentTime = nextTime; |
| 114 | + } |
| 115 | + |
| 116 | + sample = new GhostFrameSample(currentIndex, currentIndex, 0f); |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + private static int FindLastFrameIndexAtOrBeforeTime( |
| 121 | + int frameCount, |
| 122 | + float time, |
| 123 | + Func<int, float> getTimeAtIndex) |
| 124 | + { |
| 125 | + int low = 0; |
| 126 | + int high = frameCount; |
| 127 | + while (low < high) |
| 128 | + { |
| 129 | + int mid = (low + high) >> 1; |
| 130 | + if (getTimeAtIndex(mid) <= time) |
| 131 | + low = mid + 1; |
| 132 | + else |
| 133 | + high = mid; |
| 134 | + } |
| 135 | + |
| 136 | + return low - 1; |
| 137 | + } |
| 138 | + |
8 | 139 | internal static int FindFirstFrameIndexAtOrAfterTime( |
9 | 140 | int frameCount, |
10 | 141 | float time, |
@@ -41,7 +172,7 @@ internal static bool TryGetAdjacentFrameTime( |
41 | 172 | out float adjacentTime) |
42 | 173 | { |
43 | 174 | adjacentTime = 0f; |
44 | | - if (frameCount <= 0 || direction is not (1 or -1)) |
| 175 | + if (frameCount <= 1 || direction is not (1 or -1)) |
45 | 176 | return false; |
46 | 177 |
|
47 | 178 | if (direction > 0) |
|
0 commit comments