public override void Execute( )
        {
            var pointView  = evt.data as PointView;
            var polyView   = pointView.poly;
            var polyEntity = lookup.libs[polyView];

            int count = polyEntity.Points.Count;

            if (count <= 3)
            {
                return;
            }

            var index     = polyView.points.IndexOf(pointView);
            var preIndex  = (count + index - 1) % count;
            var nextIndex = (index + 1) % count;


            //Calc new curve position
            var point_0 = polyEntity.Points[preIndex];
            var point_1 = polyEntity.Points[index];
            var point_2 = polyEntity.Points[nextIndex];

            var p0 = point_0.position;
            var p1 = point_1.position;
            var p2 = point_2.position;

            var h0 = Bezier.CalcHandler(p0, p1, point_0.curvePosition);
            var h1 = Bezier.CalcHandler(p1, p2, point_1.curvePosition);

            var newCurvePos = (h0 + h1) * 0.5f;

            Vector2 skew = (Vector2)newCurvePos;

            if (CGAlgorithm.Intersect2D_2SkewSegments(p0, h0, p2, h1, ref skew))
            {
                var cIsLeft = Mathf.Sign(CGAlgorithm.IsLeft(p0, p2, p1));
                var sIsLeft = Mathf.Sign(CGAlgorithm.IsLeft(p0, p2, skew));
                if (cIsLeft * sIsLeft > 0)
                {
                    newCurvePos = Bezier.CalcCurve(p0, p2, ( Vector3 )skew);
                }
            }


            var pointEntity = polyEntity.Points[index];
            var lineView    = polyView.lines[index];
            var preLine     = polyView.lines[preIndex];
            var nextPoint   = polyView.points[nextIndex];

            preLine.endPoint      = nextPoint;
            preLine.curvePosition = newCurvePos;// pointEntity.position;
            preLine.OnRender( );

            pointView.OnRecycle( );
            lineView.OnRecycle( );

            polyView.points.Remove(pointView);
            polyView.lines.Remove(lineView);

            //polyEntity.Points [ preIndex ].isCurve = true;
            polyEntity.Points [preIndex].curvePosition = newCurvePos;  // pointEntity.position;

            polyEntity.Points.RemoveAt(index);
        }
Exemple #2
0
        void HandleOnUpdateCurve( )
        {
            var count = Mathf.Ceil(1 / curveDetail);

            //Build the curve
            var handler = Bezier.CalcHandler(startPosition, endPosition, curvePosition);;
            //var handler = curvePosition;// //Bezier.Control(startPosition, endPosition, curvePosition);
            var center = Bezier.Square(startPosition, endPosition, handler, 0.5F);
            //centerPosition = new Vector3 ( center.x, center.y, 0 );

            var vertices  = new List <Vector3>();
            var triangles = new List <int>();
            var uv        = new List <Vector2>();
            var normals   = new List <Vector3>();
            var tangents  = new List <Vector4>();

            float UVX = 0F;

            Vector2 I0, I1, I2, I3;

            for (int i = 0; i < count; i++)
            {
                var pre  = Bezier.Square(startPosition, endPosition, handler, (float)(i - 1) / count);
                var cur  = Bezier.Square(startPosition, endPosition, handler, (float)i / count);
                var next = Bezier.Square(startPosition, endPosition, handler, (float)(i + 1) / count);

                var preTegent    = cur - pre;
                var nextTegent   = next - cur;
                var normal       = Vector3.back;
                var preBinormal  = Vector3.zero;
                var nextBinormal = Vector3.zero;

                Vector3.OrthoNormalize(ref normal, ref preTegent, ref preBinormal);
                Vector3.OrthoNormalize(ref normal, ref nextTegent, ref nextBinormal);

                var preOffset  = width * 0.5F * preBinormal;
                var nextOffset = width * 0.5F * nextBinormal;

                var p0 = pre + preOffset;   // (Vector2)(pre + preOffset);
                var p1 = cur + preOffset;   // (Vector2) (cur + preOffset);

                var p2 = cur + nextOffset;  // (Vector2) (cur + nextOffset);
                var p3 = next + nextOffset; // (Vector2) (next + nextOffset);

                var p4 = pre - preOffset;   // (Vector2) (pre - preOffset);
                var p5 = cur - preOffset;   // (Vector2) (cur - preOffset);

                var p6 = cur - nextOffset;  // (Vector2) (cur - nextOffset);
                var p7 = next - nextOffset; // (Vector2)  (next - nextOffset);

                var preLength  = Vector3.Distance(cur, pre);
                var nextLength = Vector3.Distance(next, cur);

                int size = vertices.Count;

                if (size != 0 && CGAlgorithm.Intersect2D_2Segments(p0, p1, p2, p3, out I0, out I1) > 0)
                {
                    int interIndex = size - 2;
                    vertices [interIndex] = I0;
                    vertices.AddRange(new Vector3 [] { p6, I0, p3, p7 });

                    triangles.AddRange(new int [] { size, size + 2, size + 1, size, size + 3, size + 2, size - 1, size, interIndex });
                }
                else if (size != 0 && CGAlgorithm.Intersect2D_2Segments(p4, p5, p6, p7, out I2, out I3) > 0)
                {
                    int interIndex = size - 1;
                    vertices [interIndex] = I2;
                    vertices.AddRange(new Vector3 [] { I2, p2, p3, p7 });
                    triangles.AddRange(new int [] { size, size + 2, size + 1, size, size + 3, size + 2, interIndex, size + 1, size - 2 });
                }
                else
                {
                    vertices.AddRange(new Vector3 [] { p6, p2, p3, p7 });
                    triangles.AddRange(new int [] { size, size + 2, size + 1, size, size + 3, size + 2 });
                }

                var preUVX = UVX;
                UVX += nextLength / width;
                uv.AddRange(new Vector2 [] { new Vector2(preUVX, 0), new Vector2(preUVX, 1), new Vector2(UVX, 1), new Vector2(UVX, 0) });
                normals.AddRange(new Vector3 [] { Vector3.back, Vector3.back, Vector3.back, Vector3.back });
                tangents.AddRange(new Vector4 [] { new Vector4(-1, 0, 0, -1), new Vector4(-1, 0, 0, -1), new Vector4(-1, 0, 0, -1), new Vector4(-1, 0, 0, -1) });
            }

            Vertices  = vertices.ToArray( );
            Triangles = triangles.ToArray( );
            UV        = uv.ToArray( );
            Normals   = normals.ToArray( );
            Tangents  = tangents.ToArray( );
        }
 public List <Vector3> SplitToSegments(IPointEntity next, int count)
 {
     return(Bezier.Split(this.position, next.position, this.curvePosition, count));
 }
