-
-
Save kirillrybin/5269430 to your computer and use it in GitHub Desktop.
Поиск внешних точек для текстуры в Unity 3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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