Пример #1
0
    // http://shspage.com/aijs/

    private void Metaball(SVGData svg, Vector2 c1, float r1, Vector2 c2, float r2, float v)
    {
        if (r1 == 0f || r2 == 0f)
        {
            return;
        }

        var pi2 = Mathf.PI / 2f;

        var d = (c2 - c1).magnitude;

        var u1 = 0f;
        var u2 = 0f;

        if (d <= Mathf.Abs(r1 - r2))
        {
            return;
        }
        else if (d < r1 + r2)
        {
            // case circles are overlapping
            u1 = Mathf.Acos((r1 * r1 + d * d - r2 * r2) / (2 * r1 * d));
            u2 = Mathf.Acos((r2 * r2 + d * d - r1 * r1) / (2 * r2 * d));
        }

        var t1 = Mathf.Atan2(c2.y - c1.y, c2.x - c1.x);
        var t2 = Mathf.Acos((r1 - r2) / d);

        var t1a = t1 + u1 + (t2 - u1) * v;
        var t1b = t1 - u1 - (t2 - u1) * v;
        var t2a = t1 + Mathf.PI - u2 - (Mathf.PI - u2 - t2) * v;
        var t2b = t1 - Mathf.PI + u2 + (Mathf.PI - u2 - t2) * v;

        var p1a = PointOnCircle(c1, t1a, r1);
        var p1b = PointOnCircle(c1, t1b, r1);
        var p2a = PointOnCircle(c2, t2a, r2);
        var p2b = PointOnCircle(c2, t2b, r2);

        // define handle length by the distance between both ends of the curve to draw
        var handle_len_rate = 2;
        var d2 = Mathf.Min(v * handle_len_rate, (p2a - p1a).magnitude / (r1 + r2));

        d2 *= Mathf.Min(1, d * 2 / (r1 + r2)); // case circles are overlapping
        r1 *= d2;
        r2 *= d2;

        svg.Move(p1a);
        svg.Curve(PointOnCircle(p1a, t1a - pi2, r1), PointOnCircle(p2a, t2a + pi2, r2), p2a);
        svg.Line(p2b);
        svg.Curve(PointOnCircle(p2b, t2b - pi2, r2), PointOnCircle(p1b, t1b + pi2, r1), p1b);
        svg.Line(p1a);
    }
Пример #2
0
    private void DrawR(SVGData svg, Vector2 p, Vector2 pPrev, Vector2 pNext, float r, Matrix4x4 matrix, bool first = false)
    {
        var angle0 = Mathf.Atan2(p.y - pNext.y, p.x - pNext.x);
        var angle1 = Mathf.Atan2(p.y - pPrev.y, p.x - pPrev.x);

        if (angle0 > angle1)
        {
            angle0 = angle0 - Mathf.PI * 2f;
        }

        var angleBetween = Mathf.Lerp(angle0, angle1, 0.5f);

        var cx = p.x - Mathf.Cos(angleBetween) * r * Mathf.Sqrt(2f);
        var cy = p.y - Mathf.Sin(angleBetween) * r * Mathf.Sqrt(2f);

        var x0 = cx + Mathf.Cos(angle0) * r;
        var y0 = cy + Mathf.Sin(angle0) * r;
        var x1 = cx + Mathf.Cos(angle1) * r;
        var y1 = cy + Mathf.Sin(angle1) * r;

        var a        = r * (4f / 3f) * Mathf.Tan((angle1 - angle0) / 4f);
        var inAngle  = angle0 + Mathf.PI * 0.5f;
        var inX      = x0 + Mathf.Cos(inAngle) * a;
        var inY      = y0 + Mathf.Sin(inAngle) * a;
        var outAngle = angle1 - Mathf.PI * 0.5f;
        var outX     = x1 + Mathf.Cos(outAngle) * a;
        var outY     = y1 + Mathf.Sin(outAngle) * a;

        var p0   = matrix.MultiplyPoint(new Vector3(x0, y0));
        var p1   = matrix.MultiplyPoint(new Vector3(x1, y1));
        var pIn  = matrix.MultiplyPoint(new Vector3(inX, inY));
        var pOut = matrix.MultiplyPoint(new Vector3(outX, outY));

        if (first)
        {
            svg.Move(p0);
        }
        else
        {
            svg.Line(p0);
        }

        svg.Curve(pIn, pOut, p1);
    }
