Table of Contents

Struct Mesh

Namespace
BepuPhysics.Collidables
Assembly
BepuPhysics.dll

Shape designed to contain a whole bunch of triangles. Triangle collisions and ray tests are one-sided; only tests which see the triangle as wound clockwise in right handed coordinates or counterclockwise in left handed coordinates will generate contacts.

public struct Mesh : IHomogeneousCompoundShape<Triangle, TriangleWide>, IDisposableShape, IShape, IBoundsQueryableCompound
Implements
Inherited Members

Constructors

Mesh(Buffer<Triangle>, Vector3, BufferPool, IThreadDispatcher)

Creates a mesh shape.

public Mesh(Buffer<Triangle> triangles, Vector3 scale, BufferPool pool, IThreadDispatcher dispatcher = null)

Parameters

triangles Buffer<Triangle>

Triangles to use in the mesh.

scale Vector3

Scale to apply to all vertices at runtime. Note that the scale is not baked into the triangles or acceleration structure; the same set of triangles and acceleration structure can be used across multiple Mesh instances with different scales.

pool BufferPool

Pool used to allocate acceleration structures.

dispatcher IThreadDispatcher

Dispatcher to use to multithread the execution of the mesh build process. If null, the build will be single threaded.

Mesh(Span<byte>, BufferPool)

Loads a mesh from data stored in a byte buffer previously stored by the Serialize function.

public Mesh(Span<byte> data, BufferPool pool)

Parameters

data Span<byte>

Data to load the mesh from.

pool BufferPool

Pool to create the mesh with.

Fields

Id

Type id of mesh shapes.

public const int Id = 8

Field Value

int

Tree

Acceleration structure of the mesh.

public Tree Tree

Field Value

Tree

Triangles

Buffer of triangles composing the mesh. Triangles will only collide with tests which see the triangle as wound clockwise in right handed coordinates or counterclockwise in left handed coordinates.

public Buffer<Triangle> Triangles

Field Value

Buffer<Triangle>

Properties

ChildCount

Gets the number of children in the compound shape.

public readonly int ChildCount { get; }

Property Value

int

Scale

Gets or sets the scale of the mesh.

public Vector3 Scale { get; set; }

Property Value

Vector3

TypeId

Unique type id for this shape type.

public static int TypeId { get; }

Property Value

int

Methods

ComputeBounds(Quaternion, out Vector3, out Vector3)

Computes the bounding box of a compound shape.

public readonly void ComputeBounds(Quaternion orientation, out Vector3 min, out Vector3 max)

Parameters

orientation Quaternion

Orientation of the compound.

min Vector3

Minimum of the compound's bounding box.

max Vector3

Maximum of the compound's bounding box.

ComputeClosedCenterOfMass()

Computes the center of mass of the mesh. Assumes the mesh is closed and should be treated as solid.

public readonly Vector3 ComputeClosedCenterOfMass()

Returns

Vector3

Center of mass of the closed mesh.

ComputeClosedCenterOfMass(out float, out Vector3)

Computes the volume and center of mass of the mesh. Assumes the mesh is closed and should be treated as solid.

public readonly void ComputeClosedCenterOfMass(out float volume, out Vector3 center)

Parameters

volume float

Volume of the closed mesh.

center Vector3

Center of mass of the closed mesh.

ComputeClosedInertia(float)

Computes the inertia of the mesh. Assumes the mesh is closed and should be treated as solid.

public readonly BodyInertia ComputeClosedInertia(float mass)

Parameters

mass float

Mass to scale the inertia tensor with.

Returns

BodyInertia

Inertia tensor of the closed mesh.

ComputeClosedInertia(float, out Vector3)

Computes the inertia of the mesh around its volumetric center and recenters the points of the mesh around it. Assumes the mesh is closed and should be treated as solid.

public BodyInertia ComputeClosedInertia(float mass, out Vector3 center)

Parameters

mass float

Mass to scale the inertia tensor with.

center Vector3

Center of the closed mesh.

Returns

BodyInertia

Inertia tensor of the closed mesh.

ComputeOpenCenterOfMass()

Computes the center of mass of the mesh. Assumes the mesh is open and should be treated as a triangle soup.

