Mesh Tools

Heathen.UnityPhysics.API.MeshTools is a static helper class for building convex hulls and measuring meshes. PhysicsData uses it, and you can call it directly for your own tools. The methods are static and do not log, apart from hull building which reports through System Log under UnityPhysics.MeshTools.

Building hulls #

MethodWhat it does
BuildHull(root, volumes, maxVertices = 0)Builds the convex hull of a set of mesh filters, in the local space of a root transform, as a HullShape. The hull is invalid when there is nothing to enclose.
CollectPoints(root, volumes, points)Collects the vertices of mesh filters into the local space of a root transform. Meshes that cannot be read (any mesh without Read/Write enabled in a built player) are skipped and reported. Shared meshes are read and no mesh instances are created. Returns how many meshes contributed.
CollectColliderPoints(root, colliders, points)Collects points that enclose a set of colliders. Mesh colliders contribute their vertices, box colliders their corners, sphere and capsule colliders an even spread of points on their surface.
SpherePoints(count)An even spread of points on a unit sphere.
ConvexHull(...)Builds a hull as a Unity Mesh. Overloads take a root transform and mesh filters, mesh filters, meshes, a single mesh filter or a list of points. Degenerate input such as coplanar points produces the bounding box.

Measuring meshes #

MethodWhat it does
VolumeOfMesh(mesh, scale)The volume of a closed mesh, always positive.
VolumeOfConvexMesh(mesh, scale)Builds the convex hull of a mesh and returns its volume.
SurfaceArea(mesh), SurfaceArea(mesh, scale)The surface area of a mesh, exact for non-uniform scale.
FacingSurfaceArea(mesh, direction) and the scaled overloadThe area of a mesh that faces into a direction, the leading cross section of a body moving along it. For a closed convex mesh it is the same as the shadow.
GetAreaAndVolume(mesh, scale, out area, out volume)The surface area and volume of a closed mesh in one pass.
CalculateNormal(p1, p2, p3)The unit normal of a triangle following the winding p1, p2, p3.
SignedVolumeOfTriangle(p1, p2, p3)The signed volume of the tetrahedron formed by a triangle and the origin, the building block of mesh volume.

Notes #

  • Meshes must be readable (Read/Write enabled) to be used at runtime in a built player. In the editor everything is readable, so bake with PhysicsData and avoid the question.
  • Hull building scales with the number of points, and the vertex limit reduces large meshes before the hull is built.
  • The convex hull algorithm is the bundled MIConvexHull library, MIT licensed, in the namespace Heathen.UnityPhysics.MIConvexHull. See ThirdPartyNotices.md in the package.
using Heathen.UnityPhysics;
using Heathen.UnityPhysics.API;

float area, volume;
MeshTools.GetAreaAndVolume(meshFilter.sharedMesh, transform.lossyScale, out area, out volume);

HullShape hull = MeshTools.BuildHull(transform, GetComponentsInChildren<MeshFilter>(), 96);

Rate This Article!