Exemplo n.º 1
0
    private void OnSceneGUI()
    {
        BezierCurve curve     = (BezierCurve)target;
        Transform   t         = curve.transform;
        Quaternion  handleRot = Tools.pivotRotation == PivotRotation.Local ? t.rotation : Quaternion.identity;

        /* Transform curve points into world space */
        Vector3[] worldPoints = new Vector3[curve.points.Length];

        for (int i = 0; i < worldPoints.Length; i++)
        {
            worldPoints[i] = t.TransformPoint(curve.points[i]);
        }

        /* Draw handle line */
        Handles.color = SplineUIParams.handleLineColour;
        for (int i = 1; i < worldPoints.Length; i++)
        {
            Handles.DrawLine(worldPoints[i - 1], worldPoints[i]);
        }

        /* show handle for and update each point on curve */
        for (int i = 0; i < worldPoints.Length; i++)
        {
            Vector3 p = worldPoints[i];

            //display dot button for each curve point; only display handle of selected point
            //selectedIndex must persist between OnSceneGUI() calls or the handle will only appear for one call
            float size = HandleUtility.GetHandleSize(p);
            Handles.color = SplineUIParams.handleColour;
            if (Handles.Button(p, handleRot, size * SplineUIParams.handleSize,
                               size * SplineUIParams.handlePickSize, Handles.DotHandleCap))
            {
                selectedIndex = i;
            }

            if (i == selectedIndex)
            {
                EditorGUI.BeginChangeCheck();
                p = Handles.DoPositionHandle(p, handleRot); //DoPositionHandle creates an editor Handle at position and rotation
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(curve, "Move Bezier Curve Point"); //allow handle move to be undone with Undo
                    EditorUtility.SetDirty(curve);                       //set line to dirty so Unity knows a change was made and asks to save before closing etc.
                    curve.points[i] = t.InverseTransformPoint(p);        //transform moved world point back to local position and update line point with it
                }
            }
        }

        /* draw curve as line segments */
        float segmentLength = SplineUIParams.GetCurveSegmentLength(worldPoints);

        Handles.color = SplineUIParams.curveColour;
        for (float i = segmentLength; i <= 1; i += segmentLength)
        {
            Handles.DrawLine(curve.GetWorldPoint(i - segmentLength), curve.GetWorldPoint(i));
        }
    }
Exemplo n.º 2
0
    private void OnSceneGUI()
    {
        BezierCurve curve = (BezierCurve)target;

        float segmentLength = SplineUIParams.GetCurveSegmentLength(curve.points);

        for (float t = segmentLength; t <= 1; t += segmentLength)
        {
            Instantiate(windArrow, curve.GetWorldPoint(t), Quaternion.LookRotation(curve.GetWorldDir(t), Vector3.up));
        }
    }
Exemplo n.º 3
0
    private void OnSceneGUI()
    {
        BezierSpline spline    = (BezierSpline)target;
        Transform    transform = spline.transform;
        Quaternion   handleRot = Tools.pivotRotation == PivotRotation.Local ? transform.rotation : Quaternion.identity;

        /* Transform points of each curve into world space */
        List <Vector3[]> worldCurves = new List <Vector3[]>();

        //foreach(Vector3[] curve in spline.curves)
        foreach (Points curve in spline.curves)
        {
            Vector3[] worldCurve = new Vector3[curve.points.Length];
            for (int i = 0; i < curve.points.Length; i++)
            {
                worldCurve[i] = transform.TransformPoint(curve.points[i]);
            }

            worldCurves.Add(worldCurve);
        }

        /* Loop through each world space curve in the spline and update handles/lines/curves for it */
        for (int i = 0; i < worldCurves.Count; i++)
        {
            Vector3[] curve = worldCurves[i];

            /* Draw handle line */
            Handles.color = i == selectedCurveIndex ? SplineUIParams.selectedHandleLineColour : SplineUIParams.handleLineColour;
            for (int j = 1; j < curve.Length; j++)
            {
                Handles.DrawLine(curve[j - 1], curve[j]);
            }

            /* show handle for and update each point on curve */
            for (int j = 0; j < curve.Length; j++)
            {
                Vector3 p = curve[j];

                //display dot button for each curve point; only display handle of selected point
                //selectedIndex must persist between OnSceneGUI() calls or the handle will only appear for one call
                float size = HandleUtility.GetHandleSize(p);
                Handles.color = SplineUIParams.handleColour;
                if (Handles.Button(p, handleRot, size * SplineUIParams.handleSize,
                                   size * SplineUIParams.handlePickSize, Handles.DotHandleCap))
                {
                    selectedCurveIndex = i;
                    selectedPointIndex = j;
                }

                //display move handle for selected point
                if (i == selectedCurveIndex && j == selectedPointIndex)
                {
                    EditorGUI.BeginChangeCheck();
                    p = Handles.DoPositionHandle(p, handleRot); //DoPositionHandle creates an editor Handle at position and rotation
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(spline, "Move Bezier Curve Point"); //allow handle move to be undone with Undo
                        EditorUtility.SetDirty(spline);                       //set line to dirty so Unity knows a change was made and asks to save before closing etc.

                        //Transform moved world point back to local position and update selected point with it
                        Vector3 localPoint = transform.InverseTransformPoint(p);
                        spline.curves[i].points[j] = localPoint;

                        //If selected point is the last point of non-last curve in the spline, move first point of next curve to match;
                        //if selected point is the first point of non-first curve in the spline, move last point of previous curve to match
                        if (j == spline.curves[i].points.Length - 1 && i < spline.curves.Count - 1)
                        {
                            spline.curves[i + 1].points[0] = localPoint;
                        }
                        else if (j == 0 && i > 0)
                        {
                            spline.curves[i - 1].points[spline.curves[i - 1].points.Length - 1] = localPoint;
                        }
                    }
                }
            }

            /* draw curve as line segments */
            float segmentLength = SplineUIParams.GetCurveSegmentLength(curve);

            Color curveCol = i == selectedCurveIndex ? SplineUIParams.selectedCurveColour : SplineUIParams.curveColour;
            for (float t = segmentLength; t <= 1; t += segmentLength)
            {
                Handles.color = curveCol;
                Handles.DrawLine(spline.GetWorldPoint(i, t - segmentLength), spline.GetWorldPoint(i, t));

                if (showTangents)
                {
                    Handles.color = SplineUIParams.tanColour;
                    //Handles.DrawLine(spline.)
                }

                if (showNormals)
                {
                    Handles.color = SplineUIParams.normalColour;
                    //show normals
                }
            }
        }
    }