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

View File

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

View File

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