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

Merge pull request #26677 from EVAST9919/triangles-better-clamp

Implement per-axis clamping in `Triangles` backgrounds
This commit is contained in:
Dean Herbert 2024-01-23 18:28:03 +09:00 committed by GitHub
commit 413c34ce06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 59 additions and 38 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using osu.Framework.Graphics;
using osu.Game.Graphics.Backgrounds; using osu.Game.Graphics.Backgrounds;
namespace osu.Game.Rulesets.Osu.Skinning.Default namespace osu.Game.Rulesets.Osu.Skinning.Default
@ -15,7 +16,7 @@ namespace osu.Game.Rulesets.Osu.Skinning.Default
{ {
TriangleScale = 1.2f; TriangleScale = 1.2f;
HideAlphaDiscrepancies = false; HideAlphaDiscrepancies = false;
ClampToDrawable = false; ClampAxes = Axes.None;
} }
protected override void Update() protected override void Update()

View File

@ -29,7 +29,8 @@ namespace osu.Game.Tests.Visual.Background
ColourDark = Color4.Gray, ColourDark = Color4.Gray,
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
Size = new Vector2(0.9f) Size = new Vector2(0.9f),
ClampAxes = Axes.None
} }
}; };
} }
@ -40,7 +41,10 @@ namespace osu.Game.Tests.Visual.Background
AddSliderStep("Triangle scale", 0f, 10f, 1f, s => triangles.TriangleScale = s); AddSliderStep("Triangle scale", 0f, 10f, 1f, s => triangles.TriangleScale = s);
AddSliderStep("Seed", 0, 1000, 0, s => triangles.Reset(s)); AddSliderStep("Seed", 0, 1000, 0, s => triangles.Reset(s));
AddToggleStep("ClampToDrawable", c => triangles.ClampToDrawable = c); AddStep("ClampAxes X", () => triangles.ClampAxes = Axes.X);
AddStep("ClampAxes Y", () => triangles.ClampAxes = Axes.Y);
AddStep("ClampAxes Both", () => triangles.ClampAxes = Axes.Both);
AddStep("ClampAxes None", () => triangles.ClampAxes = Axes.None);
} }
} }
} }

View File

@ -86,7 +86,8 @@ namespace osu.Game.Tests.Visual.Background
{ {
Anchor = Anchor.Centre, Anchor = Anchor.Centre,
Origin = Anchor.Centre, Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both RelativeSizeAxes = Axes.Both,
ClampAxes = Axes.None
} }
} }
}, },
@ -128,7 +129,10 @@ namespace osu.Game.Tests.Visual.Background
AddStep("White colour", () => box.Colour = triangles.Colour = maskedTriangles.Colour = Color4.White); AddStep("White colour", () => box.Colour = triangles.Colour = maskedTriangles.Colour = Color4.White);
AddStep("Vertical gradient", () => box.Colour = triangles.Colour = maskedTriangles.Colour = ColourInfo.GradientVertical(Color4.White, Color4.Red)); AddStep("Vertical gradient", () => box.Colour = triangles.Colour = maskedTriangles.Colour = ColourInfo.GradientVertical(Color4.White, Color4.Red));
AddStep("Horizontal gradient", () => box.Colour = triangles.Colour = maskedTriangles.Colour = ColourInfo.GradientHorizontal(Color4.White, Color4.Red)); AddStep("Horizontal gradient", () => box.Colour = triangles.Colour = maskedTriangles.Colour = ColourInfo.GradientHorizontal(Color4.White, Color4.Red));
AddToggleStep("ClampToDrawable", c => maskedTriangles.ClampToDrawable = c); AddStep("ClampAxes X", () => maskedTriangles.ClampAxes = Axes.X);
AddStep("ClampAxes Y", () => maskedTriangles.ClampAxes = Axes.Y);
AddStep("ClampAxes Both", () => maskedTriangles.ClampAxes = Axes.Both);
AddStep("ClampAxes None", () => maskedTriangles.ClampAxes = Axes.None);
} }
} }
} }

View File

