예제 #1
0
    public void CalculateBevelOutline(List <Vector2> vertices, float strokeWidth, ref List <Vector3> verts, ref List <Vector2> uvs, ref List <int> tris)
    {
        int n = vertices.Count;

        int initIndex = verts.Count;

        int i       = 0;
        int vertNum = initIndex;

        float   pFactor = 0, nFactor = 0;
        Vector2 pVer = vertices[n - 1];
        Vector2 cVer = vertices[0];
        Vector2 nVer = vertices[1];
        float   pDis = Vector2.Distance(pVer, cVer);
        float   nDis;      // = Vector2.Distance(cVer, nVer);

//		int sign = ForceEarCut.ComputeSpannedAreaSign(pVer, cVer, nVer);
        Vector2 firstSkewPoint = pVer;

        Debug.LogError(gameObject.name + " is going on to enter while ... ! ");

        while (i < n + 1)
        {
            nDis = Vector2.Distance(cVer, nVer);
            if (nDis < strokeWidth)
            {
                pDis = Vector2.Distance(pVer, nVer);
                i++;
//				cVer = nVer;
                nVer = vertices[i % n];

                continue;
            }

            pFactor = pDis / strokeWidth;
            nFactor = nDis / strokeWidth;

            Vector2 perPC = CGAlgorithm.Perp(pVer, cVer, strokeWidth);
            Vector2 perCN = CGAlgorithm.Perp(cVer, nVer, strokeWidth);

            int sign = ForceEarCut.ComputeSpannedAreaSign(pVer, cVer, nVer);
            if (sign < 0)
            {
                //	sign < 0 :
                //		  p
                //		   \
                //			\
                //		=== c --> n ===

                Vector2 skewPoint = cVer + perPC;
                //CGAlgorithm.Intersect2D_2SkewSegments(pVer + perPC, cVer + perPC , cVer + perCN, nVer + perCN, ref skewPoint) ;
                if (CGAlgorithm.Intersect2D_2SkewSegments(pVer + perPC, cVer + perPC, cVer + perCN, nVer + perCN, ref skewPoint))
                {
                    if (vertNum > initIndex)
                    {
                        verts[vertNum - 1] = skewPoint;
                    }


                    verts.Add(pVer);
                    verts.Add(skewPoint);
                    verts.Add(cVer);
                    verts.Add(nVer + perCN);

                    uvs.Add(new Vector2(0, 0));
                    uvs.Add(new Vector2(1, pFactor));
                    uvs.Add(new Vector2(0, pFactor));
                    uvs.Add(new Vector2(1, pFactor + nFactor));

                    if (_facing == Facing.FACE_FORWARD)
                    {
                        tris.Add(vertNum);
                        tris.Add(vertNum + 2);
                        tris.Add(vertNum + 1);
                        tris.Add(vertNum + 2);
                        tris.Add(vertNum + 3);
                        tris.Add(vertNum + 1);
                    }
                    else if (_facing == Facing.FACE_BACK)
                    {
                        tris.Add(vertNum);
                        tris.Add(vertNum + 1);
                        tris.Add(vertNum + 2);
                        tris.Add(vertNum + 2);
                        tris.Add(vertNum + 1);
                        tris.Add(vertNum + 3);
                    }

                    vertNum += 4;
                }
            }
            else
            {
                // or sign > 0 :
                //					 p
                //		            /
                //				   /
                //		=== n <-- c ===
                //


                //				}else{
                //---------------------------------------------------------------------------

                verts.Add(pVer);
                verts.Add(cVer + perPC);
                verts.Add(cVer);
                verts.Add(cVer + perCN);
                verts.Add(nVer + perCN);

                uvs.Add(new Vector2(0, 0));
                uvs.Add(new Vector2(1, pFactor));
                uvs.Add(new Vector2(0, pFactor));
                uvs.Add(new Vector2(1, pFactor));
                uvs.Add(new Vector2(1, pFactor + nFactor));

                if (_facing == Facing.FACE_FORWARD)
                {
                    tris.Add(vertNum);
                    tris.Add(vertNum + 2);
                    tris.Add(vertNum + 1);
                    tris.Add(vertNum + 2);
                    tris.Add(vertNum + 3);
                    tris.Add(vertNum + 1);
                    tris.Add(vertNum + 2);
                    tris.Add(vertNum + 4);
                    tris.Add(vertNum + 3);
                }
                else if (_facing == Facing.FACE_BACK)
                {
                    tris.Add(vertNum);
                    tris.Add(vertNum + 1);
                    tris.Add(vertNum + 2);
                    tris.Add(vertNum + 2);
                    tris.Add(vertNum + 1);
                    tris.Add(vertNum + 3);
                    tris.Add(vertNum + 2);
                    tris.Add(vertNum + 3);
                    tris.Add(vertNum + 4);
                }
                vertNum += 5;
            }
            i++;
            pVer = cVer;
            cVer = nVer;
            nVer = vertices[i % n];
            pDis = nDis;
        }
    }
        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);
        }
