Skip to content

Commit 378c7f3

Browse files
author
dima.am
committed
Optional anisotropic filtering added to Image component
1 parent 5c7bc37 commit 378c7f3

File tree

2 files changed

+64
-11
lines changed

2 files changed

+64
-11
lines changed

src/Myra/Graphics2D/RenderContext.cs

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,31 @@ namespace Myra.Graphics2D
2525
public enum TextureFiltering
2626
{
2727
Nearest,
28-
Linear
29-
}
28+
Linear,
29+
Anisotropic
30+
}
3031

3132
public partial class RenderContext : IDisposable
3233
{
3334
#if MONOGAME || FNA
3435
private static RasterizerState _uiRasterizerState;
35-
36-
private static RasterizerState UIRasterizerState
36+
private static SamplerState _textureFilteringAnisotropic = new SamplerState
37+
{
38+
Filter = TextureFilter.Anisotropic,
39+
AddressU = TextureAddressMode.Clamp,
40+
AddressV = TextureAddressMode.Clamp,
41+
AddressW = TextureAddressMode.Clamp,
42+
BorderColor = Color.Transparent,
43+
MaxAnisotropy = 16,
44+
MaxMipLevel = 16,
45+
MipMapLevelOfDetailBias = 0f,
46+
ComparisonFunction = CompareFunction.Never,
47+
FilterMode = TextureFilterMode.Default
48+
};
49+
50+
private bool IsAnisotropicFilteringOn = false;
51+
52+
private static RasterizerState UIRasterizerState
3753
{
3854
get
3955
{
@@ -255,7 +271,7 @@ public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? s
255271
/// <param name="depth"></param>
256272
public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 scale, float depth = 0.0f)
257273
{
258-
SetTextureFiltering(TextureFiltering.Nearest);
274+
SetTextureFiltering(IsAnisotropicFilteringOn ? TextureFiltering.Anisotropic : TextureFiltering.Nearest);
259275
color = CrossEngineStuff.MultiplyColor(color, Opacity);
260276
scale *= Transform.Scale;
261277
rotation += Transform.Rotation;
@@ -430,7 +446,7 @@ public void DrawRichText(RichTextLayout richText, Vector2 position, Color color,
430446
public void Begin()
431447
{
432448
#if MONOGAME || FNA
433-
var samplerState = _textureFiltering == TextureFiltering.Nearest ? SamplerState.PointClamp : SamplerState.LinearClamp;
449+
var samplerState = SelectedSamplerState();
434450

435451
_renderer.Begin(SpriteSortMode.Deferred,
436452
BlendState.AlphaBlend,
@@ -455,7 +471,22 @@ public void Begin()
455471
_beginCalled = true;
456472
}
457473

458-
public void End()
474+
private SamplerState SelectedSamplerState()
475+
{
476+
switch (_textureFiltering)
477+
{
478+
case TextureFiltering.Nearest:
479+
return SamplerState.PointClamp;
480+
case TextureFiltering.Linear:
481+
return SamplerState.LinearClamp;
482+
case TextureFiltering.Anisotropic:
483+
return _textureFilteringAnisotropic;
484+
default:
485+
throw new ArgumentOutOfRangeException();
486+
}
487+
}
488+
489+
public void End()
459490
{
460491
_renderer.End();
461492
_beginCalled = false;
@@ -489,5 +520,10 @@ public void Dispose()
489520
{
490521
ReleaseUnmanagedResources();
491522
}
492-
}
523+
524+
public void SetAnisotropicFilteringMode(bool isAnisotropicFiltering)
525+
{
526+
IsAnisotropicFilteringOn = isAnisotropicFiltering;
527+
}
528+
}
493529
}

src/Myra/Graphics2D/UI/Simple/Image.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,22 @@ public class Image : Widget, IPressable
3535
{
3636
private IImage _image, _overImage, _pressedImage;
3737

38-
[Category("Appearance")]
38+
private bool _isAnisotropicFiltering = false;
39+
40+
public bool IsAnisotropicFiltering
41+
{
42+
get
43+
{
44+
return _isAnisotropicFiltering;
45+
}
46+
set
47+
{
48+
_isAnisotropicFiltering = value;
49+
InvalidateMeasure();
50+
}
51+
}
52+
53+
[Category("Appearance")]
3954
public IImage Renderable
4055
{
4156
get
@@ -164,8 +179,10 @@ public override void InternalRender(RenderContext context)
164179
bounds.Height = (int)(bounds.Width * aspect);
165180
}
166181

167-
image.Draw(context, bounds, Color);
168-
}
182+
context.SetAnisotropicFilteringMode(_isAnisotropicFiltering);
183+
image.Draw(context, bounds, Color);
184+
context.SetAnisotropicFilteringMode(false);
185+
}
169186
}
170187

171188
public void ApplyPressableImageStyle(PressableImageStyle imageStyle)

0 commit comments

Comments
 (0)