Example #1
0
        private Vector3 ShowPoint(int index)
        {
            Vector3 point = _handleTransform.TransformPoint(_spline.GetControlPoint(index));
            float   size  = HandleUtility.GetHandleSize(point);

            // To see more clearly where the spline begins, we double the size of the first point.
            if (index == 0)
            {
                size *= 2.0f;
            }
            Handles.color = s_modeColors[(int)_spline.GetControlPointMode(index)];
            if (Handles.Button(point, _handleRotation, size * handleSize, size * pickSize, Handles.DotHandleCap))
            {
                _selectedIndex = index;
                Repaint();
            }
            if (_selectedIndex == index)
            {
                EditorGUI.BeginChangeCheck();
                point = Handles.DoPositionHandle(point, _handleRotation);
                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(_spline, "Move Point");
                    EditorUtility.SetDirty(_spline);
                    _spline.SetControlPoint(index, _handleTransform.InverseTransformPoint(point));
                }
            }
            return(point);
        }
Example #2
0
 /// <summary>
 /// Gets the start ControlPoint of this curve.
 /// </summary>
 /// <returns>Returns the start ControlPoint of this curve.</returns>
 public ControlPoint GetStartPoint()
 {
     return(spline.GetControlPoint(controlPointIndices[0]));
 }
Example #3
0
        private void OnSceneGUI()
        {
            GetCurveAndTransform();
            Handles.BeginGUI();
            GUIStyle sceneLabelStyle = new GUIStyle();

            sceneLabelStyle.normal.textColor = Color.white;
            sceneLabelStyle.alignment        = TextAnchor.UpperCenter;

            #region Scene View Labels
            if (addPointMode)
            {
                GUI.Label(new Rect(Screen.width * 0.5f - 125, 10, 250, 30), "Left click to add a point", sceneLabelStyle);
            }
            if (insertPointMode)
            {
                GUI.Label(new Rect(Screen.width * 0.5f - 125, 10, 250, 30), "Left click to place a point", sceneLabelStyle);
            }
            if (selectedIndex > -1)
            {
                GUI.Label(new Rect(Screen.width * 0.5f - 125, 10, 250, 30), "Left click a point to move it", sceneLabelStyle);
            }
            if (deletePointMode)
            {
                GUI.Label(new Rect(Screen.width * 0.5f - 125, 10, 250, 30), "Left click a point to delete it", sceneLabelStyle);
            }
            Handles.EndGUI();
            #endregion

            #region Intercept Clicks
            if ((addPointMode || insertPointMode || deletePointMode) && Event.current.type == EventType.Layout)
            {
                HandleUtility.AddDefaultControl(0);
            }
            #endregion

            #region Add/Insert Control Points
            if ((addPointMode || insertPointMode) && Event.current.type == EventType.MouseDown && Event.current.button == 0)
            {
                Ray   ray   = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                Plane plane = new Plane(-Camera.current.transform.forward, handleTransform.position);
                float enter = 0.0f;

                if (addPointMode && plane.Raycast(ray, out enter))
                {
                    Vector3 hitPoint = handleTransform.InverseTransformPoint(ray.GetPoint(enter));

                    Undo.RecordObject(spline, "Add Control Point");
                    EditorUtility.SetDirty(spline);
                    lastPoint = new ControlPoint(hitPoint);

                    if (spline.GetControlPointCount() > 0)
                    {
                        Vector3 p      = spline.GetControlPoint(0);
                        Vector3 rayToP = (p - ray.origin);
                        float   angle  = Mathf.Deg2Rad * Vector3.Angle(rayToP.normalized, ray.direction);
                        float   dist   = Mathf.Sin(angle) * rayToP.magnitude;

                        if (dist < 0.2f)
                        {
                            lastPoint    = spline.GetControlPoint(0);
                            addPointMode = false;
                            ResetTools();
                            SceneView.RepaintAll();
                        }
                    }

                    spline.AddControlPoint(lastPoint);
                }
                else if (insertPointMode)
                {
                    float t;
                    float dist = -1;

                    if (spline.GetClosestDistanceToRay(ray, out dist))
                    {
                        Undo.RecordObject(spline, "Insert Control Point");
                        EditorUtility.SetDirty(spline);
                        Vector3 localPos = handleTransform.InverseTransformPoint(spline.GetPointOnSplineByDistance(dist));
                        lastPoint = new ControlPoint(localPos);
                        BezierCurve curve = spline.GetCurveFromDistance(dist, out t);
                        spline.InsertControlPoint(lastPoint, curve);
                    }
                }

                Event.current.Use();
            }

            if ((addPointMode || insertPointMode) && lastPoint != null && Event.current.type == EventType.MouseDrag && Event.current.button == 0)
            {
                Ray   ray   = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
                Plane plane = new Plane(-Camera.current.transform.forward, lastPoint);
                float enter = 0.0f;

                if (plane.Raycast(ray, out enter))
                {
                    Vector3 hitPoint = handleTransform.InverseTransformPoint(ray.GetPoint(enter));
                    lastPoint.tangentForward  = new CurveHandle(hitPoint - lastPoint);
                    lastPoint.tangentBackward = new CurveHandle(lastPoint - hitPoint);
                }

                Event.current.Use();
            }

            if ((addPointMode || insertPointMode) && Event.current.type == EventType.MouseUp && Event.current.button == 0)
            {
                Undo.RecordObject(spline, "Move Tangent");
                EditorUtility.SetDirty(spline);
                lastPoint = null;
                Event.current.Use();
            }
            #endregion

            spline.RenderCurvesInSceneView(!deletePointMode);
            RenderHandles();
        }