From 6fe73825d1f72bd0833d1baddd7e64ee7fa74069 Mon Sep 17 00:00:00 2001 From: andy840119 Date: Sun, 24 Oct 2021 23:46:35 +0800 Subject: [PATCH] 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.