From 4fdc71049fefba159706fe46dda354f480b4acf0 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 23 Oct 2021 00:24:09 +0800 Subject: [PATCH 1/2] Add sprite text in shader test case for checking no error render in edge case. --- .../Visual/BackgroundGridTestSample.cs | 16 --------- .../Visual/Shaders/ShaderTestScene.cs | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/osu.Framework.Font.Tests/Visual/BackgroundGridTestSample.cs b/osu.Framework.Font.Tests/Visual/BackgroundGridTestSample.cs index a2a29c6..1ba489c 100644 --- a/osu.Framework.Font.Tests/Visual/BackgroundGridTestSample.cs +++ b/osu.Framework.Font.Tests/Visual/BackgroundGridTestSample.cs @@ -83,14 +83,6 @@ protected IShader GetShader(string shaderName) protected T GetShaderByType() where T : class, ICustomizedShader => shaderManager.LocalCustomizedShader(); - protected class DraggableBox : Box - { - protected override bool OnDragStart(DragStartEvent e) => true; - - protected override void OnDrag(DragEvent e) - => Position += e.Delta; - } - protected class DraggableCircle : Circle { protected override bool OnDragStart(DragStartEvent e) => true; @@ -98,13 +90,5 @@ protected class DraggableCircle : Circle protected override void OnDrag(DragEvent e) => Position += e.Delta; } - - protected class DraggableTriangle : Triangle - { - protected override bool OnDragStart(DragStartEvent e) => true; - - protected override void OnDrag(DragEvent e) - => Position += e.Delta; - } } } diff --git a/osu.Framework.Font.Tests/Visual/Shaders/ShaderTestScene.cs b/osu.Framework.Font.Tests/Visual/Shaders/ShaderTestScene.cs index 62f9c65..de299c7 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/ShaderTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/ShaderTestScene.cs @@ -7,6 +7,8 @@ using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Shaders; using osu.Framework.Graphics.Shapes; +using osu.Framework.Graphics.Sprites; +using osu.Framework.Input.Events; using osuTK; using osuTK.Graphics; @@ -48,6 +50,15 @@ protected ShaderTestScene() Origin = Anchor.Centre, Colour = Color4.Green, }, + new DraggableText + { + X = -100, + Y = 50, + Text = "CA", + Font = FontUsage.Default.With(size: 72), + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + }, createBorderBox("Left up cube", Anchor.TopLeft), createBorderBox("Right up cube", Anchor.TopRight), createBorderBox("Left down cube", Anchor.BottomLeft), @@ -66,6 +77,30 @@ static Box createBorderBox(string name, Anchor position) }; } + protected class DraggableBox : Box + { + protected override bool OnDragStart(DragStartEvent e) => true; + + protected override void OnDrag(DragEvent e) + => Position += e.Delta; + } + + protected class DraggableTriangle : Triangle + { + protected override bool OnDragStart(DragStartEvent e) => true; + + protected override void OnDrag(DragEvent e) + => Position += e.Delta; + } + + protected class DraggableText : SpriteText + { + protected override bool OnDragStart(DragStartEvent e) => true; + + protected override void OnDrag(DragEvent e) + => Position += e.Delta; + } + protected class TestShaderContainer : Container, IMultiShaderBufferedDrawable { public IShader TextureShader { get; private set; } From b0cc93125baf83f70dc15c38f839de02ae09dd9f Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 23 Oct 2021 23:13:31 +0800 Subject: [PATCH 2/2] Implement pixel shader for rendering 8-bit effect -like style. --- .../Shaders/CustomizedShaderTestScene.cs | 18 ++++++++++++ .../Extensions/ShaderManagerExtensions.cs | 1 + .../Graphics/Shaders/PixelShader.cs | 29 +++++++++++++++++++ .../Resources/Shaders/sh_Pixel.fs | 18 ++++++++++++ 4 files changed, 66 insertions(+) create mode 100644 osu.Framework.Font/Graphics/Shaders/PixelShader.cs create mode 100644 osu.Framework.Font/Resources/Shaders/sh_Pixel.fs 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