Files
kaiju/Assets/Audio/ThemeShaderBinder.cs
T
2022-05-06 16:53:30 +10:00

53 lines
1.4 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using Assets.WasapiAudio.Scripts.Unity;
[RequireComponent(typeof(Material))]
public class ThemeShaderBinder : MonoBehaviour
{
public Material material;
public Preferences preferences;
private Texture2D _texture;
private Color[] _colorCache;
private int textureSize = 128;
private void Start() {
if (preferences == null) {
Debug.Log("Theme Shader Binder needs Preferences.");
gameObject.SetActive(false);
}
}
private void Update() {
UpdateTexture();
UpdateBinding();
}
private void InitialiseTexture() {
Debug.Log("Reinitialising spectrum texture...");
_texture = new Texture2D(textureSize, 1);
_colorCache = new Color[textureSize];
_texture.name = $"ThemeTexture{textureSize}";
}
private void UpdateTexture() {
// init
if (_texture == null) InitialiseTexture();
Gradient theme = preferences.Theme();
for (int i = 0; i < textureSize; i++) {
_colorCache[i] = theme.Evaluate((float)i / (float)textureSize);
}
_texture.SetPixels(_colorCache);
_texture.Apply();
}
public void UpdateBinding() {
UpdateTexture();
material.SetFloat("_ThemeSize", textureSize);
material.SetTexture("_ThemeTexture", _texture);
}
}