public int GetSubMeshCount() { Mesh mesh = MeshHelper.GetObjectMesh(this.gameObject); if (mesh != null) { return(MeshHelper.GetMeshSubMeshTriangleRanges(mesh).Length); } return(0); }
private void Initialize() { _mesh = MeshHelper.GetObjectMesh(this.gameObject); _meshSubMeshTriangleRanges = MeshHelper.GetMeshSubMeshTriangleRanges(_mesh); }
private void BooleanMesh(UnityEngine.Mesh surfaceMesh, RaycastHit raycastHit) { // Define the surfaces vertices and triangles Vector3[] vertices = surfaceMesh.vertices; int[] triangles = surfaceMesh.triangles; int startTriangle = 0; int endTriangle = triangles.Length; if (_onlyBooleanHitSubMesh) { int[] subMeshRanges = MeshHelper.GetMeshSubMeshTriangleRanges(surfaceMesh); int subMeshIndex = MeshHelper.GetSubMeshIndexByTriangle(raycastHit.triangleIndex, surfaceMesh.subMeshCount, subMeshRanges); MeshHelper.GetSubMeshRange(subMeshRanges, subMeshIndex, out startTriangle, out endTriangle); } // Define clipping planes Plane right = new Plane(Vector3.right, Vector3.right / 2f); Plane left = new Plane(-Vector3.right, -Vector3.right / 2f); Plane top = new Plane(Vector3.up, Vector3.up / 2f); Plane bottom = new Plane(-Vector3.up, -Vector3.up / 2f); Plane front = new Plane(Vector3.forward, Vector3.forward / 2f); Plane back = new Plane(-Vector3.forward, -Vector3.forward / 2f); // Define vertex matrix Matrix4x4 matrix = this.transform.worldToLocalMatrix * raycastHit.collider.transform.localToWorldMatrix; for (int i = startTriangle; i < endTriangle; i += 3) { // Define indices int indice0 = triangles[i]; int indice1 = triangles[i + 1]; int indice2 = triangles[i + 2]; // Define and offset verts to world space Vector3 vertice0 = matrix.MultiplyPoint(vertices[indice0]); Vector3 vertice1 = matrix.MultiplyPoint(vertices[indice1]); Vector3 vertice2 = matrix.MultiplyPoint(vertices[indice2]); Vector3 normal = GetTriangleNormal(vertice0, vertice1, vertice2); if (Vector3.Angle(-Vector3.forward, normal) >= _maxAngle) { continue; } List <Vector3> poly = new List <Vector3> { vertice0, vertice1, vertice2 }; ClipPoly(ref poly, right); if (poly.Count == 0) { continue; } ClipPoly(ref poly, left); if (poly.Count == 0) { continue; } ClipPoly(ref poly, top); if (poly.Count == 0) { continue; } ClipPoly(ref poly, bottom); if (poly.Count == 0) { continue; } ClipPoly(ref poly, front); if (poly.Count == 0) { continue; } ClipPoly(ref poly, back); if (poly.Count == 0) { continue; } AddPoly(poly, normal); } FinalizeMesh(); CompositeMesh(); }