Skip to content

Commit

Permalink
Add border property.
Browse files Browse the repository at this point in the history
  • Loading branch information
andy840119 committed Oct 24, 2021
1 parent 363bcc7 commit 3e5c270
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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", () =>
{
Expand All @@ -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<RepeatMovingBackgroundShader>().With(s =>
{
s.Texture = textures.Get(textureName);
s.TextureDisplaySize = new Vector2(30);
s.TextureDisplayBorder = new Vector2(width, height);
})
};
});
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -43,8 +45,8 @@ public override void ApplyValue(FrameBuffer current)
var textureDisplaySize = TextureDisplaySize;
GetUniform<Vector2>("g_DisplaySize").UpdateValue(ref textureDisplaySize);

//var border = Border;
//GetUniform<Vector2>("g_Border").UpdateValue(ref border);
var textureDisplayBorder = TextureDisplayBorder;
GetUniform<Vector2>("g_DisplayBorder").UpdateValue(ref textureDisplayBorder);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3e5c270

Please sign in to comment.