Created
November 8, 2017 11:17
-
-
Save zcyemi/39947991d257de8ba34b6a27f87b71a6 to your computer and use it in GitHub Desktop.
Unity-Sprite mesh outline caculation
This file contains hidden or 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
public struct LineP | |
{ | |
public uint v1; | |
public uint v2; | |
public LineP(uint v1,uint v2) | |
{ | |
this.v1 = v1; | |
this.v2 = v2; | |
} | |
public override bool Equals(object obj) | |
{ | |
if (!(obj is LineP)) return false; | |
LineP o = (LineP)obj; | |
return (o.v1 == v1 && o.v2 == v2)||(o.v1 == v2 && o.v2 == v1); | |
} | |
public override string ToString() | |
{ | |
return v1 + " " + v2; | |
} | |
} | |
public static List<Vector3> ProcessSpr(Sprite spr,out Polygon2D polygon) | |
{ | |
List<LineP> line = new List<LineP>(); | |
for(int i = 0; i < spr.triangles.Length / 3; i++) | |
{ | |
var v1 = spr.triangles[i * 3]; | |
var v2 = spr.triangles[i * 3 + 1]; | |
var v3 = spr.triangles[i * 3 + 2]; | |
var p1 = new LineP(v1, v2); | |
var p2 = new LineP(v2, v3); | |
var p3 = new LineP(v3, v1); | |
if (line.Contains(p1)) | |
{ | |
line.Remove(p1); | |
} | |
else | |
{ | |
line.Add(p1); | |
} | |
if (line.Contains(p2)) | |
{ | |
line.Remove(p2); | |
} | |
else | |
{ | |
line.Add(p2); | |
} | |
if (line.Contains(p3)) | |
{ | |
line.Remove(p3); | |
} | |
else | |
{ | |
line.Add(p3); | |
} | |
} | |
uint n = 0; | |
List<uint> linstr = new List<uint>(); | |
linstr.Add(n); | |
while (true) | |
{ | |
var la = line.Find((l) => l.v1 == n); | |
if (la.v2 == 0) break; | |
linstr.Add(la.v2); | |
n = la.v2; | |
} | |
List<Vector3> vert = new List<Vector3>(); | |
List<Vector2> counter = new List<Vector2>(); | |
for (int i = 0; i < linstr.Count; i++) | |
{ | |
Vector2 v2 = spr.vertices[linstr[i]]; | |
vert.Add(new Vector3(v2.x, 0, v2.y)); | |
counter.Add(spr.vertices[linstr[i]]); | |
} | |
polygon = new Polygon2D(counter.ToArray()); | |
return vert; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment