Merge pull request #21242 from EVAST9919/triangles-fix

Triangles background improvements
This commit is contained in:
Dean Herbert 2022-11-16 19:26:52 +09:00 committed by GitHub
commit 335e4e8ec5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 77 additions and 26 deletions

View File

@ -0,0 +1,40 @@
// 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.
using osu.Game.Graphics.Backgrounds;
using osu.Framework.Graphics;
using osuTK.Graphics;
using osu.Framework.Graphics.Shapes;
namespace osu.Game.Tests.Visual.Background
{
public class TestSceneTrianglesBackground : OsuTestScene
{
private readonly Triangles triangles;
public TestSceneTrianglesBackground()
{
Children = new Drawable[]
{
new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black
},
triangles = new Triangles
{
RelativeSizeAxes = Axes.Both,
ColourLight = Color4.White,
ColourDark = Color4.Gray
}
};
}
protected override void LoadComplete()
{
base.LoadComplete();
AddSliderStep("Triangle scale", 0f, 10f, 1f, s => triangles.TriangleScale = s);
}
}
}

View File

@ -17,6 +17,7 @@
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Rendering.Vertices;
using osu.Framework.Lists;
using osu.Framework.Bindables;
namespace osu.Game.Graphics.Backgrounds
{
@ -25,6 +26,11 @@ public class Triangles : Drawable
private const float triangle_size = 100;
private const float base_velocity = 50;
/// <summary>
/// sqrt(3) / 2
/// </summary>
private const float equilateral_triangle_ratio = 0.866f;
/// <summary>
/// How many screen-space pixels are smoothed over.
/// Same behavior as Sprite's EdgeSmoothness.
@ -69,7 +75,13 @@ public Color4 ColourDark
/// </summary>
protected virtual float SpawnRatio => 1;
private float triangleScale = 1;
private readonly BindableFloat triangleScale = new BindableFloat(1f);
public float TriangleScale
{
get => triangleScale.Value;
set => triangleScale.Value = value;
}
/// <summary>
/// Whether we should drop-off alpha values of triangles more quickly to improve
@ -109,24 +121,7 @@ private void load(IRenderer renderer, ShaderManager shaders)
protected override void LoadComplete()
{
base.LoadComplete();
addTriangles(true);
}
public float TriangleScale
{
get => triangleScale;
set
{
float change = value / triangleScale;
triangleScale = value;
for (int i = 0; i < parts.Count; i++)
{
TriangleParticle newParticle = parts[i];
newParticle.Scale *= change;
parts[i] = newParticle;
}
}
triangleScale.BindValueChanged(_ => Reset(), true);
}
protected override void Update()
@ -147,7 +142,7 @@ protected override void Update()
// Since position is relative, the velocity needs to scale inversely with DrawHeight.
// Since we will later multiply by the scale of individual triangles we normalize by
// dividing by triangleScale.
float movedDistance = -elapsedSeconds * Velocity * base_velocity / (DrawHeight * triangleScale);
float movedDistance = -elapsedSeconds * Velocity * base_velocity / (DrawHeight * TriangleScale);
for (int i = 0; i < parts.Count; i++)
{
@ -159,7 +154,7 @@ protected override void Update()
parts[i] = newParticle;
float bottomPos = parts[i].Position.Y + triangle_size * parts[i].Scale * 0.866f / DrawHeight;
float bottomPos = parts[i].Position.Y + triangle_size * parts[i].Scale * equilateral_triangle_ratio / DrawHeight;
if (bottomPos < 0)
parts.RemoveAt(i);
}
@ -185,9 +180,11 @@ private void addTriangles(bool randomY)
// Limited by the maximum size of QuadVertexBuffer for safety.
const int max_triangles = ushort.MaxValue / (IRenderer.VERTICES_PER_QUAD + 2);
AimCount = (int)Math.Min(max_triangles, (DrawWidth * DrawHeight * 0.002f / (triangleScale * triangleScale) * SpawnRatio));
AimCount = (int)Math.Min(max_triangles, DrawWidth * DrawHeight * 0.002f / (TriangleScale * TriangleScale) * SpawnRatio);
for (int i = 0; i < AimCount - parts.Count; i++)
int currentCount = parts.Count;
for (int i = 0; i < AimCount - currentCount; i++)
parts.Add(createTriangle(randomY));
}
@ -195,13 +192,27 @@ private TriangleParticle createTriangle(bool randomY)
{
TriangleParticle particle = CreateTriangle();
particle.Position = new Vector2(nextRandom(), randomY ? nextRandom() : 1);
particle.Position = getRandomPosition(randomY, particle.Scale);
particle.ColourShade = nextRandom();
particle.Colour = CreateTriangleShade(particle.ColourShade);
return particle;
}
private Vector2 getRandomPosition(bool randomY, float scale)
{
float y = 1;
if (randomY)
{
// since triangles are drawn from the top - allow them to be positioned a bit above the screen
float maxOffset = triangle_size * scale * equilateral_triangle_ratio / DrawHeight;
y = Interpolation.ValueAt(nextRandom(), -maxOffset, 1f, 0f, 1f);
}
return new Vector2(nextRandom(), y);
}
/// <summary>
/// Creates a triangle particle with a random scale.
/// </summary>
@ -214,7 +225,7 @@ protected virtual TriangleParticle CreateTriangle()
float u1 = 1 - nextRandom(); //uniform(0,1] random floats
float u2 = 1 - nextRandom();
float randStdNormal = (float)(Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2)); // random normal(0,1)
float scale = Math.Max(triangleScale * (mean + std_dev * randStdNormal), 0.1f); // random normal(mean,stdDev^2)
float scale = Math.Max(TriangleScale * (mean + std_dev * randStdNormal), 0.1f); // random normal(mean,stdDev^2)
return new TriangleParticle { Scale = scale };
}
@ -284,7 +295,7 @@ public override void Draw(IRenderer renderer)
foreach (TriangleParticle particle in parts)
{
var offset = triangle_size * new Vector2(particle.Scale * 0.5f, particle.Scale * 0.866f);
var offset = triangle_size * new Vector2(particle.Scale * 0.5f, particle.Scale * equilateral_triangle_ratio);
var triangle = new Triangle(
Vector2Extensions.Transform(particle.Position * size, DrawInfo.Matrix),