1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:17:23 +08:00

Implement Masking property for TrianglesBackground

This commit is contained in:
Andrei Zavatski 2023-01-25 12:20:51 +03:00
parent 67e9de43be
commit d09d6f31d7
3 changed files with 42 additions and 9 deletions

View File

@ -15,6 +15,7 @@ namespace osu.Game.Rulesets.Osu.Skinning.Default
{
TriangleScale = 1.2f;
HideAlphaDiscrepancies = false;
Masking = false;
}
protected override void Update()

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; } = true;
/// <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);