Пример #1
0
    public static void Node_Add(SPData sPData)
    {
        SceneView.currentDrawingSceneView.Repaint();
        EditorGUIUtility.AddCursorRect(Camera.current.pixelRect, MouseCursor.ArrowPlus);

        int     nIndex        = 0;
        string  vertexPostion = "init";
        Vector3 vert          = Vector3.zero;

        if (sPData.Nodes.Count >= 2)
        {
            vert = HandleUtility.ClosestPointToPolyLine(sPData.Vertices.ToArray());

            if (sPData.Close)
            {
                nIndex        = BranchClosestNodeToVertex(sPData, vert);
                vertexPostion = "mid";
            }
            else
            {
                if (Vector3.Distance(sPData.FirstNode.Point.position, vert) <= float.Epsilon)
                {
                    nIndex        = 0;
                    vertexPostion = "beg";
                }

                else if (Vector3.Distance(sPData.LastNode.Point.position, vert) <= float.Epsilon)
                {
                    nIndex        = sPData.Nodes.Count - 1;
                    vertexPostion = "end";
                }
                else
                {
                    nIndex        = BranchClosestNodeToVertex(sPData, vert);
                    vertexPostion = "mid";
                }
            }
        }



        // convert mouse position to vector3 world space
        Func <Vector2, Vector3> MousePos_To_Vector3 = delegate(Vector2 mousePosition)
        {
            float      distance = 0.0f;
            RaycastHit Hit;
            Ray        ray = new Ray();
            Plane      m_Plane;
            Vector3    hitPoint = Vector3.zero;

            ray = HandleUtility.GUIPointToWorldRay(mousePosition);

            if (Camera.current.transform.eulerAngles == new Vector3(0, 0, 0) || Camera.current.transform.eulerAngles == new Vector3(0, 180, 0))
            {
                m_Plane = new Plane(Vector3.forward, Vector3.zero);
            }
            else if (Camera.current.transform.eulerAngles == new Vector3(90, 0, 0) || Camera.current.transform.eulerAngles == new Vector3(270, 0, 0))
            {
                m_Plane = new Plane(Vector3.up, Vector3.zero);
            }
            else if (Camera.current.transform.eulerAngles == new Vector3(0, 90, 0) || Camera.current.transform.eulerAngles == new Vector3(0, 270, 0))
            {
                m_Plane = new Plane(Vector3.right, Vector3.zero);
            }
            else
            {
                m_Plane = new Plane(Vector3.up, Vector3.zero);
            }

            if (Physics.Raycast(ray, out Hit))
            {
                hitPoint = Hit.point;
            }
            else if (m_Plane.Raycast(ray, out distance))
            {
                hitPoint = ray.GetPoint(distance);
            }

            return(hitPoint);
        };

        // Add node to branch when click is performed
        Action <Vector3> Add_Node_Click = delegate(Vector3 pos)
        {
            if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
            {
                Undo.RecordObject(sPData.SplinePlus, "Node added");
                Func <string, Vector3, Quaternion, Transform, Transform> CreatePoint = delegate(string name, Vector3 Pos, Quaternion Rot, Transform Parent)
                {
                    var Obj = new GameObject(name);
                    Obj.hideFlags          = HideFlags.HideInHierarchy;
                    Obj.transform.position = Pos;
                    Obj.transform.rotation = Rot;
                    Obj.transform.parent   = Parent;
                    return(Obj.transform);
                };

                var node = new Node();

                node._Type = sPData.SplineSettings.NodeType;

                node.Point  = CreatePoint("p", pos, Quaternion.identity, sPData.SplinePlus.gameObject.transform);
                node.Point1 = CreatePoint("p1", node.Point.position, Quaternion.identity, node.Point);
                node.Point2 = CreatePoint("p2", node.Point.position, Quaternion.identity, node.Point);

                if (sPData.Close)
                {
                    SplinePlusAPI.Add_Node_At_Index(sPData, node, nIndex + 1);
                }
                else
                {
                    if (vertexPostion == "beg" || vertexPostion == "init")
                    {
                        SplinePlusAPI.Add_Node_Beginning(sPData, node);
                    }
                    else if (vertexPostion == "end")
                    {
                        SplinePlusAPI.Add_Node_End(sPData, node);
                    }
                    else
                    {
                        SplinePlusAPI.Add_Node_At_Index(sPData, node, nIndex + 1);
                    }
                }
            }
        };

        //perform node adding when click event happens
        if (vertexPostion == "init")
        {
            var hitPoint = MousePos_To_Vector3(Event.current.mousePosition);
            Handles.color = Color.yellow;
            Handles.SphereHandleCap(0, hitPoint, Quaternion.identity, sPData.SplineSettings.GizmosSize * 2, EventType.Repaint);

            Add_Node_Click(hitPoint);
        }
        // node added to the beginning of the branch
        else if (vertexPostion == "beg")
        {
            var pos  = MousePos_To_Vector3(Event.current.mousePosition);
            var node = sPData.Nodes[0];

            Preview(sPData, node.Point.position, pos);
            Add_Node_Click(pos);
        }
        // node added to the end of the branch
        else if (vertexPostion == "end")
        {
            var pos  = MousePos_To_Vector3(Event.current.mousePosition);
            var node = sPData.Nodes[sPData.Nodes.Count - 1];

            Preview(sPData, node.Point.position, pos);
            Add_Node_Click(pos);
        }
        // node added in the middle of the branch
        else
        {
            Handles.color = Color.yellow;
            Handles.SphereHandleCap(0, vert, Quaternion.identity, sPData.SplineSettings.GizmosSize * 2, EventType.Repaint);

            Add_Node_Click(vert);
        }
    }