Skip to content

Commit 50cec21

Browse files
committed
Add polygon outline support
1 parent 38635cb commit 50cec21

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

Assets/Scripts/Core/Mesh/PolygonMesh.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ public class PolygonMesh : IMeshFactory, IHitTest
1818
/// </summary>
1919
public readonly List<Vector2> texcoords;
2020

21+
/// <summary>
22+
///
23+
/// </summary>
24+
public float lineWidth;
25+
26+
/// <summary>
27+
///
28+
/// </summary>
29+
public Color32 lineColor;
30+
2131
/// <summary>
2232
///
2333
/// </summary>
@@ -162,6 +172,53 @@ public void OnPopulateMesh(VertexBuffer vb)
162172

163173
if (colors != null)
164174
vb.RepeatColors(colors, 0, vb.currentVertCount);
175+
176+
if (lineWidth > 0)
177+
DrawOutline(vb);
178+
}
179+
180+
void DrawOutline(VertexBuffer vb)
181+
{
182+
int numVertices = points.Count;
183+
int start = vb.currentVertCount - numVertices;
184+
int k = vb.currentVertCount;
185+
for (int i = 0; i < numVertices; i++)
186+
{
187+
Vector3 p0 = vb.vertices[start + i];
188+
p0.y = -p0.y;
189+
Vector3 p1;
190+
if (i < numVertices - 1)
191+
p1 = vb.vertices[start + i + 1];
192+
else
193+
p1 = vb.vertices[start];
194+
p1.y = -p1.y;
195+
196+
Vector3 lineVector = p1 - p0;
197+
Vector3 widthVector = Vector3.Cross(lineVector, new Vector3(0, 0, 1));
198+
widthVector.Normalize();
199+
200+
vb.AddVert(p0 - widthVector * lineWidth * 0.5f, lineColor);
201+
vb.AddVert(p0 + widthVector * lineWidth * 0.5f, lineColor);
202+
vb.AddVert(p1 - widthVector * lineWidth * 0.5f, lineColor);
203+
vb.AddVert(p1 + widthVector * lineWidth * 0.5f, lineColor);
204+
205+
k += 4;
206+
vb.AddTriangle(k - 4, k - 3, k - 1);
207+
vb.AddTriangle(k - 4, k - 1, k - 2);
208+
209+
//joint
210+
if (i != 0)
211+
{
212+
vb.AddTriangle(k - 6, k - 5, k - 3);
213+
vb.AddTriangle(k - 6, k - 3, k - 4);
214+
}
215+
if (i == numVertices - 1)
216+
{
217+
start += numVertices;
218+
vb.AddTriangle(k - 2, k - 1, start + 1);
219+
vb.AddTriangle(k - 2, start + 1, start);
220+
}
221+
}
165222
}
166223

167224
bool IsPointInTriangle(ref Vector2 p, ref Vector2 a, ref Vector2 b, ref Vector2 c)

Assets/Scripts/Core/Shape.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,28 @@ public void DrawPolygon(Vector2[] points, Color fillColor)
147147
mesh.points.AddRange(points);
148148
mesh.fillColor = null;
149149
mesh.colors = null;
150+
mesh.lineWidth = 0;
151+
152+
graphics.color = fillColor;
153+
graphics.SetMeshDirty();
154+
}
155+
156+
/// <summary>
157+
///
158+
/// </summary>
159+
/// <param name="points"></param>
160+
/// <param name="fillColor"></param>
161+
/// <param name="lineSize"></param>
162+
/// <param name="lineColor"></param>
163+
public void DrawPolygon(Vector2[] points, Color fillColor, float lineSize, Color lineColor)
164+
{
165+
PolygonMesh mesh = graphics.GetMeshFactory<PolygonMesh>();
166+
mesh.points.Clear();
167+
mesh.points.AddRange(points);
168+
mesh.fillColor = fillColor;
169+
mesh.lineWidth = lineSize;
170+
mesh.lineColor = lineColor;
171+
mesh.colors = null;
150172

151173
graphics.color = fillColor;
152174
graphics.SetMeshDirty();
@@ -163,6 +185,7 @@ public void DrawPolygon(Vector2[] points, Color32[] colors)
163185
mesh.points.Clear();
164186
mesh.points.AddRange(points);
165187
mesh.fillColor = null;
188+
mesh.lineWidth = 0;
166189
mesh.colors = colors;
167190

168191
graphics.SetMeshDirty();

Assets/Scripts/UI/GGraph.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,21 @@ public void DrawPolygon(float aWidth, float aHeight, Vector2[] points, Color fil
199199
_shape.DrawPolygon(points, fillColor);
200200
}
201201

202+
/// <summary>
203+
///
204+
/// </summary>
205+
/// <param name="aWidth"></param>
206+
/// <param name="aHeight"></param>
207+
/// <param name="points"></param>
208+
/// <param name="fillColor"></param>
209+
/// <param name="lineSize"></param>
210+
/// <param name="lineColor"></param>
211+
public void DrawPolygon(float aWidth, float aHeight, Vector2[] points, Color fillColor, float lineSize, Color lineColor)
212+
{
213+
this.SetSize(aWidth, aHeight);
214+
_shape.DrawPolygon(points, fillColor, lineSize, lineColor);
215+
}
216+
202217
override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
203218
{
204219
base.Setup_BeforeAdd(buffer, beginPos);
@@ -235,7 +250,7 @@ override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
235250
for (int i = 0; i < cnt; i++)
236251
points[i].Set(buffer.ReadFloat(), buffer.ReadFloat());
237252

238-
_shape.DrawPolygon(points, fillColor);
253+
_shape.DrawPolygon(points, fillColor, lineSize, lineColor);
239254
}
240255
else if (type == 4)
241256
{

0 commit comments

Comments
 (0)