@ -77,10 +77,10 @@ namespace osu.Game.Graphics.Backgrounds
} }
/// <summary> /// <summary>
/// If enabled, only the portion of triangles that falls within this <see cref="Drawable"/>'s /// Controls on which <see cref="Axes"/> the portion of triangles that falls within this <see cref="Drawable"/>'s
/// shape is drawn to the screen. Default is true. /// shape is drawn to the screen. Default is Axes.Both.
/// </summary> /// </summary>
public bool ClampToDrawable { get; set; } = true; public Axes ClampAxes { get; set; } = Axes.Both;
/// <summary> /// <summary>
/// Whether we should drop-off alpha values of triangles more quickly to improve /// Whether we should drop-off alpha values of triangles more quickly to improve
@ -257,7 +257,7 @@ namespace osu.Game.Graphics.Backgrounds
private IShader shader; private IShader shader;
private Texture texture; private Texture texture;
private bool clamp; private Axes clampAxes;
private readonly List<TriangleParticle> parts = new List<TriangleParticle>(); private readonly List<TriangleParticle> parts = new List<TriangleParticle>();
private readonly Vector2 triangleSize = new Vector2(1f, equilateral_triangle_ratio) * triangle_size; private readonly Vector2 triangleSize = new Vector2(1f, equilateral_triangle_ratio) * triangle_size;
@ -276,7 +276,7 @@ namespace osu.Game.Graphics.Backgrounds
shader = Source.shader; shader = Source.shader;
texture = Source.texture; texture = Source.texture;
size = Source.DrawSize; size = Source.DrawSize;
clamp = Source.ClampToDrawable; clampAxes = Source.ClampAxes;
parts.Clear(); parts.Clear();
parts.AddRange(Source.parts); parts.AddRange(Source.parts);
@ -306,7 +306,7 @@ namespace osu.Game.Graphics.Backgrounds
Vector2 topLeft = particle.Position - new Vector2(relativeSize.X * 0.5f, 0f); Vector2 topLeft = particle.Position - new Vector2(relativeSize.X * 0.5f, 0f);
Quad triangleQuad = clamp ? clampToDrawable(topLeft, relativeSize) : new Quad(topLeft.X, topLeft.Y, relativeSize.X, relativeSize.Y); Quad triangleQuad = getClampedQuad(clampAxes, topLeft, relativeSize);
var drawQuad = new Quad( var drawQuad = new Quad(
Vector2Extensions.Transform(triangleQuad.TopLeft * size, DrawInfo.Matrix), Vector2Extensions.Transform(triangleQuad.TopLeft * size, DrawInfo.Matrix),
@ -331,17 +331,23 @@ namespace osu.Game.Graphics.Backgrounds
shader.Unbind(); shader.Unbind();
} }
private static Quad clampToDrawable(Vector2 topLeft, Vector2 size) private static Quad getClampedQuad(Axes clampAxes, Vector2 topLeft, Vector2 size)
{ {
float leftClamped = Math.Clamp(topLeft.X, 0f, 1f); Vector2 clampedTopLeft = topLeft;
float topClamped = Math.Clamp(topLeft.Y, 0f, 1f);
return new Quad( if (clampAxes == Axes.X || clampAxes == Axes.Both)
leftClamped, {
topClamped, clampedTopLeft.X = Math.Clamp(topLeft.X, 0f, 1f);
Math.Clamp(topLeft.X + size.X, 0f, 1f) - leftClamped, size.X = Math.Clamp(topLeft.X + size.X, 0f, 1f) - clampedTopLeft.X;
Math.Clamp(topLeft.Y + size.Y, 0f, 1f) - topClamped }
);
if (clampAxes == Axes.Y || clampAxes == Axes.Both)
{
clampedTopLeft.Y = Math.Clamp(topLeft.Y, 0f, 1f);
size.Y = Math.Clamp(topLeft.Y + size.Y, 0f, 1f) - clampedTopLeft.Y;
}
return new Quad(clampedTopLeft.X, clampedTopLeft.Y, size.X, size.Y);
} }
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)

View File

