예제 #1
0
 /// <summary>
 /// Gets the index of a ControlPoint.
 /// </summary>
 /// <param name="cP">ControlPoint to find the index of.</param>
 /// <returns>Returns the index of [cP].</returns>
 public int GetControlPointIndex(ControlPoint cP)
 {
     return(controlPoints.IndexOf(cP));
 }
예제 #2
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();
        }