Exemple #1
0
    /// <summary>
    /// 切割模型
    /// </summary>
    /// <param name="includeSection"></param>
    /// <param name="includeOppositeFace"></param>
    /// <param name="remainOrigin">是否保留原始模型</param>
    /// <returns></returns>
    public SlicedMesh Slice(bool includeSection, bool includeOppositeFace)
    {
        this.m_includeSection      = includeSection;
        this.m_includeOppositeFace = includeOppositeFace;

        Mesh sliceMesh = this.m_originMesh;

        GeometryUtility.TriangleWindingOrder meshWindingOrder = GeometryUtility.GetMeshWindingOrder(sliceMesh);
        SlicedMesh slicedMesh = new SlicedMesh(meshWindingOrder);

        for (int j = 0; j < sliceMesh.triangles.Length; j += 3)
        {
            int vertexIndex0 = sliceMesh.triangles[j];
            int vertexIndex1 = sliceMesh.triangles[j + 1];
            int vertexIndex2 = sliceMesh.triangles[j + 2];

            Vector3 vertexPosition0 = sliceMesh.vertices[vertexIndex0];
            Vector3 vertexPosition1 = sliceMesh.vertices[vertexIndex1];
            Vector3 vertexPosition2 = sliceMesh.vertices[vertexIndex2];

            Vector2 uv0 = Vector2.zero;
            Vector2 uv1 = Vector2.zero;
            Vector2 uv2 = Vector2.zero;

            if (sliceMesh.uv.Length != 0)
            {
                uv0 = sliceMesh.uv[vertexIndex0];
                uv1 = sliceMesh.uv[vertexIndex1];
                uv2 = sliceMesh.uv[vertexIndex2];
            }

            GeometryUtility.Triangle triangle = new GeometryUtility.Triangle(vertexPosition0, vertexPosition1, vertexPosition2, uv0, uv1, uv2);
            GeometryUtility.PlaneTriangleIntersectionResult intersectionResult = GeometryUtility.GetPlaneTriangleIntersectionResult(this.m_slicerPlane, triangle, includeOppositeFace);

            slicedMesh.UpperMeshTriangleList.AddRange(intersectionResult.UpperTriangleList);
            slicedMesh.UnderMeshTriangleList.AddRange(intersectionResult.UnderTriangleList);
            slicedMesh.CrossMeshVertexList.AddRange(intersectionResult.IntersectionPointList);
        }

        this.m_slicedUpperMesh = slicedMesh.UpperMesh;

        if (includeOppositeFace)
        {
            this.m_slicedUnderMesh = slicedMesh.UnderMesh;
        }

        //交界处的mesh
        if (includeSection)
        {
            //上交界处的法线是切面的反方向
            this.m_slicedUpperCrossMesh = slicedMesh.GetUpperCrossMesh(m_slicerPlane.normal);

            if (includeOppositeFace)
            {
                this.m_slicedUnderCrossMesh = slicedMesh.GetUnderCrossMesh(m_slicerPlane.normal);
            }
        }

        return(slicedMesh);
    }
Exemple #2
0
    /// <summary>
    /// 提供方便方法直接切割TriangleList
    /// </summary>
    /// <param name="triangleList"></param>
    /// <param name="includeSection"></param>
    /// <param name="includeOppositeFace"></param>
    /// <returns></returns>
    public static SlicedMesh SliceTriangleList(List <GeometryUtility.Triangle> triangleList, Plane slicerPlane, bool includeSection, bool includeOppositeFace)
    {
        if (triangleList.Count < 1)
        {
            return(null);
        }

        GeometryUtility.TriangleWindingOrder windingOrder = triangleList[0].WindingOrder;
        SlicedMesh slicedMesh = new SlicedMesh(windingOrder);

        for (int i = 0; i < triangleList.Count; ++i)
        {
            GeometryUtility.Triangle sliceTriangle = triangleList[i];
            GeometryUtility.PlaneTriangleIntersectionResult planeTriangleIntersectionResult = GeometryUtility.GetPlaneTriangleIntersectionResult(slicerPlane, sliceTriangle, includeOppositeFace);

            if (!planeTriangleIntersectionResult.IsIntersection)
            {
                continue;
            }

            slicedMesh.UpperMeshTriangleList.AddRange(planeTriangleIntersectionResult.UpperTriangleList);
            if (includeSection)
            {
                slicedMesh.CrossMeshVertexList.AddRange(planeTriangleIntersectionResult.IntersectionPointList);
            }
        }

        return(slicedMesh);
    }