1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-24 06:43:03 +08:00
osu-lazer/osu.Game/Graphics/Backgrounds/Triangles.cs

279 lines
10 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Graphics;
using osu.Framework.MathUtils;
using OpenTK;
2017-01-30 15:53:12 +08:00
using OpenTK.Graphics;
using System;
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Textures;
using OpenTK.Graphics.ES30;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Allocation;
using System.Collections.Generic;
using osu.Framework.Graphics.Batches;
2017-06-08 16:57:06 +08:00
using osu.Framework.Graphics.OpenGL.Vertices;
using osu.Framework.Lists;
namespace osu.Game.Graphics.Backgrounds
{
public class Triangles : Drawable
{
private const float triangle_size = 100;
private const float base_velocity = 50;
2017-05-30 00:20:51 +08:00
/// <summary>
/// How many screen-space pixels are smoothed over.
/// Same behavior as Sprite's EdgeSmoothness.
/// </summary>
private const float edge_smoothness = 1;
public override bool HandleInput => false;
2017-01-30 15:53:12 +08:00
public Color4 ColourLight = Color4.White;
public Color4 ColourDark = Color4.Black;
/// <summary>
/// Whether we want to expire triangles as they exit our draw area completely.
/// </summary>
protected virtual bool ExpireOffScreenTriangles => true;
/// <summary>
/// Whether we should create new triangles as others expire.
/// </summary>
protected virtual bool CreateNewTriangles => true;
/// <summary>
/// The amount of triangles we want compared to the default distribution.
/// </summary>
protected virtual float SpawnRatio => 1;
2016-12-01 19:21:14 +08:00
private float triangleScale = 1;
/// <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
2017-06-21 10:35:19 +08:00
/// aesthetically pleasing, but should be turned off in buffered containers.
/// </summary>
public bool HideAlphaDiscrepancies = true;
/// <summary>
/// The relative velocity of the triangles. Default is 1.
/// </summary>
public float Velocity = 1;
private readonly SortedList<TriangleParticle> parts = new SortedList<TriangleParticle>(Comparer<TriangleParticle>.Default);
private Shader shader;
private readonly Texture texture;
public Triangles()
2016-12-01 19:21:14 +08:00
{
texture = Texture.WhitePixel;
}
2016-12-01 19:21:14 +08:00
[BackgroundDependencyLoader]
private void load(ShaderManager shaders)
{
2017-05-25 20:33:49 +08:00
shader = shaders?.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED);
2016-12-01 19:21:14 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
2017-04-05 19:00:22 +08:00
addTriangles(true);
}
public float TriangleScale
{
get { return 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;
}
}
}
protected override void Update()
{
base.Update();
Invalidate(Invalidation.DrawNode, shallPropagate: false);
if (CreateNewTriangles)
addTriangles(false);
float adjustedAlpha = HideAlphaDiscrepancies ?
// Cubically scale alpha to make it drop off more sharply.
(float)Math.Pow(DrawInfo.Colour.AverageColour.Linear.A, 3) :
1;
float elapsedSeconds = (float)Time.Elapsed / 1000;
// 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);
for (int i = 0; i < parts.Count; i++)
{
TriangleParticle newParticle = parts[i];
// Scale moved distance by the size of the triangle. Smaller triangles should move more slowly.
newParticle.Position.Y += parts[i].Scale * movedDistance;
newParticle.Colour.A = adjustedAlpha;
parts[i] = newParticle;
float bottomPos = parts[i].Position.Y + triangle_size * parts[i].Scale * 0.866f / DrawHeight;
if (bottomPos < 0)
parts.RemoveAt(i);
}
}
private void addTriangles(bool randomY)
{
int aimTriangleCount = (int)(DrawWidth * DrawHeight * 0.002f / (triangleScale * triangleScale) * SpawnRatio);
for (int i = 0; i < aimTriangleCount - parts.Count; i++)
parts.Add(createTriangle(randomY));
}
private TriangleParticle createTriangle(bool randomY)
{
2017-05-25 21:09:02 +08:00
TriangleParticle particle = CreateTriangle();
particle.Position = new Vector2(RNG.NextSingle(), randomY ? RNG.NextSingle() : 1);
particle.Colour = CreateTriangleShade();
return particle;
}
/// <summary>
/// Creates a triangle particle with a random scale.
/// </summary>
/// <returns>The triangle particle.</returns>
protected virtual TriangleParticle CreateTriangle()
{
2017-03-23 12:52:38 +08:00
const float std_dev = 0.16f;
const float mean = 0.5f;
float u1 = 1 - RNG.NextSingle(); //uniform(0,1] random floats
float u2 = 1 - RNG.NextSingle();
float randStdNormal = (float)(Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2)); //random normal(0,1)
2017-03-23 12:52:38 +08:00
var scale = Math.Max(triangleScale * (mean + std_dev * randStdNormal), 0.1f); //random normal(mean,stdDev^2)
return new TriangleParticle { Scale = scale };
}
2016-12-01 19:21:14 +08:00
/// <summary>
/// Creates a shade of colour for the triangles.
/// </summary>
/// <returns>The colour.</returns>
protected virtual Color4 CreateTriangleShade() => Interpolation.ValueAt(RNG.NextSingle(), ColourDark, ColourLight, 0, 1);
protected override DrawNode CreateDrawNode() => new TrianglesDrawNode();
2017-05-25 20:33:49 +08:00
private readonly TrianglesDrawNodeSharedData sharedData = new TrianglesDrawNodeSharedData();
protected override void ApplyDrawNode(DrawNode node)
{
base.ApplyDrawNode(node);
2017-05-25 20:33:49 +08:00
var trianglesNode = (TrianglesDrawNode)node;
trianglesNode.Shader = shader;
trianglesNode.Texture = texture;
trianglesNode.Size = DrawSize;
trianglesNode.Shared = sharedData;
trianglesNode.Parts.Clear();
trianglesNode.Parts.AddRange(parts);
2016-12-01 19:21:14 +08:00
}
2017-05-25 21:09:02 +08:00
private class TrianglesDrawNodeSharedData
{
2017-05-25 21:09:02 +08:00
public readonly LinearBatch<TexturedVertex2D> VertexBatch = new LinearBatch<TexturedVertex2D>(100 * 3, 10, PrimitiveType.Triangles);
}
2017-01-30 15:53:12 +08:00
2017-05-25 21:09:02 +08:00
private class TrianglesDrawNode : DrawNode
2016-12-01 19:21:14 +08:00
{
public Shader Shader;
public Texture Texture;
public TrianglesDrawNodeSharedData Shared;
public readonly List<TriangleParticle> Parts = new List<TriangleParticle>();
public Vector2 Size;
public override void Draw(Action<TexturedVertex2D> vertexAction)
2017-04-05 19:00:22 +08:00
{
base.Draw(vertexAction);
Shader.Bind();
Texture.TextureGL.Bind();
2017-05-30 00:20:51 +08:00
Vector2 localInflationAmount = edge_smoothness * DrawInfo.MatrixInverse.ExtractScale().Xy;
2017-05-25 20:33:49 +08:00
foreach (TriangleParticle particle in Parts)
{
2017-05-30 00:20:51 +08:00
var offset = triangle_size * new Vector2(particle.Scale * 0.5f, particle.Scale * 0.866f);
var size = new Vector2(2 * offset.X, offset.Y);
var triangle = new Triangle(
Vector2Extensions.Transform(particle.Position * Size, DrawInfo.Matrix),
Vector2Extensions.Transform(particle.Position * Size + offset, DrawInfo.Matrix),
Vector2Extensions.Transform(particle.Position * Size + new Vector2(-offset.X, offset.Y), DrawInfo.Matrix)
);
ColourInfo colourInfo = DrawInfo.Colour;
colourInfo.ApplyChild(particle.Colour);
2017-05-30 00:20:51 +08:00
Texture.DrawTriangle(
triangle,
colourInfo,
null,
Shared.VertexBatch.Add,
Vector2.Divide(localInflationAmount, size));
}
Shader.Unbind();
2017-04-05 19:00:22 +08:00
}
}
2017-05-25 21:09:02 +08:00
protected struct TriangleParticle : IComparable<TriangleParticle>
{
2017-05-25 21:09:02 +08:00
/// <summary>
/// The position of the top vertex of the triangle.
/// </summary>
public Vector2 Position;
2017-05-25 21:09:02 +08:00
/// <summary>
/// The colour of the triangle.
/// </summary>
public Color4 Colour;
2017-05-25 21:09:02 +08:00
/// <summary>
/// The scale of the triangle.
/// </summary>
public float Scale;
/// <summary>
/// Compares two <see cref="TriangleParticle"/>s. This is a reverse comparer because when the
/// triangles are added to the particles list, they should be drawn from largest to smallest
/// such that the smaller triangles appear on top.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public int CompareTo(TriangleParticle other) => other.Scale.CompareTo(Scale);
}
}
}