Exemple #4
0
        public override void Execute( )
        {
            var args     = evt.data as InsertAtLineEventArgs;
            var lineView = args.view;
            var pos      = args.pos;

            var polyView = lineView.poly;
            var index    = polyView.lines.IndexOf(lineView);

            int count     = polyView.lines.Count;
            int nextIndex = (index + 1) % count;

            var nextPoint = polyView.points [nextIndex];

            var polyEntity = lookup.libs[polyView];

            #region Calc 1 new end point ans 2 curves point 's position
            var point_0 = polyEntity.Points[index];
            var point_1 = polyEntity.Points[nextIndex];

            var p0 = point_0.position;
            var p1 = point_1.position;
            var c  = point_0.curvePosition;
            var h  = Bezier.CalcHandler(p0, p1, c);

            var factor = Bezier.BinaryFactor(p0, p1, c, pos);

            //var pNew = Bezier.Square(p0, h, p1, factor);

            var h0          = Vector3.Lerp(p0, h, factor);
            var h1          = Vector3.Lerp(h, p1, factor);
            var newPointPos = Vector3.Lerp(h0, h1, factor);
            var c0          = Bezier.CalcCurve(p0, newPointPos, h0);
            var c1          = Bezier.CalcCurve(newPointPos, p1, h1);

            #endregion

            polyEntity.Points [index].curvePosition = c0;
            //polyEntity.Points [ index ].isCurve = true;

            //var pointEntity = new PointEntity(lineView.centerPosition);
            var pointEntity = new PointEntity(newPointPos, c1);

            var newPoint = pointPrefab.Spawn();
            newPoint.transform.parent        = polyView.pointParent;
            newPoint.transform.localPosition = newPointPos;//lineView.centerPosition;

            var newLine = linePrefab.Spawn();
            newLine.transform.parent        = polyView.lineParent;
            newLine.transform.localPosition = Vector3.zero;
            newLine.startPoint    = newPoint;
            newLine.curvePosition = c1;
            newLine.endPoint      = nextPoint;

            lineView.endPoint      = newPoint;
            lineView.curvePosition = c0;

            newLine.OnRender( );
            lineView.OnRender( );

            newPoint.poly = polyView;
            newLine.poly  = polyView;


            if (nextIndex != 0)
            {
                polyView.points.Insert(nextIndex, newPoint);
                polyView.lines.Insert(nextIndex, newLine);
                polyEntity.Points.Insert(nextIndex, pointEntity);
            }
            else
            {
                polyView.points.Add(newPoint);
                polyView.lines.Add(newLine);
                polyEntity.Points.Add(pointEntity);
            }

            //Debug.Log ( "Insert at " + nextIndex );
        }
 public float DistanceTo(IPointEntity next)
 {
     return(Bezier.ArcLength(this.position, next.position, this.curvePosition));
 }