GitHub找到一个用耳切法进行多边形三角化的库,简单测试了一下,感觉还行,推荐给大家
项目地址:https://github.com/SebLague/Ear-Clipping-Triangulation
测试代码:
using Sebastian.Geometry; using System.Collections; using System.Collections.Generic; using UnityEngine; public class Test : MonoBehaviour { public PolygonCollider2D polygonCollider; public MeshFilter mf; public MeshFilter mf2; void Update() { Vector3[] verts = new Vector3[polygonCollider.points.Length]; for (int i = 0; i < polygonCollider.points.Length; i++) { verts[i] = polygonCollider.points[i]; } mf.mesh.vertices = verts; Polygon polygon = new Polygon(polygonCollider.points); Triangulator triangulator = new Triangulator(polygon); mf.mesh.triangles = triangulator.Triangulate(); mf2.mesh = polygonCollider.CreateMesh(false,false); } }
效果:
左边是这个项目生成的mesh,多边形相交也能正确绘制mesh
中间是通过PolygonCollider2D编辑生成的顶点
右边是PolygonCollider2D生成的mesh,相交的部分有个洞,如果业务逻辑中确定多边形不会相交,用unity自带的PolygonCollider2D创建mesh也是不错的选择
标签:多边形,三角,public,Unity,mesh,using,PolygonCollider2D,polygonCollider From: https://www.cnblogs.com/Jason-c/p/18220916