2019-01-24 08:43:03 +00:00
|
|
|
|
// 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 09:19:50 +00:00
|
|
|
|
|
2020-06-08 21:45:40 +00:00
|
|
|
|
using System;
|
2020-06-23 04:55:44 +00:00
|
|
|
|
using System.Collections.Generic;
|
2017-07-19 09:35:27 +00:00
|
|
|
|
using osu.Framework.Allocation;
|
2020-06-23 04:55:44 +00:00
|
|
|
|
using osu.Framework.Audio;
|
2020-06-23 12:33:03 +00:00
|
|
|
|
using osu.Framework.Audio.Track;
|
2020-11-22 15:53:27 +00:00
|
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2020-06-08 21:45:40 +00:00
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Colour;
|
|
|
|
|
using osu.Framework.Graphics.Primitives;
|
2022-07-29 13:33:34 +00:00
|
|
|
|
using osu.Framework.Graphics.Rendering;
|
2022-08-05 11:36:28 +00:00
|
|
|
|
using osu.Framework.Graphics.Rendering.Vertices;
|
2020-06-08 21:45:40 +00:00
|
|
|
|
using osu.Framework.Graphics.Shaders;
|
|
|
|
|
using osu.Framework.Graphics.Textures;
|
|
|
|
|
using osu.Framework.Utils;
|
2022-08-02 08:54:42 +00:00
|
|
|
|
using osu.Game.Beatmaps;
|
|
|
|
|
using osuTK;
|
|
|
|
|
using osuTK.Graphics;
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2017-06-19 00:33:50 +00:00
|
|
|
|
namespace osu.Game.Screens.Menu
|
|
|
|
|
{
|
2020-06-08 21:45:40 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// A visualiser that reacts to music coming from beatmaps.
|
|
|
|
|
/// </summary>
|
2020-11-22 16:34:56 +00:00
|
|
|
|
public class LogoVisualisation : Drawable
|
2017-06-19 00:33:50 +00:00
|
|
|
|
{
|
2020-06-08 21:45:40 +00: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>
|
|
|
|
|
/// How much should each bar go down each millisecond (based on a full bar).
|
|
|
|
|
/// </summary>
|
2021-12-10 05:04:31 +00:00
|
|
|
|
private const float decay_per_millisecond = 0.0024f;
|
2020-06-08 21:45:40 +00:00
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
|
2020-06-23 04:55:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// The relative movement of bars based on input amplification. Defaults to 1.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float Magnitude { get; set; } = 1;
|
|
|
|
|
|
2020-06-08 21:45:40 +00:00
|
|
|
|
private readonly float[] frequencyAmplitudes = new float[256];
|
|
|
|
|
|
2022-08-02 08:54:42 +00:00
|
|
|
|
private IShader shader = null!;
|
2022-08-05 11:33:40 +00:00
|
|
|
|
private Texture texture = null!;
|
2020-06-08 21:45:40 +00:00
|
|
|
|
|
|
|
|
|
public LogoVisualisation()
|
|
|
|
|
{
|
|
|
|
|
Blending = BlendingParameters.Additive;
|
|
|
|
|
}
|
2019-03-31 12:10:44 +00:00
|
|
|
|
|
2020-06-23 04:55:44 +00:00
|
|
|
|
private readonly List<IHasAmplitudes> amplitudeSources = new List<IHasAmplitudes>();
|
|
|
|
|
|
|
|
|
|
public void AddAmplitudeSource(IHasAmplitudes amplitudeSource)
|
|
|
|
|
{
|
|
|
|
|
amplitudeSources.Add(amplitudeSource);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-19 09:35:27 +00:00
|
|
|
|
[BackgroundDependencyLoader]
|
2022-08-05 11:33:40 +00:00
|
|
|
|
private void load(IRenderer renderer, ShaderManager shaders)
|
2020-06-08 21:45:40 +00:00
|
|
|
|
{
|
2022-08-02 10:50:57 +00:00
|
|
|
|
texture = renderer.WhitePixel;
|
2020-06-08 21:45:40 +00:00
|
|
|
|
shader = shaders.Load(VertexShaderDescriptor.TEXTURE_2, FragmentShaderDescriptor.TEXTURE_ROUNDED);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 01:22:30 +00:00
|
|
|
|
private readonly float[] temporalAmplitudes = new float[ChannelAmplitudes.AMPLITUDES_SIZE];
|
2020-06-23 04:55:44 +00:00
|
|
|
|
|
2022-08-02 08:54:42 +00:00
|
|
|
|
[Resolved]
|
|
|
|
|
private IBeatSyncProvider beatSyncProvider { get; set; } = null!;
|
|
|
|
|
|
2020-06-08 21:45:40 +00:00
|
|
|
|
private void updateAmplitudes()
|
|
|
|
|
{
|
2020-06-23 04:55:44 +00:00
|
|
|
|
for (int i = 0; i < temporalAmplitudes.Length; i++)
|
|
|
|
|
temporalAmplitudes[i] = 0;
|
2020-06-08 21:45:40 +00:00
|
|
|
|
|
2022-08-02 08:54:42 +00:00
|
|
|
|
if (beatSyncProvider.Clock != null)
|
|
|
|
|
addAmplitudesFromSource(beatSyncProvider);
|
2020-06-23 04:55:44 +00:00
|
|
|
|
|
|
|
|
|
foreach (var source in amplitudeSources)
|
|
|
|
|
addAmplitudesFromSource(source);
|
2020-06-08 21:45:40 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < bars_per_visualiser; i++)
|
|
|
|
|
{
|
2022-08-03 11:07:54 +00:00
|
|
|
|
float targetAmplitude = (temporalAmplitudes[(i + indexOffset) % bars_per_visualiser]) * (beatSyncProvider.CheckIsKiaiTime() ? 1 : 0.5f);
|
2020-06-23 04:55:44 +00:00
|
|
|
|
if (targetAmplitude > frequencyAmplitudes[i])
|
|
|
|
|
frequencyAmplitudes[i] = targetAmplitude;
|
2020-06-08 21:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
indexOffset = (indexOffset + index_change) % bars_per_visualiser;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
|
{
|
|
|
|
|
base.LoadComplete();
|
|
|
|
|
|
|
|
|
|
var delayed = Scheduler.AddDelayed(updateAmplitudes, time_between_updates, true);
|
|
|
|
|
delayed.PerformRepeatCatchUpExecutions = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
2017-06-19 00:33:50 +00:00
|
|
|
|
{
|
2020-06-08 21:45:40 +00:00
|
|
|
|
base.Update();
|
2019-03-31 12:10:44 +00:00
|
|
|
|
|
2021-12-10 05:04:31 +00:00
|
|
|
|
float decayFactor = (float)Time.Elapsed * decay_per_millisecond;
|
2020-06-08 21:45:40 +00: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);
|
2017-06-19 00:33:50 +00:00
|
|
|
|
}
|
2018-04-13 09:19:50 +00:00
|
|
|
|
|
2020-06-08 21:45:40 +00:00
|
|
|
|
protected override DrawNode CreateDrawNode() => new VisualisationDrawNode(this);
|
|
|
|
|
|
2022-08-02 08:54:42 +00:00
|
|
|
|
private void addAmplitudesFromSource(IHasAmplitudes source)
|
2020-06-23 04:55:44 +00:00
|
|
|
|
{
|
|
|
|
|
if (source == null) throw new ArgumentNullException(nameof(source));
|
|
|
|
|
|
2020-06-24 01:22:30 +00:00
|
|
|
|
var amplitudes = source.CurrentAmplitudes.FrequencyAmplitudes.Span;
|
2020-06-23 04:55:44 +00:00
|
|
|
|
|
|
|
|
|
for (int i = 0; i < amplitudes.Length; i++)
|
|
|
|
|
{
|
|
|
|
|
if (i < temporalAmplitudes.Length)
|
|
|
|
|
temporalAmplitudes[i] += amplitudes[i];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-08 21:45:40 +00:00
|
|
|
|
private class VisualisationDrawNode : DrawNode
|
2019-03-31 12:10:44 +00:00
|
|
|
|
{
|
2020-06-08 21:45:40 +00:00
|
|
|
|
protected new LogoVisualisation Source => (LogoVisualisation)base.Source;
|
|
|
|
|
|
2022-08-02 08:54:42 +00:00
|
|
|
|
private IShader shader = null!;
|
|
|
|
|
private Texture texture = null!;
|
2020-06-08 21:45:40 +00:00
|
|
|
|
|
|
|
|
|
// Assuming the logo is a circle, we don't need a second dimension.
|
|
|
|
|
private float size;
|
|
|
|
|
|
2020-11-23 05:27:02 +00:00
|
|
|
|
private static readonly Color4 transparent_white = Color4.White.Opacity(0.2f);
|
2020-11-22 17:37:49 +00:00
|
|
|
|
|
2022-03-26 16:39:31 +00:00
|
|
|
|
private readonly float[] audioData = new float[256];
|
2020-06-08 21:45:40 +00:00
|
|
|
|
|
2022-08-05 11:33:40 +00:00
|
|
|
|
private IVertexBatch<TexturedVertex2D>? vertexBatch;
|
2020-06-08 21:45:40 +00:00
|
|
|
|
|
|
|
|
|
public VisualisationDrawNode(LogoVisualisation source)
|
|
|
|
|
: base(source)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void ApplyState()
|
|
|
|
|
{
|
|
|
|
|
base.ApplyState();
|
|
|
|
|
|
|
|
|
|
shader = Source.shader;
|
|
|
|
|
texture = Source.texture;
|
|
|
|
|
size = Source.DrawSize.X;
|
2022-03-26 16:39:31 +00:00
|
|
|
|
|
|
|
|
|
Source.frequencyAmplitudes.AsSpan().CopyTo(audioData);
|
2020-06-08 21:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-29 13:33:34 +00:00
|
|
|
|
public override void Draw(IRenderer renderer)
|
2020-06-08 21:45:40 +00:00
|
|
|
|
{
|
2022-07-29 13:33:34 +00:00
|
|
|
|
base.Draw(renderer);
|
2020-06-08 21:45:40 +00:00
|
|
|
|
|
2022-07-29 14:32:06 +00:00
|
|
|
|
vertexBatch ??= renderer.CreateQuadBatch<TexturedVertex2D>(100, 10);
|
|
|
|
|
|
2020-06-08 21:45:40 +00:00
|
|
|
|
shader.Bind();
|
|
|
|
|
|
|
|
|
|
Vector2 inflation = DrawInfo.MatrixInverse.ExtractScale().Xy;
|
|
|
|
|
|
|
|
|
|
ColourInfo colourInfo = DrawColourInfo.Colour;
|
2020-11-23 04:35:02 +00:00
|
|
|
|
colourInfo.ApplyChild(transparent_white);
|
2020-06-08 21:45:40 +00:00
|
|
|
|
|
2022-08-02 08:54:42 +00:00
|
|
|
|
for (int j = 0; j < visualiser_rounds; j++)
|
2020-06-08 21:45:40 +00:00
|
|
|
|
{
|
2022-08-02 08:54:42 +00:00
|
|
|
|
for (int i = 0; i < bars_per_visualiser; i++)
|
2020-06-08 21:45:40 +00:00
|
|
|
|
{
|
2022-08-02 08:54:42 +00:00
|
|
|
|
if (audioData[i] < amplitude_dead_zone)
|
|
|
|
|
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);
|
|
|
|
|
// taking the cos and sin to the 0..1 range
|
|
|
|
|
var barPosition = new Vector2(rotationCos / 2 + 0.5f, rotationSin / 2 + 0.5f) * size;
|
|
|
|
|
|
|
|
|
|
var barSize = new Vector2(size * MathF.Sqrt(2 * (1 - MathF.Cos(MathUtils.DegreesToRadians(360f / bars_per_visualiser)))) / 2f, bar_length * audioData[i]);
|
|
|
|
|
// 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)
|
|
|
|
|
);
|
|
|
|
|
|
2022-08-05 11:33:40 +00:00
|
|
|
|
renderer.DrawQuad(
|
2022-08-02 08:54:42 +00:00
|
|
|
|
texture,
|
|
|
|
|
rectangle,
|
|
|
|
|
colourInfo,
|
|
|
|
|
null,
|
|
|
|
|
vertexBatch.AddAction,
|
|
|
|
|
// 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));
|
2020-06-08 21:45:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
shader.Unbind();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(isDisposing);
|
2019-04-01 04:13:56 +00:00
|
|
|
|
|
2022-07-29 14:32:06 +00:00
|
|
|
|
vertexBatch?.Dispose();
|
2020-06-08 21:45:40 +00:00
|
|
|
|
}
|
2019-03-31 12:10:44 +00:00
|
|
|
|
}
|
2017-06-19 00:33:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|