Skip to content

Commit

Permalink
Merge branch 'master' into fix-full-area-reset
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzibyte authored Nov 16, 2022
2 parents bb762d8 + 335e4e8 commit c167377
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 26 deletions.
40 changes: 40 additions & 0 deletions osu.Game.Tests/Visual/Background/TestSceneTrianglesBackground.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. 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);
}
}
}
63 changes: 37 additions & 26 deletions osu.Game/Graphics/Backgrounds/Triangles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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++)
{
Expand All @@ -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);
}
Expand All @@ -185,23 +180,39 @@ 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);

int currentCount = parts.Count;

for (int i = 0; i < AimCount - parts.Count; i++)
for (int i = 0; i < AimCount - currentCount; i++)
parts.Add(createTriangle(randomY));
}

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>
Expand All @@ -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 };
}
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit c167377

Please sign in to comment.