1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-24 05:42:55 +08:00
osu-lazer/osu.Game/Screens/Menu/LogoVisualisation.cs

252 lines
9.5 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Batches;
using osu.Framework.Graphics.Colour;
using osu.Framework.Graphics.OpenGL.Vertices;
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Shaders;
using osu.Framework.Graphics.Textures;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Skinning;
using osu.Game.Online.API;
using osu.Game.Users;
2018-04-13 17:19:50 +08:00
using System;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Utils;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Menu
{
public class LogoVisualisation : Drawable, IHasAccentColour
{
private readonly IBindable<WorkingBeatmap> beatmap = new Bindable<WorkingBeatmap>();
2018-04-13 17:19:50 +08:00
/// <summary>
/// The number of bars to jump each update iteration.
/// </summary>
private const int index_change = 5;
/// <summary>
/// The maximum length of each bar in the visualiser. Will be reduced when kiai is not activated.
/// </summary>
private const float bar_length = 600;
/// <summary>
/// The number of bars in one rotation of the visualiser.
/// </summary>
private const int bars_per_visualiser = 200;
/// <summary>
/// How many times we should stretch around the circumference (overlapping overselves).
/// </summary>
private const float visualiser_rounds = 5;
/// <summary>
2019-08-23 10:37:01 +08:00
/// How much should each bar go down each millisecond (based on a full bar).
2018-04-13 17:19:50 +08:00
/// </summary>
private const float decay_per_milisecond = 0.0024f;
/// <summary>
/// Number of milliseconds between each amplitude update.
/// </summary>
private const float time_between_updates = 50;
/// <summary>
/// The minimum amplitude to show a bar.
/// </summary>
private const float amplitude_dead_zone = 1f / bar_length;
private int indexOffset;
public Color4 AccentColour { get; set; }
private readonly float[] frequencyAmplitudes = new float[256];
2019-03-07 17:30:18 +08:00
private IShader shader;
2018-04-13 17:19:50 +08:00
private readonly Texture texture;
private Bindable<User> user;
private Bindable<Skin> skin;
2018-04-13 17:19:50 +08:00
public LogoVisualisation()
{
texture = Texture.WhitePixel;
2019-08-21 12:29:50 +08:00
Blending = BlendingParameters.Additive;
2018-04-13 17:19:50 +08:00
}
[BackgroundDependencyLoader]
private void load(ShaderManager shaders, IBindable<WorkingBeatmap> beatmap, IAPIProvider api, SkinManager skinManager)
2018-04-13 17:19:50 +08:00
{
this.beatmap.BindTo(beatmap);
2018-04-13 17:19:50 +08:00
shader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED);
user = api.LocalUser.GetBoundCopy();
skin = skinManager.CurrentSkin.GetBoundCopy();
user.ValueChanged += _ => updateColour();
skin.BindValueChanged(_ => updateColour(), true);
2018-04-13 17:19:50 +08:00
}
private void updateAmplitudes()
{
var track = beatmap.Value.TrackLoaded ? beatmap.Value.Track : null;
var effect = beatmap.Value.BeatmapLoaded ? beatmap.Value.Beatmap.ControlPointInfo.EffectPointAt(track?.CurrentTime ?? Time.Current) : null;
float[] temporalAmplitudes = track?.CurrentAmplitudes.FrequencyAmplitudes;
2018-04-13 17:19:50 +08:00
for (int i = 0; i < bars_per_visualiser; i++)
{
if (track?.IsRunning ?? false)
{
float targetAmplitude = (temporalAmplitudes?[(i + indexOffset) % bars_per_visualiser] ?? 0) * (effect?.KiaiMode == true ? 1 : 0.5f);
2018-04-13 17:19:50 +08:00
if (targetAmplitude > frequencyAmplitudes[i])
frequencyAmplitudes[i] = targetAmplitude;
}
else
{
int index = (i + index_change) % bars_per_visualiser;
if (frequencyAmplitudes[index] > frequencyAmplitudes[i])
frequencyAmplitudes[i] = frequencyAmplitudes[index];
}
}
indexOffset = (indexOffset + index_change) % bars_per_visualiser;
}
private void updateColour()
{
Color4 defaultColour = Color4.White.Opacity(0.2f);
if (user.Value?.IsSupporter ?? false)
AccentColour = skin.Value.GetConfig<GlobalSkinColour, Color4>(GlobalSkinColour.MenuGlow)?.Value ?? defaultColour;
else
AccentColour = defaultColour;
}
2018-04-13 17:19:50 +08:00
protected override void LoadComplete()
{
base.LoadComplete();
var delayed = Scheduler.AddDelayed(updateAmplitudes, time_between_updates, true);
delayed.PerformRepeatCatchUpExecutions = false;
2018-04-13 17:19:50 +08:00
}
protected override void Update()
{
base.Update();
float decayFactor = (float)Time.Elapsed * decay_per_milisecond;
2019-04-01 11:16:05 +08:00
2018-04-13 17:19:50 +08:00
for (int i = 0; i < bars_per_visualiser; i++)
{
//3% of extra bar length to make it a little faster when bar is almost at it's minimum
frequencyAmplitudes[i] -= decayFactor * (frequencyAmplitudes[i] + 0.03f);
if (frequencyAmplitudes[i] < 0)
frequencyAmplitudes[i] = 0;
}
Invalidate(Invalidation.DrawNode, shallPropagate: false);
}
2019-04-02 10:56:22 +08:00
protected override DrawNode CreateDrawNode() => new VisualisationDrawNode(this);
2018-04-13 17:19:50 +08:00
private class VisualisationDrawNode : DrawNode
{
2019-04-02 10:56:22 +08:00
protected new LogoVisualisation Source => (LogoVisualisation)base.Source;
private IShader shader;
private Texture texture;
2019-02-28 12:31:40 +08:00
2019-08-23 10:37:01 +08:00
//Assuming the logo is a circle, we don't need a second dimension.
2019-04-02 10:56:22 +08:00
private float size;
2018-04-13 17:19:50 +08:00
2019-04-02 10:56:22 +08:00
private Color4 colour;
private float[] audioData;
2018-04-13 17:19:50 +08:00
private readonly QuadBatch<TexturedVertex2D> vertexBatch = new QuadBatch<TexturedVertex2D>(100, 10);
2019-04-02 10:56:22 +08:00
public VisualisationDrawNode(LogoVisualisation source)
: base(source)
{
}
public override void ApplyState()
{
base.ApplyState();
shader = Source.shader;
texture = Source.texture;
size = Source.DrawSize.X;
colour = Source.AccentColour;
audioData = Source.frequencyAmplitudes;
}
2018-04-13 17:19:50 +08:00
public override void Draw(Action<TexturedVertex2D> vertexAction)
{
base.Draw(vertexAction);
2019-04-02 10:56:22 +08:00
shader.Bind();
2018-04-13 17:19:50 +08:00
Vector2 inflation = DrawInfo.MatrixInverse.ExtractScale().Xy;
2018-09-06 17:02:04 +08:00
ColourInfo colourInfo = DrawColourInfo.Colour;
2019-04-02 10:56:22 +08:00
colourInfo.ApplyChild(colour);
2018-04-13 17:19:50 +08:00
2019-04-02 10:56:22 +08:00
if (audioData != null)
2018-04-13 17:19:50 +08:00
{
for (int j = 0; j < visualiser_rounds; j++)
{
for (int i = 0; i < bars_per_visualiser; i++)
{
2019-04-02 10:56:22 +08:00
if (audioData[i] < amplitude_dead_zone)
2018-04-13 17:19:50 +08:00
continue;
float rotation = MathUtils.DegreesToRadians(i / (float)bars_per_visualiser * 360 + j * 360 / visualiser_rounds);
float rotationCos = MathF.Cos(rotation);
float rotationSin = MathF.Sin(rotation);
2018-04-13 17:19:50 +08:00
//taking the cos and sin to the 0..1 range
2019-04-02 10:56:22 +08:00
var barPosition = new Vector2(rotationCos / 2 + 0.5f, rotationSin / 2 + 0.5f) * size;
2018-04-13 17:19:50 +08:00
var barSize = new Vector2(size * MathF.Sqrt(2 * (1 - MathF.Cos(MathUtils.DegreesToRadians(360f / bars_per_visualiser)))) / 2f, bar_length * audioData[i]);
2018-04-13 17:19:50 +08:00
//The distance between the position and the sides of the bar.
var bottomOffset = new Vector2(-rotationSin * barSize.X / 2, rotationCos * barSize.X / 2);
//The distance between the bottom side of the bar and the top side.
var amplitudeOffset = new Vector2(rotationCos * barSize.Y, rotationSin * barSize.Y);
var rectangle = new Quad(
Vector2Extensions.Transform(barPosition - bottomOffset, DrawInfo.Matrix),
Vector2Extensions.Transform(barPosition - bottomOffset + amplitudeOffset, DrawInfo.Matrix),
Vector2Extensions.Transform(barPosition + bottomOffset, DrawInfo.Matrix),
Vector2Extensions.Transform(barPosition + bottomOffset + amplitudeOffset, DrawInfo.Matrix)
);
2019-06-06 15:33:14 +08:00
DrawQuad(
texture,
2018-04-13 17:19:50 +08:00
rectangle,
colourInfo,
null,
vertexBatch.AddAction,
2018-04-13 17:19:50 +08:00
//barSize by itself will make it smooth more in the X axis than in the Y axis, this reverts that.
Vector2.Divide(inflation, barSize.Yx));
}
}
}
2019-02-28 12:31:40 +08:00
2019-04-02 10:56:22 +08:00
shader.Unbind();
2018-04-13 17:19:50 +08:00
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
vertexBatch.Dispose();
}
2018-04-13 17:19:50 +08:00
}
}
}