Beispiel #1
0
        private void DrawBezierCurve()
        {
            this.ContentGrid.Children.Remove(_bezierCanvas);
            _bezierCanvas.Children.Clear();
            BezierPoints.Clear();

            var pointList = new List <Point>();

            for (int i = 0; i <= _insertedPoint; i++)
            {
                pointList.Add(PointList[i].Point);
            }

            DrawCasteljau(pointList);

            for (int i = 0; i < BezierPoints.Count - 1; i++)
            {
                var line = new Line
                {
                    Stroke          = Brushes.Red,
                    StrokeThickness = 1,
                    X1 = BezierPoints[i].X,
                    Y1 = BezierPoints[i].Y,
                    X2 = BezierPoints[i + 1].X,
                    Y2 = BezierPoints[i + 1].Y
                };

                _bezierCanvas.Children.Add(line);
            }

            this.ContentGrid.Children.Add(_bezierCanvas);
        }
        public void Load(EngineNS.IO.XndNode xndNode)
        {
            BezierPoints.Clear();
            var att = xndNode.FindAttrib("_bezierPts");

            if (att != null)
            {
                att.BeginRead();
                switch (att.Version)
                {
                case 0:
                {
                    int count;
                    att.Read(out count);
                    for (int i = 0; i < count; i++)
                    {
                        var bPt = att.ReadMetaObject() as BezierPoint;
                        if (bPt != null)
                        {
                            bPt.CalculateOpposite();
                            BezierPoints.Add(bPt);
                        }
                    }
                    att.Read(out mBezierWidth);
                    att.Read(out mBezierHeight);
                }
                break;
                }
                att.EndRead();
            }
        }
Beispiel #3
0
        private void DrawCasteljau(List <Point> points)
        {
            Point tmp;

            for (double t = 0; t <= 1; t += 0.01)
            {
                tmp = GetCasteljauPoint(points.Count - 1, 0, t);

                BezierPoints.Add(new Point(tmp.X, tmp.Y));
            }
        }
Beispiel #4
0
 public void Clear()
 {
     if (BezierPoints.Count > 0)
     {
         BezierPoints.Clear();
     }
     if (_lstCtrPoints.Count > 0)
     {
         _lstCtrPoints.Clear();
     }
 }
