1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-15 13:27:20 +08:00

Make triangles use raw vertices instead of sprites.

This commit is contained in:
smoogipooo 2017-05-25 21:33:04 +09:00
parent 4d7d4b7389
commit 97e57178a7
2 changed files with 166 additions and 47 deletions

@ -1 +1 @@
Subproject commit 777996fb9731ba1895a5ab1323cbbc97259ff741
Subproject commit 3e989ae63031a75e622225d6f4d14c6e26170b97

View File

@ -5,16 +5,28 @@ using System.Linq;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
using osu.Framework.MathUtils;
using OpenTK;
using OpenTK.Graphics;
using System;
using System.Runtime.InteropServices;
using osu.Framework.Graphics.OpenGL;
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Textures;
using osu.Framework.Graphics.OpenGL.Buffers;
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;
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 Color4 ColourLight = Color4.White;
@ -49,6 +61,28 @@ namespace osu.Game.Graphics.Backgrounds
/// </summary>
public float Velocity = 1;
private readonly List<TriangleParticle> parts = new List<TriangleParticle>();
private Shader shader;
private readonly Texture texture;
public Triangles()
{
texture = Texture.WhitePixel;
}
[BackgroundDependencyLoader]
private void load(ShaderManager shaders)
{
shader = shaders?.Load("Triangles", FragmentShaderDescriptor.TEXTURE_ROUNDED);
}
protected override void LoadComplete()
{
base.LoadComplete();
addTriangles(true);
}
public float TriangleScale
{
get { return triangleScale; }
@ -57,42 +91,69 @@ namespace osu.Game.Graphics.Backgrounds
float change = value / triangleScale;
triangleScale = value;
if (change != 1)
Children.ForEach(t => t.Scale *= change);
for (int i = 0; i < parts.Count; i++)
{
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()
{
base.Update();
float adjustedAlpha = HideAlphaDiscrepancies ?
// Cubically scale alpha to make it drop off more sharply.
(float)Math.Pow(DrawInfo.Colour.AverageColour.Linear.A, 3) :
1;
Invalidate(Invalidation.DrawNode, shallPropagate: false);
foreach (var t in Children)
for (int i = 0; i < parts.Count; i++)
{
t.Alpha = adjustedAlpha;
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)
t.Expire();
}
TriangleParticle newParticle = parts[i];
if (CreateNewTriangles)
addTriangles(false);
newParticle.Position += new Vector2(0, -(float)(parts[i].Scale * (50 / DrawHeight)) / triangleScale * Velocity) * ((float)Time.Elapsed / 950);
float adjustedAlpha = HideAlphaDiscrepancies ?
// Cubically scale alpha to make it drop off more sharply.
(float)Math.Pow(DrawInfo.Colour.AverageColour.Linear.A, 3) :
1;
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[i] = createTriangle(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; i++)
parts.Add(createTriangle(randomY));
}
private TriangleParticle createTriangle(bool randomY)
{
var 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 mean = 0.5f;
@ -102,32 +163,90 @@ 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)
var scale = Math.Max(triangleScale * (mean + std_dev * randStdNormal), 0.1f); //random normal(mean,stdDev^2)
const float size = 100;
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,
};
return new TriangleParticle { Scale = 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 TrianglesDrawNodeSharedData sharedData = new TrianglesDrawNodeSharedData();
protected override void ApplyDrawNode(DrawNode node)
{
int addCount = aimTriangleCount - Children.Count();
for (int i = 0; i < addCount; i++)
base.ApplyDrawNode(node);
var trianglesNode = node as TrianglesDrawNode;
trianglesNode.Shader = shader;
trianglesNode.Texture = texture;
trianglesNode.Size = DrawSize;
trianglesNode.Shared = sharedData;
trianglesNode.Parts.Clear();
trianglesNode.Parts.AddRange(parts);
}
public class TrianglesDrawNodeSharedData
{
public LinearBatch<TexturedVertex2D> VertexBatch = new LinearBatch<TexturedVertex2D>(100 * 3, 10, PrimitiveType.Triangles);
}
public class TrianglesDrawNode : DrawNode
{
public Shader Shader;
public Texture Texture;
public float Time;
public TrianglesDrawNodeSharedData Shared;
public readonly List<TriangleParticle> Parts = new List<TriangleParticle>();
public Vector2 Size;
public override void Draw(Action<TexturedVertex2D> vertexAction)
{
var sprite = CreateTriangle();
float triangleHeight = sprite.DrawHeight / DrawHeight;
sprite.Position = new Vector2(RNG.NextSingle(), randomY ? RNG.NextSingle() * (1 + triangleHeight) - triangleHeight : 1);
Add(sprite);
base.Draw(vertexAction);
Shader.Bind();
Texture.TextureGL.Bind();
int updateStart = -1, updateEnd = 0;
for (int i = 0; i < Parts.Count; ++i)
{
if (updateStart == -1)
updateStart = i;
updateEnd = i + 1;
TriangleParticle particle = Parts[i];
Vector2 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
);
int index = i * 6;
ColourInfo colourInfo = DrawInfo.Colour;
colourInfo.ApplyChild(particle.Colour);
Texture.DrawTriangle(triangle, colourInfo, null, Shared.VertexBatch.Add);
}
Shader.Unbind();
}
}
public struct TriangleParticle
{
public Vector2 Position;
public Color4 Colour;
public float Scale;
}
}
}