예제 #1
0
    private static void MakeHole(ISpline mainSpline, ISpline subSpline)
    {
        float   cutPointSplinePosition     = 0;
        float   holeCutPointSplinePosition = 0;
        Vector3 cutPointPosition           = mainSpline.GetPointAt(0).Position;
        Vector3 holeCutPointPosition       = subSpline.GetPointAt(0).Position;
        float   cutPointDistance           = 0;

        GetCutPointData(mainSpline, subSpline, ref cutPointSplinePosition, ref holeCutPointSplinePosition, ref cutPointPosition, ref holeCutPointPosition, ref cutPointDistance);

        int startCutPointIdx = mainSpline.Rs.spline.GetNearestSplinePointIndex(cutPointSplinePosition);

        if (Vector2.Distance(mainSpline.GetPointAt(startCutPointIdx).Position, cutPointPosition) > 0.1f)
        {
            startCutPointIdx = mainSpline.Rs.AddPoint(cutPointSplinePosition);
        }
        var startCutPoint = mainSpline.GetPointAt(startCutPointIdx);

        var endCutPointIdx = mainSpline.AddPointLocal(startCutPointIdx + 1, startCutPoint.PositionLocal, Vector3.zero, startCutPoint.OutTangentLocal, 1.0f, false).Index;

        startCutPoint.OutTangentLocal = Vector3.zero;

        int holeCutPointIdx = subSpline.Rs.spline.GetNearestSplinePointIndex(holeCutPointSplinePosition);

        if (Vector2.Distance(subSpline.GetPointAt(holeCutPointIdx).Position, holeCutPointPosition) > 0.1)
        {
            holeCutPointIdx = subSpline.Rs.AddPoint(holeCutPointSplinePosition);
        }

        if ((holeCutPointIdx != 0) && (holeCutPointIdx != subSpline.PointsCount - 1))
        {
            while (holeCutPointIdx != 0)
            {
                subSpline.GetPointAt(subSpline.PointsCount - 1).OutTangentLocal = subSpline.GetPointAt(0).OutTangentLocal;
                subSpline.RemovePointAt(0);
                var firstHolePoint = subSpline.GetPointAt(0);
                subSpline.AddPointLocal(subSpline.PointsCount, firstHolePoint.PositionLocal, firstHolePoint.InTangentLocal, firstHolePoint.OutTangentLocal, 1.0f, false);
                holeCutPointIdx--;
            }
        }

        float   distanceOffset = 0.01f;
        Vector3 fowardVector   = subSpline.PointsAreInClockWiseOrder() ? Vector3.forward : Vector3.back;
        Vector3 vectorOffset   = Vector3.Cross(fowardVector, subSpline.GetPointAt(0).Position - startCutPoint.Position);

        vectorOffset = distanceOffset * vectorOffset.normalized;
        subSpline.GetPointAt(0).Position += vectorOffset;
        startCutPoint.Position           += vectorOffset;

        for (int i = 0; i < subSpline.PointsCount; i++)
        {
            var holePoint = subSpline.GetPointAt(i);
            if (i == subSpline.PointsCount - 1)
            {
                mainSpline.AddPointLocal(endCutPointIdx, holePoint.PositionLocal, holePoint.InTangentLocal, Vector3.zero, 1.0f, false);
            }
            else
            if (i == 0)
            {
                mainSpline.AddPointLocal(endCutPointIdx, holePoint.PositionLocal, Vector3.zero, holePoint.OutTangentLocal, 1.0f, false);
            }
            else
            {
                mainSpline.AddPointLocal(endCutPointIdx, holePoint.PositionLocal, holePoint.InTangentLocal, holePoint.OutTangentLocal, 1.0f, false);
            }
            endCutPointIdx++;
        }
    }