2019-10-01 21:01:56 +00:00
|
|
|
using System;
|
2019-09-29 10:00:16 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace cylvester
|
|
|
|
{
|
|
|
|
interface ISpectrumGenerator
|
|
|
|
{
|
|
|
|
Texture2D Spectrum { get; }
|
2019-10-01 17:30:17 +00:00
|
|
|
int Update(IPdArray pdArray, Rect selectionRect);
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class SpectrumGenerator : ISpectrumGenerator
|
|
|
|
{
|
2019-10-01 20:20:55 +00:00
|
|
|
public Texture2D Spectrum { get; }
|
|
|
|
|
2019-09-29 10:00:16 +00:00
|
|
|
public SpectrumGenerator(int width, int height)
|
|
|
|
{
|
2019-10-01 20:20:55 +00:00
|
|
|
Spectrum = new Texture2D(width, height);
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 17:30:17 +00:00
|
|
|
public int Update(IPdArray pdArray, Rect selectionRect)
|
2019-10-01 21:01:56 +00:00
|
|
|
{
|
|
|
|
if (pdArray != null)
|
|
|
|
{
|
|
|
|
return UpdatePlayMode(pdArray, selectionRect);
|
|
|
|
}
|
|
|
|
UpdateEditMode(selectionRect);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private int UpdatePlayMode(IPdArray pdArray, Rect selectionRect)
|
2019-09-29 10:00:16 +00:00
|
|
|
{
|
2019-09-29 17:47:17 +00:00
|
|
|
var numPixels = 0;
|
2019-10-01 21:01:56 +00:00
|
|
|
var data = pdArray.Data;
|
|
|
|
OnAllPixels((x, y) =>
|
2019-09-29 10:00:16 +00:00
|
|
|
{
|
2019-10-01 21:01:56 +00:00
|
|
|
var magnitude = data[x] * 20f;
|
|
|
|
var validPixel = magnitude > y;
|
|
|
|
var color = validPixel ? Color.green : Color.black;
|
2019-09-29 16:04:26 +00:00
|
|
|
|
2019-10-01 21:01:56 +00:00
|
|
|
if (IsInSelection(x, y, ref selectionRect))
|
|
|
|
{
|
|
|
|
color.a = 1f;
|
|
|
|
if (validPixel)
|
|
|
|
numPixels++;
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
2019-10-01 21:01:56 +00:00
|
|
|
else
|
|
|
|
color.a = 0.2f;
|
|
|
|
|
|
|
|
Spectrum.SetPixel(x, y, color);
|
|
|
|
});
|
2019-10-01 20:20:55 +00:00
|
|
|
Spectrum.Apply();
|
2019-09-29 17:47:17 +00:00
|
|
|
return numPixels;
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
2019-10-01 20:20:55 +00:00
|
|
|
|
2019-10-01 21:01:56 +00:00
|
|
|
private void UpdateEditMode(Rect selectionRect)
|
|
|
|
{
|
|
|
|
OnAllPixels((x, y) =>
|
|
|
|
{
|
|
|
|
var color = Color.black;
|
|
|
|
if (IsInSelection(x, y, ref selectionRect))
|
|
|
|
color.a = 1f;
|
|
|
|
else
|
|
|
|
color.a = 0.2f;
|
|
|
|
Spectrum.SetPixel(x, y, color);
|
|
|
|
});
|
|
|
|
Spectrum.Apply();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void OnAllPixels(Action<int, int> action)
|
|
|
|
{
|
|
|
|
for (var x = 0; x < Spectrum.width; x++)
|
|
|
|
for (var y = 0; y < Spectrum.height; y++)
|
|
|
|
action(x, y);
|
|
|
|
}
|
|
|
|
|
2019-10-01 20:20:55 +00:00
|
|
|
private bool IsInSelection(int x, int y, ref Rect selectionRect)
|
|
|
|
{
|
|
|
|
var inRectHorizontally = selectionRect.x < x && x < selectionRect.x + (selectionRect.width-1);
|
|
|
|
var mY = Spectrum.height - selectionRect.y;
|
|
|
|
var inRectVertically = mY - (selectionRect.height-1) < y && y < mY;
|
|
|
|
return inRectHorizontally && inRectVertically;
|
|
|
|
}
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
|
|
|
}
|