Ejemplo n.º 1
0
    public static Triangle[] SliceTri(Triangle source, Vector2 lineStart, Vector2 lineEnd)
    {
        Triangle[] output = new Triangle[3];

        for (int i = 0; i < 3; i++)
        {
            output[i] = new Triangle();
        }

        Vector2 soloPoint;
        Vector2 soloUV;
        float   soloY;

        Vector2 pairPoint0;
        Vector2 pairPoint1;
        Vector2 pairUV0;
        Vector2 pairUV1;
        float   pairY0;
        float   pairY1;

        bool sign0 = MathsHelper.sign(source.p0, lineStart, lineEnd) <= 0.0f;
        bool sign1 = MathsHelper.sign(source.p1, lineStart, lineEnd) <= 0.0f;
        bool sign2 = MathsHelper.sign(source.p2, lineStart, lineEnd) <= 0.0f;

        if (sign0 == sign1)
        {
            soloPoint  = source.p2;
            pairPoint0 = source.p0;
            pairPoint1 = source.p1;
            soloUV     = source.uv2;
            pairUV0    = source.uv0;
            pairUV1    = source.uv1;
            soloY      = source.y2;
            pairY0     = source.y0;
            pairY1     = source.y1;
        }
        else if (sign1 == sign2)
        {
            soloPoint  = source.p0;
            pairPoint0 = source.p1;
            pairPoint1 = source.p2;
            soloUV     = source.uv0;
            pairUV0    = source.uv1;
            pairUV1    = source.uv2;
            soloY      = source.y0;
            pairY0     = source.y1;
            pairY1     = source.y2;
        }
        else
        {
            soloPoint  = source.p1;
            pairPoint0 = source.p0;
            pairPoint1 = source.p2;
            soloUV     = source.uv1;
            pairUV0    = source.uv0;
            pairUV1    = source.uv2;
            soloY      = source.y1;
            pairY0     = source.y0;
            pairY1     = source.y2;
        }

        // Right, so we know which verts are on which side of the slice-line. Time for the intersections.

        Vector2 intersection0, intersection1;

        MathsHelper.LineIntersectionPoint(lineStart, lineEnd, pairPoint0, soloPoint, out intersection0);
        MathsHelper.LineIntersectionPoint(lineStart, lineEnd, pairPoint1, soloPoint, out intersection1);

        // Lerp dem UVs
        float lerpDistance0 = (intersection0 - pairPoint0).magnitude / (soloPoint - pairPoint0).magnitude;
        float lerpDistance1 = (intersection1 - pairPoint1).magnitude / (soloPoint - pairPoint1).magnitude;

        Vector2 intersectionUV0 = pairUV0 + (soloUV - pairUV0) * lerpDistance0;
        Vector2 intersectionUV1 = pairUV1 + (soloUV - pairUV1) * lerpDistance1;

        float intersectionY0 = pairY0 + (soloY - pairY0) * lerpDistance0;
        float intersectionY1 = pairY1 + (soloY - pairY1) * lerpDistance1;

        // Three triangles. One for the isolated tri, two for the trapezoid base.

        output[0].p0 = soloPoint;
        output[0].p1 = intersection1;
        output[0].p2 = intersection0;

        output[0].uv0 = soloUV;
        output[0].uv1 = intersectionUV1;
        output[0].uv2 = intersectionUV0;

        output[0].y0 = soloY;
        output[0].y1 = intersectionY1;
        output[0].y2 = intersectionY0;

        output[1].p0 = pairPoint0;
        output[1].p1 = intersection0;
        output[1].p2 = pairPoint1;

        output[1].uv0 = pairUV0;
        output[1].uv1 = intersectionUV0;
        output[1].uv2 = pairUV1;

        output[1].y0 = pairY0;
        output[1].y1 = intersectionY0;
        output[1].y2 = pairY1;

        output[2].p0 = pairPoint1;
        output[2].p1 = intersection0;
        output[2].p2 = intersection1;

        output[2].uv0 = pairUV1;
        output[2].uv1 = intersectionUV0;
        output[2].uv2 = intersectionUV1;

        output[2].y0 = pairY1;
        output[2].y1 = intersectionY0;
        output[2].y2 = intersectionY1;

        return(output);
    }