Пример #1
0
        // Get List Of Polygons from Collider (Usually Used Before Creating Slicer2D Object)
        static public List <Polygon2D> CreateFromPolygonColliderToWorldSpace(PolygonCollider2D collider)
        {
            List <Polygon2D> result = new List <Polygon2D> ();

            if (collider != null && collider.pathCount > 0)
            {
                Polygon2D newPolygon = new Polygon2D();

                foreach (Vector2 p in collider.GetPath(0))
                {
                    newPolygon.AddPoint(p + collider.offset);
                }

                newPolygon = newPolygon.ToWorldSpace(collider.transform);

                result.Add(newPolygon);

                for (int i = 1; i < collider.pathCount; i++)
                {
                    Polygon2D hole = new Polygon2D();
                    foreach (Vector2 p in collider.GetPath(i))
                    {
                        hole.AddPoint(p + collider.offset);
                    }

                    hole = hole.ToWorldSpace(collider.transform);

                    if (newPolygon.PolyInPoly(hole) == true)
                    {
                        newPolygon.AddHole(hole);
                    }
                    else
                    {
                        result.Add(hole);
                    }
                }
            }
            return(result);
        }
Пример #2
0
        static public List <Vector2D> Get(Polygon2D polygon, float multiplier = 1f)
        {
            Polygon2D newPolygon = new Polygon2D();

            polygon.Normalize();

            Vector2D result;

            double   rotA, rotC;
            Vector2D pB;

            for (int i = 0; i < polygon.pointsList.Count; i++)
            {
                pB = polygon.pointsList[i];

                int indexB = polygon.pointsList.IndexOf(pB);

                int indexA = (indexB - 1);
                if (indexA < 0)
                {
                    indexA += polygon.pointsList.Count;
                }

                int indexC = (indexB + 1);
                if (indexC >= polygon.pointsList.Count)
                {
                    indexC -= polygon.pointsList.Count;
                }

                pair.A = polygon.pointsList[indexA];
                pair.B = pB;
                pair.C = polygon.pointsList[indexC];

                rotA = Vector2D.Atan2(pair.B, pair.A);
                rotC = Vector2D.Atan2(pair.B, pair.C);

                pairA.x = pair.A.x;
                pairA.y = pair.A.y;
                pairA.Push(rotA - Mathf.PI / 2, precision * multiplier);

                pairC.x = pair.C.x;
                pairC.y = pair.C.y;
                pairC.Push(rotC + Mathf.PI / 2, precision * multiplier);

                vecA.x = pair.B.x;
                vecA.y = pair.B.y;
                vecA.Push(rotA - Mathf.PI / 2, precision * multiplier);
                vecA.Push(rotA, 100f);

                vecC.x = pair.B.x;
                vecC.y = pair.B.y;
                vecC.Push(rotC + Mathf.PI / 2, precision * multiplier);
                vecC.Push(rotC, 100f);

                pair0.A = pairA;
                pair0.B = vecA;

                pair1.A = pairC;
                pair1.B = vecC;

                result = Math2D.GetPointLineIntersectLine(pair0, pair1);

                if (result != null)
                {
                    newPolygon.AddPoint(result);
                }
            }

            return(newPolygon.pointsList);
        }
Пример #3
0
 public void AddHole(Polygon2D poly)
 {
     holesList.Add(poly);
 }
