mirror of
https://github.com/osukey/osukey.git
synced 2025-08-05 07:33:55 +09:00
Make triangles use raw vertices instead of sprites.
This commit is contained in:
Submodule osu-framework updated: 777996fb97...3e989ae630
@ -5,16 +5,28 @@ using System.Linq;
|
|||||||
using osu.Framework.Extensions.IEnumerableExtensions;
|
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 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
|
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 +61,28 @@ namespace osu.Game.Graphics.Backgrounds
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public float Velocity = 1;
|
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
|
public float TriangleScale
|
||||||
{
|
{
|
||||||
get { return triangleScale; }
|
get { return triangleScale; }
|
||||||
@ -57,42 +91,69 @@ 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
protected override void LoadComplete()
|
|
||||||
{
|
{
|
||||||
base.LoadComplete();
|
TriangleParticle newParticle = parts[i];
|
||||||
|
newParticle.Scale *= change;
|
||||||
addTriangles(true);
|
parts[i] = newParticle;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private int aimTriangleCount => (int)(DrawWidth * DrawHeight * 0.002f / (triangleScale * triangleScale) * SpawnRatio);
|
|
||||||
|
|
||||||
protected override void Update()
|
protected override void Update()
|
||||||
{
|
{
|
||||||
base.Update();
|
base.Update();
|
||||||
|
|
||||||
|
Invalidate(Invalidation.DrawNode, shallPropagate: false);
|
||||||
|
|
||||||
|
for (int i = 0; i < parts.Count; i++)
|
||||||
|
{
|
||||||
|
TriangleParticle newParticle = parts[i];
|
||||||
|
|
||||||
|
newParticle.Position += new Vector2(0, -(float)(parts[i].Scale * (50 / DrawHeight)) / triangleScale * Velocity) * ((float)Time.Elapsed / 950);
|
||||||
|
|
||||||
float adjustedAlpha = HideAlphaDiscrepancies ?
|
float adjustedAlpha = HideAlphaDiscrepancies ?
|
||||||
// Cubically scale alpha to make it drop off more sharply.
|
// Cubically scale alpha to make it drop off more sharply.
|
||||||
(float)Math.Pow(DrawInfo.Colour.AverageColour.Linear.A, 3) :
|
(float)Math.Pow(DrawInfo.Colour.AverageColour.Linear.A, 3) :
|
||||||
1;
|
1;
|
||||||
|
|
||||||
foreach (var t in Children)
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addTriangles(bool randomY)
|
||||||
{
|
{
|
||||||
t.Alpha = adjustedAlpha;
|
int aimTriangleCount = (int)(DrawWidth * DrawHeight * 0.002f / (triangleScale * triangleScale) * SpawnRatio);
|
||||||
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)
|
for (int i = 0; i < aimTriangleCount; i++)
|
||||||
t.Expire();
|
parts.Add(createTriangle(randomY));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (CreateNewTriangles)
|
private TriangleParticle createTriangle(bool randomY)
|
||||||
addTriangles(false);
|
{
|
||||||
|
var particle = CreateTriangle();
|
||||||
|
|
||||||
|
particle.Position = new Vector2(RNG.NextSingle(), randomY ? RNG.NextSingle() : 1);
|
||||||
|
particle.Colour = CreateTriangleShade();
|
||||||
|
|
||||||
|
return particle;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected virtual Triangle CreateTriangle()
|
/// <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 +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)
|
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 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 = node as TrianglesDrawNode;
|
||||||
var sprite = CreateTriangle();
|
|
||||||
float triangleHeight = sprite.DrawHeight / DrawHeight;
|
trianglesNode.Shader = shader;
|
||||||
sprite.Position = new Vector2(RNG.NextSingle(), randomY ? RNG.NextSingle() * (1 + triangleHeight) - triangleHeight : 1);
|
trianglesNode.Texture = texture;
|
||||||
Add(sprite);
|
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)
|
||||||
|
{
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user