1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 08:43:01 +08:00

Merge pull request #22409 from EVAST9919/triangles-og-masking

Implement Masking property for `Triangles` background
This commit is contained in:
Dean Herbert 2023-01-26 14:46:59 +09:00 committed by GitHub
commit 044234b1ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 9 deletions

View File

@ -5,6 +5,7 @@ using osu.Game.Graphics.Backgrounds;
using osu.Framework.Graphics;
using osuTK.Graphics;
using osu.Framework.Graphics.Shapes;
using osuTK;
namespace osu.Game.Tests.Visual.Background
{
@ -25,7 +26,10 @@ namespace osu.Game.Tests.Visual.Background
{
RelativeSizeAxes = Axes.Both,
ColourLight = Color4.White,
ColourDark = Color4.Gray
ColourDark = Color4.Gray,
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(0.9f)
}
};
}
@ -36,6 +40,7 @@ namespace osu.Game.Tests.Visual.Background
AddSliderStep("Triangle scale", 0f, 10f, 1f, s => triangles.TriangleScale = s);
AddSliderStep("Seed", 0, 1000, 0, s => triangles.Reset(s));
AddToggleStep("Masking", m => triangles.Masking = m);
}
}
}

View File

@ -77,6 +77,12 @@ namespace osu.Game.Graphics.Backgrounds
set => triangleScale.Value = value;
}
/// <summary>
/// If enabled, only the portion of triangles that falls within this <see cref="Drawable"/>'s
/// shape is drawn to the screen.
/// </summary>
public bool Masking { get; set; }
/// <summary>
/// Whether we should drop-off alpha values of triangles more quickly to improve
/// the visual appearance of fading. This defaults to on as it is generally more
@ -252,6 +258,7 @@ namespace osu.Game.Graphics.Backgrounds
private IShader shader;
private Texture texture;
private bool masking;
private readonly List<TriangleParticle> parts = new List<TriangleParticle>();
private readonly Vector2 triangleSize = new Vector2(1f, equilateral_triangle_ratio) * triangle_size;
@ -271,6 +278,7 @@ namespace osu.Game.Graphics.Backgrounds
shader = Source.shader;
texture = Source.texture;
size = Source.DrawSize;
masking = Source.Masking;
parts.Clear();
parts.AddRange(Source.parts);
@ -294,26 +302,45 @@ namespace osu.Game.Graphics.Backgrounds
Vector2 relativeSize = Vector2.Divide(triangleSize * particle.Scale, size);
Vector2 topLeft = particle.Position - new Vector2(relativeSize.X * 0.5f, 0f);
Vector2 topRight = topLeft + new Vector2(relativeSize.X, 0f);
Vector2 bottomLeft = topLeft + new Vector2(0f, relativeSize.Y);
Vector2 bottomRight = bottomLeft + new Vector2(relativeSize.X, 0f);
Quad triangleQuad = masking ? clampToDrawable(topLeft, relativeSize) : new Quad(topLeft.X, topLeft.Y, relativeSize.X, relativeSize.Y);
var drawQuad = new Quad(
Vector2Extensions.Transform(topLeft * size, DrawInfo.Matrix),
Vector2Extensions.Transform(topRight * size, DrawInfo.Matrix),
Vector2Extensions.Transform(bottomLeft * size, DrawInfo.Matrix),
Vector2Extensions.Transform(bottomRight * size, DrawInfo.Matrix)
Vector2Extensions.Transform(triangleQuad.TopLeft * size, DrawInfo.Matrix),
Vector2Extensions.Transform(triangleQuad.TopRight * size, DrawInfo.Matrix),
Vector2Extensions.Transform(triangleQuad.BottomLeft * size, DrawInfo.Matrix),
Vector2Extensions.Transform(triangleQuad.BottomRight * size, DrawInfo.Matrix)
);
ColourInfo colourInfo = DrawColourInfo.Colour;
colourInfo.ApplyChild(particle.Colour);
renderer.DrawQuad(texture, drawQuad, colourInfo, vertexAction: vertexBatch.AddAction);
RectangleF textureCoords = new RectangleF(
triangleQuad.TopLeft.X - topLeft.X,
triangleQuad.TopLeft.Y - topLeft.Y,
triangleQuad.Width,
triangleQuad.Height
) / relativeSize;
renderer.DrawQuad(texture, drawQuad, colourInfo, new RectangleF(0, 0, 1, 1), vertexBatch.AddAction, textureCoords: textureCoords);
}
shader.Unbind();
}
private static Quad clampToDrawable(Vector2 topLeft, Vector2 size)
{
float leftClamped = Math.Clamp(topLeft.X, 0f, 1f);
float topClamped = Math.Clamp(topLeft.Y, 0f, 1f);
return new Quad(
leftClamped,
topClamped,
Math.Clamp(topLeft.X + size.X, 0f, 1f) - leftClamped,
Math.Clamp(topLeft.Y + size.Y, 0f, 1f) - topClamped
);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);