diff --git a/Mirage.Shader/Mirage.hlsl b/Mirage.Shader/Mirage.hlsl index bbc046c..9d838b6 100644 --- a/Mirage.Shader/Mirage.hlsl +++ b/Mirage.Shader/Mirage.hlsl @@ -19,6 +19,10 @@ static const float HalfTexel = 0.5; static const float MinimumDepth = 1e-4; static const float MinimumShift = 1e-9; static const int MaximumTaps = 16; +static const float FillRadiusFraction = 0.006; +static const int FillHalfTaps = 4; +static const float FillTaps = 9.0; +static const float FillThreshold = 0.5; static const float MaximumSine = 0.1; float ArcSine(float value) @@ -37,6 +41,16 @@ float ReadShift(float elevation) return MapTexture.SampleLevel(MapSampler, mapUv, 0).x; } +float ReadFill(float elevation) +{ + float position = (elevation - mapStart) / mapSpan; + if (position < 0.0 || position > 1.0) + return 0.0; + + float2 mapUv = float2((position * (mapWidth - 1.0) + HalfTexel) / mapWidth, HalfTexel); + return MapTexture.SampleLevel(MapSampler, mapUv, 0).y; +} + float4 main( float4 position : SV_POSITION, float4 scenePosition : SCENE_POSITION, @@ -61,6 +75,7 @@ float4 main( float sinAzimuth = direction.x / horizontal; float cosAzimuth = worldForward / horizontal; float pixelAngle = 1.0 / focalLength; + float fillStep = FillRadiusFraction * (inputBounds.z - inputBounds.x) / FillHalfTaps; float lowShift = ReadShift(elevation - 0.5 * pixelAngle); float highShift = ReadShift(elevation + 0.5 * pixelAngle); @@ -85,8 +100,25 @@ float4 main( float seenForward = cosAzimuth * cosSeen; float3 seenDirection = float3(sinAzimuth * cosSeen, seenDown * pitchCosine + seenForward * pitchSine, -seenDown * pitchSine + seenForward * pitchCosine); float2 seenPosition = center + focalLength * seenDirection.xy / max(seenDirection.z, MinimumDepth); - float2 clamped = clamp(seenPosition, inputBounds.xy + HalfTexel, inputBounds.zw - HalfTexel); - sum += SourceTexture.SampleLevel(SourceSampler, uv0.xy + (clamped - scenePosition.xy) * uv0.zw, 0); + float2 lowerBound = inputBounds.xy + HalfTexel; + float2 upperBound = inputBounds.zw - HalfTexel; + if (ReadFill(sampled) > FillThreshold) + { + float4 blurred = float4(0.0, 0.0, 0.0, 0.0); + [unroll] + for (int offset = -FillHalfTaps; offset <= FillHalfTaps; offset++) + { + float2 shifted = clamp(seenPosition + float2(offset * fillStep, 0.0), lowerBound, upperBound); + blurred += SourceTexture.SampleLevel(SourceSampler, uv0.xy + (shifted - scenePosition.xy) * uv0.zw, 0); + } + + sum += blurred / FillTaps; + } + else + { + float2 clamped = clamp(seenPosition, lowerBound, upperBound); + sum += SourceTexture.SampleLevel(SourceSampler, uv0.xy + (clamped - scenePosition.xy) * uv0.zw, 0); + } } return lerp(source, sum / taps, amount); diff --git a/Mirage.Tests/MirageEffectProcessorTests.cs b/Mirage.Tests/MirageEffectProcessorTests.cs index 0bcffc4..3f0c5b7 100644 --- a/Mirage.Tests/MirageEffectProcessorTests.cs +++ b/Mirage.Tests/MirageEffectProcessorTests.cs @@ -54,6 +54,7 @@ public sealed class MirageEffectProcessorTests double Value(Animation animation) => animation.GetValue(frame, Length, EffectDescriptions.Fps); var eyeHeight = Math.Clamp(Value(effect.EyeHeight), MirageSettings.MinimumEyeHeightMeters, MirageSettings.MaximumEyeHeightMeters); float[] shifts; + float[]? fill = null; if (effect.Kind == MirageKind.Inferior) { shifts = MirageTable.BuildInferior(eyeHeight, Value(effect.HeatDifference), Value(effect.HeatThickness)); @@ -61,11 +62,12 @@ public sealed class MirageEffectProcessorTests else { var distance = Value(effect.Distance) * MirageSettings.MetersPerKilometer; - shifts = MirageTable.BuildSuperior(eyeHeight, Value(effect.InversionDifference), Value(effect.InversionHeight), Value(effect.InversionThickness), distance, effect.Source, MirageTable.BuildStandard(eyeHeight, distance)); + shifts = MirageTable.BuildSuperior(eyeHeight, Value(effect.InversionDifference), Value(effect.InversionHeight), Value(effect.InversionThickness), distance, effect.Source, MirageTable.BuildStandard(eyeHeight, distance), out var unseen); + fill = unseen; } var pitch = Value(effect.CameraPitch) * MirageSettings.RadiansPerDegree; - shader = new MirageShaderModel(width, height, source, MirageTable.ToTexels(shifts), MirageSettings.GetHalfFieldOfViewTangent(Value(effect.FieldOfView)), (float)Math.Cos(pitch), (float)Math.Sin(pitch), (float)(Value(effect.Amount) / 100.0)); + shader = new MirageShaderModel(width, height, source, MirageTable.ToTexels(shifts, fill), MirageSettings.GetHalfFieldOfViewTangent(Value(effect.FieldOfView)), (float)Math.Cos(pitch), (float)Math.Sin(pitch), (float)(Value(effect.Amount) / 100.0)); } public Bgra Expected(int x, int y) => shader.Shade(x, y); diff --git a/Mirage.Tests/MirageShaderModel.cs b/Mirage.Tests/MirageShaderModel.cs index 6de3c3c..59e1cc4 100644 --- a/Mirage.Tests/MirageShaderModel.cs +++ b/Mirage.Tests/MirageShaderModel.cs @@ -8,6 +8,9 @@ internal sealed class MirageShaderModel(int width, int height, Func 1f) @@ -34,11 +37,15 @@ internal sealed class MirageShaderModel(int width, int height, Func Read(elevation, 0); + + float ReadFill(float elevation) => Read(elevation, 1); + Vector4 Sample(Vector2 position) { var x = Math.Clamp(position.X - 0.5f, 0f, width - 1f); @@ -85,6 +92,9 @@ internal sealed class MirageShaderModel(int width, int height, Func FillThreshold) + { + var blurred = Vector4.Zero; + for (var offset = -FillHalfTaps; offset <= FillHalfTaps; offset++) + blurred += Sample(Vector2.Clamp(position + new Vector2(offset * fillStep, 0f), lowerBound, upperBound)); + + sum += blurred / FillTaps; + } + else + { + sum += Sample(Vector2.Clamp(position, lowerBound, upperBound)); + } } return ToBgra(Vector4.Lerp(original, sum / taps, amount)); diff --git a/Mirage/MirageEffectProcessor.cs b/Mirage/MirageEffectProcessor.cs index 668ae12..5880baf 100644 --- a/Mirage/MirageEffectProcessor.cs +++ b/Mirage/MirageEffectProcessor.cs @@ -88,7 +88,8 @@ internal sealed class MirageEffectProcessor(IGraphicsDevicesAndContext devices, _standardKey = standardKey; } - return MirageTable.ToTexels(MirageTable.BuildSuperior(key.EyeHeight, key.InversionDifference, key.InversionHeight, key.InversionThickness, distanceMeters, key.Source, _standard)); + var shifts = MirageTable.BuildSuperior(key.EyeHeight, key.InversionDifference, key.InversionHeight, key.InversionThickness, distanceMeters, key.Source, _standard, out var fill); + return MirageTable.ToTexels(shifts, fill); } private void WriteMap(float[] texels) diff --git a/Mirage/MirageSettings.cs b/Mirage/MirageSettings.cs index 1677974..850ab90 100644 --- a/Mirage/MirageSettings.cs +++ b/Mirage/MirageSettings.cs @@ -46,6 +46,8 @@ internal static class MirageSettings public const double MinimumSlope = 1e-4; public const int MaximumSteps = 2_000_000; public const int MaximumTaps = 16; + public const double FillRadiusFraction = 0.006; + public const int FillTaps = 9; public static readonly float MinimumHalfFieldTangent = GetHalfFieldOfViewTangent(MinimumFieldOfViewDegrees); public static readonly float MaximumHalfFieldTangent = GetHalfFieldOfViewTangent(MaximumFieldOfViewDegrees); diff --git a/Mirage/MirageTable.cs b/Mirage/MirageTable.cs index d34d622..543ba4d 100644 --- a/Mirage/MirageTable.cs +++ b/Mirage/MirageTable.cs @@ -16,9 +16,14 @@ internal static class MirageTable } public static float[] BuildSuperior(double eyeHeight, double difference, double height, double thickness, double distanceMeters, MirageSource source, double[] standard) + => BuildSuperior(eyeHeight, difference, height, thickness, distanceMeters, source, standard, out _); + + public static float[] BuildSuperior(double eyeHeight, double difference, double height, double thickness, double distanceMeters, MirageSource source, double[] standard, out float[] fill) { var atmosphere = new Atmosphere(AtmosphereLayer.Inversion, difference, height, thickness); var shifts = new float[MirageSettings.MapWidth]; + var unseen = new float[MirageSettings.MapWidth]; + var (gapLow, gapHigh) = HiddenCoordinates(standard); Parallel.For(0, MirageSettings.MapWidth, index => { var angle = MirageSettings.GetMapAngleRadians(index); @@ -26,10 +31,19 @@ internal static class MirageTable var coordinate = Coordinate(result, distanceMeters); var seen = source == MirageSource.Photo ? StandardElevation(standard, coordinate) : GeometricElevation(coordinate, eyeHeight, distanceMeters); shifts[index] = (float)(seen - angle); + if (source == MirageSource.Photo && coordinate > gapLow && coordinate < gapHigh) + unseen[index] = 1f; }); + fill = unseen; return shifts; } + public static (double Low, double High) HiddenCoordinates(double[] standard) + { + var first = Array.FindIndex(standard, coordinate => coordinate >= 0.0); + return first > 0 ? (standard[first - 1], standard[first]) : (double.NaN, double.NaN); + } + public static float[] BuildInferior(double eyeHeight, double difference, double thickness) { var heated = new Atmosphere(AtmosphereLayer.Heated, difference, 0.0, thickness); @@ -97,12 +111,14 @@ internal static class MirageTable return values[low] + fraction * (values[high] - values[low]); } - public static float[] ToTexels(float[] shifts) + public static float[] ToTexels(float[] shifts, float[]? fill = null) { var texels = new float[shifts.Length * MirageSettings.TexelChannels]; for (var index = 0; index < shifts.Length; index++) { texels[index * MirageSettings.TexelChannels] = shifts[index]; + if (fill is not null) + texels[index * MirageSettings.TexelChannels + 1] = fill[index]; texels[index * MirageSettings.TexelChannels + 3] = 1f; }