diff --git a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs index 66bfb0d..92d18bd 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs @@ -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 { @@ -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().With(s => + { + s.Size = new Vector2(x, y); + }) + }; + }); + } } } diff --git a/osu.Framework.Font/Extensions/ShaderManagerExtensions.cs b/osu.Framework.Font/Extensions/ShaderManagerExtensions.cs index ed33ed8..b6377f8 100644 --- a/osu.Framework.Font/Extensions/ShaderManagerExtensions.cs +++ b/osu.Framework.Font/Extensions/ShaderManagerExtensions.cs @@ -17,6 +17,7 @@ public static T LocalCustomizedShader(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() }; } diff --git a/osu.Framework.Font/Graphics/Shaders/PixelShader.cs b/osu.Framework.Font/Graphics/Shaders/PixelShader.cs new file mode 100644 index 0000000..f82a0d3 --- /dev/null +++ b/osu.Framework.Font/Graphics/Shaders/PixelShader.cs @@ -0,0 +1,29 @@ +// Copyright (c) andy840119 . 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(@"g_Size").UpdateValue(ref size); + + var textureSize = current.Size; + GetUniform(@"g_TexSize").UpdateValue(ref textureSize); + } + } +} diff --git a/osu.Framework.Font/Resources/Shaders/sh_Pixel.fs b/osu.Framework.Font/Resources/Shaders/sh_Pixel.fs new file mode 100644 index 0000000..2b24ccd --- /dev/null +++ b/osu.Framework.Font/Resources/Shaders/sh_Pixel.fs @@ -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); +} \ No newline at end of file