Exemplo n.º 1
0
 // Token: 0x0600429B RID: 17051 RVA: 0x00152BC8 File Offset: 0x00150FC8
 private static void CollapsePolygons(List <Polygon> polygon)
 {
     if (polygon.Count > 0)
     {
         bool flag = true;
         while (flag)
         {
             flag = false;
             for (int i = 0; i < polygon.Count; i++)
             {
                 if (flag)
                 {
                     break;
                 }
                 for (int j = 0; j < polygon.Count; j++)
                 {
                     if (flag)
                     {
                         break;
                     }
                     if (i != j)
                     {
                         Polygon polygon2 = polygon[i];
                         Polygon polygon3 = polygon[j];
                         if (polygon2.IsPolygonInside(polygon3))
                         {
                             polygon2.AddHole(polygon3);
                             polygon.Remove(polygon3);
                             flag = true;
                         }
                         if (polygon3.IsPolygonInside(polygon2))
                         {
                             polygon3.AddHole(polygon2);
                             polygon.Remove(polygon2);
                             flag = true;
                         }
                     }
                 }
             }
         }
     }
 }
Exemplo n.º 2
0
        private void Triangulate(List <Dictionary <int, int> > contours, Utils.Plane plane, List <Vector3>[] vertices, List <Vector3>[] normals, List <Vector2>[] uvs, List <int>[] triangles, bool uvCutMesh)
        {
            if (contours.Count == 0 || contours[0].Count < 3)
            {
                return;
            }

            // prepare plane matrix
            var m    = plane.GetPlaneMatrix();
            var mInv = m.inverse;

            var zShit = 0.0f;

            var polygons = new List <Polygon>(contours.Count);

            // construct polygons from contours
            Polygon highAreaPoly = null;

            foreach (var ctr in contours)
            {
                var polygonPoints = new Vector2[ctr.Count];
                var j             = 0;

                foreach (var i in ctr.Values)
                {
                    var p = mInv * vertices[0][i];
                    polygonPoints[j++] = p;

                    // save z-coordinate
                    zShit = p.z;
                }

                var polygon = new Polygon(polygonPoints);
                polygons.Add(polygon);

                if (highAreaPoly == null || highAreaPoly.Area < polygon.Area)
                {
                    highAreaPoly = polygon;
                }
            }

            MeshUtils.Assert(polygons.Count > 0, "Zero polygons!");

            // test for holes
            if (polygons.Count > 0)
            {
                var polyToRemove = new List <Polygon>();

                foreach (var polygon in polygons)
                {
                    if (polygon != highAreaPoly)
                    {
                        if (highAreaPoly.IsPointInside(polygon.Points[0]))
                        {
                            highAreaPoly.AddHole(polygon);
                            polyToRemove.Add(polygon);
                        }
                    }
                }

                foreach (var polygon in polyToRemove)
                {
                    polygons.Remove(polygon);
                }
            }

            var vertCounter0 = vertices[0].Count;
            var vertCounter1 = vertices[1].Count;

            foreach (var polygon in polygons)
            {
                var indices = polygon.Triangulate();

                // get polygon bounding square size
                var min         = Mathf.Min(polygon.Min.x, polygon.Min.y);
                var max         = Mathf.Max(polygon.Max.x, polygon.Max.y);
                var polygonSize = min - max;

//                MeshUtils.Log("PolygonSize: " + polygonSize + " " + polygon.Min + " " + polygon.Max);

                foreach (var polyPoint in polygon.Points)
                {
                    var p = m * new Vector3(polyPoint.x, polyPoint.y, zShit);

                    vertices[0].Add(p);
                    vertices[1].Add(p);
                    normals[0].Add(-plane.Normal);
                    normals[1].Add(plane.Normal);

                    if (uvCutMesh)
                    {
                        var uv0 = new Vector2((polyPoint.x - min) / polygonSize,
                                              (polyPoint.y - min) / polygonSize);
                        var uv1 = new Vector2((polyPoint.x - min) / polygonSize,
                                              (polyPoint.y - min) / polygonSize);

                        // normalize uv to fit cross-section uv area
                        var areaSizeX = crossSectionUV.z - crossSectionUV.x;
                        var areaSizeY = crossSectionUV.w - crossSectionUV.y;

                        uv0.x = crossSectionUV.x + uv0.x * areaSizeX;
                        uv0.y = crossSectionUV.y + uv0.y * areaSizeY;
                        uv1.x = crossSectionUV.x + uv1.x * areaSizeX;
                        uv1.y = crossSectionUV.y + uv1.y * areaSizeY;

                        uvs[0].Add(uv0);
                        uvs[1].Add(uv1);
                    }
                    else
                    {
                        uvs[0].Add(Vector2.zero);
                        uvs[1].Add(Vector2.zero);
                    }
                }

                var indicesCount = indices.Count;
                var j            = indicesCount - 1;
                for (var i = 0; i < indicesCount; i++)
                {
                    triangles[0].Add(vertCounter0 + indices[i]);
                    triangles[1].Add(vertCounter1 + indices[j]);
                    j--;
                }

                vertCounter0 += polygon.Points.Length;
                vertCounter1 += polygon.Points.Length;
            }
        }