Beispiel #5
0
        protected void CountBezierPoints(List <Vector3> pts)
        {
            int curveDivision = 10;

            curvePoints = new List <Vector3>();
            var     bp           = new BezierPoints();
            Vector3 tempVec      = new Vector3();
            int     current      = 0;
            var     cameraMatrix = scene.ActiveCamera.ViewProjectionMatrix;

            Action <int> getSize = (y) =>
            {
                var rectPts = pts.Skip(current).Take(y).Select(x => (cameraMatrix * new Vector4(x, 1.0)).ToNormalizedVector3()).ToList();
                var xMin    = rectPts.Select(x => x.X).Min();
                var yMin    = rectPts.Select(x => x.Y).Min();
                var xMax    = rectPts.Select(x => x.X).Max();
                var yMax    = rectPts.Select(x => x.Y).Max();
                var size    = new Vector2(xMax - xMin, yMax - yMin);
                size.X        = size.X * scene.ScreenSize.X;
                size.Y        = size.Y * scene.ScreenSize.Y;
                curveDivision = (int)(System.Math.Max(size.X, size.Y) / 5);
                curveDivision = System.Math.Min(curveDivision, 500);
            };

            Action <double> berenstein4Points = (x) =>
            {
                double x2  = x * x;
                double x3  = x2 * x;
                double x11 = (1 - x);
                double x12 = x11 * (1 - x);
                double x13 = x12 * (1 - x);

                tempVec.X = bp.p0.X * x13 + 3 * bp.p1.X * x12 * x
                            + 3 * bp.p2.X * x2 * x11 + bp.p3.X * x3;

                tempVec.Y = bp.p0.Y * x13 + 3 * bp.p1.Y * x12 * x
                            + 3 * bp.p2.Y * x2 * x11 + bp.p3.Y * x3;

                tempVec.Z = bp.p0.Z * x13 + 3 * bp.p1.Z * x12 * x
                            + 3 * bp.p2.Z * x2 * x11 + bp.p3.Z * x3;

                curvePoints.Add(tempVec);
            };
            Action <double> berenstein3Points = (x) =>
            {
                double x2  = x * x;
                double x11 = (1 - x);
                double x12 = x11 * (1 - x);

                tempVec.X = bp.p0.X * x12 + 2 * bp.p1.X * x11 * x
                            + bp.p2.X * x2;

                tempVec.Y = bp.p0.Y * x12 + 2 * bp.p1.Y * x11 * x
                            + bp.p2.Y * x2;

                tempVec.Z = bp.p0.Z * x12 + 2 * bp.p1.Z * x11 * x
                            + bp.p2.Z * x2;

                curvePoints.Add(tempVec);
            };
            int max = pts.Count;

            while (current + 4 <= max)
            {
                bp.p0 = pts[current];
                bp.p1 = pts[current + 1];
                bp.p2 = pts[current + 2];
                bp.p3 = pts[current + 3];

                getSize(4);

                for (int i = 0; i <= curveDivision; i++)
                {
                    berenstein4Points(i / (double)curveDivision);
                }
                current += 3;
            }
            if (current < max - 1)
            {
                if (max - 1 - current == 2)
                {
                    bp.p0 = pts[current];
                    bp.p1 = pts[current + 1];
                    bp.p2 = pts[current + 2];

                    getSize(3);

                    for (int i = 0; i <= curveDivision; i++)
                    {
                        berenstein3Points(i / (double)curveDivision);
                    }
                }
                else
                {
                    curvePoints.Add(pts[current]);
                    curvePoints.Add(pts[current + 1]);
                }
            }
        }
    private void OnScene(SceneView sceneview)
    {
        BezierPoints bezierPoints = target as BezierPoints;

        if (target != null && bezierPoints.gameObject.activeSelf)
        {
            if (bezierPoints == null || bezierPoints.CurvesCount == 0)
            {
                return;
            }

            if (!hideTools)
            {
                for (int i = 0; i < bezierPoints.CurvesCount; i++)
                {
                    EditorGUI.BeginChangeCheck();

                    Vector3 endPosition1 = bezierPoints.getEndpoint1(i);
                    Vector3 endPosition2 = bezierPoints.getEndpoint2(i);

                    Vector3 controlPosition1 = bezierPoints.getControlPoint1(i);
                    Vector3 controlPosition2 = bezierPoints.getControlPoint2(i);

                    Quaternion startRotation = bezierPoints.getStartRotation(i);
                    Quaternion endRotation   = bezierPoints.getEndRotation(i);

                    Handles.color = Color.green;
                    Handles.DrawLine(endPosition1, endPosition2);
                    Handles.color = Color.red;
                    Handles.DrawLine(endPosition1, controlPosition1);
                    Handles.DrawLine(endPosition2, controlPosition2);
                    Handles.DrawLine(controlPosition1, controlPosition2);

                    endPosition1     = Handles.PositionHandle(endPosition1, Quaternion.identity);
                    endPosition2     = Handles.PositionHandle(endPosition2, Quaternion.identity);
                    controlPosition1 = Handles.PositionHandle(controlPosition1, Quaternion.identity);
                    controlPosition2 = Handles.PositionHandle(controlPosition2, Quaternion.identity);
                    startRotation    = Handles.RotationHandle(startRotation, endPosition1);
                    endRotation      = Handles.RotationHandle(endRotation, endPosition2);

                    //Draw handle
                    Handles.DrawBezier(endPosition1, endPosition2, controlPosition1, controlPosition2, bezierPoints.lineColor, null, 5);


                    GUIStyle style = new GUIStyle();
                    style.fontSize = (Mathf.Clamp(Mathf.RoundToInt((endPosition1 - endPosition2).magnitude * (10 - Mathf.RoundToInt(Vector3.Distance(Camera.current.transform.position, (endPosition1 + endPosition2) / 2)))),
                                                  25, Mathf.RoundToInt((endPosition1 - endPosition2).magnitude)));
                    //Handles.Label((endPosition1 + endPosition2 + controlPosition1 + controlPosition2) / 4, i.ToString(), style);
                    Handles.BeginGUI();
                    Vector3 pos   = (endPosition1 + endPosition2 + controlPosition1 + controlPosition2) / 4;
                    Vector2 pos2D = HandleUtility.WorldToGUIPoint(pos);
                    GUI.Label(new Rect(pos2D.x, pos2D.y, 100, 100), i.ToString(), style);
                    Handles.EndGUI();

                    //Handles.DrawBezier(endPosition1, endPosition2, controlPosition1, controlPosition2, Color.white, null, 2f);


                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(bezierPoints, "Beizer point changed");

                        bezierPoints.setEndPoint1(i, endPosition1);
                        bezierPoints.setEndPoint2(i, endPosition2);
                        bezierPoints.setControlPoint1(i, controlPosition1);
                        bezierPoints.setControlPoint2(i, controlPosition2);
                        bezierPoints.setStartRotation(i, startRotation);
                        bezierPoints.setEndRotation(i, endRotation);
                    }
                }
            }

            Handles.color = bezierPoints.lineColor;
        }
    }
    public override void OnInspectorGUI()
    {
        BezierPoints bezierPoints = (BezierPoints)target;

        bezierPoints.lineColor = EditorGUILayout.ColorField("Line Color", bezierPoints.lineColor);

        if (GUILayout.Button("Clear all curves"))
        {
            clearFoldedPrefs(bezierPoints.CurvesCount);

            Undo.RecordObject(bezierPoints, "Cleared beizer curves");
            bezierPoints.clearAll();
        }

        GUILayout.BeginHorizontal();

        bezierPoints.autoLerp      = EditorGUILayout.Toggle("Auto lerp", bezierPoints.autoLerp);
        bezierPoints.lerpBackwards = EditorGUILayout.Toggle("Lerp backwards", bezierPoints.lerpBackwards);

        GUILayout.EndHorizontal();

        bezierPoints.looping = EditorGUILayout.Toggle("Looping", bezierPoints.looping);

        EditorGUILayout.Space();

        //add curve button
        if (GUILayout.Button("Add Beizer curve"))
        {
            setFoldedPref(bezierPoints.CurvesCount, false);

            Undo.RecordObject(bezierPoints, "Added beizer curve");
            bezierPoints.addBeizeCurve();
        }

        //if points exists
        if (bezierPoints.CurvesCount > 0)
        {
            //slider for lerp value
            bezierPoints.lerpValue = EditorGUILayout.Slider("Lerp Value", bezierPoints.lerpValue, 0, 1);

            //show all points for each curve
            for (int i = 0; i < bezierPoints.CurvesCount; i++)
            {
                //Debug.Log(EditorPrefs.HasKey("folded" + (i / 2)) + " : " + i);
                setFoldedPref(i, EditorGUILayout.Foldout(EditorPrefs.GetBool("folded" + (i)), "Beizer curve " + i, true));
                if (EditorPrefs.HasKey("folded" + (i)) && EditorPrefs.GetBool("folded" + (i)))
                {
                    EditorGUI.indentLevel++;

                    EditorGUI.BeginChangeCheck();

                    Vector3 endPoint1 = EditorGUILayout.Vector3Field("End Point 1", bezierPoints.getEndpoint1(i));
                    Vector3 endPoint2 = EditorGUILayout.Vector3Field("End Point 2", bezierPoints.getEndpoint2(i));

                    Vector3 controlPoint1 = EditorGUILayout.Vector3Field("Control Point 1", bezierPoints.getControlPoint1(i));
                    Vector3 controlPoint2 = EditorGUILayout.Vector3Field("Control Point 2", bezierPoints.getControlPoint2(i));

                    GUILayout.BeginHorizontal();

                    AnimationCurve animCurve       = EditorGUILayout.CurveField("Speed curve", bezierPoints.getAnimationCurve(i));
                    float          speedMultiplier = EditorGUILayout.FloatField("Speed Multiplier", bezierPoints.getCurveMultiplier(i));

                    GUILayout.EndHorizontal();

                    EditorGUILayout.Space();

                    Quaternion startRotation = Quaternion.Euler(EditorGUILayout.Vector3Field("Start Rotation", bezierPoints.getStartRotation(i).eulerAngles));
                    Quaternion endRotation   = Quaternion.Euler(EditorGUILayout.Vector3Field("End Rotation", bezierPoints.getEndRotation(i).eulerAngles));

                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(bezierPoints, "Changed point position");

                        bezierPoints.setEndPoint1(i, endPoint1);
                        bezierPoints.setEndPoint2(i, endPoint2);

                        bezierPoints.setControlPoint1(i, controlPoint1);
                        bezierPoints.setControlPoint2(i, controlPoint2);

                        bezierPoints.setStartRotation(i, startRotation);
                        bezierPoints.setEndRotation(i, endRotation);

                        bezierPoints.setAnimationCurve(i, animCurve);
                        bezierPoints.setCurveMultiplier(i, Mathf.Clamp(speedMultiplier, 0.1f, int.MaxValue));
                    }

                    EditorGUI.indentLevel--;

                    GUILayout.BeginHorizontal();

                    if (GUILayout.Button("Clear"))
                    {
                        removeFoldedPrefAt(i, bezierPoints.CurvesCount);

                        Undo.RecordObject(bezierPoints, "Removed curve");
                        bezierPoints.removeCurve(i);
                    }

                    if (bezierPoints.CurvesCount > i + 1)
                    {
                        if (GUILayout.Button("Snap to next"))
                        {
                            Undo.RecordObject(bezierPoints, "Snap curve to next");
                            bezierPoints.setEndPoint2(i, bezierPoints.getEndpoint1(i + 1));
                        }
                    }
                    else
                    {
                        GUI.enabled = false;
                        GUILayout.Button("Snap to next");
                        GUI.enabled = true;
                    }

                    GUILayout.EndHorizontal();
                }
            }

            //clear all curves
            if (GUILayout.Button("Clear all curves"))
            {
                clearFoldedPrefs(bezierPoints.CurvesCount);

                Undo.RecordObject(bezierPoints, "Cleared beizer curves");
                bezierPoints.clearAll();
            }
        }
    }