@ -33,10 +33,10 @@ namespace osu.Game.Graphics.Backgrounds
protected virtual bool CreateNewTriangles => true; protected virtual bool CreateNewTriangles => true;
/// <summary> /// <summary>
/// If enabled, only the portion of triangles that falls within this <see cref="Drawable"/>'s /// Controls on which <see cref="Axes"/> the portion of triangles that falls within this <see cref="Drawable"/>'s
/// shape is drawn to the screen. Default is true. /// shape is drawn to the screen. Default is Axes.Both.
/// </summary> /// </summary>
public bool ClampToDrawable { get; set; } = true; public Axes ClampAxes { get; set; } = Axes.Both;
private readonly BindableFloat spawnRatio = new BindableFloat(1f); private readonly BindableFloat spawnRatio = new BindableFloat(1f);
@ -193,7 +193,7 @@ namespace osu.Game.Graphics.Backgrounds
private Vector2 size; private Vector2 size;
private float thickness; private float thickness;
private float texelSize; private float texelSize;
private bool clamp; private Axes clampAxes;
public TrianglesDrawNode(TrianglesV2 source) public TrianglesDrawNode(TrianglesV2 source)
: base(source) : base(source)
@ -208,7 +208,7 @@ namespace osu.Game.Graphics.Backgrounds
texture = Source.texture; texture = Source.texture;
size = Source.DrawSize; size = Source.DrawSize;
thickness = Source.Thickness; thickness = Source.Thickness;
clamp = Source.ClampToDrawable; clampAxes = Source.ClampAxes;
Quad triangleQuad = new Quad( Quad triangleQuad = new Quad(
Vector2Extensions.Transform(Vector2.Zero, DrawInfo.Matrix), Vector2Extensions.Transform(Vector2.Zero, DrawInfo.Matrix),
@ -248,7 +248,7 @@ namespace osu.Game.Graphics.Backgrounds
{ {
Vector2 topLeft = particle.Position - new Vector2(relativeSize.X * 0.5f, 0f); Vector2 topLeft = particle.Position - new Vector2(relativeSize.X * 0.5f, 0f);
Quad triangleQuad = clamp ? clampToDrawable(topLeft, relativeSize) : new Quad(topLeft.X, topLeft.Y, relativeSize.X, relativeSize.Y); Quad triangleQuad = getClampedQuad(clampAxes, topLeft, relativeSize);
var drawQuad = new Quad( var drawQuad = new Quad(
Vector2Extensions.Transform(triangleQuad.TopLeft * size, DrawInfo.Matrix), Vector2Extensions.Transform(triangleQuad.TopLeft * size, DrawInfo.Matrix),
@ -270,17 +270,23 @@ namespace osu.Game.Graphics.Backgrounds
shader.Unbind(); shader.Unbind();
} }
private static Quad clampToDrawable(Vector2 topLeft, Vector2 size) private static Quad getClampedQuad(Axes clampAxes, Vector2 topLeft, Vector2 size)
{ {
float leftClamped = Math.Clamp(topLeft.X, 0f, 1f); Vector2 clampedTopLeft = topLeft;
float topClamped = Math.Clamp(topLeft.Y, 0f, 1f);
return new Quad( if (clampAxes == Axes.X || clampAxes == Axes.Both)
leftClamped, {
topClamped, clampedTopLeft.X = Math.Clamp(topLeft.X, 0f, 1f);
Math.Clamp(topLeft.X + size.X, 0f, 1f) - leftClamped, size.X = Math.Clamp(topLeft.X + size.X, 0f, 1f) - clampedTopLeft.X;
Math.Clamp(topLeft.Y + size.Y, 0f, 1f) - topClamped }
);
if (clampAxes == Axes.Y || clampAxes == Axes.Both)
{
clampedTopLeft.Y = Math.Clamp(topLeft.Y, 0f, 1f);
size.Y = Math.Clamp(topLeft.Y + size.Y, 0f, 1f) - clampedTopLeft.Y;
}
return new Quad(clampedTopLeft.X, clampedTopLeft.Y, size.X, size.Y);
} }
protected override void Dispose(bool isDisposing) protected override void Dispose(bool isDisposing)

View File

@ -150,7 +150,7 @@ namespace osu.Game.Graphics.UserInterface
TriangleScale = 4, TriangleScale = 4,
ColourDark = OsuColour.Gray(0.88f), ColourDark = OsuColour.Gray(0.88f),
Shear = new Vector2(-0.2f, 0), Shear = new Vector2(-0.2f, 0),
ClampToDrawable = false ClampAxes = Axes.Y
}, },
}, },
}, },

View File

@ -34,7 +34,7 @@ namespace osu.Game.Overlays.Mods
var hsv = new Colour4(value.R, value.G, value.B, 1f).ToHSV(); var hsv = new Colour4(value.R, value.G, value.B, 1f).ToHSV();
var trianglesColour = Colour4.FromHSV(hsv.X, hsv.Y + 0.2f, hsv.Z - 0.1f); var trianglesColour = Colour4.FromHSV(hsv.X, hsv.Y + 0.2f, hsv.Z - 0.1f);
triangles.Colour = ColourInfo.GradientVertical(trianglesColour, trianglesColour.MultiplyAlpha(0f)); triangles.Colour = ColourInfo.GradientVertical(trianglesColour, value);
} }
} }
@ -95,7 +95,7 @@ namespace osu.Game.Overlays.Mods
Height = header_height, Height = header_height,
Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0), Shear = new Vector2(-ShearedOverlayContainer.SHEAR, 0),
Velocity = 0.7f, Velocity = 0.7f,
ClampToDrawable = false ClampAxes = Axes.Y
}, },
headerText = new OsuTextFlowContainer(t => headerText = new OsuTextFlowContainer(t =>
{ {