Exemplo n.º 3
0
        // Token: 0x060042AE RID: 17070 RVA: 0x00154E98 File Offset: 0x00153298
        private void Triangulate(List <Dictionary <int, int> > contours, PrimitivesPro.Utils.Plane plane, List <Vector3>[] vertices, List <Vector3>[] normals, List <Vector2>[] uvs, List <int>[] triangles, bool uvCutMesh)
        {
            if (contours.Count == 0 || contours[0].Count < 3)
            {
                return;
            }
            Matrix4x4      planeMatrix = plane.GetPlaneMatrix();
            Matrix4x4      inverse     = planeMatrix.inverse;
            float          z           = 0f;
            List <Polygon> list        = new List <Polygon>(contours.Count);
            Polygon        polygon     = null;

            foreach (Dictionary <int, int> dictionary in contours)
            {
                Vector2[] array = new Vector2[dictionary.Count];
                int       num   = 0;
                foreach (int index in dictionary.Values)
                {
                    Vector4 v = inverse * vertices[0][index];
                    array[num++] = v;
                    z            = v.z;
                }
                Polygon polygon2 = new Polygon(array);
                list.Add(polygon2);
                if (polygon == null || polygon.Area < polygon2.Area)
                {
                    polygon = polygon2;
                }
            }
            if (list.Count > 0)
            {
                List <Polygon> list2 = new List <Polygon>();
                foreach (Polygon polygon3 in list)
                {
                    if (polygon3 != polygon && polygon.IsPointInside(polygon3.Points[0]))
                    {
                        polygon.AddHole(polygon3);
                        list2.Add(polygon3);
                    }
                }
                foreach (Polygon item in list2)
                {
                    list.Remove(item);
                }
            }
            int num2 = vertices[0].Count;
            int num3 = vertices[1].Count;

            foreach (Polygon polygon4 in list)
            {
                List <int> list3 = polygon4.Triangulate();
                float      num4  = Mathf.Min(polygon4.Min.x, polygon4.Min.y);
                float      num5  = Mathf.Max(polygon4.Max.x, polygon4.Max.y);
                float      num6  = num4 - num5;
                foreach (Vector2 vector in polygon4.Points)
                {
                    Vector4 v2 = planeMatrix * new Vector3(vector.x, vector.y, z);
                    vertices[0].Add(v2);
                    vertices[1].Add(v2);
                    normals[0].Add(-plane.Normal);
                    normals[1].Add(plane.Normal);
                    if (uvCutMesh)
                    {
                        Vector2 item2 = new Vector2((vector.x - num4) / num6, (vector.y - num4) / num6);
                        Vector2 item3 = new Vector2((vector.x - num4) / num6, (vector.y - num4) / num6);
                        float   num7  = this.crossSectionUV.z - this.crossSectionUV.x;
                        float   num8  = this.crossSectionUV.w - this.crossSectionUV.y;
                        item2.x = this.crossSectionUV.x + item2.x * num7;
                        item2.y = this.crossSectionUV.y + item2.y * num8;
                        item3.x = this.crossSectionUV.x + item3.x * num7;
                        item3.y = this.crossSectionUV.y + item3.y * num8;
                        uvs[0].Add(item2);
                        uvs[1].Add(item3);
                    }
                    else
                    {
                        uvs[0].Add(Vector2.zero);
                        uvs[1].Add(Vector2.zero);
                    }
                }
                int count = list3.Count;
                int num9  = count - 1;
                for (int j = 0; j < count; j++)
                {
                    triangles[0].Add(num2 + list3[j]);
                    triangles[1].Add(num3 + list3[num9]);
                    num9--;
                }
                num2 += polygon4.Points.Length;
                num3 += polygon4.Points.Length;
            }
        }