From 612bb90a3d175bfa1029b8ff851884b52ddbd68f Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 16 Apr 2022 13:37:33 +0800 Subject: [PATCH 1/7] add default karaoke shader class. --- .../Shaders/DefaultKaraokeFontShader.cs | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 osu.Framework.Font/Graphics/Shaders/DefaultKaraokeFontShader.cs diff --git a/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeFontShader.cs b/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeFontShader.cs new file mode 100644 index 0000000..1713e71 --- /dev/null +++ b/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeFontShader.cs @@ -0,0 +1,35 @@ +// Copyright (c) karaoke.dev . 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; +using osuTK.Graphics; + +namespace osu.Framework.Graphics.Shaders +{ + public class DefaultKaraokeFontShader : InternalShader + { + public override string ShaderName => "DefaultKaraokeFont"; + + public Color4 Colour { get; set; } + + public int Radius { get; set; } + + public Color4 OutlineColour { get; set; } + + public override void ApplyValue(FrameBuffer current) + { + var radius = Radius; + GetUniform(@"g_Radius").UpdateValue(ref radius); + + var colourMatrix = new Vector4(Colour.R, Colour.G, Colour.B, Colour.A); + GetUniform(@"g_Colour").UpdateValue(ref colourMatrix); + + var outlineColourMatrix = new Vector4(OutlineColour.R, OutlineColour.G, OutlineColour.B, OutlineColour.A); + GetUniform(@"g_OutlineColour").UpdateValue(ref outlineColourMatrix); + + var size = current.Size; + GetUniform(@"g_TexSize").UpdateValue(ref size); + } + } +} From 3378af76a6977977f2ad621abcf835be08027801 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 16 Apr 2022 13:40:07 +0800 Subject: [PATCH 2/7] rename the class. --- ...ultKaraokeFontShader.cs => DefaultKaraokeLyricShader.cs} | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) rename osu.Framework.Font/Graphics/Shaders/{DefaultKaraokeFontShader.cs => DefaultKaraokeLyricShader.cs} (88%) diff --git a/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeFontShader.cs b/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs similarity index 88% rename from osu.Framework.Font/Graphics/Shaders/DefaultKaraokeFontShader.cs rename to osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs index 1713e71..9cce36d 100644 --- a/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeFontShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs @@ -7,7 +7,7 @@ namespace osu.Framework.Graphics.Shaders { - public class DefaultKaraokeFontShader : InternalShader + public class DefaultKaraokeLyricShader : InternalShader { public override string ShaderName => "DefaultKaraokeFont"; @@ -17,6 +17,10 @@ public class DefaultKaraokeFontShader : InternalShader public Color4 OutlineColour { get; set; } + public Vector2 ShaderOffset { get; set; } + + public int ShaderSize { get; set; } + public override void ApplyValue(FrameBuffer current) { var radius = Radius; From 7fb9d98d0e0ae81a615f57fdf3912ea7b7c2c419 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 16 Apr 2022 13:43:36 +0800 Subject: [PATCH 3/7] Add the test case to test the outline. --- .../TestSceneDefaultKaraokeLyricShader.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 osu.Framework.Font.Tests/Visual/Shaders/TestSceneDefaultKaraokeLyricShader.cs diff --git a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneDefaultKaraokeLyricShader.cs b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneDefaultKaraokeLyricShader.cs new file mode 100644 index 0000000..613fe55 --- /dev/null +++ b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneDefaultKaraokeLyricShader.cs @@ -0,0 +1,31 @@ +// Copyright (c) karaoke.dev . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using NUnit.Framework; +using osu.Framework.Extensions; +using osu.Framework.Extensions.Color4Extensions; +using osu.Framework.Graphics.Shaders; + +namespace osu.Framework.Font.Tests.Visual.Shaders +{ + public class TestSceneDefaultKaraokeLyricShader : TestSceneInternalShader + { + [TestCase("#FFFF00", 10, "#FFFF00")] + [TestCase("#FF0000", 20, "#FFFFFF")] + public void TestOutline(string colour, int radius, string outlineColour) + { + AddStep("Apply shader", () => + { + ShaderContainer.Shaders = new[] + { + GetShaderByType().With(s => + { + s.Colour = Color4Extensions.FromHex(colour); + s.Radius = radius; + s.OutlineColour = Color4Extensions.FromHex(outlineColour); + }) + }; + }); + } + } +} From 6eba92560364fa7b0ebbb59118b76181b52c84f1 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 16 Apr 2022 13:50:14 +0800 Subject: [PATCH 4/7] add the damn shader --- .../Shaders/DefaultKaraokeLyricShader.cs | 6 ++- .../Shaders/sh_DefaultKaraokeLyric.fs | 44 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs diff --git a/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs b/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs index 9cce36d..76700b1 100644 --- a/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs @@ -9,7 +9,7 @@ namespace osu.Framework.Graphics.Shaders { public class DefaultKaraokeLyricShader : InternalShader { - public override string ShaderName => "DefaultKaraokeFont"; + public override string ShaderName => "DefaultKaraokeLyric"; public Color4 Colour { get; set; } @@ -21,6 +21,10 @@ public class DefaultKaraokeLyricShader : InternalShader public int ShaderSize { get; set; } + public int ShaderSigma { get; set; } + + public Vector2 ShaderColour { get; set; } + public override void ApplyValue(FrameBuffer current) { var radius = Radius; diff --git a/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs b/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs new file mode 100644 index 0000000..2933753 --- /dev/null +++ b/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs @@ -0,0 +1,44 @@ +#include "sh_Utils.h" + +#define INV_SQRT_2PI 0.39894 + +varying mediump vec2 v_TexCoord; + +uniform lowp sampler2D m_Sampler; + +uniform mediump vec2 g_TexSize; +uniform int g_Radius; + +uniform mediump float g_Sigma; +uniform highp vec2 g_BlurDirection; + +mediump float computeGauss(in mediump float x, in mediump float sigma) +{ + return INV_SQRT_2PI * exp(-0.5*x*x / (sigma*sigma)) / sigma; +} + +lowp vec4 blur(sampler2D tex, int radius, highp vec2 direction, mediump vec2 texCoord, mediump vec2 texSize, mediump float sigma) +{ + mediump float factor = computeGauss(0.0, sigma); + mediump vec4 sum = texture2D(tex, texCoord) * factor; + + mediump float totalFactor = factor; + + for (int i = 2; i <= 200; i += 2) + { + mediump float x = float(i) - 0.5; + factor = computeGauss(x, sigma) * 2.0; + totalFactor += 2.0 * factor; + sum += texture2D(tex, texCoord + direction * x / texSize) * factor; + sum += texture2D(tex, texCoord - direction * x / texSize) * factor; + if (i >= radius) + break; + } + + return toSRGB(sum / totalFactor); +} + +void main(void) +{ + gl_FragColor = blur(m_Sampler, g_Radius, g_BlurDirection, v_TexCoord, g_TexSize, g_Sigma); +} \ No newline at end of file From 0cf4cc1bc1c9207afba06536f5c5cff092744e1c Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 16 Apr 2022 13:55:35 +0800 Subject: [PATCH 5/7] rename and adjust property. --- .../Shaders/DefaultKaraokeLyricShader.cs | 18 +++++++++++------- .../Shaders/sh_DefaultKaraokeLyric.fs | 5 +++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs b/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs index 76700b1..d1f0922 100644 --- a/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs @@ -17,25 +17,29 @@ public class DefaultKaraokeLyricShader : InternalShader public Color4 OutlineColour { get; set; } - public Vector2 ShaderOffset { get; set; } + public Vector2 ShadowOffset { get; set; } - public int ShaderSize { get; set; } + public int ShadowSize { get; set; } - public int ShaderSigma { get; set; } + public int ShadowSigma { get; set; } - public Vector2 ShaderColour { get; set; } + public Vector2 ShadowColour { get; set; } public override void ApplyValue(FrameBuffer current) { - var radius = Radius; - GetUniform(@"g_Radius").UpdateValue(ref radius); - + // outline effect var colourMatrix = new Vector4(Colour.R, Colour.G, Colour.B, Colour.A); GetUniform(@"g_Colour").UpdateValue(ref colourMatrix); + var radius = Radius; + GetUniform(@"g_Radius").UpdateValue(ref radius); + var outlineColourMatrix = new Vector4(OutlineColour.R, OutlineColour.G, OutlineColour.B, OutlineColour.A); GetUniform(@"g_OutlineColour").UpdateValue(ref outlineColourMatrix); + // shadow effect + + // common property. var size = current.Size; GetUniform(@"g_TexSize").UpdateValue(ref size); } diff --git a/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs b/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs index 2933753..d3ec773 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs @@ -7,8 +7,13 @@ varying mediump vec2 v_TexCoord; uniform lowp sampler2D m_Sampler; uniform mediump vec2 g_TexSize; + +// outline effect +uniform vec4 g_Colour; uniform int g_Radius; +uniform vec4 g_OutlineColour; +// shadow effect uniform mediump float g_Sigma; uniform highp vec2 g_BlurDirection; From e9d200c045a0f33fe63ee476a6cb09042a0a62ec Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 16 Apr 2022 14:27:50 +0800 Subject: [PATCH 6/7] runnable. --- .../Graphics/Shaders/DefaultKaraokeLyricShader.cs | 7 ++++++- .../Resources/Shaders/sh_DefaultKaraokeLyric.fs | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs b/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs index d1f0922..cb737d9 100644 --- a/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/DefaultKaraokeLyricShader.cs @@ -21,7 +21,7 @@ public class DefaultKaraokeLyricShader : InternalShader public int ShadowSize { get; set; } - public int ShadowSigma { get; set; } + public float ShadowSigma { get; set; } public Vector2 ShadowColour { get; set; } @@ -38,6 +38,11 @@ public override void ApplyValue(FrameBuffer current) GetUniform(@"g_OutlineColour").UpdateValue(ref outlineColourMatrix); // shadow effect + var shadowOffset = ShadowOffset; + GetUniform(@"g_BlurDirection").UpdateValue(ref shadowOffset); + + var shadowSigma = ShadowSigma; + GetUniform(@"g_Sigma").UpdateValue(ref shadowSigma); // common property. var size = current.Size; diff --git a/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs b/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs index d3ec773..de6652d 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs @@ -45,5 +45,8 @@ lowp vec4 blur(sampler2D tex, int radius, highp vec2 direction, mediump vec2 tex void main(void) { - gl_FragColor = blur(m_Sampler, g_Radius, g_BlurDirection, v_TexCoord, g_TexSize, g_Sigma); + mediump vec4 originTexture = texture2D(m_Sampler, v_TexCoord) * g_Colour; + lowp vec4 outline = blur(m_Sampler, g_Radius, g_BlurDirection, v_TexCoord, g_TexSize, g_Sigma) * g_OutlineColour; + lowp vec4 shadow = blur(m_Sampler, g_Radius, g_BlurDirection, v_TexCoord, g_TexSize, g_Sigma); + gl_FragColor = originTexture; } \ No newline at end of file From 25e529e1a649954db765167c3c00af0ccaf61ca9 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sat, 16 Apr 2022 16:09:09 +0800 Subject: [PATCH 7/7] [temp] got the idea why cannot make blur effect. This shit shader should run twice. --- .../TestSceneDefaultKaraokeLyricShader.cs | 9 +++++++-- .../Resources/Shaders/sh_DefaultKaraokeLyric.fs | 17 ++++++++++++----- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneDefaultKaraokeLyricShader.cs b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneDefaultKaraokeLyricShader.cs index 613fe55..0481740 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/TestSceneDefaultKaraokeLyricShader.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/TestSceneDefaultKaraokeLyricShader.cs @@ -5,13 +5,15 @@ using osu.Framework.Extensions; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Graphics.Shaders; +using osuTK; namespace osu.Framework.Font.Tests.Visual.Shaders { public class TestSceneDefaultKaraokeLyricShader : TestSceneInternalShader { - [TestCase("#FFFF00", 10, "#FFFF00")] - [TestCase("#FF0000", 20, "#FFFFFF")] + [TestCase("#FFFFFF", 10, "#0000FF")] // White text with blue outline + [TestCase("#FF0000", 5, "#FFFFFF")] // Red text with white outline + [TestCase("#FF0000", 0, "#FFFFFF")] // Red text with no outline public void TestOutline(string colour, int radius, string outlineColour) { AddStep("Apply shader", () => @@ -23,6 +25,9 @@ public void TestOutline(string colour, int radius, string outlineColour) s.Colour = Color4Extensions.FromHex(colour); s.Radius = radius; s.OutlineColour = Color4Extensions.FromHex(outlineColour); + + s.ShadowSigma = 200; + s.ShadowOffset = new Vector2(0, 1); }) }; }); diff --git a/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs b/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs index de6652d..2176b2e 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_DefaultKaraokeLyric.fs @@ -15,7 +15,6 @@ uniform vec4 g_OutlineColour; // shadow effect uniform mediump float g_Sigma; -uniform highp vec2 g_BlurDirection; mediump float computeGauss(in mediump float x, in mediump float sigma) { @@ -43,10 +42,18 @@ lowp vec4 blur(sampler2D tex, int radius, highp vec2 direction, mediump vec2 tex return toSRGB(sum / totalFactor); } +lowp vec4 outline() +{ + return blur(m_Sampler, g_Radius, highp vec2(1, 0), v_TexCoord, g_TexSize, g_Sigma) + + blur(m_Sampler, g_Radius, highp vec2(0, -1), v_TexCoord, g_TexSize, g_Sigma); +} + void main(void) { - mediump vec4 originTexture = texture2D(m_Sampler, v_TexCoord) * g_Colour; - lowp vec4 outline = blur(m_Sampler, g_Radius, g_BlurDirection, v_TexCoord, g_TexSize, g_Sigma) * g_OutlineColour; - lowp vec4 shadow = blur(m_Sampler, g_Radius, g_BlurDirection, v_TexCoord, g_TexSize, g_Sigma); - gl_FragColor = originTexture; + mediump vec4 originTexture = texture2D(m_Sampler, v_TexCoord).a * g_Colour; + lowp vec4 outline = outline().a * g_OutlineColour; + lowp vec4 shadow = blur(m_Sampler, g_Radius, highp vec2(0, 1), v_TexCoord, g_TexSize, g_Sigma); + + + gl_FragColor = blur(m_Sampler, g_Radius, highp vec2(0, -1), v_TexCoord, g_TexSize, g_Sigma); } \ No newline at end of file