Skip to content

Instantly share code, notes, and snippets.

@kirillrybin
Created March 29, 2013 08:18
Show Gist options
  • Save kirillrybin/5269430 to your computer and use it in GitHub Desktop.
Save kirillrybin/5269430 to your computer and use it in GitHub Desktop.
Поиск внешних точек для текстуры в Unity 3D
ArrayList GetOutlinePoints (Texture2D tex)
{
ArrayList outlinePoints = new ArrayList (tex.width * tex.height);
for (int x = 0; x < tex.width; x++) {
for (int y = 0; y < tex.height; y++) {
var alpha = tex.GetPixel (x, y).a;
if (alpha != 0) {
var p = new Vector2 (x, y);
if (!ContainsPoint (outlinePoints, p))
outlinePoints.Add (p);
break;
}
}
}
for (int y = 0; y < tex.height; y++) {
for (int x = tex.width - 1; x >= 0; x--) {
var alpha = tex.GetPixel (x, y).a;
if (alpha != 0) {
var p = new Vector2 (x, y);
if (!ContainsPoint (outlinePoints, p))
outlinePoints.Add (p);
break;
}
}
}
for (int x = tex.width - 1; x >= 0; x--) {
for (int y = tex.height - 1; y >= 0; y--) {
var alpha = tex.GetPixel (x, y).a;
if (alpha != 0) {
var p = new Vector2 (x, y);
if (!ContainsPoint (outlinePoints, p))
outlinePoints.Add (p);
break;
}
}
}
for (int y = tex.height - 1; y >= 0; y--) {
for (int x = 0; x < tex.width; x++) {
var alpha = tex.GetPixel (x, y).a;
if (alpha != 0) {
var p = new Vector2 (x, y);
if (!ContainsPoint (outlinePoints, p))
outlinePoints.Add (p);
break;
}
}
}
if (outlinePoints[0]) outlinePoints.Add (outlinePoints [0]);
return outlinePoints;
}
bool ContainsPoint (ArrayList points, Vector2 value)
{
foreach (Vector2 p in points) {
if (p == value)
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment