Beispiel #1
0
        // Token: 0x060042AB RID: 17067 RVA: 0x00154B1C File Offset: 0x00152F1C
        private int AddIntersectionPoint(Vector3 pos, MeshCutter.Triangle tri, int edge0, int edge1, Dictionary <long, int> cache, List <Vector3> vertices, List <Vector3> normals, List <Vector2> uvs)
        {
            int num = (edge0 >= edge1) ? ((edge1 << 16) + edge0) : ((edge0 << 16) + edge1);
            int result;

            if (cache.TryGetValue((long)num, out result))
            {
                return(result);
            }
            Vector3 vector = MeshUtils.ComputeBarycentricCoordinates(tri.pos[0], tri.pos[1], tri.pos[2], pos);

            vertices.Add(pos);
            normals.Add(new Vector3(vector.x * tri.normal[0].x + vector.y * tri.normal[1].x + vector.z * tri.normal[2].x, vector.x * tri.normal[0].y + vector.y * tri.normal[1].y + vector.z * tri.normal[2].y, vector.x * tri.normal[0].z + vector.y * tri.normal[1].z + vector.z * tri.normal[2].z));
            uvs.Add(new Vector2(vector.x * tri.uvs[0].x + vector.y * tri.uvs[1].x + vector.z * tri.uvs[2].x, vector.x * tri.uvs[0].y + vector.y * tri.uvs[1].y + vector.z * tri.uvs[2].y));
            int num2 = vertices.Count - 1;

            cache.Add((long)num, num2);
            return(num2);
        }
