Skip to content

Commit

Permalink
Implement optimized pixel-art antialiasing mode for non-integer world…
Browse files Browse the repository at this point in the history
… zoom.
  • Loading branch information
pchote authored and tovl committed Dec 13, 2019
1 parent cd368b4 commit 860117d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
5 changes: 5 additions & 0 deletions OpenRA.Game/Graphics/SpriteRenderer.cs
Expand Up @@ -172,5 +172,10 @@ public void SetDepthPreviewEnabled(bool enabled)
{
shader.SetBool("EnableDepthPreview", enabled);
}

public void SetAntialiasingPixelsPerTexel(float pxPerTx)
{
shader.SetVec("AntialiasPixelsPerTexel", pxPerTx);
}
}
}
6 changes: 6 additions & 0 deletions OpenRA.Game/Renderer.cs
Expand Up @@ -181,6 +181,9 @@ public void BeginWorld(Rectangle worldViewport)

// Render the world into a framebuffer at 1:1 scaling to allow the depth buffer to match the artwork at all zoom levels
worldBuffer = Context.CreateFrameBuffer(worldBufferSize);

// Pixel art scaling mode is a customized bilinear sampling
worldBuffer.Texture.ScaleFilter = TextureScaleFilter.Linear;
}

if (worldSprite == null || worldViewport.Size != worldSprite.Bounds.Size)
Expand Down Expand Up @@ -217,8 +220,11 @@ public void BeginUI()

var scale = Window.WindowScale;
var bufferSize = new Size((int)(screenSprite.Bounds.Width / scale), (int)(-screenSprite.Bounds.Height / scale));

SpriteRenderer.SetAntialiasingPixelsPerTexel(Window.SurfaceSize.Height * 1f / worldSprite.Bounds.Height);
RgbaSpriteRenderer.DrawSprite(worldSprite, float3.Zero, new float2(bufferSize));
Flush();
SpriteRenderer.SetAntialiasingPixelsPerTexel(0);
}
else
{
Expand Down
37 changes: 36 additions & 1 deletion glsl/combined.frag
Expand Up @@ -16,6 +16,7 @@ uniform sampler2D Palette;

uniform bool EnableDepthPreview;
uniform float DepthTextureScale;
uniform float AntialiasPixelsPerTexel;

in vec4 vTexCoord;
in vec2 vTexMetadata;
Expand Down Expand Up @@ -44,6 +45,24 @@ float jet_b(float x)
return x < 0.3 ? 4.0 * x + 0.5 : -4.0 * x + 2.5;
}

ivec2 Size(float samplerIndex)
{
if (samplerIndex < 0.5)
return textureSize(Texture0, 0);
else if (samplerIndex < 1.5)
return textureSize(Texture1, 0);
else if (samplerIndex < 2.5)
return textureSize(Texture2, 0);
else if (samplerIndex < 3.5)
return textureSize(Texture3, 0);
else if (samplerIndex < 4.5)
return textureSize(Texture4, 0);
else if (samplerIndex < 5.5)
return textureSize(Texture5, 0);

return textureSize(Texture6, 0);
}

vec4 Sample(float samplerIndex, vec2 pos)
{
if (samplerIndex < 0.5)
Expand All @@ -64,7 +83,23 @@ vec4 Sample(float samplerIndex, vec2 pos)

void main()
{
vec4 x = Sample(vTexSampler.s, vTexCoord.st);
vec2 coords = vTexCoord.st;

if (AntialiasPixelsPerTexel > 0)
{
vec2 textureSize = Size(vTexSampler.s);
vec2 offset = fract(coords.st * textureSize);

// Offset the sampling point to simulate bilinear intepolation in window coordinates instead of texture coordinates
// https://csantosbh.wordpress.com/2014/01/25/manual-texture-filtering-for-pixelated-games-in-webgl/
// https://csantosbh.wordpress.com/2014/02/05/automatically-detecting-the-texture-filter-threshold-for-pixelated-magnifications/
// ik is defined as 1/k from the articles, set to 1/0.7 because it looks good
float ik = 1.43;
vec2 interp = clamp(offset * ik * AntialiasPixelsPerTexel, 0.0, .5) + clamp((offset - 1.0) * ik * AntialiasPixelsPerTexel + .5, 0.0, .5);
coords = (floor(coords.st * textureSize) + interp) / textureSize;
}

vec4 x = Sample(vTexSampler.s, coords);
vec2 p = vec2(dot(x, vChannelMask), vTexMetadata.s);
vec4 c = vPalettedFraction * texture(Palette, p) + vRGBAFraction * x + vColorFraction * vTexCoord;

Expand Down

0 comments on commit 860117d

Please sign in to comment.