Ejemplo n.º 1
0
 public static List <PSPolygon> difference(PSPolygon subject, PSPolygon clip)
 {
     return(PSClipperHelper.difference(subject.points, clip.points).Select(p => new PSPolygon(p)).ToList());
 }
Ejemplo n.º 2
0
    private MeshData UpdateMeshData(MeshData oldData, IClipShape clipShape)
    {
        var clipPolygon  = clipShape.ClipPolygon();
        var oldVertices  = oldData.vertices;
        var oldTriangles = oldData.triangles;
        var oldPaths     = oldData.paths;

        var newVertices = new List <Vector2>(oldVertices.Length);

        var     vertexIndex = new Dictionary <int, int>(oldVertices.Length);
        Vector2 v;

        for (int i = 0; i < oldVertices.Length; i++)
        {
            v = oldVertices[i];
            if (clipPolygon.PointInPolygon(v))
            {
                vertexIndex.Add(i, -1);
            }
            else
            {
                vertexIndex.Add(i, newVertices.Count);
                newVertices.Add(v);
            }
        }

        var        newTriangles     = new List <int>(oldTriangles.Length);
        var        clippedTriangles = new List <int[]>();
        List <int> oldTriangle      = new List <int>(3);

        for (int i = 0; i < oldTriangles.Length / 3; i++)
        {
            oldTriangle.Clear();
            oldTriangle.Add(oldTriangles[3 * i]);
            oldTriangle.Add(oldTriangles[3 * i + 1]);
            oldTriangle.Add(oldTriangles[3 * i + 2]);
            var newTriangle = oldTriangle.Select(oldIndex => vertexIndex[oldIndex]).ToArray();
            if (newTriangle.Any(newIndex => newIndex > -1))
            {
                if (newTriangle.All(newIndex => newIndex > -1) && !clipShape.ShouldClipTriangle(newTriangle.Select(newIndex => newVertices[newIndex])))
                {
                    newTriangles.AddRange(newTriangle);
                }
                else
                {
                    clippedTriangles.Add(oldTriangle.ToArray());
                }
            }
        }

        var newPolygons = clippedTriangles
                          .Select(t => t.Select(oldIndex => (Vector2)oldVertices[oldIndex]))
                          .SelectMany(t => PSClipperHelper.difference(t, clipPolygon.points));

        var mesher = new GenericMesher();

        foreach (var points in newPolygons)
        {
            var poly = new Polygon();
            poly.Add(TerrainMesh.createContour(points));
            var imesh = mesher.Triangulate(poly);
            TerrainMesh.getTriangles(imesh, ref newVertices, ref newTriangles);
        }

        var newPaths = oldPaths
                       .SelectMany(p => PSClipperHelper.difference(p, clipPolygon.points))
                       .Select(p => p.ToArray())
                       .ToArray();

        var particles = oldPaths
                        .SelectMany(p => PSClipperHelper.intersection(p, clipPolygon.points))
                        .Select(p => GenerateParticles(new PSPolygon(p)))
                        .ToArray();

        return(new MeshData(
                   newVertices.Select(c => new Vector3(c.x, c.y, 0)).ToArray(),
                   newVertices.Select(c => (Color32)terrainMesh.terrainTintColor(c, doNotWrapUV)).ToArray(),
                   newVertices.Select(c => terrainMesh.getUV(c, doNotWrapUV)).ToArray(),
                   newVertices.Select(c => terrainMesh.getUV2(c, doNotWrapUV, floorEdges)).ToArray(),
                   newTriangles.ToArray(),
                   newPaths,
                   particles));
    }