public readonly Vector3 ComputeOpenCenterOfMass()

Returns

Vector3

Center of mass of the open mesh.

ComputeOpenInertia(float)

Computes the inertia of the mesh. Assumes the mesh is open and should be treated as a triangle soup.

public readonly BodyInertia ComputeOpenInertia(float mass)

Parameters

mass float

Mass to scale the inertia tensor with.

Returns

BodyInertia

Inertia of the open mesh.

ComputeOpenInertia(float, out Vector3)

Computes the inertia of the mesh around its volumetric center and recenters the points of the mesh around it. Assumes the mesh is open and should be treated as a triangle soup.

public BodyInertia ComputeOpenInertia(float mass, out Vector3 center)

Parameters

mass float

Mass to scale the inertia tensor with.

center Vector3

Center of the open mesh.

Returns

BodyInertia

Inertia tensor of the closed mesh.

CreateShapeBatch(BufferPool, int, Shapes)

Creates a shape batch for this type of shape.

public static ShapeBatch CreateShapeBatch(BufferPool pool, int initialCapacity, Shapes shapeBatches)

Parameters

pool BufferPool

Buffer pool used to create the batch.

initialCapacity int

Initial capacity to allocate within the batch.

shapeBatches Shapes

The set of shapes to contain this batch.

Returns

ShapeBatch

Shape batch for the shape type.

Remarks

This is typically used internally to initialize new shape collections in response to shapes being added. It is not likely to be useful outside of the engine.

CreateWithSweepBuild(Buffer<Triangle>, Vector3, BufferPool)

Creates a mesh shape instance and builds an acceleration structure using a sweep builder.

public static Mesh CreateWithSweepBuild(Buffer<Triangle> triangles, Vector3 scale, BufferPool pool)

Parameters

triangles Buffer<Triangle>

Triangles to use in the mesh.

scale Vector3

Scale to apply to all vertices at runtime. Note that the scale is not baked into the triangles or acceleration structure; the same set of triangles and acceleration structure can be used across multiple Mesh instances with different scales.

pool BufferPool

Pool used to allocate acceleration structures.

Returns

Mesh

Created mesh shape.

Remarks

The sweep builder is significantly slower than the binned builder, but can sometimes create higher quality trees.

Note that the binned builder can be tuned to create higher quality trees. That is usually a better choice than trying to use the sweep builder; this is here primarily for legacy reasons.

CreateWithoutTreeBuild(Buffer<Triangle>, Vector3, BufferPool)

Creates a mesh shape instance, but leaves the Tree in an unbuilt state. The tree must be built before the mesh can be used.

public static Mesh CreateWithoutTreeBuild(Buffer<Triangle> triangles, Vector3 scale, BufferPool pool)

Parameters

triangles Buffer<Triangle>

Triangles to use in the mesh.

scale Vector3

Scale to apply to all vertices at runtime. Note that the scale is not baked into the triangles or acceleration structure; the same set of triangles and acceleration structure can be used across multiple Mesh instances with different scales.

pool BufferPool

Pool used to allocate acceleration structures.

Returns

Mesh

Created mesh shape.

Remarks

In some cases, the default binned build may not be the ideal builder. This function does everything needed to set up a tree without the expense of figuring out the details of the acceleration structure. The user can then run whatever build/refinement process is appropriate.

Dispose(BufferPool)

Returns the mesh's resources to a buffer pool.

public void Dispose(BufferPool bufferPool)

Parameters

bufferPool BufferPool

Pool to return the mesh's resources to.

FillSubtreesForTriangles(Span<Triangle>, Span<NodeChild>)

Fills a buffer of subtrees according to a buffer of triangles.

public static void FillSubtreesForTriangles(Span<Triangle> triangles, Span<NodeChild> subtrees)

Parameters

triangles Span<Triangle>

Triangles to build subtrees from.

subtrees Span<NodeChild>

Subtrees created for the triangles.

Remarks

The term "subtree" is used because the binned builder does not care whether the input came from leaf nodes or a refinement process's internal nodes.

FindLocalOverlaps<TOverlaps>(Vector3, Vector3, Vector3, float, BufferPool, Shapes, void*)