Beispiel #2
0
        // Token: 0x060042AA RID: 17066 RVA: 0x00153634 File Offset: 0x00151A34
        private float Cut(Mesh mesh, Transform meshTransform, PrimitivesPro.Utils.Plane plane, bool triangulateHoles, bool fixPivot, bool getContourList, bool dontCut, Vector4 crossSection, out Mesh mesh0, out Mesh mesh1, out Vector3 centroid0, out Vector3 centroid1, out ContourData intersectionData)
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();
            int num         = mesh.triangles.Length;
            int verticesNum = mesh.vertices.Length;

            int[]     array  = mesh.triangles;
            Vector3[] array2 = mesh.vertices;
            Vector3[] array3 = mesh.normals;
            Vector2[] uv     = mesh.uv;
            this.crossSectionUV = crossSection;
            this.AllocateBuffers(num, verticesNum);
            plane.InverseTransform(meshTransform);
            for (int i = 0; i < num; i += 3)
            {
                Vector3 vector   = array2[array[i]];
                Vector3 vector2  = array2[array[i + 1]];
                Vector3 vector3  = array2[array[i + 2]];
                bool    sideFix  = plane.GetSideFix(ref vector);
                bool    sideFix2 = plane.GetSideFix(ref vector2);
                bool    sideFix3 = plane.GetSideFix(ref vector3);
                array2[array[i]]     = vector;
                array2[array[i + 1]] = vector2;
                array2[array[i + 2]] = vector3;
                if (sideFix == sideFix2 && sideFix2 == sideFix3)
                {
                    int num2 = (!sideFix) ? 1 : 0;
                    if (this.triCache[array[i]] == 0)
                    {
                        this.triangles[num2].Add(this.triCounter[num2]);
                        this.vertices[num2].Add(array2[array[i]]);
                        this.normals[num2].Add(array3[array[i]]);
                        this.uvs[num2].Add(uv[array[i]]);
                        this.centroid[num2]    += array2[array[i]];
                        this.triCache[array[i]] = this.triCounter[num2] + 1;
                        this.triCounter[num2]++;
                    }
                    else
                    {
                        this.triangles[num2].Add(this.triCache[array[i]] - 1);
                    }
                    if (this.triCache[array[i + 1]] == 0)
                    {
                        this.triangles[num2].Add(this.triCounter[num2]);
                        this.vertices[num2].Add(array2[array[i + 1]]);
                        this.normals[num2].Add(array3[array[i + 1]]);
                        this.uvs[num2].Add(uv[array[i + 1]]);
                        this.centroid[num2]        += array2[array[i + 1]];
                        this.triCache[array[i + 1]] = this.triCounter[num2] + 1;
                        this.triCounter[num2]++;
                    }
                    else
                    {
                        this.triangles[num2].Add(this.triCache[array[i + 1]] - 1);
                    }
                    if (this.triCache[array[i + 2]] == 0)
                    {
                        this.triangles[num2].Add(this.triCounter[num2]);
                        this.vertices[num2].Add(array2[array[i + 2]]);
                        this.normals[num2].Add(array3[array[i + 2]]);
                        this.uvs[num2].Add(uv[array[i + 2]]);
                        this.centroid[num2]        += array2[array[i + 2]];
                        this.triCache[array[i + 2]] = this.triCounter[num2] + 1;
                        this.triCounter[num2]++;
                    }
                    else
                    {
                        this.triangles[num2].Add(this.triCache[array[i + 2]] - 1);
                    }
                }
                else
                {
                    this.cutTris.Add(i);
                }
            }
            if (this.vertices[0].Count == 0)
            {
                this.centroid[0] = array2[0];
            }
            else
            {
                this.centroid[0] /= (float)this.vertices[0].Count;
            }
            if (this.vertices[1].Count == 0)
            {
                this.centroid[1] = array2[1];
            }
            else
            {
                this.centroid[1] /= (float)this.vertices[1].Count;
            }
            mesh0            = null;
            mesh1            = null;
            centroid0        = this.centroid[0];
            centroid1        = this.centroid[1];
            intersectionData = null;
            if (this.cutTris.Count < 1)
            {
                stopwatch.Stop();
                return((float)stopwatch.ElapsedMilliseconds);
            }
            this.AllocateContours(this.cutTris.Count);
            foreach (int num3 in this.cutTris)
            {
                MeshCutter.Triangle tri = new MeshCutter.Triangle
                {
                    ids = new int[]
                    {
                        array[num3],
                        array[num3 + 1],
                        array[num3 + 2]
                    },
                    pos = new Vector3[]
                    {
                        array2[array[num3]],
                        array2[array[num3 + 1]],
                        array2[array[num3 + 2]]
                    },
                    normal = new Vector3[]
                    {
                        array3[array[num3]],
                        array3[array[num3 + 1]],
                        array3[array[num3 + 2]]
                    },
                    uvs = new Vector2[]
                    {
                        uv[array[num3]],
                        uv[array[num3 + 1]],
                        uv[array[num3 + 2]]
                    }
                };
                bool side  = plane.GetSide(tri.pos[0]);
                bool side2 = plane.GetSide(tri.pos[1]);
                bool side3 = plane.GetSide(tri.pos[2]);
                int  num4  = (!side) ? 1 : 0;
                int  num5  = 1 - num4;
                if (side == side2)
                {
                    float   num6;
                    Vector3 vector4;
                    bool    flag = plane.IntersectSegment(tri.pos[2], tri.pos[0], out num6, out vector4);
                    float   num7;
                    Vector3 vector5;
                    bool    flag2 = plane.IntersectSegment(tri.pos[2], tri.pos[1], out num7, out vector5);
                    int     num8  = this.AddIntersectionPoint(vector4, tri, tri.ids[2], tri.ids[0], this.cutVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    int     num9  = this.AddIntersectionPoint(vector5, tri, tri.ids[2], tri.ids[1], this.cutVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    int     item  = this.AddTrianglePoint(tri.pos[0], tri.normal[0], tri.uvs[0], tri.ids[0], this.triCache, this.cornerVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    int     item2 = this.AddTrianglePoint(tri.pos[1], tri.normal[1], tri.uvs[1], tri.ids[1], this.triCache, this.cornerVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    this.triangles[num4].Add(num8);
                    this.triangles[num4].Add(item);
                    this.triangles[num4].Add(num9);
                    this.triangles[num4].Add(num9);
                    this.triangles[num4].Add(item);
                    this.triangles[num4].Add(item2);
                    int num10 = this.AddIntersectionPoint(vector4, tri, tri.ids[2], tri.ids[0], this.cutVertCache[num5], this.vertices[num5], this.normals[num5], this.uvs[num5]);
                    int num11 = this.AddIntersectionPoint(vector5, tri, tri.ids[2], tri.ids[1], this.cutVertCache[num5], this.vertices[num5], this.normals[num5], this.uvs[num5]);
                    int item3 = this.AddTrianglePoint(tri.pos[2], tri.normal[2], tri.uvs[2], tri.ids[2], this.triCache, this.cornerVertCache[num5], this.vertices[num5], this.normals[num5], this.uvs[num5]);
                    this.triangles[num5].Add(item3);
                    this.triangles[num5].Add(num10);
                    this.triangles[num5].Add(num11);
                    if (triangulateHoles)
                    {
                        if (num4 == 0)
                        {
                            this.contour.AddTriangle(num3, num8, num9, vector4, vector5);
                        }
                        else
                        {
                            this.contour.AddTriangle(num3, num10, num11, vector4, vector5);
                        }
                    }
                }
                else if (side == side3)
                {
                    float   num6;
                    Vector3 vector5;
                    bool    flag3 = plane.IntersectSegment(tri.pos[1], tri.pos[0], out num6, out vector5);
                    Vector3 vector4;
                    float   num7;
                    bool    flag4 = plane.IntersectSegment(tri.pos[1], tri.pos[2], out num7, out vector4);
                    int     num12 = this.AddIntersectionPoint(vector4, tri, tri.ids[1], tri.ids[2], this.cutVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    int     num13 = this.AddIntersectionPoint(vector5, tri, tri.ids[1], tri.ids[0], this.cutVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    int     item4 = this.AddTrianglePoint(tri.pos[0], tri.normal[0], tri.uvs[0], tri.ids[0], this.triCache, this.cornerVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    int     item5 = this.AddTrianglePoint(tri.pos[2], tri.normal[2], tri.uvs[2], tri.ids[2], this.triCache, this.cornerVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    this.triangles[num4].Add(item5);
                    this.triangles[num4].Add(num13);
                    this.triangles[num4].Add(num12);
                    this.triangles[num4].Add(item5);
                    this.triangles[num4].Add(item4);
                    this.triangles[num4].Add(num13);
                    int num14 = this.AddIntersectionPoint(vector4, tri, tri.ids[1], tri.ids[2], this.cutVertCache[num5], this.vertices[num5], this.normals[num5], this.uvs[num5]);
                    int num15 = this.AddIntersectionPoint(vector5, tri, tri.ids[1], tri.ids[0], this.cutVertCache[num5], this.vertices[num5], this.normals[num5], this.uvs[num5]);
                    int item6 = this.AddTrianglePoint(tri.pos[1], tri.normal[1], tri.uvs[1], tri.ids[1], this.triCache, this.cornerVertCache[num5], this.vertices[num5], this.normals[num5], this.uvs[num5]);
                    this.triangles[num5].Add(num14);
                    this.triangles[num5].Add(num15);
                    this.triangles[num5].Add(item6);
                    if (triangulateHoles)
                    {
                        if (num4 == 0)
                        {
                            this.contour.AddTriangle(num3, num12, num13, vector4, vector5);
                        }
                        else
                        {
                            this.contour.AddTriangle(num3, num14, num15, vector4, vector5);
                        }
                    }
                }
                else
                {
                    float   num6;
                    Vector3 vector4;
                    bool    flag5 = plane.IntersectSegment(tri.pos[0], tri.pos[1], out num6, out vector4);
                    float   num7;
                    Vector3 vector5;
                    bool    flag6 = plane.IntersectSegment(tri.pos[0], tri.pos[2], out num7, out vector5);
                    int     num16 = this.AddIntersectionPoint(vector4, tri, tri.ids[0], tri.ids[1], this.cutVertCache[num5], this.vertices[num5], this.normals[num5], this.uvs[num5]);
                    int     num17 = this.AddIntersectionPoint(vector5, tri, tri.ids[0], tri.ids[2], this.cutVertCache[num5], this.vertices[num5], this.normals[num5], this.uvs[num5]);
                    int     item7 = this.AddTrianglePoint(tri.pos[1], tri.normal[1], tri.uvs[1], tri.ids[1], this.triCache, this.cornerVertCache[num5], this.vertices[num5], this.normals[num5], this.uvs[num5]);
                    int     item8 = this.AddTrianglePoint(tri.pos[2], tri.normal[2], tri.uvs[2], tri.ids[2], this.triCache, this.cornerVertCache[num5], this.vertices[num5], this.normals[num5], this.uvs[num5]);
                    this.triangles[num5].Add(item8);
                    this.triangles[num5].Add(num17);
                    this.triangles[num5].Add(item7);
                    this.triangles[num5].Add(num17);
                    this.triangles[num5].Add(num16);
                    this.triangles[num5].Add(item7);
                    int num18 = this.AddIntersectionPoint(vector4, tri, tri.ids[0], tri.ids[1], this.cutVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    int num19 = this.AddIntersectionPoint(vector5, tri, tri.ids[0], tri.ids[2], this.cutVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    int item9 = this.AddTrianglePoint(tri.pos[0], tri.normal[0], tri.uvs[0], tri.ids[0], this.triCache, this.cornerVertCache[num4], this.vertices[num4], this.normals[num4], this.uvs[num4]);
                    this.triangles[num4].Add(num19);
                    this.triangles[num4].Add(item9);
                    this.triangles[num4].Add(num18);
                    if (triangulateHoles)
                    {
                        if (num4 == 0)
                        {
                            this.contour.AddTriangle(num3, num18, num19, vector4, vector5);
                        }
                        else
                        {
                            this.contour.AddTriangle(num3, num16, num17, vector4, vector5);
                        }
                    }
                }
            }
            if (triangulateHoles || getContourList)
            {
                this.contour.FindContours();
            }
            List <int>[] array4 = null;
            if (triangulateHoles)
            {
                array4 = new List <int>[]
                {
                    new List <int>(this.contour.MidPointsCount),
                    new List <int>(this.contour.MidPointsCount)
                };
                this.Triangulate(this.contour.contour, plane, this.vertices, this.normals, this.uvs, array4, true);
            }
            intersectionData = null;
            if (getContourList)
            {
                List <Vector3[]> contourList = null;
                this.GetContourList(this.contour.contour, this.vertices[0], out contourList);
                intersectionData = new ContourData(contourList, meshTransform);
            }
            centroid0 = this.centroid[0];
            centroid1 = this.centroid[1];
            if (dontCut)
            {
                mesh0 = null;
                mesh1 = null;
                return((float)stopwatch.ElapsedMilliseconds);
            }
            if (this.vertices[0].Count > 0 && this.vertices[1].Count > 0)
            {
                mesh0 = new Mesh();
                mesh1 = new Mesh();
                Vector3[] array5 = this.vertices[0].ToArray();
                Vector3[] array6 = this.vertices[1].ToArray();
                if (fixPivot)
                {
                    MeshUtils.CenterPivot(array5, this.centroid[0]);
                    MeshUtils.CenterPivot(array6, this.centroid[1]);
                }
                mesh0.vertices = array5;
                mesh0.normals  = this.normals[0].ToArray();
                mesh0.uv       = this.uvs[0].ToArray();
                mesh1.vertices = array6;
                mesh1.normals  = this.normals[1].ToArray();
                mesh1.uv       = this.uvs[1].ToArray();
                if (triangulateHoles && array4[0].Count > 0)
                {
                    mesh0.subMeshCount = 2;
                    mesh0.SetTriangles(this.triangles[0].ToArray(), 0);
                    mesh0.SetTriangles(array4[0].ToArray(), 1);
                    mesh1.subMeshCount = 2;
                    mesh1.SetTriangles(this.triangles[1].ToArray(), 0);
                    mesh1.SetTriangles(array4[1].ToArray(), 1);
                }
                else
                {
                    mesh0.triangles = this.triangles[0].ToArray();
                    mesh1.triangles = this.triangles[1].ToArray();
                }
                stopwatch.Stop();
                return((float)stopwatch.ElapsedMilliseconds);
            }
            mesh0 = null;
            mesh1 = null;
            stopwatch.Stop();
            return((float)stopwatch.ElapsedMilliseconds);
        }