Skip to content

Commit

Permalink
Merge pull request #69 from andy840119/pixel-shader
Browse files Browse the repository at this point in the history
Implement pixel shader
  • Loading branch information
andy840119 authored Oct 23, 2021
2 parents d78ce91 + b0cc931 commit 17f5950
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 16 deletions.
16 changes: 0 additions & 16 deletions osu.Framework.Font.Tests/Visual/BackgroundGridTestSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,28 +83,12 @@ protected IShader GetShader(string shaderName)
protected T GetShaderByType<T>() where T : class, ICustomizedShader
=> shaderManager.LocalCustomizedShader<T>();

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;

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;
}
}
}
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);
})
};
});
}
}
}
35 changes: 35 additions & 0 deletions osu.Framework.Font.Tests/Visual/Shaders/ShaderTestScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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),
Expand All @@ -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; }
Expand Down
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 17f5950

Please sign in to comment.