2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-06-19 08:33:50 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
using OpenTK.Graphics.ES30;
|
|
|
|
|
using osu.Framework.Configuration;
|
|
|
|
|
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 System;
|
2017-07-19 17:35:27 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
2017-06-19 08:33:50 +08:00
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Menu
|
|
|
|
|
{
|
2017-11-03 16:54:35 +08:00
|
|
|
|
public class LogoVisualisation : Drawable, IHasAccentColour
|
2017-06-19 08:33:50 +08:00
|
|
|
|
{
|
2017-06-19 11:01:07 +08:00
|
|
|
|
private readonly Bindable<WorkingBeatmap> beatmap = new Bindable<WorkingBeatmap>();
|
2017-06-19 08:33:50 +08:00
|
|
|
|
|
2017-06-19 17:41:11 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The number of bars to jump each update iteration.
|
|
|
|
|
/// </summary>
|
2017-06-19 08:33:50 +08:00
|
|
|
|
private const int index_change = 5;
|
2017-06-19 17:41:11 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The maximum length of each bar in the visualiser. Will be reduced when kiai is not activated.
|
|
|
|
|
/// </summary>
|
2017-06-19 17:30:13 +08:00
|
|
|
|
private const float bar_length = 600;
|
2017-06-19 17:41:11 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The number of bars in one rotation of the visualiser.
|
|
|
|
|
/// </summary>
|
2017-06-19 17:41:35 +08:00
|
|
|
|
private const int bars_per_visualiser = 200;
|
2017-06-19 17:41:11 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// How many times we should stretch around the circumference (overlapping overselves).
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const float visualiser_rounds = 5;
|
2017-06-19 08:33:50 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-06-22 07:32:31 +08:00
|
|
|
|
/// How much should each bar go down each milisecond (based on a full bar).
|
2017-06-19 08:33:50 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
private const float decay_per_milisecond = 0.0024f;
|
|
|
|
|
|
2017-06-19 17:41:11 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Number of milliseconds between each amplitude update.
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const float time_between_updates = 50;
|
|
|
|
|
|
2017-06-22 07:32:31 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The minimum amplitude to show a bar.
|
|
|
|
|
/// </summary>
|
2017-06-22 07:40:35 +08:00
|
|
|
|
private const float amplitude_dead_zone = 1f / bar_length;
|
2017-06-22 07:32:31 +08:00
|
|
|
|
|
2017-06-19 08:33:50 +08:00
|
|
|
|
private int indexOffset;
|
|
|
|
|
|
2017-06-19 17:38:39 +08:00
|
|
|
|
public Color4 AccentColour { get; set; }
|
2017-06-19 08:33:50 +08:00
|
|
|
|
|
2017-06-19 11:01:07 +08:00
|
|
|
|
private readonly float[] frequencyAmplitudes = new float[256];
|
2017-06-19 08:33:50 +08:00
|
|
|
|
|
2018-01-08 04:40:00 +08:00
|
|
|
|
public override bool HandleKeyboardInput => false;
|
|
|
|
|
public override bool HandleMouseInput => false;
|
2017-06-19 08:33:50 +08:00
|
|
|
|
|
|
|
|
|
private Shader shader;
|
|
|
|
|
private readonly Texture texture;
|
|
|
|
|
|
|
|
|
|
public LogoVisualisation()
|
|
|
|
|
{
|
|
|
|
|
texture = Texture.WhitePixel;
|
2017-06-19 17:30:13 +08:00
|
|
|
|
AccentColour = new Color4(1, 1, 1, 0.2f);
|
2017-09-07 21:46:21 +08:00
|
|
|
|
Blending = BlendingMode.Additive;
|
2017-06-19 08:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-19 17:35:27 +08:00
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(ShaderManager shaders, OsuGameBase game)
|
2017-06-19 08:33:50 +08:00
|
|
|
|
{
|
2017-07-19 17:35:27 +08:00
|
|
|
|
beatmap.BindTo(game.Beatmap);
|
|
|
|
|
shader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED);
|
2017-06-19 08:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 17:41:11 +08:00
|
|
|
|
private void updateAmplitudes()
|
2017-06-19 08:33:50 +08:00
|
|
|
|
{
|
2017-11-21 11:40:05 +08:00
|
|
|
|
var track = beatmap.Value.TrackLoaded ? beatmap.Value.Track : null;
|
2017-11-21 18:47:12 +08:00
|
|
|
|
var effect = beatmap.Value.BeatmapLoaded ? beatmap.Value.Beatmap.ControlPointInfo.EffectPointAt(track?.CurrentTime ?? Time.Current) : null;
|
2017-06-19 17:29:44 +08:00
|
|
|
|
|
2017-07-19 14:45:23 +08:00
|
|
|
|
float[] temporalAmplitudes = track?.CurrentAmplitudes.FrequencyAmplitudes ?? new float[256];
|
|
|
|
|
|
2017-06-19 17:41:35 +08:00
|
|
|
|
for (int i = 0; i < bars_per_visualiser; i++)
|
2017-06-19 08:33:50 +08:00
|
|
|
|
{
|
2017-07-19 14:45:23 +08:00
|
|
|
|
if (track?.IsRunning ?? false)
|
2017-06-19 08:33:50 +08:00
|
|
|
|
{
|
2017-06-22 07:32:31 +08:00
|
|
|
|
float targetAmplitude = temporalAmplitudes[(i + indexOffset) % bars_per_visualiser] * (effect?.KiaiMode == true ? 1 : 0.5f);
|
|
|
|
|
if (targetAmplitude > frequencyAmplitudes[i])
|
|
|
|
|
frequencyAmplitudes[i] = targetAmplitude;
|
2017-06-19 08:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2017-06-22 07:32:31 +08:00
|
|
|
|
int index = (i + index_change) % bars_per_visualiser;
|
|
|
|
|
if (frequencyAmplitudes[index] > frequencyAmplitudes[i])
|
|
|
|
|
frequencyAmplitudes[i] = frequencyAmplitudes[index];
|
2017-06-19 08:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-19 17:41:11 +08:00
|
|
|
|
|
2017-06-19 17:41:35 +08:00
|
|
|
|
indexOffset = (indexOffset + index_change) % bars_per_visualiser;
|
2017-06-19 17:41:11 +08:00
|
|
|
|
Scheduler.AddDelayed(updateAmplitudes, time_between_updates);
|
2017-06-19 08:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
2017-06-19 17:41:11 +08:00
|
|
|
|
updateAmplitudes();
|
2017-06-19 08:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
|
|
float decayFactor = (float)Time.Elapsed * decay_per_milisecond;
|
2017-06-19 17:41:35 +08:00
|
|
|
|
for (int i = 0; i < bars_per_visualiser; i++)
|
2017-06-19 08:33:50 +08:00
|
|
|
|
{
|
2017-06-22 07:32:31 +08:00
|
|
|
|
//3% of extra bar length to make it a little faster when bar is almost at it's minimum
|
2017-06-19 11:01:07 +08:00
|
|
|
|
frequencyAmplitudes[i] -= decayFactor * (frequencyAmplitudes[i] + 0.03f);
|
|
|
|
|
if (frequencyAmplitudes[i] < 0)
|
|
|
|
|
frequencyAmplitudes[i] = 0;
|
2017-06-19 08:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Invalidate(Invalidation.DrawNode, shallPropagate: false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override DrawNode CreateDrawNode() => new VisualisationDrawNode();
|
|
|
|
|
|
2017-06-19 17:41:35 +08:00
|
|
|
|
private readonly VisualiserSharedData sharedData = new VisualiserSharedData();
|
2017-06-22 07:32:31 +08:00
|
|
|
|
|
2017-06-19 08:33:50 +08:00
|
|
|
|
protected override void ApplyDrawNode(DrawNode node)
|
|
|
|
|
{
|
|
|
|
|
base.ApplyDrawNode(node);
|
|
|
|
|
|
|
|
|
|
var visNode = (VisualisationDrawNode)node;
|
|
|
|
|
|
|
|
|
|
visNode.Shader = shader;
|
|
|
|
|
visNode.Texture = texture;
|
|
|
|
|
visNode.Size = DrawSize.X;
|
|
|
|
|
visNode.Shared = sharedData;
|
2017-06-19 17:38:39 +08:00
|
|
|
|
visNode.Colour = AccentColour;
|
2017-06-19 11:01:07 +08:00
|
|
|
|
visNode.AudioData = frequencyAmplitudes;
|
2017-06-19 08:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
2017-06-19 17:41:35 +08:00
|
|
|
|
private class VisualiserSharedData
|
2017-06-19 08:33:50 +08:00
|
|
|
|
{
|
|
|
|
|
public readonly LinearBatch<TexturedVertex2D> VertexBatch = new LinearBatch<TexturedVertex2D>(100 * 4, 10, PrimitiveType.Quads);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class VisualisationDrawNode : DrawNode
|
|
|
|
|
{
|
|
|
|
|
public Shader Shader;
|
|
|
|
|
public Texture Texture;
|
2017-06-19 17:41:35 +08:00
|
|
|
|
public VisualiserSharedData Shared;
|
2017-06-19 08:33:50 +08:00
|
|
|
|
//Asuming the logo is a circle, we don't need a second dimension.
|
|
|
|
|
public float Size;
|
|
|
|
|
|
|
|
|
|
public Color4 Colour;
|
|
|
|
|
public float[] AudioData;
|
|
|
|
|
|
|
|
|
|
public override void Draw(Action<TexturedVertex2D> vertexAction)
|
|
|
|
|
{
|
|
|
|
|
base.Draw(vertexAction);
|
|
|
|
|
|
|
|
|
|
Shader.Bind();
|
|
|
|
|
Texture.TextureGL.Bind();
|
|
|
|
|
|
|
|
|
|
Vector2 inflation = DrawInfo.MatrixInverse.ExtractScale().Xy;
|
|
|
|
|
|
|
|
|
|
ColourInfo colourInfo = DrawInfo.Colour;
|
|
|
|
|
colourInfo.ApplyChild(Colour);
|
|
|
|
|
|
|
|
|
|
if (AudioData != null)
|
|
|
|
|
{
|
2017-06-19 17:41:11 +08:00
|
|
|
|
for (int j = 0; j < visualiser_rounds; j++)
|
2017-06-19 08:33:50 +08:00
|
|
|
|
{
|
2017-06-19 17:41:35 +08:00
|
|
|
|
for (int i = 0; i < bars_per_visualiser; i++)
|
2017-06-19 12:52:42 +08:00
|
|
|
|
{
|
2017-06-22 07:40:35 +08:00
|
|
|
|
if (AudioData[i] < amplitude_dead_zone)
|
2017-06-22 07:32:31 +08:00
|
|
|
|
continue;
|
|
|
|
|
|
2017-06-19 17:41:35 +08:00
|
|
|
|
float rotation = MathHelper.DegreesToRadians(i / (float)bars_per_visualiser * 360 + j * 360 / visualiser_rounds);
|
2017-06-19 12:52:42 +08:00
|
|
|
|
float rotationCos = (float)Math.Cos(rotation);
|
|
|
|
|
float rotationSin = (float)Math.Sin(rotation);
|
|
|
|
|
//taking the cos and sin to the 0..1 range
|
|
|
|
|
var barPosition = new Vector2(rotationCos / 2 + 0.5f, rotationSin / 2 + 0.5f) * Size;
|
|
|
|
|
|
2017-06-22 07:32:31 +08:00
|
|
|
|
var barSize = new Vector2(Size * (float)Math.Sqrt(2 * (1 - Math.Cos(MathHelper.DegreesToRadians(360f / bars_per_visualiser)))) / 2f, bar_length * AudioData[i]);
|
2017-06-19 12:52:42 +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(
|
2017-09-13 19:25:40 +08:00
|
|
|
|
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)
|
2017-06-19 12:52:42 +08:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
Texture.DrawQuad(
|
|
|
|
|
rectangle,
|
|
|
|
|
colourInfo,
|
|
|
|
|
null,
|
|
|
|
|
Shared.VertexBatch.Add,
|
|
|
|
|
//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));
|
|
|
|
|
}
|
2017-06-19 08:33:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Shader.Unbind();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|