Пример #1
0
        public static TriangleNet.Mesh TriangulateMesh(List <PointF> path, float minAngle, float maxAngle, bool conformingDelaunay, bool quality, bool convex)
        {
            if (path == null)
            {
                return(null);
            }
            if (path.Count <= 2)
            {
                return(null);
            }

            List <FTriangle> outTris = new List <FTriangle>();

            MeshRenderer.Core.RenderData       renderData    = new MeshRenderer.Core.RenderData();
            MeshRenderer.Core.RenderManager    renderManager = new MeshRenderer.Core.RenderManager();
            TriangleNet.Geometry.InputGeometry input         = new TriangleNet.Geometry.InputGeometry(path.Count);
            TriangleNet.Mesh mesh = new TriangleNet.Mesh();

            input.AddPoint(path[0].X, path[0].Y);
            for (int i = 1; i < path.Count; i++)
            {
                input.AddPoint(path[i].X, path[i].Y);
                input.AddSegment(i - 1, i);
            }
            input.AddSegment(path.Count - 1, 0);

            renderData.SetInputGeometry(input);

            renderManager.CreateDefaultControl();
            renderManager.SetData(renderData);

            mesh.Behavior.MinAngle           = FMath.Clamp(0, 40, minAngle);
            mesh.Behavior.MaxAngle           = FMath.Clamp(80, 180, maxAngle);
            mesh.Behavior.ConformingDelaunay = conformingDelaunay;
            mesh.Behavior.Quality            = quality;
            mesh.Behavior.Convex             = convex;

            mesh.Triangulate(input);

            return(mesh);
        }
Пример #2
0
        private Mesh TriangulateMesh()
        {
            if (verts.Count > 2)
            {
                var tnMesh = new TriangleNet.Mesh();
                var input  = new TriangleNet.Geometry.InputGeometry();

                var localVertices = verts.Select(v => spriteRenderer.transform.InverseTransformPoint(v.position)).ToArray();

                for (int i = 0; i < verts.Count; i++)
                {
                    verts[i].index = i;
                    input.AddPoint(verts[i].position.x, verts[i].position.y);
                }

                foreach (var seg in segments)
                {
                    if (!seg.IsDeleted())
                    {
                        input.AddSegment(seg.first.index, seg.second.index);
                    }
                }

                foreach (var hole in holes)
                {
                    input.AddHole(hole.x, hole.y);
                }

                tnMesh.Triangulate(input);

                try {
                    Mesh mesh = new Mesh();
                    mesh.vertices  = localVertices;
                    mesh.triangles = tnMesh.Triangles.ToUnityMeshTriangleIndices();
                    mesh.uv        = genUV(mesh.vertices);
                    mesh.RecalculateBounds();
                    mesh.RecalculateNormals();
                    return(mesh);
                }
                catch {
                    Debug.LogError("Mesh topology was wrong. Make sure you dont have intersecting edges.");
                    throw;
                }
            }
            else
            {
                return(null);
            }
        }