2019-09-29 10:00:16 +00:00
|
|
|
using UnityEngine;
|
2019-10-02 16:17:08 +00:00
|
|
|
using UnityEngine.Events;
|
2019-09-29 10:00:16 +00:00
|
|
|
|
|
|
|
namespace cylvester
|
|
|
|
{
|
2019-10-02 16:17:08 +00:00
|
|
|
[System.Serializable]
|
|
|
|
class UnityFloatEvent : UnityEvent<float> { }
|
|
|
|
|
2019-09-29 10:00:16 +00:00
|
|
|
public interface IPdSpectrumBind
|
|
|
|
{
|
2019-09-29 19:53:47 +00:00
|
|
|
int Channel { get; set; }
|
2019-10-02 16:17:08 +00:00
|
|
|
int Energy { get; }
|
|
|
|
int TextureWidth { get; }
|
|
|
|
int TextureHeight { get; }
|
|
|
|
Texture2D Spectrum { get; }
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class PdSpectrumBind : MonoBehaviour, IPdSpectrumBind
|
|
|
|
{
|
2019-09-29 19:53:47 +00:00
|
|
|
[SerializeField] private PdBackend pdBackend;
|
2019-10-01 17:30:17 +00:00
|
|
|
[SerializeField] private Rect selection;
|
2019-10-02 16:17:08 +00:00
|
|
|
[SerializeField] private UnityFloatEvent energyChanged;
|
|
|
|
|
|
|
|
private ISpectrumGenerator spectrumGenerator_;
|
|
|
|
|
|
|
|
private void Start()
|
2019-09-29 10:00:16 +00:00
|
|
|
{
|
2019-10-02 16:17:08 +00:00
|
|
|
var spectrumArray = pdBackend.fftArrayContainer[Channel];
|
|
|
|
spectrumGenerator_ = new SpectrumGeneratorPlayMode(TextureWidth, TextureHeight, spectrumArray);
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
2019-09-29 19:53:47 +00:00
|
|
|
|
2019-10-02 16:17:08 +00:00
|
|
|
public int TextureWidth { get; } = 512;
|
|
|
|
public int TextureHeight { get; } = 256;
|
|
|
|
public Texture2D Spectrum => spectrumGenerator_.Spectrum;
|
2019-09-29 19:53:47 +00:00
|
|
|
public int Channel { get; set; }
|
2019-10-02 16:17:08 +00:00
|
|
|
public int Energy { get; private set; }
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
{
|
|
|
|
var energy = spectrumGenerator_.Update(selection);
|
|
|
|
if (energy == Energy)
|
|
|
|
return;
|
|
|
|
Energy = energy;
|
|
|
|
energyChanged.Invoke(Energy);
|
|
|
|
}
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
|
|
|
}
|