HeightFieldShape.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Jolt/Physics/Collision/Shape/Shape.h>
  5. #include <Jolt/Physics/Collision/PhysicsMaterial.h>
  6. #ifdef JPH_DEBUG_RENDERER
  7. #include <Jolt/Renderer/DebugRenderer.h>
  8. #endif // JPH_DEBUG_RENDERER
  9. JPH_NAMESPACE_BEGIN
  10. class ConvexShape;
  11. class CollideShapeSettings;
  12. /// Constants for HeightFieldShape, this was moved out of the HeightFieldShape because of a linker bug
  13. namespace HeightFieldShapeConstants
  14. {
  15. /// Value used to create gaps in the height field
  16. constexpr float cNoCollisionValue = FLT_MAX;
  17. /// Stack size to use during WalkHeightField
  18. constexpr int cStackSize = 128;
  19. /// A position in the hierarchical grid is defined by a level (which grid), x and y position. We encode this in a single uint32 as: level << 28 | y << 14 | x
  20. constexpr uint cNumBitsXY = 14;
  21. constexpr uint cMaskBitsXY = (1 << cNumBitsXY) - 1;
  22. constexpr uint cLevelShift = 2 * cNumBitsXY;
  23. /// When height samples are converted to 16 bit:
  24. constexpr uint16 cNoCollisionValue16 = 0xffff; ///< This is the magic value for 'no collision'
  25. constexpr uint16 cMaxHeightValue16 = 0xfffe; ///< This is the maximum allowed height value
  26. };
  27. /// Class that constructs a HeightFieldShape
  28. class HeightFieldShapeSettings final : public ShapeSettings
  29. {
  30. public:
  31. JPH_DECLARE_SERIALIZABLE_VIRTUAL(HeightFieldShapeSettings)
  32. /// Default constructor for deserialization
  33. HeightFieldShapeSettings() = default;
  34. /// Create a height field shape of inSampleCount * inSampleCount vertices.
  35. /// The height field is a surface defined by: inOffset + inScale * (x, inSamples[y * inSampleCount + x], y).
  36. /// where x and y are integers in the range x and y e [0, inSampleCount - 1].
  37. /// inSampleCount: inSampleCount / mBlockSize must be a power of 2 and minimally 2.
  38. /// inSamples: inSampleCount^2 vertices.
  39. /// inMaterialIndices: (inSampleCount - 1)^2 indices that index into inMaterialList.
  40. HeightFieldShapeSettings(const float *inSamples, Vec3Arg inOffset, Vec3Arg inScale, uint32 inSampleCount, const uint8 *inMaterialIndices = nullptr, const PhysicsMaterialList &inMaterialList = PhysicsMaterialList());
  41. // See: ShapeSettings
  42. virtual ShapeResult Create() const override;
  43. /// Determine the minimal and maximal value of mHeightSamples (will ignore cNoCollisionValue)
  44. /// @param outMinValue The minimal value fo mHeightSamples or FLT_MAX if no samples have collision
  45. /// @param outMaxValue The maximal value fo mHeightSamples or -FLT_MAX if no samples have collision
  46. /// @param outQuantizationScale (value - outMinValue) * outQuantizationScale quantizes a height sample to 16 bits
  47. void DetermineMinAndMaxSample(float &outMinValue, float &outMaxValue, float &outQuantizationScale) const;
  48. /// Given mBlockSize, mSampleCount and mHeightSamples, calculate the amount of bits needed to stay below absolute error inMaxError
  49. /// @param inMaxError Maximum allowed error in mHeightSamples after compression (note that this does not take mScale.Y into account)
  50. /// @return Needed bits per sample in the range [1, 8].
  51. uint32 CalculateBitsPerSampleForError(float inMaxError) const;
  52. /// The height field is a surface defined by: mOffset + mScale * (x, mHeightSamples[y * mSampleCount + x], y).
  53. /// where x and y are integers in the range x and y e [0, mSampleCount - 1].
  54. Vec3 mOffset = Vec3::sZero();
  55. Vec3 mScale = Vec3::sReplicate(1.0f);
  56. uint32 mSampleCount = 0;
  57. /// The heightfield is divided in blocks of mBlockSize * mBlockSize * 2 triangles and the acceleration structure culls blocks only,
  58. /// bigger block sizes reduce memory consumption but also reduce query performance. Sensible values are [2, 8], does not need to be
  59. /// a power of 2. Note that at run-time we'll perform one more grid subdivision, so the effective block size is half of what is provided here.
  60. uint32 mBlockSize = 2;
  61. /// How many bits per sample to use to compress the height field. Can be in the range [1, 8].
  62. /// Note that each sample is compressed relative to the min/max value of its block of mBlockSize * mBlockSize pixels so the effective precision is higher.
  63. /// Also note that increasing mBlockSize saves more memory than reducing the amount of bits per sample.
  64. uint32 mBitsPerSample = 8;
  65. Array<float> mHeightSamples;
  66. Array<uint8> mMaterialIndices;
  67. /// The materials of square at (x, y) is: mMaterials[mMaterialIndices[x + y * (mSampleCount - 1)]]
  68. PhysicsMaterialList mMaterials;
  69. };
  70. /// A height field shape. Cannot be used as a dynamic object.
  71. class HeightFieldShape final : public Shape
  72. {
  73. public:
  74. JPH_OVERRIDE_NEW_DELETE
  75. /// Constructor
  76. HeightFieldShape() : Shape(EShapeType::HeightField, EShapeSubType::HeightField) { }
  77. HeightFieldShape(const HeightFieldShapeSettings &inSettings, ShapeResult &outResult);
  78. // See Shape::MustBeStatic
  79. virtual bool MustBeStatic() const override { return true; }
  80. // See Shape::GetLocalBounds
  81. virtual AABox GetLocalBounds() const override;
  82. // See Shape::GetSubShapeIDBitsRecursive
  83. virtual uint GetSubShapeIDBitsRecursive() const override { return GetSubShapeIDBits(); }
  84. // See Shape::GetInnerRadius
  85. virtual float GetInnerRadius() const override { return 0.0f; }
  86. // See Shape::GetMassProperties
  87. virtual MassProperties GetMassProperties() const override;
  88. // See Shape::GetMaterial
  89. virtual const PhysicsMaterial * GetMaterial(const SubShapeID &inSubShapeID) const override;
  90. /// Overload to get the material at a particular location
  91. const PhysicsMaterial * GetMaterial(uint inX, uint inY) const;
  92. // See Shape::GetSurfaceNormal
  93. virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
  94. // See Shape::GetSupportingFace
  95. virtual void GetSupportingFace(const SubShapeID &inSubShapeID, Vec3Arg inDirection, Vec3Arg inScale, Mat44Arg inCenterOfMassTransform, SupportingFace &outVertices) const override;
  96. // See Shape::GetSubmergedVolume
  97. 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"); }
  98. #ifdef JPH_DEBUG_RENDERER
  99. // See Shape::Draw
  100. virtual void Draw(DebugRenderer *inRenderer, RMat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
  101. #endif // JPH_DEBUG_RENDERER
  102. // See Shape::CastRay
  103. virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
  104. virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  105. // See: Shape::CollidePoint
  106. virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector, const ShapeFilter &inShapeFilter = { }) const override;
  107. // See Shape::GetTrianglesStart
  108. virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
  109. // See Shape::GetTrianglesNext
  110. virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
  111. /// Get height field position at sampled location (inX, inY).
  112. /// where inX and inY are integers in the range inX e [0, mSampleCount - 1] and inY e [0, mSampleCount - 1].
  113. Vec3 GetPosition(uint inX, uint inY) const;
  114. /// Check if height field at sampled location (inX, inY) has collision (has a hole or not)
  115. bool IsNoCollision(uint inX, uint inY) const;
  116. /// Projects inLocalPosition (a point in the space of the shape) along the Y axis onto the surface and returns it in outSurfacePosition.
  117. /// When there is no surface position (because of a hole or because the point is outside the heightfield) the function will return false.
  118. bool ProjectOntoSurface(Vec3Arg inLocalPosition, Vec3 &outSurfacePosition, SubShapeID &outSubShapeID) const;
  119. // See Shape
  120. virtual void SaveBinaryState(StreamOut &inStream) const override;
  121. virtual void SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
  122. virtual void RestoreMaterialState(const PhysicsMaterialRefC *inMaterials, uint inNumMaterials) override;
  123. // See Shape::GetStats
  124. virtual Stats GetStats() const override;
  125. // See Shape::GetVolume
  126. virtual float GetVolume() const override { return 0; }
  127. #ifdef JPH_DEBUG_RENDERER
  128. // Settings
  129. static bool sDrawTriangleOutlines;
  130. #endif // JPH_DEBUG_RENDERER
  131. // Register shape functions with the registry
  132. static void sRegister();
  133. protected:
  134. // See: Shape::RestoreBinaryState
  135. virtual void RestoreBinaryState(StreamIn &inStream) override;
  136. private:
  137. class DecodingContext; ///< Context class for walking through all nodes of a heightfield
  138. struct HSGetTrianglesContext; ///< Context class for GetTrianglesStart/Next
  139. /// Calculate commonly used values and store them in the shape
  140. void CacheValues();
  141. /// Calculate bit mask for all active edges in the heightfield
  142. void CalculateActiveEdges();
  143. /// Store material indices in the least amount of bits per index possible
  144. void StoreMaterialIndices(const Array<uint8> &inMaterialIndices);
  145. /// Get the amount of horizontal/vertical blocks
  146. inline uint GetNumBlocks() const { return mSampleCount / mBlockSize; }
  147. /// Get the maximum level (amount of grids) of the tree
  148. static inline uint sGetMaxLevel(uint inNumBlocks) { return CountTrailingZeros(inNumBlocks); }
  149. /// Get the range block offset and stride for GetBlockOffsetAndScale
  150. static inline void sGetRangeBlockOffsetAndStride(uint inNumBlocks, uint inMaxLevel, uint &outRangeBlockOffset, uint &outRangeBlockStride);
  151. /// For block (inBlockX, inBlockY) get the offset and scale needed to decode a uint8 height sample to a uint16
  152. inline void GetBlockOffsetAndScale(uint inBlockX, uint inBlockY, uint inRangeBlockOffset, uint inRangeBlockStride, float &outBlockOffset, float &outBlockScale) const;
  153. /// Get the height sample at position (inX, inY)
  154. inline uint8 GetHeightSample(uint inX, uint inY) const;
  155. /// Faster version of GetPosition when block offset and scale are already known
  156. inline Vec3 GetPosition(uint inX, uint inY, float inBlockOffset, float inBlockScale, bool &outNoCollision) const;
  157. /// Determine amount of bits needed to encode sub shape id
  158. uint GetSubShapeIDBits() const;
  159. /// En/decode a sub shape ID. inX and inY specify the coordinate of the triangle. inTriangle == 0 is the lower triangle, inTriangle == 1 is the upper triangle.
  160. inline SubShapeID EncodeSubShapeID(const SubShapeIDCreator &inCreator, uint inX, uint inY, uint inTriangle) const;
  161. inline void DecodeSubShapeID(const SubShapeID &inSubShapeID, uint &outX, uint &outY, uint &outTriangle) const;
  162. /// Get the edge flags for a triangle
  163. inline uint8 GetEdgeFlags(uint inX, uint inY, uint inTriangle) const;
  164. // Helper functions called by CollisionDispatch
  165. static void sCollideConvexVsHeightField(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);
  166. static void sCollideSphereVsHeightField(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);
  167. static void sCastConvexVsHeightField(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  168. static void sCastSphereVsHeightField(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, const Shape *inShape, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector);
  169. /// Visit the entire height field using a visitor pattern
  170. template <class Visitor>
  171. JPH_INLINE void WalkHeightField(Visitor &ioVisitor) const;
  172. /// A block of 2x2 ranges used to form a hierarchical grid, ordered left top, right top, left bottom, right bottom
  173. struct alignas(16) RangeBlock
  174. {
  175. uint16 mMin[4];
  176. uint16 mMax[4];
  177. };
  178. /// Offset of first RangedBlock in grid per level
  179. static const uint sGridOffsets[];
  180. /// The height field is a surface defined by: mOffset + mScale * (x, mHeightSamples[y * mSampleCount + x], y).
  181. /// where x and y are integers in the range x and y e [0, mSampleCount - 1].
  182. Vec3 mOffset = Vec3::sZero();
  183. Vec3 mScale = Vec3::sReplicate(1.0f);
  184. /// Height data
  185. uint32 mSampleCount = 0; ///< See HeightFieldShapeSettings::mSampleCount
  186. uint32 mBlockSize = 2; ///< See HeightFieldShapeSettings::mBlockSize
  187. uint8 mBitsPerSample = 8; ///< See HeightFieldShapeSettings::mBitsPerSample
  188. uint8 mSampleMask = 0xff; ///< All bits set for a sample: (1 << mBitsPerSample) - 1, used to indicate that there's no collision
  189. uint16 mMinSample = HeightFieldShapeConstants::cNoCollisionValue16; ///< Min and max value in mHeightSamples quantized to 16 bit, for calculating bounding box
  190. uint16 mMaxSample = HeightFieldShapeConstants::cNoCollisionValue16;
  191. Array<RangeBlock> mRangeBlocks; ///< Hierarchical grid of range data describing the height variations within 1 block. The grid for level <level> starts at offset sGridOffsets[<level>]
  192. Array<uint8> mHeightSamples; ///< mBitsPerSample-bit height samples. Value [0, mMaxHeightValue] maps to highest detail grid in mRangeBlocks [mMin, mMax]. mNoCollisionValue is reserved to indicate no collision.
  193. Array<uint8> mActiveEdges; ///< (mSampleCount - 1)^2 * 3-bit active edge flags.
  194. /// Materials
  195. PhysicsMaterialList mMaterials; ///< The materials of square at (x, y) is: mMaterials[mMaterialIndices[x + y * (mSampleCount - 1)]]
  196. Array<uint8> mMaterialIndices; ///< Compressed to the minimum amount of bits per material index (mSampleCount - 1) * (mSampleCount - 1) * mNumBitsPerMaterialIndex bits of data
  197. uint32 mNumBitsPerMaterialIndex = 0; ///< Number of bits per material index
  198. #ifdef JPH_DEBUG_RENDERER
  199. /// Temporary rendering data
  200. mutable Array<DebugRenderer::GeometryRef> mGeometry;
  201. mutable bool mCachedUseMaterialColors = false; ///< This is used to regenerate the triangle batch if the drawing settings change
  202. #endif // JPH_DEBUG_RENDERER
  203. };
  204. JPH_NAMESPACE_END