95 lines
3.3 KiB
C#
95 lines
3.3 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.VFX;
|
|
using UnityEngine.VFX.Utility;
|
|
|
|
namespace Assets.WasapiAudio.Scripts.Unity
|
|
{
|
|
[AddComponentMenu("VFX/Property Binders/Spectre - VFX SDF Binder")]
|
|
[VFXBinder("Kaiju/Kaiju - Spectrum to 3d SDF Binder")]
|
|
public class SpectreVfxSdfBinder : VFXBinderBase {
|
|
[SerializeReference]
|
|
[SerializeReferenceButton]
|
|
public List<SpectrumTransformer> Transformers = new List<SpectrumTransformer>();
|
|
|
|
[SpectrumDataPreview]
|
|
public SpectrumData Preview;
|
|
|
|
public AudioSource source;
|
|
public bool symmetric = false;
|
|
|
|
private Texture3D _texture;
|
|
private Color[] _colorCache;
|
|
|
|
private int z = 0;
|
|
|
|
private void UpdateTexture() {
|
|
if (_texture == null)
|
|
{
|
|
_texture = new Texture3D(source.SpectrumSize * 2, source.SpectrumSize * 2, source.SpectrumSize * 2, TextureFormat.RFloat, false);
|
|
_texture.wrapMode = TextureWrapMode.Clamp;
|
|
_texture.name = $"AudioSpectrumDataSDF{source.SpectrumSize}";
|
|
_colorCache = new Color[
|
|
source.SpectrumSize * 2 *
|
|
source.SpectrumSize * 2 *
|
|
source.SpectrumSize * 2
|
|
];
|
|
}
|
|
|
|
var spectrumData = GetSpectrumData();
|
|
if (spectrumData == null) return;
|
|
|
|
// go from 0 to end of spectrum
|
|
for (int x = 0; x < source.SpectrumSize; x++) {
|
|
float height = spectrumData[x] * source.SpectrumSize * 2;
|
|
|
|
// go from 0 to double the spectrum height
|
|
for (int y = 0; y < source.SpectrumSize * 2; y++) {
|
|
float distance = (y - height);
|
|
|
|
var z_part = z * (source.SpectrumSize * 2 * source.SpectrumSize * 2);
|
|
var y_part = y * source.SpectrumSize * 2;
|
|
|
|
_colorCache[z_part + y_part + (source.SpectrumSize + x)] = new Color(distance, 0, 0, 0);
|
|
_colorCache[z_part + y_part + (source.SpectrumSize - (x + 1))] = new Color(distance, 0, 0, 0);
|
|
}
|
|
}
|
|
|
|
z = (z + 1) % (source.SpectrumSize * 2);
|
|
|
|
// Debug.Log($"{z}");
|
|
|
|
_texture.SetPixels(_colorCache);
|
|
_texture.Apply();
|
|
}
|
|
|
|
public override bool IsValid(VisualEffect component) {
|
|
return source != null;
|
|
}
|
|
|
|
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) {
|
|
spectrumData = transformer.Transform(spectrumData);
|
|
}
|
|
}
|
|
|
|
Preview.Values = spectrumData;
|
|
return spectrumData;
|
|
}
|
|
|
|
public override void UpdateBinding(VisualEffect component) {
|
|
UpdateTexture();
|
|
component.SetUInt("SpectrumSize", (uint) source.SpectrumSize);
|
|
component.SetTexture("SpectrumDataTexture", _texture);
|
|
}
|
|
|
|
public override string ToString() {
|
|
return "Audio Spectrum Data";
|
|
}
|
|
}
|
|
} |