mirror of
https://github.com/osukey/osukey.git
synced 2025-05-05 05:37:18 +09:00
Merge pull request #857 from smoogipooo/triangles-particles
Triangles particles
This commit is contained in:
commit
e67a00f1f6
@ -1 +1 @@
|
|||||||
Subproject commit 777996fb9731ba1895a5ab1323cbbc97259ff741
|
Subproject commit 8baad1b9484b9f35724e2f965c18cfe710907d80
|
@ -1,20 +1,29 @@
|
|||||||
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
||||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||||
|
|
||||||
using System.Linq;
|
|
||||||
using osu.Framework.Extensions.IEnumerableExtensions;
|
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Framework.Graphics.Sprites;
|
|
||||||
using osu.Framework.MathUtils;
|
using osu.Framework.MathUtils;
|
||||||
using OpenTK;
|
using OpenTK;
|
||||||
using OpenTK.Graphics;
|
using OpenTK.Graphics;
|
||||||
using System;
|
using System;
|
||||||
|
using osu.Framework.Graphics.OpenGL;
|
||||||
|
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;
|
||||||
|
using osu.Framework.Lists;
|
||||||
|
|
||||||
namespace osu.Game.Graphics.Backgrounds
|
namespace osu.Game.Graphics.Backgrounds
|
||||||
{
|
{
|
||||||
public class Triangles : Container<Triangle>
|
public class Triangles : Drawable
|
||||||
{
|
{
|
||||||
|
private const float triangle_size = 100;
|
||||||
|
|
||||||
public override bool HandleInput => false;
|
public override bool HandleInput => false;
|
||||||
|
|
||||||
public Color4 ColourLight = Color4.White;
|
public Color4 ColourLight = Color4.White;
|
||||||
@ -49,6 +58,28 @@ namespace osu.Game.Graphics.Backgrounds
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public float Velocity = 1;
|
public float Velocity = 1;
|
||||||
|
|
||||||
|
private readonly SortedList<TriangleParticle> parts = new SortedList<TriangleParticle>(Comparer<TriangleParticle>.Default);
|
||||||
|
|
||||||
|
private Shader shader;
|
||||||
|
private readonly Texture texture;
|
||||||
|
|
||||||
|
public Triangles()
|
||||||
|
{
|
||||||
|
texture = Texture.WhitePixel;
|
||||||
|
}
|
||||||
|
|
||||||
|
[BackgroundDependencyLoader]
|
||||||
|
private void load(ShaderManager shaders)
|
||||||
|
{
|
||||||
|
shader = shaders?.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void LoadComplete()
|
||||||
|
{
|
||||||
|
base.LoadComplete();
|
||||||
|
addTriangles(true);
|
||||||
|
}
|
||||||
|
|
||||||
public float TriangleScale
|
public float TriangleScale
|
||||||
{
|
{
|
||||||
get { return triangleScale; }
|
get { return triangleScale; }
|
||||||
@ -57,42 +88,71 @@ namespace osu.Game.Graphics.Backgrounds
|
|||||||
float change = value / triangleScale;
|
float change = value / triangleScale;
|
||||||
triangleScale = value;
|
triangleScale = value;
|
||||||
|
|
||||||
if (change != 1)
|
for (int i = 0; i < parts.Count; i++)
|
||||||
Children.ForEach(t => t.Scale *= change);
|
{
|
||||||
|
TriangleParticle newParticle = parts[i];
|
||||||
|
newParticle.Scale *= change;
|
||||||
|
parts[i] = newParticle;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void LoadComplete()
|
|
||||||
{
|
|
||||||
base.LoadComplete();
|
|
||||||
|
|
||||||
addTriangles(true);
|
|
||||||
}
|
|
||||||
|
|
||||||
private int aimTriangleCount => (int)(DrawWidth * DrawHeight * 0.002f / (triangleScale * triangleScale) * SpawnRatio);
|
|
||||||
|
|
||||||
protected override void Update()
|
protected override void Update()
|
||||||
{
|
{
|
||||||
base.Update();
|
base.Update();
|
||||||
|
|
||||||
float adjustedAlpha = HideAlphaDiscrepancies ?
|
Invalidate(Invalidation.DrawNode, shallPropagate: false);
|
||||||
// Cubically scale alpha to make it drop off more sharply.
|
|
||||||
(float)Math.Pow(DrawInfo.Colour.AverageColour.Linear.A, 3) :
|
|
||||||
1;
|
|
||||||
|
|
||||||
foreach (var t in Children)
|
for (int i = 0; i < parts.Count; i++)
|
||||||
{
|
{
|
||||||
t.Alpha = adjustedAlpha;
|
TriangleParticle newParticle = parts[i];
|
||||||
t.Position -= new Vector2(0, (float)(t.Scale.X * (50 / DrawHeight) * (Time.Elapsed / 950)) / triangleScale * Velocity);
|
|
||||||
if (ExpireOffScreenTriangles && t.DrawPosition.Y + t.DrawSize.Y * t.Scale.Y < 0)
|
float adjustedAlpha = HideAlphaDiscrepancies ?
|
||||||
t.Expire();
|
// Cubically scale alpha to make it drop off more sharply.
|
||||||
|
(float)Math.Pow(DrawInfo.Colour.AverageColour.Linear.A, 3) :
|
||||||
|
1;
|
||||||
|
|
||||||
|
|
||||||
|
newParticle.Position += new Vector2(0, -(parts[i].Scale * (50 / DrawHeight)) / triangleScale * Velocity) * ((float)Time.Elapsed / 950);
|
||||||
|
newParticle.Colour.A = adjustedAlpha;
|
||||||
|
|
||||||
|
parts[i] = newParticle;
|
||||||
|
|
||||||
|
if (!CreateNewTriangles)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
float bottomPos = parts[i].Position.Y + triangle_size * parts[i].Scale * 0.866f / DrawHeight;
|
||||||
|
|
||||||
|
if (bottomPos < 0)
|
||||||
|
parts.RemoveAt(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CreateNewTriangles)
|
addTriangles(false);
|
||||||
addTriangles(false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual Triangle CreateTriangle()
|
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)
|
||||||
|
{
|
||||||
|
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()
|
||||||
{
|
{
|
||||||
const float std_dev = 0.16f;
|
const float std_dev = 0.16f;
|
||||||
const float mean = 0.5f;
|
const float mean = 0.5f;
|
||||||
@ -102,32 +162,100 @@ namespace osu.Game.Graphics.Backgrounds
|
|||||||
float randStdNormal = (float)(Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2)); //random normal(0,1)
|
float randStdNormal = (float)(Math.Sqrt(-2.0 * Math.Log(u1)) * Math.Sin(2.0 * Math.PI * u2)); //random normal(0,1)
|
||||||
var scale = Math.Max(triangleScale * (mean + std_dev * randStdNormal), 0.1f); //random normal(mean,stdDev^2)
|
var scale = Math.Max(triangleScale * (mean + std_dev * randStdNormal), 0.1f); //random normal(mean,stdDev^2)
|
||||||
|
|
||||||
const float size = 100;
|
return new TriangleParticle { Scale = scale };
|
||||||
|
|
||||||
return new EquilateralTriangle
|
|
||||||
{
|
|
||||||
Origin = Anchor.TopCentre,
|
|
||||||
RelativePositionAxes = Axes.Both,
|
|
||||||
Size = new Vector2(size),
|
|
||||||
Scale = new Vector2(scale),
|
|
||||||
EdgeSmoothness = new Vector2(1),
|
|
||||||
Colour = GetTriangleShade(),
|
|
||||||
Depth = scale,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual Color4 GetTriangleShade() => Interpolation.ValueAt(RNG.NextSingle(), ColourDark, ColourLight, 0, 1);
|
/// <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);
|
||||||
|
|
||||||
private void addTriangles(bool randomY)
|
protected override DrawNode CreateDrawNode() => new TrianglesDrawNode();
|
||||||
|
|
||||||
|
private readonly TrianglesDrawNodeSharedData sharedData = new TrianglesDrawNodeSharedData();
|
||||||
|
protected override void ApplyDrawNode(DrawNode node)
|
||||||
{
|
{
|
||||||
int addCount = aimTriangleCount - Children.Count();
|
base.ApplyDrawNode(node);
|
||||||
for (int i = 0; i < addCount; i++)
|
|
||||||
|
var trianglesNode = (TrianglesDrawNode)node;
|
||||||
|
|
||||||
|
trianglesNode.Shader = shader;
|
||||||
|
trianglesNode.Texture = texture;
|
||||||
|
trianglesNode.Size = DrawSize;
|
||||||
|
trianglesNode.Shared = sharedData;
|
||||||
|
|
||||||
|
trianglesNode.Parts.Clear();
|
||||||
|
trianglesNode.Parts.AddRange(parts);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TrianglesDrawNodeSharedData
|
||||||
|
{
|
||||||
|
public readonly LinearBatch<TexturedVertex2D> VertexBatch = new LinearBatch<TexturedVertex2D>(100 * 3, 10, PrimitiveType.Triangles);
|
||||||
|
}
|
||||||
|
|
||||||
|
private class TrianglesDrawNode : DrawNode
|
||||||
|
{
|
||||||
|
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)
|
||||||
{
|
{
|
||||||
var sprite = CreateTriangle();
|
base.Draw(vertexAction);
|
||||||
float triangleHeight = sprite.DrawHeight / DrawHeight;
|
|
||||||
sprite.Position = new Vector2(RNG.NextSingle(), randomY ? RNG.NextSingle() * (1 + triangleHeight) - triangleHeight : 1);
|
Shader.Bind();
|
||||||
Add(sprite);
|
Texture.TextureGL.Bind();
|
||||||
|
|
||||||
|
foreach (TriangleParticle particle in Parts)
|
||||||
|
{
|
||||||
|
var offset = new Vector2(particle.Scale * 0.5f, particle.Scale * 0.866f);
|
||||||
|
|
||||||
|
var triangle = new Triangle(
|
||||||
|
particle.Position * Size * DrawInfo.Matrix,
|
||||||
|
(particle.Position * Size + offset * triangle_size) * DrawInfo.Matrix,
|
||||||
|
(particle.Position * Size + new Vector2(-offset.X, offset.Y) * triangle_size) * DrawInfo.Matrix
|
||||||
|
);
|
||||||
|
|
||||||
|
ColourInfo colourInfo = DrawInfo.Colour;
|
||||||
|
colourInfo.ApplyChild(particle.Colour);
|
||||||
|
|
||||||
|
Texture.DrawTriangle(triangle, colourInfo, null, Shared.VertexBatch.Add);
|
||||||
|
}
|
||||||
|
|
||||||
|
Shader.Unbind();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected struct TriangleParticle : IComparable<TriangleParticle>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The position of the top vertex of the triangle.
|
||||||
|
/// </summary>
|
||||||
|
public Vector2 Position;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The colour of the triangle.
|
||||||
|
/// </summary>
|
||||||
|
public Color4 Colour;
|
||||||
|
|
||||||
|
/// <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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user