Skip to content

Commit

Permalink
Implement pixel shader for rendering 8-bit effect -like style.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Oct 23, 2021
1 parent 4fdc710 commit b0cc931
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Font.Tests.Helper;
using osu.Framework.Graphics.Shaders;
using osuTK;

namespace osu.Framework.Font.Tests.Visual.Shaders
{
Expand Down Expand Up @@ -72,5 +73,22 @@ public void TestRainbowShader(string uv, float speed, float saturation, float br
};
});
}

[TestCase(5, 5)]
[TestCase(5, 20)]
[TestCase(20, 20)]
public void TestPixelShader(float x, float y)
{
AddStep("Apply shader", () =>
{
ShaderContainer.Shaders = new[]
{
GetShaderByType<PixelShader>().With(s =>
{
s.Size = new Vector2(x, y);
})
};
});
}
}
}
1 change: 1 addition & 0 deletions osu.Framework.Font/Extensions/ShaderManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public static T LocalCustomizedShader<T>(this ShaderManager shaderManager) where
Type _ when type == typeof(OutlineShader) => new OutlineShader(shaderManager.Load(VertexShaderDescriptor.TEXTURE_2, OutlineShader.SHADER_NAME)) as T,
Type _ when type == typeof(RainbowShader) => new RainbowShader(shaderManager.Load(VertexShaderDescriptor.TEXTURE_2, RainbowShader.SHADER_NAME)) as T,
Type _ when type == typeof(ShadowShader) => new ShadowShader(shaderManager.Load(VertexShaderDescriptor.TEXTURE_2, ShadowShader.SHADER_NAME)) as T,
Type _ when type == typeof(PixelShader) => new PixelShader(shaderManager.Load(VertexShaderDescriptor.TEXTURE_2, PixelShader.SHADER_NAME)) as T,
_ => throw new NotImplementedException()
};
}
Expand Down
29 changes: 29 additions & 0 deletions osu.Framework.Font/Graphics/Shaders/PixelShader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using osu.Framework.Graphics.OpenGL.Buffers;
using osuTK;

namespace osu.Framework.Graphics.Shaders
{
public class PixelShader : CustomizedShader
{
public const string SHADER_NAME = "Pixel";

public Vector2 Size { get; set; } = new Vector2(5);

public PixelShader(IShader originShader)
: base(originShader)
{
}

public override void ApplyValue(FrameBuffer current)
{
var size = Size;
GetUniform<Vector2>(@"g_Size").UpdateValue(ref size);

var textureSize = current.Size;
GetUniform<Vector2>(@"g_TexSize").UpdateValue(ref textureSize);
}
}
}
18 changes: 18 additions & 0 deletions osu.Framework.Font/Resources/Shaders/sh_Pixel.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// see the demo: https://www.geeks3d.com/20101029/shader-library-pixelation-post-processing-effect-glsl/

varying mediump vec2 v_TexCoord;

uniform lowp sampler2D m_Sampler;

uniform mediump vec2 g_TexSize;
uniform mediump vec2 g_Size;

void main(void)
{
vec2 separaorParts = g_TexSize / g_Size;
vec2 uv = v_TexCoord;
uv = uv * separaorParts;
uv = floor(uv);
uv = uv / separaorParts;
gl_FragColor = texture2D(m_Sampler, uv);
}

0 comments on commit b0cc931

Please sign in to comment.