lucid-super-dream/Assets/Scripts/Rendering/AudioVisualizer.cs

95 lines
3.3 KiB
C#
Raw Normal View History

2021-01-07 03:00:17 +01:00
using System;
using System.Runtime.InteropServices;
2021-01-09 06:32:47 +01:00
using FMOD;
using Ktyl.Util;
2021-01-07 03:00:17 +01:00
using UnityEngine;
2021-01-09 06:32:47 +01:00
using INITFLAGS = FMOD.Studio.INITFLAGS;
2021-01-07 03:00:17 +01:00
2021-01-09 06:32:47 +01:00
[CreateAssetMenu(menuName = "Audio Visualizer Settings")]
public class AudioVisualizer : ScriptableObject
2021-01-07 03:00:17 +01:00
{
2021-01-09 06:32:47 +01:00
[Serializable]
private struct ShaderPropertyAnimation
{
public string name;
public float multiplier;
[UnityEngine.Range(0, 5)]
public int channel;
[UnityEngine.Range(0, 2048)]
public int sample;
public float Initial { get; set; }
}
2021-01-07 06:44:42 +01:00
[SerializeField] private SerialFloat _distanceToNextBeat;
[SerializeField] private SerialFloat _distanceSinceLastBeat;
2021-01-09 06:32:47 +01:00
[SerializeField] private ShaderPropertyAnimation[] _fractalAnimations;
[SerializeField] private ShaderPropertyAnimation[] _tunnelAnimations;
[SerializeField] private ShaderPropertyAnimation[] _shipAnimations;
[SerializeField] private AccessibilityOptions _accessibility;
2021-01-07 03:00:17 +01:00
2021-01-09 06:32:47 +01:00
public void Initialise(Material fractal, Material tunnel, Material ship)
{
// _fractal = fractal;
// _tunnel = tunnel;
// _ship = ship;
InitialiseAnimations(_fractalAnimations, fractal);
InitialiseAnimations(_tunnelAnimations, tunnel);
InitialiseAnimations(_shipAnimations, ship);
}
2021-01-07 03:00:17 +01:00
2021-01-09 06:32:47 +01:00
private void InitialiseAnimations(ShaderPropertyAnimation[] animations, Material material)
{
for (int i = 0; i < animations.Length; i++)
{
var anim = animations[i];
anim.Initial = material.GetFloat(anim.name);
animations[i] = anim;
}
}
2021-01-07 06:44:42 +01:00
2021-01-10 06:55:45 +01:00
public void UpdateAudio(DSP fft, Renderer fractal, Renderer tunnel, Renderer ship)
2021-01-07 03:00:17 +01:00
{
// update beat-based things
Shader.SetGlobalFloat("_DistanceSinceLastBeat", _distanceSinceLastBeat);
Shader.SetGlobalFloat("_DistanceToNextBeat", _distanceToNextBeat);
2021-01-09 06:32:47 +01:00
fft.getParameterData((int) FMOD.DSP_FFT.SPECTRUMDATA, out var unmanagedData, out var length);
var fftData = (FMOD.DSP_PARAMETER_FFT) Marshal.PtrToStructure(unmanagedData, typeof(FMOD.DSP_PARAMETER_FFT));
var spectrum = fftData.spectrum;
var l = spectrum.Length;
if (l == 0) return;
2021-01-07 03:00:17 +01:00
2021-01-09 06:32:47 +01:00
for (int i = 0; i < _fractalAnimations.Length; i++)
{
var anim = _fractalAnimations[i];
var a = spectrum[anim.channel][anim.sample];
fractal.material.SetFloat(anim.name, anim.Initial + anim.multiplier * a);
}
for (int i = 0; i < _tunnelAnimations.Length; i++)
{
var anim = _tunnelAnimations[i];
var a = spectrum[anim.channel][anim.sample];
tunnel.material.SetFloat(anim.name, anim.Initial + anim.multiplier * a);
}
2021-01-07 06:44:42 +01:00
2021-01-09 06:32:47 +01:00
UpdateAnimations(spectrum, _shipAnimations, ship);
2021-01-07 03:00:17 +01:00
}
2021-01-09 06:32:47 +01:00
private void UpdateAnimations(float[][] spectrum, ShaderPropertyAnimation[] animations, Renderer renderer)
2021-01-07 03:00:17 +01:00
{
2021-01-09 06:32:47 +01:00
for (int i = 0; i < animations.Length; i++)
{
var anim = animations[i];
var a = spectrum[anim.channel][anim.sample];
renderer.material.SetFloat(anim.name, anim.Initial + anim.multiplier * a);
}
2021-01-07 03:00:17 +01:00
}
2021-01-09 06:32:47 +01:00
}