Esempio n. 1
0
    private void drawPoints(ref Vector2Array points, bool isCap = true)
    {
        int count = points.Count;

        for (int i = 0; i < count; i++)
        {
            Handles.Label(points[i], string.Format("{0}", i));
            if (!isCap)
            {
                if (i >= count - 1)
                {
                    break;
                }
            }
            var p1 = points[i];
            var p2 = points[(i + 1 < count)?i + 1:0];
            if (i == _nearestID)
            {
                Handles.color = new Color(0, 1, 0);
            }
            else
            {
                Handles.color = new Color(0.5f, 1, 0.5f);
            }
            Handles.DrawLine(p1, p2);
        }
    }
Esempio n. 2
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        SlopeGen script = (SlopeGen)target;

        if (GUILayout.Button("Save points"))
        {
            Vector2Array points = new Vector2Array()
            {
                array = script.points
            };

            StreamWriter writer = new StreamWriter(getFileSavePath, false);
            writer.Write(JsonUtility.ToJson(points));
            writer.Close();
            Debug.Log("Saved new points");
        }

        if (GUILayout.Button("Load points"))
        {
            StreamReader reader = new StreamReader(getFileSavePath);
            script.points = JsonUtility.FromJson <Vector2Array>(reader.ReadToEnd()).array;
            reader.Close();
            Debug.Log("Loaded new points");
        }
    }
    private void drawPoints(ref Vector2Array points, bool isCap = true)
    {
        var gameObjPos = _asTarget.gameObject.transform.position;
        int count      = points.Count;

        for (int i = 0; i < count; i++)
        {
            var p1 = points[i];
            p1 = new Vector2(p1.x + gameObjPos.x, p1.y + gameObjPos.y);
            var p2 = points[(i + 1 < count)?i + 1:0];
            p2 = new Vector2(p2.x + gameObjPos.x, p2.y + gameObjPos.y);

            Handles.Label(p1, string.Format("{0}", i));
            if (!isCap)
            {
                if (i >= count - 1)
                {
                    break;
                }
            }

            if (i == _nearestID)
            {
                Handles.color = new Color(0, 1, 0);
            }
            else
            {
                Handles.color = new Color(0.5f, 1, 0.5f);
            }
            Handles.DrawLine(p1, p2);
        }
    }
Esempio n. 4
0
 private void deletePointWithIndex(ref Vector2Array points, int index)
 {
     if (points.Count < 3)
     {
         return;
     }
     Undo.RecordObject(_asTarget, "delete point");
     points.RemoveAt(index);
 }
    private void onSceneGUIAppendPointsHandler(ref Vector2Array points)
    {
        HandleUtility.Repaint();
        var gameObjPos = _asTarget.gameObject.transform.position;
        //鼠标位置
        var mousePos = Event.current.mousePosition;

        mousePos = HandleUtility.GUIPointToWorldRay(mousePos).origin;
        if (Event.current.isMouse)
        {
            EventType eventType = Event.current.type;
            if (Event.current.button == 0)
            {
                if (eventType == EventType.MouseDown)
                {
                    _isMousePress = true;
                    if (_editID == -1)
                    {
                        var mouseGUI = HandleUtility.WorldToGUIPoint(mousePos);
                        //Debug.Log("Insert");
                        Undo.RecordObject(_asTarget, "add point");
                        points.Insert(points.Count, new Vector2(mousePos.x - gameObjPos.x, mousePos.y - gameObjPos.y));
                        _editID = points.Count - 1;
                    }
                }
                else if (eventType == EventType.MouseUp)
                {
                    _isMousePress = false;
                    _editID       = -1;
                }
                if (!_isMousePress)                //鼠标没有按下时
                {
                    var cpt = new Vector2(mousePos.x, mousePos.y);

                    int     nearestId      = getNearestPointId(points, new Vector2(cpt.x - gameObjPos.x, cpt.y - gameObjPos.y));
                    Vector2 nearestWorldPt = new Vector2(points[nearestId].x + gameObjPos.x, points[nearestId].y + gameObjPos.y);
                    float   d = Vector2.Distance(HandleUtility.WorldToGUIPoint(cpt), HandleUtility.WorldToGUIPoint(nearestWorldPt));
                    if (nearestId > -1 && d <= SnapDistance)
                    {
                        cpt.Set(nearestWorldPt.x, nearestWorldPt.y);
                        //Debug.Log("_editID:"+_editID);
                        _editID = nearestId;
                    }
                    else
                    {
                        _editID = -1;
                    }

                    _asTarget.point.Set(cpt.x, cpt.y);
                }
            }
        }

        editPointHandler(ref points);
        //画点列表
        drawPoints(ref points, false);
    }
Esempio n. 6
0
        public void TestLinqAccess()
        {
            var buffer = new Byte[] { 1, 52, 43, 6, 23, 234 };

            var accessor = new Vector2Array(buffer, 0, Schema2.EncodingType.BYTE, true);

            var result = accessor.ToArray();

            Assert.AreEqual(3, result.Length);
        }
