73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
using UnityEngine;
|
|
using UnityEngine.VFX;
|
|
using UnityEngine.VFX.Utility;
|
|
using Assets.WasapiAudio.Scripts.Unity;
|
|
|
|
[AddComponentMenu("VFX/Property Binders/Kaiju/VFX Spectrum Binder")]
|
|
[VFXBinder("Kaiju/Kaiju - VFX Spectrum Binder")]
|
|
public class VfxSpectrumBinder : VfxAudioVisualizationEffect {
|
|
[VFXPropertyBinding("System.UInt32"), SerializeField]
|
|
protected ExposedProperty SpectrumSizeParameter = "SpectrumSize";
|
|
|
|
[VFXPropertyBinding("UnityEngine.Texture2D"), SerializeField]
|
|
protected ExposedProperty SpectrumDataTextureParameter = "SpectrumDataTexture";
|
|
|
|
public bool symmetric = false;
|
|
|
|
private Texture2D _texture;
|
|
private Color[] _colorCache;
|
|
|
|
private void InitialiseTexture() {
|
|
Debug.Log("Reinitialising spectrum texture...");
|
|
if (symmetric) {
|
|
_texture = new Texture2D(SpectrumSize * 2, 1, TextureFormat.RFloat, false);
|
|
_colorCache = new Color[SpectrumSize * 2];
|
|
} else {
|
|
_texture = new Texture2D(SpectrumSize, 1, TextureFormat.RFloat, false);
|
|
_colorCache = new Color[SpectrumSize];
|
|
}
|
|
|
|
_texture.name = $"AudioSpectrumData{SpectrumSize}";
|
|
}
|
|
|
|
private void UpdateTexture() {
|
|
// init
|
|
if (_texture == null) InitialiseTexture();
|
|
|
|
// has symmetric been toggled and the texture needs updating?
|
|
if (symmetric && _texture.width != SpectrumSize * 2) InitialiseTexture();
|
|
if (!symmetric && _texture.width != SpectrumSize) InitialiseTexture();
|
|
|
|
var spectrumData = GetSpectrumData();
|
|
|
|
if (symmetric) {
|
|
for (int i = 0; i < SpectrumSize; i++) {
|
|
_colorCache[SpectrumSize + i] = new Color(spectrumData[i], 0, 0, 0);
|
|
_colorCache[SpectrumSize - (i + 1)] = new Color(spectrumData[i], 0, 0, 0);
|
|
}
|
|
} else {
|
|
for (int i = 0; i < SpectrumSize; i++) {
|
|
_colorCache[i] = new Color(spectrumData[i], 0, 0, 0);
|
|
}
|
|
}
|
|
|
|
_texture.SetPixels(_colorCache);
|
|
_texture.Apply();
|
|
}
|
|
|
|
public override void UpdateBinding(VisualEffect component) {
|
|
base.UpdateBinding(component);
|
|
UpdateTexture();
|
|
if (symmetric) {
|
|
component.SetUInt(SpectrumSizeParameter, (uint) SpectrumSize * 2);
|
|
} else {
|
|
component.SetUInt(SpectrumSizeParameter, (uint) SpectrumSize);
|
|
}
|
|
component.SetTexture(SpectrumDataTextureParameter, _texture);
|
|
}
|
|
|
|
public override string ToString() {
|
|
return "Kaiju VFX Spectrum Binder";
|
|
}
|
|
}
|