Beispiel #1
0
    private void OnEnable()
    {
        Vector3 halfSize = size / 2f;

        meshFilter            = GetComponent <MeshFilter>();
        meshFilter.sharedMesh = new Mesh();

        meshCollider            = GetComponent <MeshCollider>();
        meshCollider.sharedMesh = meshFilter.sharedMesh;

        clips = new List <Vector2[]>();

        Vector2[] contourn =
        {
            new Vector2(-halfSize.x,      0f),
            new Vector2(+halfSize.x,      0f),
            new Vector2(+halfSize.x, +size.y),
            new Vector2(-halfSize.x, +size.y)
        };

        subjects = new MultiPolygonData(Allocator.Persistent);
        subjects.AddPolygon(contourn);

        ScheduleJobs();
    }
Beispiel #2
0
    private void ScheduleJobs()
    {
        Vector2 halfSize = size / 2f;

        jobClips       = new MultiPolygonData(Allocator.TempJob);
        OutVertices    = new NativeList <Vector3>(Allocator.TempJob);
        OutTriangles   = new NativeList <int>(Allocator.TempJob);
        clippingOutput = new MultiPolygonData(Allocator.Persistent);

        foreach (Vector2[] clip in clips)
        {
            jobClips.AddPolygon(clip);
        }
        clips.Clear();

        WallClipperJob clipperJob = new WallClipperJob()
        {
            subjects          = subjects,
            clips             = jobClips,
            output            = clippingOutput,
            MinWallAnglePoint = new float2(-halfSize.x, 0f),
            MaxWallAnglePoint = new float2(+halfSize.x, +size.y)
        };

        PrepareTrianglesListJob prepareTrianglesListJob = new PrepareTrianglesListJob()
        {
            Polygons     = clippingOutput,
            OutTriangles = OutTriangles
        };

        ECTriangulatorParallelJob triangulatorParallelJob = new ECTriangulatorParallelJob()
        {
            Polygons     = clippingOutput,
            OutTriangles = OutTriangles.AsDeferredJobArray()
        };

        ExtruderJob extruderJob = new ExtruderJob()
        {
            Polygons         = clippingOutput,
            ExtrudeDirection = new Vector3(0f, 0f, size.z),
            OutTriangles     = OutTriangles,
            OutVertices      = OutVertices,
        };

        JobHandle clippingHandler             = clipperJob.Schedule();
        JobHandle prepareTrianglesListHandler = prepareTrianglesListJob.Schedule(clippingHandler);
        JobHandle triangulatorHandler         = triangulatorParallelJob.Schedule(clippingOutput.PolygonSupportList, 1, prepareTrianglesListHandler);

        lastJobHandle = extruderJob.Schedule(triangulatorHandler);

        waitForJobsCoroutine = StartCoroutine(WaitForJobsCompleted());
    }
Beispiel #3
0
    public void Execute()
    {
        List <List <IntPoint> > subjsList = new List <List <IntPoint> >();

        for (int pi = 0; pi < subjects.PolygonsCount; ++pi)
        {
            List <IntPoint> contour = new List <IntPoint>();
            foreach (var contourPoint in subjects.GetContourPoints(pi))
            {
                contour.Add(Vector2ToIntPoint(contourPoint.Point));
            }
            subjsList.Add(contour);

            for (int hi = 0; hi < subjects.GetPolygonHolesNum(pi); ++hi)
            {
                List <IntPoint> holeList = new List <IntPoint>();
                var             hole     = subjects.GetPolygonHole(pi, hi);
                foreach (var holePoint in hole)
                {
                    holeList.Add(Vector2ToIntPoint(holePoint.Point));
                }
                subjsList.Add(holeList);
            }
        }

        List <List <IntPoint> > clipsList = new List <List <IntPoint> >();

        for (int pi = 0; pi < clips.PolygonsCount; ++pi)
        {
            List <IntPoint> contour = new List <IntPoint>();
            foreach (var contourPoint in clips.GetContourPoints(pi))
            {
                contour.Add(Vector2ToIntPoint(contourPoint.Point));
            }
            clipsList.Add(contour);
        }

        //Run Clipper
        Clipper clipper = new Clipper();

        clipper.AddPaths(subjsList, PolyType.ptSubject, true);
        clipper.AddPaths(clipsList, PolyType.ptClip, true);
        PolyTree solution = new PolyTree();

        clipper.Execute(ClipType.ctDifference, solution, PolyFillType.pftPositive);

        //Save the results
        output.ClearPolygons();
        foreach (PolyNode node in solution.Childs)
        {
            bool polygonOk = false;

            Vector2[] contour = new Vector2[node.Contour.Count];
            for (int i = node.Contour.Count - 1; i >= 0; --i)
            {
                contour[i] = IntPointToVector2(node.Contour[i]);

                polygonOk |= (math.abs(contour[i].x - MinWallAnglePoint.x) <= float.Epsilon) ||
                             (math.abs(contour[i].x - MaxWallAnglePoint.x) <= float.Epsilon) ||
                             (math.abs(contour[i].y - MinWallAnglePoint.y) <= float.Epsilon) ||
                             (math.abs(contour[i].y - MaxWallAnglePoint.y) <= float.Epsilon);
            }

            Vector2[][] holes     = new Vector2[node.ChildCount][];
            int         holeIndex = 0;
            foreach (PolyNode holeNode in node.Childs)
            {
                holes[holeIndex] = new Vector2[holeNode.Contour.Count];
                for (int j = 0; j < holeNode.Contour.Count; ++j)
                {
                    holes[holeIndex][j] = IntPointToVector2(holeNode.Contour[j]);
                }
                ++holeIndex;
            }

            if (polygonOk)
            {
                output.AddPolygon(contour, holes);
            }
        }
    }