Esempio n. 7
0
    private void onAdd(ReorderableList list)
    {
        int listID = list.count;

        var          gameObjPos = _asTarget.gameObject.transform.position;
        Vector2Array vector2s   = new Vector2Array();

        vector2s.Add(new Vector2(-1 + gameObjPos.x, -1 + gameObjPos.y));
        vector2s.Add(new Vector2(1 + gameObjPos.x, 1 + gameObjPos.y));

        vector2s.listID = listID; //记录Drawer中使用的id
        _asTarget.paths.Add(vector2s);
        list.index = list.count;  //焦点移到最后一项
    }
Esempio n. 8
0
        public static (Vector2, Vector2) GetBounds(Vector2Array accesor)
        {
            var min = new Vector2(Single.MaxValue);
            var max = new Vector2(Single.MinValue);

            int c = accesor.Count;

            for (int i = 0; i < c; ++i)
            {
                var v = accesor[i];
                min = Vector2.Min(min, v);
                max = Vector2.Max(max, v);
            }

            return(min, max);
        }
Esempio n. 9
0
    private int getNearestPointId(Vector2Array points, Vector2 check)
    {
        int   id          = -1;
        float minDistance = float.MaxValue;

        for (int i = 0; i < points.Count; i++)
        {
            float d = Vector2.Distance(points[i], check);
            if (d < minDistance)
            {
                minDistance = d;
                id          = i;
            }
        }
        return(id);
    }
Esempio n. 10
0
        public static MeshPrimitive WithVertexAccessor(this MeshPrimitive primitive, string attribute, IReadOnlyList <Vector2> values)
        {
            var root = primitive.LogicalParent.LogicalParent;

            // create a vertex buffer and fill it
            var view  = root.UseBufferView(new Byte[8 * values.Count], 0, null, 0, BufferMode.ARRAY_BUFFER);
            var array = new Vector2Array(view.Content);

            array.FillFrom(0, values);

            var accessor = root.CreateAccessor();

            primitive.SetVertexAccessor(attribute, accessor);

            accessor.SetVertexData(view, 0, values.Count, DimensionType.VEC2, EncodingType.FLOAT, false);

            return(primitive);
        }
Esempio n. 11
0
    private void editPointHandler(ref Vector2Array points)
    {
        //Handles.Label(_asTarget.point,string.Format("({0},{1})",_asTarget.point.x,_asTarget.point.y));
        EditorGUI.BeginChangeCheck();
        float size     = HandleUtility.GetHandleSize(_asTarget.point) * 0.05f;
        var   snap     = Vector2.one * 0.05f;
        var   newPoint = Handles.FreeMoveHandle(_asTarget.point, Quaternion.identity, size, snap, Handles.DotHandleCap);

        if (EditorGUI.EndChangeCheck())
        {
            Undo.RecordObject(_asTarget, "edit point");           //记录更改,实现撤消回退
            _asTarget.point = newPoint;
            //Debug.Log(_editID);
            if (_editID > -1)
            {
                points[_editID] = newPoint;
            }
        }
    }
Esempio n. 12
0
 private void OnSceneGUI()
 {
     if (_asTarget.isActiveAndEnabled)
     {
         for (int i = 0; i < _asTarget.paths.Count; i++)
         {
             Vector2Array stroke = _asTarget.paths[i];
             if (stroke.Count >= 2)
             {
                 if (stroke.isEdit)
                 {
                     onSceneGUIEditPointsHandler(ref stroke);
                 }
                 else if (stroke.append)
                 {
                     onSceneGUIAppendPointsHandler(ref stroke);
                 }
             }
         }
     }
 }
Esempio n. 13
0
    private static void parseCharPoints(CharPoints charPoints, XmlElement parentElement, XmlDocument xml)
    {
        XmlElement charPointsElement = xml.CreateElement("CharPoints");

        List <Vector2Array> paths = charPoints.paths;

        for (int i = 0; i < paths.Count; i++)
        {
            Vector2Array stroke        = paths[i];
            XmlElement   strokeElement = xml.CreateElement("Stroke");
            for (int j = 0; j < stroke.Count; j++)
            {
                Vector2    v        = stroke[j];
                XmlElement vElement = xml.CreateElement("Vector2");
                vElement.SetAttribute("x", v.x.ToString());
                vElement.SetAttribute("y", v.y.ToString());
                strokeElement.AppendChild(vElement);
            }
            charPointsElement.AppendChild(strokeElement);
        }

        parentElement.AppendChild(charPointsElement);
    }
Esempio n. 14
0
    private void findSetNearestLineSegment(Vector2 refPoint, ref Vector2Array points, ref Vector2[] nearestLineSegment)
    {
        int   count = points.Count;
        float nearestLineDistance = 1e6f;

        for (int i = 0; i < count; i++)
        {
            var p1 = points[i];
            var p2 = points[(i + 1 < count)?i + 1:0];

            var perp = getPerpendicularPt(refPoint.x, refPoint.y, p1.x, p1.y, p2.x, p2.y);
            if (onSegment(perp.x, perp.y, p1.x, p1.y, p2.x, p2.y))
            {
                float distance = HandleUtility.DistancePointToLine(refPoint, p1, p2);
                if (distance < nearestLineDistance)
                {
                    nearestLineDistance   = distance;
                    _nearestID            = i;
                    nearestLineSegment[0] = p1;
                    nearestLineSegment[1] = p2;
                }
            }
        }
    }
