Skip to content

Commit

Permalink
Implement mix property.
Browse files Browse the repository at this point in the history
Almost there.
  • Loading branch information
andy840119 committed Oct 24, 2021
1 parent 6fe7382 commit 8037fcb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<RepeatMovingBackgroundShader>().With(s =>
{
s.Texture = textures.Get("sample-texture");
s.TextureDisplaySize = new Vector2(30);
s.Speed = new Vector2(1);
s.Mix = mix;
})
};
});
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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;
Expand All @@ -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)
{
Expand Down Expand Up @@ -52,6 +55,9 @@ public override void ApplyValue(FrameBuffer current)

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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);
}

0 comments on commit 8037fcb

Please sign in to comment.