CubicBezier() публичный статический Метод

public static CubicBezier ( float t, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3 ) : Vector2
t float
p0 Vector2
p1 Vector2
p2 Vector2
p3 Vector2
Результат Vector2
Пример #1
0
    // Token: 0x06000025 RID: 37 RVA: 0x00002AA8 File Offset: 0x00000EA8
    public static Vector2 NearestPointOnBezier(Vector2 p, Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3)
    {
        float   num    = float.PositiveInfinity;
        float   num2   = 0f;
        Vector2 result = Vector2.zero;

        for (float num3 = 0f; num3 < 1f; num3 += 0.01f)
        {
            Vector2 vector       = Mathfx.CubicBezier(num3, p0, p1, p2, p3);
            float   sqrMagnitude = (p - vector).sqrMagnitude;
            if (sqrMagnitude < num)
            {
                num    = sqrMagnitude;
                num2   = num3;
                result = vector;
            }
        }
        float num4 = Mathf.Clamp01(num2 - 0.01f);
        float num5 = Mathf.Clamp01(num2 + 0.01f);

        for (float num6 = num4; num6 < num5; num6 += 0.001f)
        {
            Vector2 vector2       = Mathfx.CubicBezier(num6, p0, p1, p2, p3);
            float   sqrMagnitude2 = (p - vector2).sqrMagnitude;
            if (sqrMagnitude2 < num)
            {
                num    = sqrMagnitude2;
                result = vector2;
            }
        }
        return(result);
    }
Пример #2
0
    private void DrawGizmoBezier(Vector3 p1, Vector3 p2)
    {
        Vector3 dir = p2 - p1;

        if (dir == Vector3.zero)
        {
            return;
        }

        Vector3 normal   = Vector3.Cross(Vector3.up, dir);
        Vector3 normalUp = Vector3.Cross(dir, normal);

        normalUp  = normalUp.normalized;
        normalUp *= dir.magnitude * 0.1f;

        Vector3 p1c = p1 + normalUp;
        Vector3 p2c = p2 + normalUp;

        Vector3 prev = p1;

        for (int i = 1; i <= 20; i++)
        {
            float   t = i / 20.0f;
            Vector3 p = Mathfx.CubicBezier(p1, p1c, p2c, p2, t);
            Gizmos.DrawLine(prev, p);
            prev = p;
        }
    }
Пример #3
0
    public List <Vector3> SmoothBezier(List <Vector3> path)
    {
        if (subdivisions < 0)
        {
            subdivisions = 0;
        }

        int            subMult    = 1 << subdivisions;
        List <Vector3> subdivided = ListPool <Vector3> .Claim();

        //new Vector3[(path.Length-1)*(int)subMult+1];

        for (int i = 0; i < path.Count - 1; i++)
        {
            Vector3 tangent1 = Vector3.zero;
            Vector3 tangent2 = Vector3.zero;
            if (i == 0)
            {
                tangent1 = path[i + 1] - path[i];
            }
            else
            {
                tangent1 = path[i + 1] - path[i - 1];
            }

            if (i == path.Count - 2)
            {
                tangent2 = path[i] - path[i + 1];
            }
            else
            {
                tangent2 = path[i] - path[i + 2];
            }

            tangent1 *= bezierTangentLength;
            tangent2 *= bezierTangentLength;

            Vector3 v1 = path[i];
            Vector3 v2 = v1 + tangent1;
            Vector3 v4 = path[i + 1];
            Vector3 v3 = v4 + tangent2;


            for (int j = 0; j < subMult; j++)
            {
                subdivided.Add(Mathfx.CubicBezier(v1, v2, v3, v4, (float)j / subMult));
            }
        }

        //Assign the last point
        subdivided.Add(path[path.Count - 1]);

        return(subdivided);
    }
