1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 09:32:55 +08:00

Remove texture from TrianglesV2

This commit is contained in:
Andrei Zavatski 2022-11-30 04:09:46 +03:00
parent c3b5b19c32
commit 3b13ca1167
3 changed files with 30 additions and 65 deletions

View File

@ -7,6 +7,7 @@ using osu.Framework.Graphics.Shapes;
using osuTK;
using osuTK.Graphics;
using osu.Game.Graphics.Backgrounds;
using osu.Framework.Graphics.Colour;
namespace osu.Game.Tests.Visual.Background
{
@ -42,8 +43,7 @@ namespace osu.Game.Tests.Visual.Background
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
ColourTop = Color4.White,
ColourBottom = Color4.Red
Colour = ColourInfo.GradientVertical(Color4.White, Color4.Red)
}
}
}

View File

@ -11,9 +11,7 @@ using osu.Framework.Allocation;
using System.Collections.Generic;
using osu.Framework.Graphics.Rendering;
using osu.Framework.Graphics.Rendering.Vertices;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp;
using osuTK.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
@ -23,28 +21,12 @@ namespace osu.Game.Graphics.Backgrounds
{
private const float triangle_size = 100;
private const float base_velocity = 50;
private const int texture_height = 128;
/// <summary>
/// sqrt(3) / 2
/// </summary>
private const float equilateral_triangle_ratio = 0.866f;
private readonly Bindable<Color4> colourTop = new Bindable<Color4>(Color4.White);
private readonly Bindable<Color4> colourBottom = new Bindable<Color4>(Color4.Black);
public Color4 ColourTop
{
get => colourTop.Value;
set => colourTop.Value = value;
}
public Color4 ColourBottom
{
get => colourBottom.Value;
set => colourBottom.Value = value;
}
public float Thickness { get; set; } = 0.02f; // No need for invalidation since it's happening in Update()
/// <summary>
@ -89,42 +71,19 @@ namespace osu.Game.Graphics.Backgrounds
}
[BackgroundDependencyLoader]
private void load(ShaderManager shaders)
private void load(ShaderManager shaders, IRenderer renderer)
{
shader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, "TriangleBorder");
texture = renderer.WhitePixel;
}
protected override void LoadComplete()
{
base.LoadComplete();
colourTop.BindValueChanged(_ => updateTexture());
colourBottom.BindValueChanged(_ => updateTexture(), true);
spawnRatio.BindValueChanged(_ => Reset(), true);
}
private void updateTexture()
{
var image = new Image<Rgba32>(texture_height, 1);
texture = renderer.CreateTexture(1, texture_height, true);
for (int i = 0; i < texture_height; i++)
{
float ratio = (float)i / texture_height;
image[i, 0] = new Rgba32(
colourBottom.Value.R * ratio + colourTop.Value.R * (1f - ratio),
colourBottom.Value.G * ratio + colourTop.Value.G * (1f - ratio),
colourBottom.Value.B * ratio + colourTop.Value.B * (1f - ratio)
);
}
texture.SetData(new TextureUpload(image));
Invalidate(Invalidation.DrawNode);
}
protected override void Update()
{
base.Update();
@ -280,36 +239,42 @@ namespace osu.Game.Graphics.Backgrounds
shader.GetUniform<float>("thickness").UpdateValue(ref thickness);
shader.GetUniform<float>("texelSize").UpdateValue(ref texelSize);
float texturePartWidth = triangleSize.X / size.X;
float texturePartHeight = triangleSize.Y / size.Y * texture_height;
float relativeHeight = triangleSize.Y / size.Y;
float relativeWidth = triangleSize.X / size.X;
foreach (TriangleParticle particle in parts)
{
Vector2 topLeft = particle.Position * size - new Vector2(triangleSize.X * 0.5f, 0f);
Vector2 topRight = topLeft + new Vector2(triangleSize.X, 0f);
Vector2 bottomLeft = topLeft + new Vector2(0f, triangleSize.Y);
Vector2 bottomRight = topLeft + triangleSize;
Vector2 topLeft = particle.Position - new Vector2(relativeWidth * 0.5f, 0f);
Vector2 topRight = topLeft + new Vector2(relativeWidth, 0f);
Vector2 bottomLeft = topLeft + new Vector2(0f, relativeHeight);
Vector2 bottomRight = bottomLeft + new Vector2(relativeWidth, 0f);
var drawQuad = new Quad(
Vector2Extensions.Transform(topLeft, DrawInfo.Matrix),
Vector2Extensions.Transform(topRight, DrawInfo.Matrix),
Vector2Extensions.Transform(bottomLeft, DrawInfo.Matrix),
Vector2Extensions.Transform(bottomRight, DrawInfo.Matrix)
Vector2Extensions.Transform(topLeft * size, DrawInfo.Matrix),
Vector2Extensions.Transform(topRight * size, DrawInfo.Matrix),
Vector2Extensions.Transform(bottomLeft * size, DrawInfo.Matrix),
Vector2Extensions.Transform(bottomRight * size, DrawInfo.Matrix)
);
var tRect = new Quad(
topLeft.X / size.X,
topLeft.Y / size.Y * texture_height,
texturePartWidth,
texturePartHeight
).AABBFloat;
ColourInfo colourInfo = triangleColourInfo(DrawColourInfo.Colour, new Quad(topLeft, topRight, bottomLeft, bottomRight));
renderer.DrawQuad(texture, drawQuad, DrawColourInfo.Colour, tRect, vertexBatch.AddAction, textureCoords: tRect);
renderer.DrawQuad(texture, drawQuad, colourInfo, vertexAction: vertexBatch.AddAction);
}
shader.Unbind();
}
private static ColourInfo triangleColourInfo(ColourInfo source, Quad quad)
{
return new ColourInfo
{
TopLeft = source.Interpolate(quad.TopLeft),
TopRight = source.Interpolate(quad.TopRight),
BottomLeft = source.Interpolate(quad.BottomLeft),
BottomRight = source.Interpolate(quad.BottomRight)
};
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);

View File

@ -6,6 +6,7 @@ using System.Diagnostics;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Framework.Localisation;
@ -79,8 +80,7 @@ namespace osu.Game.Graphics.UserInterfaceV2
Debug.Assert(triangleGradientSecondColour != null);
Triangles.ColourTop = triangleGradientSecondColour.Value;
Triangles.ColourBottom = BackgroundColour;
Triangles.Colour = ColourInfo.GradientVertical(triangleGradientSecondColour.Value, BackgroundColour);
}
protected override bool OnHover(HoverEvent e)