soundvision/UnityProject/Assets/Editor/SpectrumGenerator.cs
2019-09-29 12:00:16 +02:00

42 lines
No EOL
1.2 KiB
C#

using UnityEngine;
namespace cylvester
{
interface ISpectrumGenerator
{
Texture2D Spectrum { get; }
void Update(ref Rect selectionRect);
}
public class SpectrumGenerator : ISpectrumGenerator
{
private Texture2D texture_;
private readonly int height_;
public Texture2D Spectrum => texture_;
public SpectrumGenerator(int width, int height)
{
texture_ = new Texture2D(width, height);
height_ = height;
}
public void Update(ref Rect selectionRect)
{
for (var y = 0; y < texture_.height; y++)
{
for (var x = 0; x < texture_.width; x++)
{
var alpha = 0.4f;
if ((selectionRect.x < x && x < (selectionRect.x + selectionRect.width)) &&
(selectionRect.y < y && y < (selectionRect.y + selectionRect.height)))
{
alpha = 1f;
}
var color = new Color(Random.value, Random.value, Random.value, alpha);
texture_.SetPixel(x, height_-y, color);
}
}
texture_.Apply();
}
}
}