mirror of
https://github.com/ppy/osu.git
synced 2025-01-13 03:33:22 +08:00
Merge pull request #22428 from EVAST9919/triangles-new-masking
Implement Masking property for `TrianglesV2` background
This commit is contained in:
commit
50659f3d52
@ -8,12 +8,14 @@ using osuTK;
|
|||||||
using osuTK.Graphics;
|
using osuTK.Graphics;
|
||||||
using osu.Game.Graphics.Backgrounds;
|
using osu.Game.Graphics.Backgrounds;
|
||||||
using osu.Framework.Graphics.Colour;
|
using osu.Framework.Graphics.Colour;
|
||||||
|
using osu.Game.Graphics.Sprites;
|
||||||
|
|
||||||
namespace osu.Game.Tests.Visual.Background
|
namespace osu.Game.Tests.Visual.Background
|
||||||
{
|
{
|
||||||
public partial class TestSceneTrianglesV2Background : OsuTestScene
|
public partial class TestSceneTrianglesV2Background : OsuTestScene
|
||||||
{
|
{
|
||||||
private readonly TrianglesV2 triangles;
|
private readonly TrianglesV2 triangles;
|
||||||
|
private readonly TrianglesV2 maskedTriangles;
|
||||||
private readonly Box box;
|
private readonly Box box;
|
||||||
|
|
||||||
public TestSceneTrianglesV2Background()
|
public TestSceneTrianglesV2Background()
|
||||||
@ -31,12 +33,20 @@ namespace osu.Game.Tests.Visual.Background
|
|||||||
Origin = Anchor.Centre,
|
Origin = Anchor.Centre,
|
||||||
AutoSizeAxes = Axes.Both,
|
AutoSizeAxes = Axes.Both,
|
||||||
Direction = FillDirection.Vertical,
|
Direction = FillDirection.Vertical,
|
||||||
Spacing = new Vector2(0, 5),
|
Spacing = new Vector2(0, 10),
|
||||||
Children = new Drawable[]
|
Children = new Drawable[]
|
||||||
{
|
{
|
||||||
|
new OsuSpriteText
|
||||||
|
{
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
Text = "Masked"
|
||||||
|
},
|
||||||
new Container
|
new Container
|
||||||
{
|
{
|
||||||
Size = new Vector2(500, 100),
|
Size = new Vector2(500, 100),
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
Masking = true,
|
Masking = true,
|
||||||
CornerRadius = 40,
|
CornerRadius = 40,
|
||||||
Children = new Drawable[]
|
Children = new Drawable[]
|
||||||
@ -54,9 +64,43 @@ namespace osu.Game.Tests.Visual.Background
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
new OsuSpriteText
|
||||||
|
{
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
Text = "Non-masked"
|
||||||
|
},
|
||||||
new Container
|
new Container
|
||||||
{
|
{
|
||||||
Size = new Vector2(500, 100),
|
Size = new Vector2(500, 100),
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
Children = new Drawable[]
|
||||||
|
{
|
||||||
|
new Box
|
||||||
|
{
|
||||||
|
RelativeSizeAxes = Axes.Both,
|
||||||
|
Colour = Color4.Red
|
||||||
|
},
|
||||||
|
maskedTriangles = new TrianglesV2
|
||||||
|
{
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
RelativeSizeAxes = Axes.Both
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
new OsuSpriteText
|
||||||
|
{
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
|
Text = "Gradient comparison box"
|
||||||
|
},
|
||||||
|
new Container
|
||||||
|
{
|
||||||
|
Size = new Vector2(500, 100),
|
||||||
|
Anchor = Anchor.Centre,
|
||||||
|
Origin = Anchor.Centre,
|
||||||
Masking = true,
|
Masking = true,
|
||||||
CornerRadius = 40,
|
CornerRadius = 40,
|
||||||
Child = box = new Box
|
Child = box = new Box
|
||||||
@ -75,14 +119,16 @@ namespace osu.Game.Tests.Visual.Background
|
|||||||
|
|
||||||
AddSliderStep("Spawn ratio", 0f, 10f, 1f, s =>
|
AddSliderStep("Spawn ratio", 0f, 10f, 1f, s =>
|
||||||
{
|
{
|
||||||
triangles.SpawnRatio = s;
|
triangles.SpawnRatio = maskedTriangles.SpawnRatio = s;
|
||||||
triangles.Reset(1234);
|
triangles.Reset(1234);
|
||||||
|
maskedTriangles.Reset(1234);
|
||||||
});
|
});
|
||||||
AddSliderStep("Thickness", 0f, 1f, 0.02f, t => triangles.Thickness = t);
|
AddSliderStep("Thickness", 0f, 1f, 0.02f, t => triangles.Thickness = maskedTriangles.Thickness = t);
|
||||||
|
|
||||||
AddStep("White colour", () => box.Colour = triangles.Colour = Color4.White);
|
AddStep("White colour", () => box.Colour = triangles.Colour = maskedTriangles.Colour = Color4.White);
|
||||||
AddStep("Vertical gradient", () => box.Colour = triangles.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 = ColourInfo.GradientHorizontal(Color4.White, Color4.Red));
|
AddStep("Horizontal gradient", () => box.Colour = triangles.Colour = maskedTriangles.Colour = ColourInfo.GradientHorizontal(Color4.White, Color4.Red));
|
||||||
|
AddToggleStep("Masking", m => maskedTriangles.Masking = m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -34,6 +34,12 @@ namespace osu.Game.Graphics.Backgrounds
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
protected virtual bool CreateNewTriangles => true;
|
protected virtual bool CreateNewTriangles => true;
|
||||||
|
|
||||||
|
/// <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; }
|
||||||
|
|
||||||
private readonly BindableFloat spawnRatio = new BindableFloat(1f);
|
private readonly BindableFloat spawnRatio = new BindableFloat(1f);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -189,6 +195,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 masking;
|
||||||
|
|
||||||
private IVertexBatch<TexturedVertex2D>? vertexBatch;
|
private IVertexBatch<TexturedVertex2D>? vertexBatch;
|
||||||
|
|
||||||
@ -205,6 +212,7 @@ namespace osu.Game.Graphics.Backgrounds
|
|||||||
texture = Source.texture;
|
texture = Source.texture;
|
||||||
size = Source.DrawSize;
|
size = Source.DrawSize;
|
||||||
thickness = Source.Thickness;
|
thickness = Source.Thickness;
|
||||||
|
masking = Source.Masking;
|
||||||
|
|
||||||
Quad triangleQuad = new Quad(
|
Quad triangleQuad = new Quad(
|
||||||
Vector2Extensions.Transform(Vector2.Zero, DrawInfo.Matrix),
|
Vector2Extensions.Transform(Vector2.Zero, DrawInfo.Matrix),
|
||||||
@ -236,26 +244,31 @@ namespace osu.Game.Graphics.Backgrounds
|
|||||||
shader.GetUniform<float>("thickness").UpdateValue(ref thickness);
|
shader.GetUniform<float>("thickness").UpdateValue(ref thickness);
|
||||||
shader.GetUniform<float>("texelSize").UpdateValue(ref texelSize);
|
shader.GetUniform<float>("texelSize").UpdateValue(ref texelSize);
|
||||||
|
|
||||||
float relativeHeight = triangleSize.Y / size.Y;
|
Vector2 relativeSize = Vector2.Divide(triangleSize, size);
|
||||||
float relativeWidth = triangleSize.X / size.X;
|
|
||||||
|
|
||||||
foreach (TriangleParticle particle in parts)
|
foreach (TriangleParticle particle in parts)
|
||||||
{
|
{
|
||||||
Vector2 topLeft = particle.Position - new Vector2(relativeWidth * 0.5f, 0f);
|
Vector2 topLeft = particle.Position - new Vector2(relativeSize.X * 0.5f, 0f);
|
||||||
Vector2 topRight = topLeft + new Vector2(relativeWidth, 0f);
|
|
||||||
Vector2 bottomLeft = topLeft + new Vector2(0f, relativeHeight);
|
Quad triangleQuad = masking ? clampToDrawable(topLeft, relativeSize) : new Quad(topLeft.X, topLeft.Y, relativeSize.X, relativeSize.Y);
|
||||||
Vector2 bottomRight = bottomLeft + new Vector2(relativeWidth, 0f);
|
|
||||||
|
|
||||||
var drawQuad = new Quad(
|
var drawQuad = new Quad(
|
||||||
Vector2Extensions.Transform(topLeft * size, DrawInfo.Matrix),
|
Vector2Extensions.Transform(triangleQuad.TopLeft * size, DrawInfo.Matrix),
|
||||||
Vector2Extensions.Transform(topRight * size, DrawInfo.Matrix),
|
Vector2Extensions.Transform(triangleQuad.TopRight * size, DrawInfo.Matrix),
|
||||||
Vector2Extensions.Transform(bottomLeft * size, DrawInfo.Matrix),
|
Vector2Extensions.Transform(triangleQuad.BottomLeft * size, DrawInfo.Matrix),
|
||||||
Vector2Extensions.Transform(bottomRight * size, DrawInfo.Matrix)
|
Vector2Extensions.Transform(triangleQuad.BottomRight * size, DrawInfo.Matrix)
|
||||||
);
|
);
|
||||||
|
|
||||||
ColourInfo colourInfo = triangleColourInfo(DrawColourInfo.Colour, new Quad(topLeft, topRight, bottomLeft, bottomRight));
|
ColourInfo colourInfo = triangleColourInfo(DrawColourInfo.Colour, triangleQuad);
|
||||||
|
|
||||||
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();
|
shader.Unbind();
|
||||||
@ -272,6 +285,19 @@ namespace osu.Game.Graphics.Backgrounds
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
protected override void Dispose(bool isDisposing)
|
||||||
{
|
{
|
||||||
base.Dispose(isDisposing);
|
base.Dispose(isDisposing);
|
||||||
|
Loading…
Reference in New Issue
Block a user