123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
- // SPDX-License-Identifier: MIT
- #pragma once
- #include <Physics/Collision/Shape/Shape.h>
- #include <Physics/Collision/PhysicsMaterial.h>
- #ifdef JPH_DEBUG_RENDERER
- #include <Renderer/DebugRenderer.h>
- #endif // JPH_DEBUG_RENDERER
- namespace JPH {
- class ConvexShape;
- class CollideShapeSettings;
- /// Constants for HeightFieldShape, this was moved out of the HeightFieldShape because of a linker bug
- namespace HeightFieldShapeConstants
- {
- /// Value used to create gaps in the height field
- constexpr float cNoCollisionValue = FLT_MAX;
- /// Stack size to use during WalkHeightField
- constexpr int cStackSize = 128;
- /// 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
- constexpr uint cNumBitsXY = 14;
- constexpr uint cMaskBitsXY = (1 << cNumBitsXY) - 1;
- constexpr uint cLevelShift = 2 * cNumBitsXY;
- /// When height samples are converted to 16 bit:
- constexpr uint16 cNoCollisionValue16 = 0xffff; ///< This is the magic value for 'no collision'
- constexpr uint16 cMaxHeightValue16 = 0xfffe; ///< This is the maximum allowed height value
- };
- /// Class that constructs a HeightFieldShape
- class HeightFieldShapeSettings final : public ShapeSettings
- {
- public:
- JPH_DECLARE_SERIALIZABLE_VIRTUAL(HeightFieldShapeSettings)
- /// Default constructor for deserialization
- HeightFieldShapeSettings() = default;
- /// Create a height field shape of inSampleCount * inSampleCount vertices.
- /// The height field is a surface defined by: inOffset + inScale * (x, inSamples[y * inSampleCount + x], y).
- /// where x and y are integers in the range x and y e [0, inSampleCount - 1].
- /// inSampleCount: inSampleCount / mBlockSize must be a power of 2 and minimally 2.
- /// inSamples: inSampleCount^2 vertices.
- /// inMaterialIndices: (inSampleCount - 1)^2 indices that index into inMaterialList.
- HeightFieldShapeSettings(const float *inSamples, Vec3Arg inOffset, Vec3Arg inScale, uint32 inSampleCount, const uint8 *inMaterialIndices = nullptr, const PhysicsMaterialList &inMaterialList = PhysicsMaterialList());
- // See: ShapeSettings
- virtual ShapeResult Create() const override;
- /// Determine the minimal and maximal value of mHeightSamples (will ignore cNoCollisionValue)
- /// @param outMinValue The minimal value fo mHeightSamples or FLT_MAX if no samples have collision
- /// @param outMaxValue The maximal value fo mHeightSamples or -FLT_MAX if no samples have collision
- /// @param outQuantizationScale (value - outMinValue) * outQuantizationScale quantizes a height sample to 16 bits
- void DetermineMinAndMaxSample(float &outMinValue, float &outMaxValue, float &outQuantizationScale) const;
- /// Given mBlockSize, mSampleCount and mHeightSamples, calculate the amount of bits needed to stay below absolute error inMaxError
- /// @param inMaxError Maximum allowed error in mHeightSamples after compression (note that this does not take mScale.Y into account)
- /// @return Needed bits per sample in the range [1, 8].
- uint32 CalculateBitsPerSampleForError(float inMaxError) const;
- /// The height field is a surface defined by: mOffset + mScale * (x, mHeightSamples[y * mSampleCount + x], y).
- /// where x and y are integers in the range x and y e [0, mSampleCount - 1].
- Vec3 mOffset = Vec3::sZero();
- Vec3 mScale = Vec3::sReplicate(1.0f);
- uint32 mSampleCount = 0;
- /// The heightfield is divided in blocks of mBlockSize * mBlockSize * 2 triangles and the acceleration structure culls blocks only,
- /// bigger block sizes reduce memory consumption but also reduce query performance. Sensible values are [1, 4], does not need to be
- /// a power of 2.
- uint32 mBlockSize = 2;
- /// How many bits per sample to use to compress the height field. Can be in the range [1, 8].
- /// 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.
- /// Also note that increasing mBlockSize saves more memory than reducing the amount of bits per sample.
- uint32 mBitsPerSample = 8;
- vector<float> mHeightSamples;
- vector<uint8> mMaterialIndices;
- /// The materials of square at (x, y) is: mMaterials[mMaterialIndices[x + y * (mSampleCount - 1)]]
- PhysicsMaterialList mMaterials;
- };
- /// A height field shape. Cannot be used as a dynamic object.
- class HeightFieldShape final : public Shape
- {
- public:
- JPH_DECLARE_RTTI_VIRTUAL(HeightFieldShape)
- /// Constructor
- HeightFieldShape() = default;
- HeightFieldShape(const HeightFieldShapeSettings &inSettings, ShapeResult &outResult);
- /// Get type
- virtual EShapeType GetType() const override { return EShapeType::HeightField; }
- // See Shape::MustBeStatic
- virtual bool MustBeStatic() const override { return true; }
- // See Shape::GetLocalBounds
- virtual AABox GetLocalBounds() const override;
- // See Shape::GetSubShapeIDBitsRecursive
- virtual uint GetSubShapeIDBitsRecursive() const override { return GetSubShapeIDBits(); }
- // See Shape::GetInnerRadius
- virtual float GetInnerRadius() const override { return 0.0f; }
- // See Shape::GetMassProperties
- virtual MassProperties GetMassProperties() const override;
-
- // See Shape::GetMaterial
- virtual const PhysicsMaterial * GetMaterial(const SubShapeID &inSubShapeID) const override;
- /// Overload to get the material at a particular location
- const PhysicsMaterial * GetMaterial(uint inX, uint inY) const;
- // See Shape::GetSurfaceNormal
- virtual Vec3 GetSurfaceNormal(const SubShapeID &inSubShapeID, Vec3Arg inLocalSurfacePosition) const override;
- // See Shape::GetSubmergedVolume
- virtual void GetSubmergedVolume(Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, const Plane &inSurface, float &outTotalVolume, float &outSubmergedVolume, Vec3 &outCenterOfBuoyancy) const override { JPH_ASSERT(false, "Not supported"); }
- #ifdef JPH_DEBUG_RENDERER
- // See Shape::Draw
- virtual void Draw(DebugRenderer *inRenderer, Mat44Arg inCenterOfMassTransform, Vec3Arg inScale, ColorArg inColor, bool inUseMaterialColors, bool inDrawWireframe) const override;
- #endif // JPH_DEBUG_RENDERER
- // See Shape::CastRay
- virtual bool CastRay(const RayCast &inRay, const SubShapeIDCreator &inSubShapeIDCreator, RayCastResult &ioHit) const override;
- virtual void CastRay(const RayCast &inRay, const RayCastSettings &inRayCastSettings, const SubShapeIDCreator &inSubShapeIDCreator, CastRayCollector &ioCollector) const override;
- // See: Shape::CollidePoint
- virtual void CollidePoint(Vec3Arg inPoint, const SubShapeIDCreator &inSubShapeIDCreator, CollidePointCollector &ioCollector) const override;
- // See Shape::CastShape
- virtual void CastShape(const ShapeCast &inShapeCast, const ShapeCastSettings &inShapeCastSettings, Vec3Arg inScale, const ShapeFilter &inShapeFilter, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, CastShapeCollector &ioCollector) const override;
- // See Shape::GetTrianglesStart
- virtual void GetTrianglesStart(GetTrianglesContext &ioContext, const AABox &inBox, Vec3Arg inPositionCOM, QuatArg inRotation, Vec3Arg inScale) const override;
- // See Shape::GetTrianglesNext
- virtual int GetTrianglesNext(GetTrianglesContext &ioContext, int inMaxTrianglesRequested, Float3 *outTriangleVertices, const PhysicsMaterial **outMaterials = nullptr) const override;
- /// Collide 2 shapes and pass any collisions on to ioCollector
- static void sCollideConvexVsHeightField(const ConvexShape *inShape1, const HeightFieldShape *inShape2, Vec3Arg inScale1, Vec3Arg inScale2, Mat44Arg inCenterOfMassTransform1, Mat44Arg inCenterOfMassTransform2, const SubShapeIDCreator &inSubShapeIDCreator1, const SubShapeIDCreator &inSubShapeIDCreator2, const CollideShapeSettings &inCollideShapeSettings, CollideShapeCollector &ioCollector);
- /// Get height field position at sampled location (inX, inY).
- /// where inX and inY are integers in the range inX e [0, mSampleCount - 1] and inY e [0, mSampleCount - 1].
- Vec3 GetPosition(uint inX, uint inY) const;
- /// Check if height field at sampled location (inX, inY) has collision (has a hole or not)
- bool IsNoCollision(uint inX, uint inY) const;
- /// Projects inLocalPosition (a point in the space of the shape) along the Y axis onto the surface and returns it in outSurfacePosition.
- /// When there is no surface position (because of a hole or because the point is outside the heightfield) the function will return false.
- bool ProjectOntoSurface(Vec3Arg inLocalPosition, Vec3 &outSurfacePosition, SubShapeID &outSubShapeID) const;
- // See Shape
- virtual void SaveBinaryState(StreamOut &inStream) const override;
- virtual void SaveMaterialState(PhysicsMaterialList &outMaterials) const override;
- virtual void RestoreMaterialState(const PhysicsMaterialList &inMaterials) override;
- // See Shape::GetStats
- virtual Stats GetStats() const override;
- // See Shape::GetVolume
- virtual float GetVolume() const override { return 0; }
- #ifdef JPH_DEBUG_RENDERER
- // Settings
- static bool sDrawTriangleOutlines;
- #endif // JPH_DEBUG_RENDERER
- protected:
- // See: Shape::RestoreBinaryState
- virtual void RestoreBinaryState(StreamIn &inStream) override;
- private:
- class DecodingContext; ///< Context class for walking through all nodes of a heightfield
- struct HSGetTrianglesContext; ///< Context class for GetTrianglesStart/Next
- /// Calculate commonly used values and store them in the shape
- void CacheValues();
- /// Calculate bit mask for all active edges in the heightfield
- void CalculateActiveEdges();
-
- /// Store material indices in the least amount of bits per index possible
- void StoreMaterialIndices(const vector<uint8> &inMaterialIndices);
-
- /// For location (inX, inY) get the block that contains this position and get the offset and scale needed to decode a uint8 height sample to a uint16
- void GetBlockOffsetAndScale(uint inX, uint inY, float &outBlockOffset, float &outBlockScale) const;
- /// Get the height sample at position (inX, inY)
- inline uint8 GetHeightSample(uint inX, uint inY) const;
- /// Faster version of GetPosition when block offset and scale are already known
- Vec3 GetPosition(uint inX, uint inY, float inBlockOffset, float inBlockScale) const;
-
- /// Determine amount of bits needed to encode sub shape id
- uint GetSubShapeIDBits() const;
- /// 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.
- inline SubShapeID EncodeSubShapeID(const SubShapeIDCreator &inCreator, uint inX, uint inY, uint inTriangle) const;
- inline void DecodeSubShapeID(const SubShapeID &inSubShapeID, uint &outX, uint &outY, uint &outTriangle) const;
- /// Get the edge flags for a triangle
- inline uint8 GetEdgeFlags(uint inX, uint inY, uint inTriangle) const;
- /// Visit the entire height field using a visitor pattern
- template <class Visitor>
- JPH_INLINE void WalkHeightField(Visitor &ioVisitor) const;
- /// A block of 2x2 ranges used to form a hierarchical grid, ordered left top, right top, left bottom, right bottom
- struct alignas(16) RangeBlock
- {
- uint16 mMin[4];
- uint16 mMax[4];
- };
- /// Offset of first RangedBlock in grid per level
- static const uint sGridOffsets[];
- /// The height field is a surface defined by: mOffset + mScale * (x, mHeightSamples[y * mSampleCount + x], y).
- /// where x and y are integers in the range x and y e [0, mSampleCount - 1].
- Vec3 mOffset = Vec3::sZero();
- Vec3 mScale = Vec3::sReplicate(1.0f);
- /// Height data
- uint32 mSampleCount = 0; ///< See HeightFieldShapeSettings::mSampleCount
- uint32 mBlockSize = 2; ///< See HeightFieldShapeSettings::mBlockSize
- uint8 mBitsPerSample = 8; ///< See HeightFieldShapeSettings::mBitsPerSample
- uint8 mSampleMask = 0xff; ///< All bits set for a sample: (1 << mBitsPerSample) - 1, used to indicate that there's no collision
- uint16 mMinSample = HeightFieldShapeConstants::cNoCollisionValue16; ///< Min and max value in mHeightSamples quantized to 16 bit, for calculating bounding box
- uint16 mMaxSample = HeightFieldShapeConstants::cNoCollisionValue16;
- vector<RangeBlock> mRangeBlocks; ///< Hierarchical grid of range data describing the height variations within 1 block. The grid for level <level> starts at offset sGridOffsets[<level>]
- vector<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.
- vector<uint8> mActiveEdges; ///< (mSampleCount - 1)^2 * 3-bit active edge flags.
- /// Materials
- PhysicsMaterialList mMaterials; ///< The materials of square at (x, y) is: mMaterials[mMaterialIndices[x + y * (mSampleCount - 1)]]
- vector<uint8> mMaterialIndices; ///< Compressed to the minimum amount of bits per material index (mSampleCount - 1) * (mSampleCount - 1) * mNumBitsPerMaterialIndex bits of data
- uint32 mNumBitsPerMaterialIndex = 0; ///< Number of bits per material index
- #ifdef JPH_DEBUG_RENDERER
- /// Temporary rendering data
- mutable vector<DebugRenderer::GeometryRef> mGeometry;
- mutable bool mCachedUseMaterialColors = false; ///< This is used to regenerate the triangle batch if the drawing settings change
- #endif // JPH_DEBUG_RENDERER
- };
- } // JPH
|