예제 #3
0
    public void CalculateMeshData(List <Vector2> vertices, float strokeWidth, ref List <Vector3> verts, ref List <Vector2> uvs, ref List <int> tris)
    {
        int n = vertices.Count;

        int initIndex = verts.Count;

        int i       = 0;
        int vertNum = initIndex;

        float   pFactor = 0, nFactor = 0;
        Vector2 pVer = vertices[n - 1];
        Vector2 cVer = vertices[0];
        Vector2 nVer = vertices[1];
        float   pDis = Vector2.Distance(pVer, cVer);
        float   nDis;      // = Vector2.Distance(cVer, nVer);

        //		int sign = ForceEarCut.ComputeSpannedAreaSign(pVer, cVer, nVer);
        Vector2 firstSkewPoint = pVer;

        while (i < n + 1)
        {
            nDis = Vector2.Distance(cVer, nVer);
            if (nDis < strokeWidth)
            {
                pDis = Vector2.Distance(pVer, nVer);
                i++;
                //				cVer = nVer;
                nVer = vertices[i % n];

                continue;
            }

            pFactor = pDis / strokeWidth;
            nFactor = nDis / strokeWidth;

            Vector2 perPC = CGAlgorithm.Perp(pVer, cVer, strokeWidth);
            Vector2 perCN = CGAlgorithm.Perp(cVer, nVer, strokeWidth);

            Vector2 skewPoint = cVer + perPC;
            //CGAlgorithm.Intersect2D_2SkewSegments(pVer + perPC, cVer + perPC , cVer + perCN, nVer + perCN, ref skewPoint) ;
            Vector2 pcDir = (cVer - pVer).normalized * strokeWidth * 5;
            Vector2 cnDir = (cVer - nVer).normalized * strokeWidth * 5;
            if (CGAlgorithm.Intersect2D_2SkewSegments(
                    pVer + perPC, cVer + perPC + pcDir,
                    cVer + perCN + cnDir, nVer + perCN,
                    ref skewPoint))
            {
                if (vertNum == initIndex)
                {
                    firstSkewPoint = skewPoint;
                }
                else
                {
                    verts[vertNum - 1] = skewPoint;
                }
                verts.Add(pVer);
                verts.Add(skewPoint);
                verts.Add(cVer);
                if (i == n)
                {
                    verts.Add(firstSkewPoint);
                }
                else
                {
                    verts.Add(nVer);                     //(nVer + perCN);
                }

                uvs.Add(new Vector2(0, 0));
                uvs.Add(new Vector2(1, pFactor));
                uvs.Add(new Vector2(0, pFactor));

                uvs.Add(new Vector2(1, pFactor + nFactor));

                if (_facing == Facing.FACE_FORWARD)
                {
                    tris.Add(vertNum);
                    tris.Add(vertNum + 2);
                    tris.Add(vertNum + 1);
                    tris.Add(vertNum + 2);
                    tris.Add(vertNum + 3);
                    tris.Add(vertNum + 1);
                }
                else if (_facing == Facing.FACE_BACK)
                {
                    tris.Add(vertNum);
                    tris.Add(vertNum + 1);
                    tris.Add(vertNum + 2);
                    tris.Add(vertNum + 2);
                    tris.Add(vertNum + 1);
                    tris.Add(vertNum + 3);
                }

                vertNum += 4;
            }
            else
            {
                Debug.LogError("Outline .... no intersect???");
            }
            i++;
            pVer = cVer;
            cVer = nVer;
            nVer = vertices[i % n];
            pDis = nDis;
        }
    }