-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from andy840119/implement-repeat-moving-backgr…
…ound-shader Implement repeat moving background effect
- Loading branch information
Showing
4 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
osu.Framework.Font/Graphics/Shaders/RepeatMovingBackgroundShader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) andy840119 <[email protected]>. 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; | ||
using osuTK.Graphics.ES30; | ||
|
||
namespace osu.Framework.Graphics.Shaders | ||
{ | ||
public class RepeatMovingBackgroundShader : InternalShader, IApplicableToCurrentTime | ||
{ | ||
public override string ShaderName => "RepeatMovingBackground"; | ||
|
||
public Texture Texture { get; set; } | ||
|
||
public Vector2 TextureDisplaySize { get; set; } = new Vector2(10); | ||
|
||
public Vector2 TextureDisplayBorder { get; set; } | ||
|
||
public Vector2 Speed { get; set; } | ||
|
||
public float Mix { get; set; } = 1f; | ||
|
||
public RepeatMovingBackgroundShader(IShader originShader) | ||
: base(originShader) | ||
{ | ||
} | ||
|
||
public override void ApplyValue(FrameBuffer current) | ||
{ | ||
if (Texture == null) | ||
return; | ||
|
||
var size = current.Size; | ||
GetUniform<Vector2>(@"g_TexSize").UpdateValue(ref size); | ||
|
||
Texture.TextureGL.Bind(TextureUnit.Texture1); | ||
|
||
var unitId = TextureUnit.Texture1 - TextureUnit.Texture0; | ||
GetUniform<int>(@"g_RepeatSample").UpdateValue(ref unitId); | ||
|
||
var textureCoord = Texture.GetTextureRect().TopLeft; | ||
GetUniform<Vector2>(@"g_RepeatSampleCoord").UpdateValue(ref textureCoord); | ||
|
||
var textureSize = Texture.GetTextureRect().Size; | ||
GetUniform<Vector2>(@"g_RepeatSampleSize").UpdateValue(ref textureSize); | ||
|
||
var textureDisplaySize = TextureDisplaySize; | ||
GetUniform<Vector2>("g_DisplaySize").UpdateValue(ref textureDisplaySize); | ||
|
||
var textureDisplayBorder = TextureDisplayBorder; | ||
GetUniform<Vector2>("g_DisplayBorder").UpdateValue(ref textureDisplayBorder); | ||
|
||
var speed = Speed; | ||
GetUniform<Vector2>("g_Speed").UpdateValue(ref speed); | ||
|
||
var mix = Math.Clamp(Mix, 0, 1); | ||
GetUniform<float>(@"g_Mix").UpdateValue(ref mix); | ||
} | ||
|
||
public void ApplyCurrentTime(float currentTime) | ||
{ | ||
GetUniform<float>("g_Time").UpdateValue(ref currentTime); | ||
} | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
osu.Framework.Font/Resources/Shaders/sh_RepeatMovingBackground.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// see : https://github.com/kiwipxl/GLSL-shaders/blob/master/repeat.glsl | ||
|
||
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_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. | ||
vec2 repeat = g_TexSize / (g_DisplaySize + g_DisplayBorder); | ||
|
||
// get the repeat texture cooldinate. | ||
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. | ||
vec2 fixedTexCoord = repeatTexCoord * g_RepeatSampleSize + g_RepeatSampleCoord; | ||
|
||
// get point colour from sample. | ||
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); | ||
} |