Esempio n. 15
0
 public _MapVector2ToVector4(Vector2Array source)
 {
     _Accessor = source;
 }
Esempio n. 16
0
    private void onSceneGUIEditPointsHandler(ref Vector2Array points)
    {
        HandleUtility.Repaint();

        //鼠标位置
        var mousePos = Event.current.mousePosition;

        mousePos = HandleUtility.GUIPointToWorldRay(mousePos).origin;

        //寻找最近线段
        Vector2[] nearestLineSegment = new Vector2[2];
        findSetNearestLineSegment(mousePos, ref points, ref nearestLineSegment);

        if (Event.current.isMouse)
        {
            EventType eventType = Event.current.type;
            if (Event.current.button == 0)
            {
                if (eventType == EventType.MouseDown)
                {
                    _isMousePress = true;
                    var mouseGUI    = HandleUtility.WorldToGUIPoint(mousePos);
                    var nearestGUI0 = HandleUtility.WorldToGUIPoint(nearestLineSegment[0]);
                    var nearestGUI1 = HandleUtility.WorldToGUIPoint(nearestLineSegment[1]);
                    //float mouseToNearestLineSegment=HandleUtility.DistancePointToLineSegment(mouseGUI,nearestGUI0,nearestGUI1);
                    float d0 = Vector2.Distance(mouseGUI, nearestGUI0);
                    float d1 = Vector2.Distance(mouseGUI, nearestGUI1);
                    if (_editID == -1)
                    {
                        if (d0 <= SnapDistance)
                        {
                            //Debug.Log("d0<=SnapDistance");
                            if (Event.current.control)
                            {
                                deletePointWithIndex(ref points, _nearestID);
                            }
                            else
                            {
                                _editID = _nearestID;
                            }
                        }
                        else if (d1 <= SnapDistance)
                        {
                            //Debug.Log("d1<=SnapDistance");
                            int lineEndId = _nearestID + 1 <= points.Count - 1?_nearestID + 1:0;
                            if (Event.current.control)
                            {
                                deletePointWithIndex(ref points, lineEndId);
                            }
                            else
                            {
                                _editID = lineEndId;
                            }
                        }
                        else
                        {
                            //Debug.Log("Insert id:"+(_nearestID+1));
                            Undo.RecordObject(_asTarget, "add point");
                            points.Insert(_nearestID + 1, new Vector2(mousePos.x, mousePos.y));
                            _editID = _nearestID + 1;
                            findSetNearestLineSegment(mousePos, ref points, ref nearestLineSegment);
                        }
                    }
                }
                else if (eventType == EventType.MouseUp)
                {
                    _isMousePress = false;
                    _editID       = -1;
                }

                if (!_isMousePress)                //鼠标没有按下时
                //设置控制柄到最近线段的垂线
                {
                    var   perp        = getPerpendicularPt(mousePos.x, mousePos.y, nearestLineSegment[0].x, nearestLineSegment[0].y, nearestLineSegment[1].x, nearestLineSegment[1].y);
                    var   perpGUI     = HandleUtility.WorldToGUIPoint(perp);
                    var   nearestGUI0 = HandleUtility.WorldToGUIPoint(nearestLineSegment[0]);
                    var   nearestGUI1 = HandleUtility.WorldToGUIPoint(nearestLineSegment[1]);
                    float perpToNearestLineSegment = HandleUtility.DistancePointToLineSegment(perpGUI, nearestGUI0, nearestGUI1);
                    float d0  = Vector2.Distance(perpGUI, nearestGUI0);
                    float d1  = Vector2.Distance(perpGUI, nearestGUI1);
                    var   cpt = new Vector2(mousePos.x, mousePos.y);
                    //垂足不能滑出线段

                    /*if(perpToNearestLineSegment>0.01f){
                     *      if(d0<d1)perp.Set(nearestLineSegment[0].x,nearestLineSegment[0].y);
                     *      else perp.Set(nearestLineSegment[1].x,nearestLineSegment[1].y);
                     * }*/
                    //操作点贴紧端点
                    int   nearestId = getNearestPointId(points, cpt);
                    float d         = Vector2.Distance(HandleUtility.WorldToGUIPoint(cpt), HandleUtility.WorldToGUIPoint(points[nearestId]));
                    if (nearestId > -1 && d <= SnapDistance)
                    {
                        cpt.Set(points[nearestId].x, points[nearestId].y);
                        //Debug.Log("_editID:"+_editID);
                        _editID = nearestId;
                    }
                    else
                    {
                        _editID = -1;
                    }

                    _asTarget.point.Set(cpt.x, cpt.y);
                }
            }
        }
        editPointHandler(ref points);
        //画点列表
        drawPoints(ref points, false);
    }