104 lines
1.8 KiB
C#
104 lines
1.8 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class TextureScanner
|
|
{
|
|
private Texture2D img;
|
|
|
|
private int cols;
|
|
|
|
private int rows;
|
|
|
|
private float gSCuttOff;
|
|
|
|
private int[] detectedCols;
|
|
|
|
private int[] detectedRows;
|
|
|
|
private float[] grayScaleValues;
|
|
|
|
private int counter;
|
|
|
|
public TextureScanner(int r, int c)
|
|
{
|
|
rows = r;
|
|
cols = c;
|
|
}
|
|
|
|
public void setTexture(Texture2D tex)
|
|
{
|
|
img = tex;
|
|
}
|
|
|
|
public void setGSCuttOff(float gCut)
|
|
{
|
|
gSCuttOff = gCut;
|
|
}
|
|
|
|
public int getCount()
|
|
{
|
|
return counter;
|
|
}
|
|
|
|
public int[] getCols()
|
|
{
|
|
return detectedCols;
|
|
}
|
|
|
|
public int[] getRows()
|
|
{
|
|
return detectedRows;
|
|
}
|
|
|
|
public float[] getGrayscaleValues()
|
|
{
|
|
return grayScaleValues;
|
|
}
|
|
|
|
public bool ProcessTexture()
|
|
{
|
|
List<int> list = new List<int>();
|
|
List<int> list2 = new List<int>();
|
|
List<float> list3 = new List<float>();
|
|
counter = 0;
|
|
float num = 1f / (float)(cols + 1);
|
|
float num2 = 1f / (float)(rows + 1);
|
|
for (float num3 = 0f; num3 <= 1f; num3 += num)
|
|
{
|
|
for (float num4 = 0f; num4 <= 1f; num4 += num2)
|
|
{
|
|
float grayscale = img.GetPixelBilinear(num3, num4).grayscale;
|
|
if (grayscale <= gSCuttOff)
|
|
{
|
|
float num5 = (float)cols * num3;
|
|
float num6 = (float)rows * num4;
|
|
if (counter == 0)
|
|
{
|
|
list.Add((int)num5);
|
|
list2.Add((int)num6);
|
|
list3.Add(grayscale);
|
|
counter++;
|
|
}
|
|
else if (num5 != (float)list[counter - 1] && num6 != (float)list2[counter - 1])
|
|
{
|
|
list.Add((int)num5);
|
|
list2.Add((int)num6);
|
|
list3.Add(grayscale);
|
|
counter++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
detectedCols = list.ToArray();
|
|
detectedRows = list2.ToArray();
|
|
grayScaleValues = list3.ToArray();
|
|
list.Clear();
|
|
list2.Clear();
|
|
list3.Clear();
|
|
list.TrimExcess();
|
|
list2.TrimExcess();
|
|
list3.TrimExcess();
|
|
return true;
|
|
}
|
|
}
|