From 30f93a94f3661ce15d75d37863d13d968ac25b5b Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 24 Oct 2021 15:12:03 +0800 Subject: [PATCH 1/6] Implement workng deml. --- .../Resources/Textures/sample-texture.png | Bin 0 -> 7772 bytes .../Shaders/CustomizedShaderTestScene.cs | 23 ++++++++++++ .../Shaders/RepeatMovingBackgroundShader.cs | 35 ++++++++++++++++++ .../Shaders/sh_RepeatMovingBackground.fs | 11 ++++++ 4 files changed, 69 insertions(+) create mode 100644 osu.Framework.Font.Tests/Resources/Textures/sample-texture.png create mode 100644 osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs create mode 100644 osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs diff --git a/osu.Framework.Font.Tests/Resources/Textures/sample-texture.png b/osu.Framework.Font.Tests/Resources/Textures/sample-texture.png new file mode 100644 index 0000000000000000000000000000000000000000..46daf2534791d8bfcb0b44ec245bd41a93f80e16 GIT binary patch literal 7772 zcmcIpc|276-#=s0D9eZvX+#uVdl>7WkR&^8WT_9|C~;r?`L1W=JP)H4fVBIcOTdd0067bX-#7Q zU;tkk04C@kzXKY10Kg}xqj|#A8#dSf&A`NtyMeq=Bd&{=OSnB#I%R)cetZe6}v8$AL)rPLrcW9OfEHF4)hJ_$<4dC z%FT&yIQ($ZdNXtA@CM5-dc&^yEt9v(RhBs-tnXsEkT3TdlT%6#`mPbj2}1F|w(AAn zoqeHk8Pbxy*VN{&3>Vq)qhKmC7ZQ8(%Xy3Lw^u5MuPx5*o6#@a8v|+5*a+bjOpv{( z(q+l4g`vs1crX+RAAd{09-E#cup4U>@@YW;Vk*_ zI_vr_&s*zk#Z$~k91hpueuVIwoHt?{4^_wL%7i<6;q zBL%T>zk3&{CO$yTaJ zUQ$$3(`Gg8jIp^Er+Quf(bZS1c$RAdb%F_-Qdb3cXk3$7w{7@($dxXFyRvsHW23IJ zKwyS2Iu*AV(vp#=m1J<6QFTiPw>l<12ANkUKc>yY2U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>Oy%AtQW zWYxy$Z{S3at2K-YoMofIUwn4GQ(b=f%g|-ZH$u+kLzcuv>*#MzpNuU%pwBo6>(*%n zVMb?hlbo74CNDr3@#JVlh1dmlJlT1caX!nZsP5%t2=wLX%`_~F6W7EMX_6G{@uU4$VYw zR9!KAb#DXSTja#uF;mW~QRz9q8CVZ~qq0cGzV`tRZQ1VCGI~5rl>Hu$fKivguf~9m MmcC~GN$cSM0%cP71ONa4 literal 0 HcmV?d00001 diff --git a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs index 92d18bd..99b07c4 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs @@ -2,16 +2,21 @@ // See the LICENCE file in the repository root for full licence text. using NUnit.Framework; +using osu.Framework.Allocation; using osu.Framework.Extensions; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Font.Tests.Helper; using osu.Framework.Graphics.Shaders; +using osu.Framework.Graphics.Textures; using osuTK; namespace osu.Framework.Font.Tests.Visual.Shaders { public class CustomizedShaderTestScene : ShaderTestScene { + [Resolved] + private TextureStore textures { get; set; } + [TestCase(0, "#FF0000")] [TestCase(10, "#00FF00")] [TestCase(20, "#0000FF")] @@ -90,5 +95,23 @@ public void TestPixelShader(float x, float y) }; }); } + + [TestCase("sample-texture", 5, 5)] + [TestCase("sample-texture", 5, 20)] + [TestCase("sample-texture", 20, 20)] + public void TestRepeatMovingBackgroundShader(string textureName, float x, float y) + { + AddStep("Apply shader", () => + { + ShaderContainer.Shaders = new[] + { + GetShaderByType().With(s => + { + s.Texture = textures.Get(textureName); + s.Repeat = new Vector2(x, y); + }) + }; + }); + } } } diff --git a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs new file mode 100644 index 0000000..957a33c --- /dev/null +++ b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs @@ -0,0 +1,35 @@ +// 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 osu.Framework.Graphics.Textures; +using osuTK; +using osuTK.Graphics.ES30; + +namespace osu.Framework.Graphics.Shaders +{ + public class RepeatMovingBackgroundShader : InternalShader + { + public override string ShaderName => "RepeatMovingBackground"; + + public Texture Texture { get; set; } + + public Vector2 Repeat { get; set; } = new Vector2(3); + + public RepeatMovingBackgroundShader(IShader originShader) + : base(originShader) + { + } + + public override void ApplyValue(FrameBuffer current) + { + Texture?.TextureGL.Bind(TextureUnit.Texture3); + + var unitId = TextureUnit.Texture3 - TextureUnit.Texture0; + GetUniform(@"g_RepeatSample").UpdateValue(ref unitId); + + var repeat = Repeat; + GetUniform("g_Repeat").UpdateValue(ref repeat); + } + } +} diff --git a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs new file mode 100644 index 0000000..18c5f29 --- /dev/null +++ b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs @@ -0,0 +1,11 @@ +// see : https://github.com/kiwipxl/GLSL-shaders/blob/master/repeat.glsl + +varying vec2 v_TexCoord; +varying vec4 v_Colour; + +uniform lowp sampler2D g_RepeatSample; +uniform vec2 g_Repeat; + +void main(void) { + gl_FragColor = v_Colour * texture2D(g_RepeatSample, vec2(mod(v_TexCoord.x * g_Repeat.x, 1), mod(v_TexCoord.y * g_Repeat.y, 1))); +} From 7d1ace085ce953a9ca6a03d45e4c13a109d22d2c Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 24 Oct 2021 22:08:04 +0800 Subject: [PATCH 2/6] Now works with customized shader. --- .../Shaders/CustomizedShaderTestScene.cs | 6 ++--- .../Shaders/RepeatMovingBackgroundShader.cs | 25 +++++++++++++++---- .../Shaders/sh_RepeatMovingBackground.fs | 18 +++++++++++-- 3 files changed, 39 insertions(+), 10 deletions(-) diff --git a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs index 99b07c4..bf9d42b 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs @@ -96,10 +96,10 @@ public void TestPixelShader(float x, float y) }); } - [TestCase("sample-texture", 5, 5)] + [TestCase("sample-texture", 1f, 1f)] [TestCase("sample-texture", 5, 20)] [TestCase("sample-texture", 20, 20)] - public void TestRepeatMovingBackgroundShader(string textureName, float x, float y) + public void TestRepeatMovingBackgroundShader(string textureName, float width, float height) { AddStep("Apply shader", () => { @@ -108,7 +108,7 @@ public void TestRepeatMovingBackgroundShader(string textureName, float x, float GetShaderByType().With(s => { s.Texture = textures.Get(textureName); - s.Repeat = new Vector2(x, y); + s.TextureSize = new Vector2(width, height); }) }; }); diff --git a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs index 957a33c..5e95414 100644 --- a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs @@ -14,7 +14,9 @@ public class RepeatMovingBackgroundShader : InternalShader public Texture Texture { get; set; } - public Vector2 Repeat { get; set; } = new Vector2(3); + public Vector2 TextureSize { get; set; } = new Vector2(10); + + public Vector2 Border { get; set; } public RepeatMovingBackgroundShader(IShader originShader) : base(originShader) @@ -23,13 +25,26 @@ public RepeatMovingBackgroundShader(IShader originShader) public override void ApplyValue(FrameBuffer current) { - Texture?.TextureGL.Bind(TextureUnit.Texture3); + if (Texture == null) + return; + + Texture.TextureGL.Bind(TextureUnit.Texture1); - var unitId = TextureUnit.Texture3 - TextureUnit.Texture0; + var unitId = TextureUnit.Texture1 - TextureUnit.Texture0; GetUniform(@"g_RepeatSample").UpdateValue(ref unitId); - var repeat = Repeat; - GetUniform("g_Repeat").UpdateValue(ref repeat); + var coord = Texture.GetTextureRect().TopLeft; + GetUniform(@"g_RepeatSampleCoord").UpdateValue(ref coord); + + var size = Texture.GetTextureRect().Size; + + GetUniform(@"g_RepeatSampleSize").UpdateValue(ref size); + + var textureSize = TextureSize; + GetUniform("g_TextureSize").UpdateValue(ref textureSize); + + //var border = Border; + //GetUniform("g_Border").UpdateValue(ref border); } } } diff --git a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs index 18c5f29..f13622a 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs @@ -3,9 +3,23 @@ varying vec2 v_TexCoord; varying vec4 v_Colour; +uniform lowp sampler2D m_Sampler; + uniform lowp sampler2D g_RepeatSample; -uniform vec2 g_Repeat; +uniform vec2 g_RepeatSampleCoord; +uniform vec2 g_RepeatSampleSize; +uniform vec2 g_TextureSize; +uniform vec2 g_Offset; void main(void) { - gl_FragColor = v_Colour * texture2D(g_RepeatSample, vec2(mod(v_TexCoord.x * g_Repeat.x, 1), mod(v_TexCoord.y * g_Repeat.y, 1))); + // get the repeat position + float repeatTexCoordX = mod(v_TexCoord.x * g_TextureSize.x + g_Offset.x, 1); + float repeatTexCoordY = mod(v_TexCoord.y * g_TextureSize.y + g_Offset.y, 1); + vec2 repeatTexCoord = vec2(repeatTexCoordX, repeatTexCoordY); + + // because repeat texture will be the size of 1024*1024, so should make a conversion to get the target area of the texture. + vec2 fixedTexCoord = repeatTexCoord * g_RepeatSampleSize + g_RepeatSampleCoord; + + // get point colour from sample. + gl_FragColor = v_Colour * texture2D(g_RepeatSample, fixedTexCoord); } From 363bcc78b1e173e9a3c8c67a001a5d7e4653b05d Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 24 Oct 2021 23:05:59 +0800 Subject: [PATCH 3/6] Implement texture size param. --- .../Shaders/CustomizedShaderTestScene.cs | 6 +++--- .../Shaders/RepeatMovingBackgroundShader.cs | 20 +++++++++---------- .../Shaders/sh_RepeatMovingBackground.fs | 13 ++++++++---- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs index bf9d42b..d290c59 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs @@ -96,9 +96,9 @@ public void TestPixelShader(float x, float y) }); } - [TestCase("sample-texture", 1f, 1f)] + [TestCase("sample-texture", 10, 10)] + [TestCase("sample-texture", 30, 30)] [TestCase("sample-texture", 5, 20)] - [TestCase("sample-texture", 20, 20)] public void TestRepeatMovingBackgroundShader(string textureName, float width, float height) { AddStep("Apply shader", () => @@ -108,7 +108,7 @@ public void TestRepeatMovingBackgroundShader(string textureName, float width, fl GetShaderByType().With(s => { s.Texture = textures.Get(textureName); - s.TextureSize = new Vector2(width, height); + s.TextureDisplaySize = new Vector2(width, height); }) }; }); diff --git a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs index 5e95414..1f26a02 100644 --- a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs @@ -14,9 +14,7 @@ public class RepeatMovingBackgroundShader : InternalShader public Texture Texture { get; set; } - public Vector2 TextureSize { get; set; } = new Vector2(10); - - public Vector2 Border { get; set; } + public Vector2 TextureDisplaySize { get; set; } = new Vector2(10); public RepeatMovingBackgroundShader(IShader originShader) : base(originShader) @@ -28,20 +26,22 @@ public override void ApplyValue(FrameBuffer current) if (Texture == null) return; + var size = current.Size; + GetUniform(@"g_TexSize").UpdateValue(ref size); + Texture.TextureGL.Bind(TextureUnit.Texture1); var unitId = TextureUnit.Texture1 - TextureUnit.Texture0; GetUniform(@"g_RepeatSample").UpdateValue(ref unitId); - var coord = Texture.GetTextureRect().TopLeft; - GetUniform(@"g_RepeatSampleCoord").UpdateValue(ref coord); - - var size = Texture.GetTextureRect().Size; + var textureCoord = Texture.GetTextureRect().TopLeft; + GetUniform(@"g_RepeatSampleCoord").UpdateValue(ref textureCoord); - GetUniform(@"g_RepeatSampleSize").UpdateValue(ref size); + var textureSize = Texture.GetTextureRect().Size; + GetUniform(@"g_RepeatSampleSize").UpdateValue(ref textureSize); - var textureSize = TextureSize; - GetUniform("g_TextureSize").UpdateValue(ref textureSize); + var textureDisplaySize = TextureDisplaySize; + GetUniform("g_DisplaySize").UpdateValue(ref textureDisplaySize); //var border = Border; //GetUniform("g_Border").UpdateValue(ref border); diff --git a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs index f13622a..b4429d9 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs @@ -2,19 +2,24 @@ varying vec2 v_TexCoord; varying vec4 v_Colour; +varying mediump vec4 v_TexRect; uniform lowp sampler2D m_Sampler; +uniform mediump vec2 g_TexSize; uniform lowp sampler2D g_RepeatSample; uniform vec2 g_RepeatSampleCoord; uniform vec2 g_RepeatSampleSize; -uniform vec2 g_TextureSize; +uniform vec2 g_DisplaySize; uniform vec2 g_Offset; void main(void) { - // get the repeat position - float repeatTexCoordX = mod(v_TexCoord.x * g_TextureSize.x + g_Offset.x, 1); - float repeatTexCoordY = mod(v_TexCoord.y * g_TextureSize.y + g_Offset.y, 1); + // calculate how many times texture should be repeated. + vec2 repeat = g_TexSize / g_DisplaySize; + + // get the repeat texture cooldinate. + float repeatTexCoordX = mod(v_TexCoord.x * repeat.x + g_Offset.x, 1); + float repeatTexCoordY = mod(v_TexCoord.y * repeat.y + g_Offset.y, 1); vec2 repeatTexCoord = vec2(repeatTexCoordX, repeatTexCoordY); // because repeat texture will be the size of 1024*1024, so should make a conversion to get the target area of the texture. From 3e5c270f95c67fef61fffc372a1cc8ef43ba1d61 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 24 Oct 2021 23:27:19 +0800 Subject: [PATCH 4/6] Add border property. --- .../Shaders/CustomizedShaderTestScene.cs | 22 ++++++++++++++++++- .../Shaders/RepeatMovingBackgroundShader.cs | 6 +++-- .../Shaders/sh_RepeatMovingBackground.fs | 5 +++-- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs index d290c59..d1cec3d 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs @@ -99,7 +99,7 @@ public void TestPixelShader(float x, float y) [TestCase("sample-texture", 10, 10)] [TestCase("sample-texture", 30, 30)] [TestCase("sample-texture", 5, 20)] - public void TestRepeatMovingBackgroundShader(string textureName, float width, float height) + public void TestRepeatMovingBackgroundShaderDisplaySize(string textureName, float width, float height) { AddStep("Apply shader", () => { @@ -113,5 +113,25 @@ public void TestRepeatMovingBackgroundShader(string textureName, float width, fl }; }); } + + [TestCase("sample-texture", 0, 0)] + [TestCase("sample-texture", 10, 10)] + [TestCase("sample-texture", 5, 20)] + [TestCase("sample-texture", 30, 30)] + public void TestRepeatMovingBackgroundShaderBorder(string textureName, float width, float height) + { + AddStep("Apply shader", () => + { + ShaderContainer.Shaders = new[] + { + GetShaderByType().With(s => + { + s.Texture = textures.Get(textureName); + s.TextureDisplaySize = new Vector2(30); + s.TextureDisplayBorder = new Vector2(width, height); + }) + }; + }); + } } } diff --git a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs index 1f26a02..b60741b 100644 --- a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs @@ -16,6 +16,8 @@ public class RepeatMovingBackgroundShader : InternalShader public Vector2 TextureDisplaySize { get; set; } = new Vector2(10); + public Vector2 TextureDisplayBorder { get; set; } + public RepeatMovingBackgroundShader(IShader originShader) : base(originShader) { @@ -43,8 +45,8 @@ public override void ApplyValue(FrameBuffer current) var textureDisplaySize = TextureDisplaySize; GetUniform("g_DisplaySize").UpdateValue(ref textureDisplaySize); - //var border = Border; - //GetUniform("g_Border").UpdateValue(ref border); + var textureDisplayBorder = TextureDisplayBorder; + GetUniform("g_DisplayBorder").UpdateValue(ref textureDisplayBorder); } } } diff --git a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs index b4429d9..0fa8c2d 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs @@ -11,16 +11,17 @@ uniform lowp sampler2D g_RepeatSample; uniform vec2 g_RepeatSampleCoord; uniform vec2 g_RepeatSampleSize; uniform vec2 g_DisplaySize; +uniform vec2 g_DisplayBorder; uniform vec2 g_Offset; void main(void) { // calculate how many times texture should be repeated. - vec2 repeat = g_TexSize / g_DisplaySize; + vec2 repeat = g_TexSize / (g_DisplaySize + g_DisplayBorder); // get the repeat texture cooldinate. float repeatTexCoordX = mod(v_TexCoord.x * repeat.x + g_Offset.x, 1); float repeatTexCoordY = mod(v_TexCoord.y * repeat.y + g_Offset.y, 1); - vec2 repeatTexCoord = vec2(repeatTexCoordX, repeatTexCoordY); + vec2 repeatTexCoord = vec2(repeatTexCoordX, repeatTexCoordY) / g_DisplaySize * (g_DisplaySize + g_DisplayBorder); // because repeat texture will be the size of 1024*1024, so should make a conversion to get the target area of the texture. vec2 fixedTexCoord = repeatTexCoord * g_RepeatSampleSize + g_RepeatSampleCoord; From 6fe73825d1f72bd0833d1baddd7e64ee7fa74069 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 24 Oct 2021 23:46:35 +0800 Subject: [PATCH 5/6] apply time moving in the shader. --- .../Shaders/CustomizedShaderTestScene.cs | 20 +++++++++++++++++++ .../Shaders/RepeatMovingBackgroundShader.cs | 12 ++++++++++- .../Shaders/sh_RepeatMovingBackground.fs | 7 ++++--- 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs index d1cec3d..35c2bbd 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs @@ -133,5 +133,25 @@ public void TestRepeatMovingBackgroundShaderBorder(string textureName, float wid }; }); } + + [TestCase(0, 0)] + [TestCase(1, 1)] + [TestCase(0.1f, 0.1f)] + [TestCase(5, 2)] + public void TestRepeatMovingBackgroundShaderSpeed(float xSpeed, float ySpeed) + { + AddStep("Apply shader", () => + { + ShaderContainer.Shaders = new[] + { + GetShaderByType().With(s => + { + s.Texture = textures.Get("sample-texture"); + s.TextureDisplaySize = new Vector2(30); + s.Speed = new Vector2(xSpeed, ySpeed); + }) + }; + }); + } } } diff --git a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs index b60741b..05f0771 100644 --- a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs @@ -8,7 +8,7 @@ namespace osu.Framework.Graphics.Shaders { - public class RepeatMovingBackgroundShader : InternalShader + public class RepeatMovingBackgroundShader : InternalShader, IApplicableToCurrentTime { public override string ShaderName => "RepeatMovingBackground"; @@ -18,6 +18,8 @@ public class RepeatMovingBackgroundShader : InternalShader public Vector2 TextureDisplayBorder { get; set; } + public Vector2 Speed { get; set; } + public RepeatMovingBackgroundShader(IShader originShader) : base(originShader) { @@ -47,6 +49,14 @@ public override void ApplyValue(FrameBuffer current) var textureDisplayBorder = TextureDisplayBorder; GetUniform("g_DisplayBorder").UpdateValue(ref textureDisplayBorder); + + var speed = Speed; + GetUniform("g_Speed").UpdateValue(ref speed); + } + + public void ApplyCurrentTime(float currentTime) + { + GetUniform("g_Time").UpdateValue(ref currentTime); } } } diff --git a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs index 0fa8c2d..006fc71 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs @@ -12,15 +12,16 @@ uniform vec2 g_RepeatSampleCoord; uniform vec2 g_RepeatSampleSize; uniform vec2 g_DisplaySize; uniform vec2 g_DisplayBorder; -uniform vec2 g_Offset; +uniform vec2 g_Speed; +uniform float g_Time; void main(void) { // calculate how many times texture should be repeated. vec2 repeat = g_TexSize / (g_DisplaySize + g_DisplayBorder); // get the repeat texture cooldinate. - float repeatTexCoordX = mod(v_TexCoord.x * repeat.x + g_Offset.x, 1); - float repeatTexCoordY = mod(v_TexCoord.y * repeat.y + g_Offset.y, 1); + float repeatTexCoordX = mod(v_TexCoord.x * repeat.x + g_Speed.x * g_Time, 1); + float repeatTexCoordY = mod(v_TexCoord.y * repeat.y + g_Speed.y * g_Time, 1); vec2 repeatTexCoord = vec2(repeatTexCoordX, repeatTexCoordY) / g_DisplaySize * (g_DisplaySize + g_DisplayBorder); // because repeat texture will be the size of 1024*1024, so should make a conversion to get the target area of the texture. From 8037fcb847ee8030e4f6aa1c6fe18c8496079f97 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Mon, 25 Oct 2021 00:03:00 +0800 Subject: [PATCH 6/6] Implement mix property. Almost there. --- .../Shaders/CustomizedShaderTestScene.cs | 22 +++++++++++++++++++ .../Shaders/RepeatMovingBackgroundShader.cs | 6 +++++ .../Shaders/sh_RepeatMovingBackground.fs | 5 ++++- 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs index 35c2bbd..3a378e1 100644 --- a/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs +++ b/osu.Framework.Font.Tests/Visual/Shaders/CustomizedShaderTestScene.cs @@ -153,5 +153,27 @@ public void TestRepeatMovingBackgroundShaderSpeed(float xSpeed, float ySpeed) }; }); } + + [TestCase(0)] + [TestCase(0.5f)] + [TestCase(1f)] + [TestCase(-1)] // invalid + [TestCase(3)] // invalid + public void TestRepeatMovingBackgroundShaderMix(float mix) + { + AddStep("Apply shader", () => + { + ShaderContainer.Shaders = new[] + { + GetShaderByType().With(s => + { + s.Texture = textures.Get("sample-texture"); + s.TextureDisplaySize = new Vector2(30); + s.Speed = new Vector2(1); + s.Mix = mix; + }) + }; + }); + } } } diff --git a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs index 05f0771..ab0a21a 100644 --- a/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs +++ b/osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs @@ -1,6 +1,7 @@ // Copyright (c) andy840119 . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using System; using osu.Framework.Graphics.OpenGL.Buffers; using osu.Framework.Graphics.Textures; using osuTK; @@ -20,6 +21,8 @@ public class RepeatMovingBackgroundShader : InternalShader, IApplicableToCurrent public Vector2 Speed { get; set; } + public float Mix { get; set; } = 1f; + public RepeatMovingBackgroundShader(IShader originShader) : base(originShader) { @@ -52,6 +55,9 @@ public override void ApplyValue(FrameBuffer current) var speed = Speed; GetUniform("g_Speed").UpdateValue(ref speed); + + var mix = Math.Clamp(Mix, 0, 1); + GetUniform(@"g_Mix").UpdateValue(ref mix); } public void ApplyCurrentTime(float currentTime) diff --git a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs index 006fc71..1a044eb 100644 --- a/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs +++ b/osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs @@ -14,6 +14,7 @@ uniform vec2 g_DisplaySize; uniform vec2 g_DisplayBorder; uniform vec2 g_Speed; uniform float g_Time; +uniform float g_Mix; void main(void) { // calculate how many times texture should be repeated. @@ -28,5 +29,7 @@ void main(void) { vec2 fixedTexCoord = repeatTexCoord * g_RepeatSampleSize + g_RepeatSampleCoord; // get point colour from sample. - gl_FragColor = v_Colour * texture2D(g_RepeatSample, fixedTexCoord); + vec4 texColor = texture2D(m_Sampler, v_TexCoord); + vec4 repeatSampleColor = v_Colour * vec4(texture2D(g_RepeatSample, fixedTexCoord).xyz, texColor.a); + gl_FragColor = mix(texColor, repeatSampleColor, g_Mix); }