102 lines
3.4 KiB
C#
102 lines
3.4 KiB
C#
using Assets.WasapiAudio.Scripts.Unity;
|
|
using System.Collections.Generic;
|
|
using UnityEngine.VFX.Utility;
|
|
using UnityEngine.VFX;
|
|
using UnityEngine;
|
|
|
|
[AddComponentMenu("VFX/Property Binders/Kaiju/VFX Spectrum Binder")]
|
|
[VFXBinder("Kaiju/Kaiju - VFX Spectrum Binder")]
|
|
public class VfxSpectrumBinder : VFXBinderBase {
|
|
[SerializeReference]
|
|
[SerializeReferenceButton]
|
|
public List<SpectrumTransformer> Transformers = new List<SpectrumTransformer>();
|
|
|
|
[SpectrumDataPreview]
|
|
public SpectrumData Preview;
|
|
|
|
public AudioSource source;
|
|
public bool symmetric = false;
|
|
|
|
private Texture2D _texture;
|
|
private Color[] _colorCache;
|
|
|
|
private void InitialiseTexture() {
|
|
Debug.Log("Reinitialising spectrum texture...");
|
|
if (symmetric) {
|
|
_texture = new Texture2D(source.SpectrumSize * 2, 1, TextureFormat.RFloat, false);
|
|
_colorCache = new Color[source.SpectrumSize * 2];
|
|
} else {
|
|
_texture = new Texture2D(source.SpectrumSize, 1, TextureFormat.RFloat, false);
|
|
_colorCache = new Color[source.SpectrumSize];
|
|
}
|
|
|
|
_texture.name = $"AudioSpectrumData{source.SpectrumSize}";
|
|
}
|
|
|
|
private void UpdateTexture() {
|
|
// init
|
|
if (_texture == null) InitialiseTexture();
|
|
|
|
// has symmetric been toggled and the texture needs updating?
|
|
if (symmetric && _texture.width != source.SpectrumSize * 2) InitialiseTexture();
|
|
if (!symmetric && _texture.width != source.SpectrumSize) InitialiseTexture();
|
|
|
|
var spectrumData = GetSpectrumData();
|
|
|
|
// not sure why this can happen, something to do with source being loaded still maybe
|
|
if (spectrumData == null) return;
|
|
|
|
if (symmetric) {
|
|
for (int i = 0; i < source.SpectrumSize; i++) {
|
|
_colorCache[source.SpectrumSize + i] = new Color(spectrumData[i], 0, 0, 0);
|
|
_colorCache[source.SpectrumSize - (i + 1)] = new Color(spectrumData[i], 0, 0, 0);
|
|
}
|
|
} else {
|
|
for (int i = 0; i < source.SpectrumSize; i++) {
|
|
_colorCache[i] = new Color(spectrumData[i], 0, 0, 0);
|
|
}
|
|
}
|
|
|
|
_texture.SetPixels(_colorCache);
|
|
_texture.Apply();
|
|
}
|
|
|
|
public override bool IsValid(VisualEffect component) {
|
|
return source != null;
|
|
}
|
|
|
|
// public override void UpdateBinding(VisualEffect component) {
|
|
// // source.SpectrumSize = source.source.SpectrumSize;
|
|
// }
|
|
|
|
protected float[] GetSpectrumData() {
|
|
// Get raw / unmodified spectrum data
|
|
var spectrumData = source.GetSpectrumData();
|
|
|
|
// Run spectrum data through all configured transformers
|
|
if (Transformers != null && Transformers.Count > 0) {
|
|
foreach (var transformer in Transformers) {
|
|
if (spectrumData == null) continue;
|
|
spectrumData = transformer.Transform(spectrumData);
|
|
}
|
|
}
|
|
|
|
Preview.Values = spectrumData;
|
|
return spectrumData;
|
|
}
|
|
|
|
public override void UpdateBinding(VisualEffect component) {
|
|
UpdateTexture();
|
|
if (symmetric) {
|
|
component.SetUInt("SpectrumSize", (uint) source.SpectrumSize * 2);
|
|
} else {
|
|
component.SetUInt("SpectrumSize", (uint) source.SpectrumSize);
|
|
}
|
|
component.SetTexture("SpectrumDataTexture", _texture);
|
|
}
|
|
|
|
public override string ToString() {
|
|
return "Kaiju VFX Spectrum Binder";
|
|
}
|
|
}
|