Пример #4
0
        public void Init(Vector2 p0, Vector2 p1, Vector2 p2, Vector2 p3)
        {
            Vector2 topleft     = new Vector2(Mathf.Infinity, Mathf.Infinity);
            Vector2 bottomright = new Vector2(Mathf.NegativeInfinity, Mathf.NegativeInfinity);

            topleft.x = Mathf.Min(topleft.x, p0.x);
            topleft.x = Mathf.Min(topleft.x, p1.x);
            topleft.x = Mathf.Min(topleft.x, p2.x);
            topleft.x = Mathf.Min(topleft.x, p3.x);

            topleft.y = Mathf.Min(topleft.y, p0.y);
            topleft.y = Mathf.Min(topleft.y, p1.y);
            topleft.y = Mathf.Min(topleft.y, p2.y);
            topleft.y = Mathf.Min(topleft.y, p3.y);

            bottomright.x = Mathf.Max(bottomright.x, p0.x);
            bottomright.x = Mathf.Max(bottomright.x, p1.x);
            bottomright.x = Mathf.Max(bottomright.x, p2.x);
            bottomright.x = Mathf.Max(bottomright.x, p3.x);

            bottomright.y = Mathf.Max(bottomright.y, p0.y);
            bottomright.y = Mathf.Max(bottomright.y, p1.y);
            bottomright.y = Mathf.Max(bottomright.y, p2.y);
            bottomright.y = Mathf.Max(bottomright.y, p3.y);

            rect = new Rect(topleft.x, topleft.y, bottomright.x - topleft.x, bottomright.y - topleft.y);


            //ps = new Array ();
            List <Vector2> ps = new List <Vector2>();

            Vector2 point1 = Mathfx.CubicBezier(0, p0, p1, p2, p3);
            Vector2 point2 = Mathfx.CubicBezier(0.05f, p0, p1, p2, p3);
            Vector2 point3 = Mathfx.CubicBezier(0.1f, p0, p1, p2, p3);
            Vector2 point4 = Mathfx.CubicBezier(0.15f, p0, p1, p2, p3);

            Vector2 point5 = Mathfx.CubicBezier(0.5f, p0, p1, p2, p3);
            Vector2 point6 = Mathfx.CubicBezier(0.55f, p0, p1, p2, p3);
            Vector2 point7 = Mathfx.CubicBezier(0.6f, p0, p1, p2, p3);

            aproxLength = Vector2.Distance(point1, point2) + Vector2.Distance(point2, point3) + Vector2.Distance(point3, point4) + Vector2.Distance(point5, point6) + Vector2.Distance(point6, point7);

            Debug.Log(Vector2.Distance(point1, point2) + "     " + Vector2.Distance(point3, point4) + "   " + Vector2.Distance(point6, point7));
            aproxLength *= 4;

            float a2 = 0.5f / aproxLength;//Double the amount of points since the aproximation is quite bad

            for (float i = 0; i < 1; i += a2)
            {
                ps.Add(Mathfx.CubicBezier(i, p0, p1, p2, p3));
            }

            points = ps.ToArray();// ps.ToBuiltin(typeof(Vector2));
        }
Пример #5
0
    public Vector3[] SmoothBezier(Vector3[] path)
    {
        float subMult = Mathf.Pow(2, subdivisions);

        Vector3[] subdivided = new Vector3[(path.Length - 1) * (int)subMult + 1];


        for (int i = 0; i < path.Length - 1; i++)
        {
            Vector3 tangent1 = Vector3.zero;
            Vector3 tangent2 = Vector3.zero;
            if (i == 0)
            {
                tangent1 = path[i + 1] - path[i];
            }
            else
            {
                tangent1 = path[i + 1] - path[i - 1];
            }

            if (i == path.Length - 2)
            {
                tangent2 = path[i] - path[i + 1];
            }
            else
            {
                tangent2 = path[i] - path[i + 2];
            }

            tangent1 *= bezierTangentLength;
            tangent2 *= bezierTangentLength;

            Vector3 v1 = path[i];
            Vector3 v2 = v1 + tangent1;
            Vector3 v4 = path[i + 1];
            Vector3 v3 = v4 + tangent2;

            Debug.DrawLine(v1, v4, Color.black);
            Debug.DrawLine(v1, v2, Color.red);
            Debug.DrawLine(v4, v3, Color.red);

            for (int j = 0; j < (int)subMult; j++)
            {
                subdivided[i * (int)subMult + j] = Mathfx.CubicBezier(v1, v2, v3, v4, j / subMult);
                Debug.DrawLine(Vector3.Lerp(v1, v4, j / subMult), subdivided[i * (int)subMult + j], Color.blue);
            }
        }

        //Assign the last point
        subdivided[subdivided.Length - 1] = path[path.Length - 1];

        return(subdivided);
    }