Skip to content

Commit

Permalink
apply time moving in the shader.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Oct 24, 2021
1 parent 3e5c270 commit 6fe7382
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<RepeatMovingBackgroundShader>().With(s =>
{
s.Texture = textures.Get("sample-texture");
s.TextureDisplaySize = new Vector2(30);
s.Speed = new Vector2(xSpeed, ySpeed);
})
};
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace osu.Framework.Graphics.Shaders
{
public class RepeatMovingBackgroundShader : InternalShader
public class RepeatMovingBackgroundShader : InternalShader, IApplicableToCurrentTime
{
public override string ShaderName => "RepeatMovingBackground";

Expand All @@ -18,6 +18,8 @@ public class RepeatMovingBackgroundShader : InternalShader

public Vector2 TextureDisplayBorder { get; set; }

public Vector2 Speed { get; set; }

public RepeatMovingBackgroundShader(IShader originShader)
: base(originShader)
{
Expand Down Expand Up @@ -47,6 +49,14 @@ public override void ApplyValue(FrameBuffer current)

var textureDisplayBorder = TextureDisplayBorder;
GetUniform<Vector2>("g_DisplayBorder").UpdateValue(ref textureDisplayBorder);

var speed = Speed;
GetUniform<Vector2>("g_Speed").UpdateValue(ref speed);
}

public void ApplyCurrentTime(float currentTime)
{
GetUniform<float>("g_Time").UpdateValue(ref currentTime);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 6fe7382

Please sign in to comment.