Пример #4
0
        public static void SpriteToMesh3D(GameObject gameObject, VirtualSpriteRenderer spriteRenderer, float zSize, PolygonTriangulator2D.Triangulation triangulation = PolygonTriangulator2D.Triangulation.Advanced, Vector2D customUVOffset = null)
        {
            Texture2D texture = null;
            Sprite    sprite  = null;

            if (spriteRenderer.sprite != null)
            {
                sprite  = spriteRenderer.sprite;
                texture = sprite.texture;
            }

            if (customUVOffset == null)
            {
                customUVOffset = Vector2D.Zero();
            }

            float spriteSheetU = (float)(texture.width) / sprite.rect.width;
            float spriteSheetV = (float)(texture.height) / sprite.rect.height;

            Rect rect   = sprite.rect;
            Rect uvRect = new Rect((float)rect.x / texture.width, (float)rect.y / texture.height, (float)rect.width / texture.width, (float)rect.height / texture.height);

            Vector2 scale = new Vector2(spriteSheetU * rect.width / sprite.pixelsPerUnit, spriteSheetV * rect.height / sprite.pixelsPerUnit);

            if (spriteRenderer.flipX)
            {
                scale.x = -scale.x;
            }

            if (spriteRenderer.flipY)
            {
                scale.y = -scale.y;
            }

            float pivotX = sprite.pivot.x / sprite.rect.width - 0.5f;
            float pivotY = sprite.pivot.y / sprite.rect.height - 0.5f;

            float ix = -0.5f + pivotX / spriteSheetU;
            float iy = -0.5f + pivotY / spriteSheetV;

            Vector2 uvOffset = new Vector2(uvRect.center.x + ix, uvRect.center.y + iy);

            customUVOffset.x /= scale.x;
            customUVOffset.y /= scale.y;

            uvOffset += customUVOffset.ToVector2();

            Polygon2D polygon2D = Polygon2DList.CreateFromGameObject(gameObject)[0];

            polygon2D.CreateMesh3D(gameObject, zSize, scale, uvOffset, 0, triangulation);

            MeshRenderer meshRenderer = gameObject.GetComponent <MeshRenderer> ();

            if (meshRenderer == null)
            {
                meshRenderer = gameObject.AddComponent <MeshRenderer> ();
            }

            meshRenderer.sharedMaterial             = spriteRenderer.material;
            meshRenderer.sharedMaterial.mainTexture = texture;
            meshRenderer.sharedMaterial.color       = spriteRenderer.color;

            meshRenderer.sortingLayerName = spriteRenderer.sortingLayerName;
            meshRenderer.sortingLayerID   = spriteRenderer.sortingLayerID;
            meshRenderer.sortingOrder     = spriteRenderer.sortingOrder;
        }
Пример #5
0
 public Polygon2D(Polygon2D polygon)
 {
     pointsList = new List <Vector2D>(polygon.pointsList);
     holesList  = new List <Polygon2D>(polygon.holesList);
 }
Пример #6
0
        static public Polygon2D CreateFromCapsuleCollider(CapsuleCollider2D capsuleCollider, int pointsCount = -1)
        {
            if (pointsCount < 1)
            {
                pointsCount = defaultCircleVerticesCount;
            }

            Polygon2D newPolygon = new Polygon2D();

            Vector2 size   = new Vector2(capsuleCollider.size.x / 2, capsuleCollider.size.y / 2);
            float   offset = 0;
            float   i      = 0;

            switch (capsuleCollider.direction)
            {
            case CapsuleDirection2D.Vertical:
                float sizeXY = (capsuleCollider.transform.localScale.x / capsuleCollider.transform.localScale.y);
                size.x *= sizeXY;
                i       = 0;

                if (capsuleCollider.size.x < capsuleCollider.size.y)
                {
                    offset = (capsuleCollider.size.y - capsuleCollider.size.x) / 2;
                }

                while (i < 180)
                {
                    Vector2 v = new Vector2(Mathf.Cos(i * Mathf.Deg2Rad) * size.x, offset + Mathf.Sin(i * Mathf.Deg2Rad) * size.x);
                    newPolygon.AddPoint(v + capsuleCollider.offset);
                    i += 360f / (float)pointsCount;
                }

                while (i < 360)
                {
                    Vector2 v = new Vector2(Mathf.Cos(i * Mathf.Deg2Rad) * size.x, -offset + Mathf.Sin(i * Mathf.Deg2Rad) * size.x);
                    newPolygon.AddPoint(v + capsuleCollider.offset);
                    i += 360f / (float)pointsCount;
                }
                break;

            case CapsuleDirection2D.Horizontal:
                float sizeYX = (capsuleCollider.transform.localScale.y / capsuleCollider.transform.localScale.x);
                size.x *= sizeYX;                         // not size.y?
                i       = -90;

                if (capsuleCollider.size.y < capsuleCollider.size.x)
                {
                    offset = (capsuleCollider.size.x - capsuleCollider.size.y) / 2;
                }

                while (i < 90)
                {
                    Vector2 v = new Vector2(offset + Mathf.Cos(i * Mathf.Deg2Rad) * size.y, Mathf.Sin(i * Mathf.Deg2Rad) * size.y);
                    newPolygon.AddPoint(v + capsuleCollider.offset);
                    i += 360f / (float)pointsCount;
                }

                while (i < 270)
                {
                    Vector2 v = new Vector2(-offset + Mathf.Cos(i * Mathf.Deg2Rad) * size.y, Mathf.Sin(i * Mathf.Deg2Rad) * size.y);
                    newPolygon.AddPoint(v + capsuleCollider.offset);
                    i += 360f / (float)pointsCount;
                }
                break;
            }

            return(newPolygon);
        }