public readonly void FindLocalOverlaps<TOverlaps>(Vector3 min, Vector3 max, Vector3 sweep, float maximumT, BufferPool pool, Shapes shapes, void* overlaps) where TOverlaps : ICollisionTaskSubpairOverlaps

Parameters

min Vector3
max Vector3
sweep Vector3
maximumT float
pool BufferPool
shapes Shapes
overlaps void*

Type Parameters

TOverlaps

FindLocalOverlaps<TOverlaps, TSubpairOverlaps>(ref Buffer<OverlapQueryForPair>, BufferPool, Shapes, ref TOverlaps)

public readonly void FindLocalOverlaps<TOverlaps, TSubpairOverlaps>(ref Buffer<OverlapQueryForPair> pairs, BufferPool pool, Shapes shapes, ref TOverlaps overlaps) where TOverlaps : struct, ICollisionTaskOverlaps<TSubpairOverlaps> where TSubpairOverlaps : struct, ICollisionTaskSubpairOverlaps

Parameters

pairs Buffer<OverlapQueryForPair>
pool BufferPool
shapes Shapes
overlaps TOverlaps

Type Parameters

TOverlaps
TSubpairOverlaps

GetLocalChild(int, out Triangle)

Gets a child shape as it appears in the compound's local space.

public readonly void GetLocalChild(int triangleIndex, out Triangle target)

Parameters

triangleIndex int
target Triangle

GetLocalChild(int, ref TriangleWide)

Gets a child shape as it appears in the compound's local space.

public readonly void GetLocalChild(int triangleIndex, ref TriangleWide target)

Parameters

triangleIndex int
target TriangleWide

GetPosedLocalChild(int, out Triangle, out RigidPose)

Gets a child shape from the compound and compounds a pose for it in the local space of the compound parent. Useful for processes which require a child shape (like a triangle in a mesh) to have their center of mass at zero in the child's own local space.

public readonly void GetPosedLocalChild(int triangleIndex, out Triangle target, out RigidPose childPose)

Parameters

triangleIndex int
target Triangle
childPose RigidPose

Pose in the compound's local space that brings the child shape as described to the proper location in the parent compound's local space.

GetSerializedByteCount()

Gets the number of bytes it would take to store the given mesh in a byte buffer.

public readonly int GetSerializedByteCount()

Returns

int

Number of bytes it would take to store the mesh.

RayTest<TRayHitHandler>(in RigidPose, in RayData, ref float, ref TRayHitHandler)

Casts a ray against the mesh. Executes a callback for every test candidate and every hit.

public readonly void RayTest<TRayHitHandler>(in RigidPose pose, in RayData ray, ref float maximumT, ref TRayHitHandler hitHandler) where TRayHitHandler : struct, IShapeRayHitHandler

Parameters

pose RigidPose

Pose of the mesh during the ray test.

ray RayData

Ray to test against the mesh.

maximumT float

Maximum length of the ray in units of the ray direction length.

hitHandler TRayHitHandler

Callback to execute for every hit.

Type Parameters

TRayHitHandler

Type of the callback to execute for every test candidate and hit.

RayTest<TRayHitHandler>(in RigidPose, ref RaySource, ref TRayHitHandler)

Casts a bunch of rays against the mesh at the same time, executing a callback for every test candidate and every hit.

public readonly void RayTest<TRayHitHandler>(in RigidPose pose, ref RaySource rays, ref TRayHitHandler hitHandler) where TRayHitHandler : struct, IShapeRayHitHandler

Parameters

pose RigidPose

Pose of the mesh during the ray test.

rays RaySource

Set of rays to cast against the mesh.

hitHandler TRayHitHandler

Callbacks to execute.

Type Parameters

TRayHitHandler

Type of the callback to execute for every ray test candidate and every hit.

Recenter(Vector3)

Subtracts the newCenter from all points in the mesh hull.

public void Recenter(Vector3 newCenter)

Parameters

newCenter Vector3

New center that all points will be made relative to.

Serialize(Span<byte>)

Writes a mesh's data to a byte buffer.

public readonly void Serialize(Span<byte> data)

Parameters

data Span<byte>

Byte buffer to store the mesh in.