Пример #3
0
    public static SVGData BuildWave(Rect bounds, float waveCenterY, float waveHorRadius, float waveVertRadius,
                                    float sideWidth)
    {
        var rect      = bounds;
        var path      = new SVGData();
        var maskWidth = rect.width - sideWidth;

        path.Move(new Vector2(maskWidth - sideWidth, 0f - waveVertRadius * 2));
        path.Line(new Vector2(0f, 0f - waveVertRadius * 2));
        path.Line(new Vector2(0f, rect.height + waveVertRadius * 2));
        path.Line(new Vector2(maskWidth, rect.height + waveVertRadius * 2));

        var curveStartY = waveCenterY + waveVertRadius;

        path.Line(new Vector2(maskWidth, curveStartY));

        path.CurveOther(
            new Vector2(maskWidth - waveHorRadius * 0.1561501458f, curveStartY - waveVertRadius * 0.3322374268f),
            new Vector2(maskWidth, curveStartY - waveVertRadius * 0.1346194756f),
            new Vector2(maskWidth - waveHorRadius * 0.05341339583f, curveStartY - waveVertRadius * 0.2412779634f)
            );

        path.CurveOther(
            new Vector2(maskWidth - waveHorRadius * 0.5012484792f, curveStartY - waveVertRadius * 0.5350576951f),
            new Vector2(maskWidth - waveHorRadius * 0.2361659167f, curveStartY - waveVertRadius * 0.4030805244f),
            new Vector2(maskWidth - waveHorRadius * 0.3305285625f, curveStartY - waveVertRadius * 0.4561193293f)
            );

        path.CurveOther(
            new Vector2(maskWidth - waveHorRadius * 0.574934875f, curveStartY - waveVertRadius * 0.5689655122f),
            new Vector2(maskWidth - waveHorRadius * 0.515878125f, curveStartY - waveVertRadius * 0.5418222317f),
            new Vector2(maskWidth - waveHorRadius * 0.5664134792f, curveStartY - waveVertRadius * 0.5650349878f)
            );

        path.CurveOther(
            new Vector2(maskWidth - waveHorRadius * 0.8774032292f, curveStartY - waveVertRadius * 0.7399037439f),
            new Vector2(maskWidth - waveHorRadius * 0.7283715208f, curveStartY - waveVertRadius * 0.6397387195f),
            new Vector2(maskWidth - waveHorRadius * 0.8086618958f, curveStartY - waveVertRadius * 0.6833456585f)
            );

        path.CurveOther(
            new Vector2(maskWidth - waveHorRadius, curveStartY - waveVertRadius),
            new Vector2(maskWidth - waveHorRadius * 0.9653464583f, curveStartY - waveVertRadius * 0.8122605122f),
            new Vector2(maskWidth - waveHorRadius, curveStartY - waveVertRadius * 0.8936183659f)
            );

        path.CurveOther(
            new Vector2(maskWidth - waveHorRadius * 0.8608411667f, curveStartY - waveVertRadius * 1.270484439f),
            new Vector2(maskWidth - waveHorRadius, curveStartY - waveVertRadius * 1.100142878f),
            new Vector2(maskWidth - waveHorRadius * 0.9595746667f, curveStartY - waveVertRadius * 1.1887991951f)
            );

        path.CurveOther(
            new Vector2(maskWidth - waveHorRadius * 0.5291125625f, curveStartY - waveVertRadius * 1.4665102805f),
            new Vector2(maskWidth - waveHorRadius * 0.7852123333f, curveStartY - waveVertRadius * 1.3330544756f),
            new Vector2(maskWidth - waveHorRadius * 0.703382125f, curveStartY - waveVertRadius * 1.3795848049f)
            );

        path.CurveOther(
            new Vector2(maskWidth - waveHorRadius * 0.5015305417f, curveStartY - waveVertRadius * 1.4802616098f),
            new Vector2(maskWidth - waveHorRadius * 0.5241858333f, curveStartY - waveVertRadius * 1.4689677195f),
            new Vector2(maskWidth - waveHorRadius * 0.505739125f, curveStartY - waveVertRadius * 1.4781625854f)
            );

        path.CurveOther(
            new Vector2(maskWidth - waveHorRadius * 0.1541165417f, curveStartY - waveVertRadius * 1.687403f),
            new Vector2(maskWidth - waveHorRadius * 0.3187486042f, curveStartY - waveVertRadius * 1.5714239024f),
            new Vector2(maskWidth - waveHorRadius * 0.2332057083f, curveStartY - waveVertRadius * 1.6204116463f)
            );

        path.CurveOther(
            new Vector2(maskWidth, curveStartY - waveVertRadius * 2f),
            new Vector2(maskWidth - waveHorRadius * 0.0509933125f, curveStartY - waveVertRadius * 1.774752061f),
            new Vector2(maskWidth, curveStartY - waveVertRadius * 1.8709256829f)
            );

        path.Line(new Vector2(maskWidth, 0f - waveVertRadius * 2));
        return(path);
    }