Пример #7
0
        public static Mesh Triangulate3D(Polygon2D polygon, float z, Vector2 UVScale, Vector2 UVOffset, float UVRotation, Triangulation triangulation)
        {
            polygon.Normalize();

            Mesh result = null;

            switch (triangulation)
            {
            case Triangulation.Advanced:
                List <Vector2> uvs           = new List <Vector2>();
                List <Vector3> sideVertices  = new List <Vector3>();
                List <int>     sideTriangles = new List <int>();

                Vector3 pointA = new Vector3(0, 0, 0);
                Vector3 pointB = new Vector3(0, 0, 0);
                Vector3 pointC = new Vector3(0, 0, z);
                Vector3 pointD = new Vector3(0, 0, z);

                Vector2 uv0 = new Vector2();
                Vector2 uv1 = new Vector2();

                float a, b;

                int vCount = 0;

                Pair2D pair = new Pair2D(new Vector2D(polygon.pointsList.Last()), null);
                foreach (Vector2D p in polygon.pointsList)
                {
                    pair.B = p;

                    pointA.x = (float)pair.A.x;
                    pointA.y = (float)pair.A.y;
                    pointB.x = (float)pair.B.x;
                    pointB.y = (float)pair.B.y;
                    pointC.x = (float)pair.B.x;
                    pointC.y = (float)pair.B.y;
                    pointD.x = (float)pair.A.x;
                    pointD.y = (float)pair.A.y;

                    sideVertices.Add(pointA);
                    sideVertices.Add(pointB);
                    sideVertices.Add(pointC);
                    sideVertices.Add(pointD);

                    a = ((float)pair.A.x + 25f) / 50 + UVOffset.x / 2;
                    b = ((float)pair.B.x + 25f) / 50 + UVOffset.x / 2;

                    uv0.x = a;
                    uv0.y = a;
                    uv1.x = b;
                    uv1.y = b;

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

                    sideTriangles.Add(vCount + 2);
                    sideTriangles.Add(vCount + 1);
                    sideTriangles.Add(vCount + 0);

                    sideTriangles.Add(vCount + 0);
                    sideTriangles.Add(vCount + 3);
                    sideTriangles.Add(vCount + 2);

                    vCount += 4;

                    pair.A = pair.B;
                }

                Mesh mainMesh = new Mesh();
                mainMesh.subMeshCount = 2;

                Mesh surfaceMesh = PerformTriangulationAdvanced(polygon, UVScale, UVOffset, UVRotation);

                ///// UVS  /////
                foreach (Vector2 p in surfaceMesh.uv)
                {
                    uvs.Add(p);
                }
                foreach (Vector2 p in surfaceMesh.uv)
                {
                    uvs.Add(p);
                }

                surfaceMesh.triangles = surfaceMesh.triangles.Reverse().ToArray();

                ///// VERTICES ////

                List <Vector3> vertices = new List <Vector3>();
                foreach (Vector3 v in sideVertices)
                {
                    vertices.Add(v);
                }

                foreach (Vector3 v in surfaceMesh.vertices)
                {
                    Vector3 nV = v;
                    nV.z = z;
                    vertices.Add(nV);
                }

                foreach (Vector3 v in surfaceMesh.vertices)
                {
                    vertices.Add(v);
                }

                mainMesh.SetVertices(vertices);

                ///// TRIANGLES /////

                List <int> triangles = new List <int>();
                foreach (int p in sideTriangles)
                {
                    triangles.Add(p);
                }
                mainMesh.SetTriangles(triangles, 0);

                triangles.Clear();
                int count = sideVertices.Count();
                foreach (int p in surfaceMesh.triangles)
                {
                    triangles.Add(p + count);
                }

                int   trisCount = surfaceMesh.triangles.Count() / 3;
                int[] tris      = surfaceMesh.triangles;

                for (var i = 0; i < trisCount; i++)
                {
                    var tmp = tris[i * 3];
                    tris[i * 3]     = tris[i * 3 + 1];
                    tris[i * 3 + 1] = tmp;
                }

                count += surfaceMesh.vertices.Count();
                foreach (int p in tris)
                {
                    triangles.Add(p + count);
                }

                mainMesh.SetTriangles(triangles, 1);

                ///// LEFTOVERS /////

                mainMesh.uv = uvs.ToArray();
                mainMesh.RecalculateNormals();
                mainMesh.RecalculateBounds();

                result = mainMesh;

                break;
            }

            return(result);
        }