| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <Jolt/Physics/Collision/Shape/Shape.h> |
| 8 |
#include <Jolt/Physics/Collision/PhysicsMaterial.h> |
| 9 |
#include <Jolt/Core/ByteBuffer.h> |
| 10 |
#include <Jolt/Geometry/Triangle.h> |
| 11 |
#include <Jolt/Geometry/IndexedTriangle.h> |
| 12 |
#ifdef JPH_DEBUG_RENDERER |
| 13 |
#include <Jolt/Renderer/DebugRenderer.h> |
| 14 |
#endif |
| 15 |
|
| 16 |
JPH_NAMESPACE_BEGIN |
| 17 |
|
| 18 |
class ConvexShape; |
| 19 |
class CollideShapeSettings; |
| 20 |
|
| 21 |
|
| 22 |
class JPH_EXPORT MeshShapeSettings final : public ShapeSettings |
| 23 |
{ |
| 24 |
JPH_DECLARE_SERIALIZABLE_VIRTUAL(JPH_EXPORT, MeshShapeSettings) |
| 25 |
|
| 26 |
public: |
| 27 |
|
| 28 |
MeshShapeSettings() = default; |
| 29 |
|
| 30 |
|
| 31 |
MeshShapeSettings(const TriangleList &inTriangles, PhysicsMaterialList inMaterials = PhysicsMaterialList()); |
| 32 |
MeshShapeSettings(VertexList inVertices, IndexedTriangleList inTriangles, PhysicsMaterialList inMaterials = PhysicsMaterialList()); |
| 33 |
|
| 34 |
|
| 35 |
void Sanitize(); |
| 36 |
|
| 37 |
|
| 38 |
virtual ShapeResult Create() const override; |
| 39 |
|
| 40 |
|
| 41 |
VertexList mTriangleVertices; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
IndexedTriangleList mIndexedTriangles; |
| 50 |
|
| 51 |
|
| 52 |
PhysicsMaterialList mMaterials; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
uint mMaxTrianglesPerLeaf = 8; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
float mActiveEdgeCosThresholdAngle = 0.996195f; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
bool mPerTriangleUserData = false; |
| 70 |
|
| 71 |
enum class EBuildQuality |
| 72 |
{ |
| 73 |
FavorRuntimePerformance, |
| 74 |
FavorBuildSpeed, |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
EBuildQuality mBuildQuality = EBuildQuality::FavorRuntimePerformance; |
| 79 |
}; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
class JPH_EXPORT MeshShape final : public Shape |
| 85 |
{ |
| 86 |
public: |
| 87 |
JPH_OVERRIDE_NEW_DELETE |
| 88 |
|
| 89 |
|
| 90 |
MeshShape() : Shape(EShapeType::Mesh, EShapeSubType::Mesh) { } |
| 91 |
MeshShape(const MeshShapeSettings &inSettings, ShapeResult &outResult); |
| 92 |
|
| 93 |
|
| 94 |
virtual bool MustBeStatic() const override { return true; } |
| 95 |
|
| 96 |
|
| 97 |
virtual AABox GetLocalBounds() const override; |
| 98 |
|
| 99 |
|
| 100 |
virtual uint GetSubShapeIDBitsRecursive() const override; |
| 101 |
|
| 102 |
|
| 103 |
virtual float GetInnerRadius() const override { return 0.0f; } |
| 104 |
|
| 105 |
|
| 106 |
virtual MassProperties GetMassProperties() const override; |
| 107 |
|
| 108 |
|
| 109 |
virtual const PhysicsMaterial * GetMaterial(const SubShapeID &inSubShapeID) const override; |
| 110 |
|
| 111 |
|
| 112 |
const PhysicsMaterialList & GetMaterialList() const { return mMaterials; } |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
uint GetMaterialIndex(const SubShapeID &inSubShapeID) const; |
| 117 |
|
| 118 |
|
| 119 |
virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override; |
| 120 |
|
| 121 |
|
| 122 |
virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override; |
| 123 |
|
| 124 |
#ifdef JPH_DEBUG_RENDERER |
| 125 |
|
| 126 |
virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override; |
| 127 |
#endif |
| 128 |
|
| 129 |
|
| 130 |
virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override; |
| 131 |
virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override; |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override; |
| 137 |
|
| 138 |
|
| 139 |
virtual void CollideSoftBodyVertices(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const CollideSoftBodyVertexIterator &inVertices, uint inNumVertices, int inCollidingShapeIndex) const override; |
| 140 |
|
| 141 |
|
| 142 |
virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override; |
| 143 |
|
| 144 |
|
| 145 |
virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override; |
| 146 |
|
| 147 |
|
| 148 |
virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy JPH_IF_DEBUG_RENDERER(, RVec3Arg inBaseOffset)) const override { JPH_ASSERT(false, "Not supported"); } |
| 149 |
|
| 150 |
|
| 151 |
virtual void SaveBinaryState(StreamOut &inStream) const override; |
| 152 |
virtual void SaveMaterialState(PhysicsMaterialList &outMaterials) const override; |
| 153 |
virtual void RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override; |
| 154 |
|
| 155 |
|
| 156 |
virtual Stats GetStats() const override; |
| 157 |
|
| 158 |
|
| 159 |
virtual float GetVolume() const override { return 0; } |
| 160 |
|
| 161 |
|
| 162 |
uint32 GetTriangleUserData(const SubShapeID &inSubShapeID) const; |
| 163 |
|
| 164 |
#ifdef JPH_DEBUG_RENDERER |
| 165 |
|
| 166 |
static bool sDrawTriangleGroups; |
| 167 |
static bool sDrawTriangleOutlines; |
| 168 |
#endif |
| 169 |
|
| 170 |
|
| 171 |
static void sRegister(); |
| 172 |
|
| 173 |
protected: |
| 174 |
|
| 175 |
virtual void RestoreBinaryState(StreamIn &inStream) override; |
| 176 |
|
| 177 |
private: |
| 178 |
struct MSGetTrianglesContext; |
| 179 |
|
| 180 |
static constexpr int NumTriangleBits = 3; |
| 181 |
static constexpr int MaxTrianglesPerLeaf = 1 << NumTriangleBits; |
| 182 |
|
| 183 |
|
| 184 |
static void sFindActiveEdges(const MeshShapeSettings &inSettings, IndexedTriangleList &ioIndices); |
| 185 |
|
| 186 |
|
| 187 |
template <class Visitor> |
| 188 |
void WalkTree(Visitor &ioVisitor) const; |
| 189 |
|
| 190 |
|
| 191 |
template <class Visitor> |
| 192 |
void WalkTreePerTriangle(const SubShapeIDCreator &inSubShapeIDCreator2, Visitor &ioVisitor) const; |
| 193 |
|
| 194 |
|
| 195 |
inline void DecodeSubShapeID(const SubShapeID &inSubShapeID, const void *&outTriangleBlock, uint32 &outTriangleIndex) const; |
| 196 |
|
| 197 |
|
| 198 |
static void sCollideConvexVsMesh(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter); |
| 199 |
static void sCollideSphereVsMesh(const Shape *inShape1, const Shape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector, const ShapeFilter &inShapeFilter); |
| 200 |
static void sCastConvexVsMesh(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector); |
| 201 |
static void sCastSphereVsMesh(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector); |
| 202 |
|
| 203 |
|
| 204 |
PhysicsMaterialList mMaterials; |
| 205 |
|
| 206 |
ByteBuffer mTree; |
| 207 |
|
| 208 |
|
| 209 |
enum ETriangleFlags |
| 210 |
{ |
| 211 |
|
| 212 |
FLAGS_MATERIAL_BITS = 5, |
| 213 |
FLAGS_MATERIAL_MASK = (1 << FLAGS_MATERIAL_BITS) - 1, |
| 214 |
|
| 215 |
|
| 216 |
FLAGS_ACTIVE_EGDE_SHIFT = FLAGS_MATERIAL_BITS, |
| 217 |
FLAGS_ACTIVE_EDGE_BITS = 3, |
| 218 |
FLAGS_ACTIVE_EDGE_MASK = (1 << FLAGS_ACTIVE_EDGE_BITS) - 1 |
| 219 |
}; |
| 220 |
|
| 221 |
#ifdef JPH_DEBUG_RENDERER |
| 222 |
mutable DebugRenderer::GeometryRef mGeometry; |
| 223 |
mutable bool mCachedTrianglesColoredPerGroup = false; |
| 224 |
mutable bool mCachedUseMaterialColors = false; |
| 225 |
#endif |
| 226 |
}; |
| 227 |
|
| 228 |
JPH_NAMESPACE_END |