Path: blob/master/thirdparty/jolt_physics/Jolt/Physics/SoftBody/SoftBodySharedSettings.h
9912 views
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)1// SPDX-FileCopyrightText: 2023 Jorrit Rouwe2// SPDX-License-Identifier: MIT34#pragma once56#include <Jolt/Core/Reference.h>7#include <Jolt/Physics/Collision/PhysicsMaterial.h>8#include <Jolt/Core/StreamUtils.h>910JPH_NAMESPACE_BEGIN1112/// This class defines the setup of all particles and their constraints.13/// It is used during the simulation and can be shared between multiple soft bodies.14class JPH_EXPORT SoftBodySharedSettings : public RefTarget<SoftBodySharedSettings>15{16JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, SoftBodySharedSettings)1718public:19/// Which type of bend constraint should be created20enum class EBendType21{22None, ///< No bend constraints will be created23Distance, ///< A simple distance constraint24Dihedral, ///< A dihedral bend constraint (most expensive, but also supports triangles that are initially not in the same plane)25};2627/// The type of long range attachment constraint to create28enum class ELRAType29{30None, ///< Don't create a LRA constraint31EuclideanDistance, ///< Create a LRA constraint based on Euclidean distance between the closest kinematic vertex and this vertex32GeodesicDistance, ///< Create a LRA constraint based on the geodesic distance between the closest kinematic vertex and this vertex (follows the edge constraints)33};3435/// Per vertex attributes used during the CreateConstraints function.36/// For an edge or shear constraint, the compliance is averaged between the two attached vertices.37/// For a bend constraint, the compliance is averaged between the two vertices on the shared edge.38struct JPH_EXPORT VertexAttributes39{40/// Constructor41VertexAttributes() = default;42VertexAttributes(float inCompliance, float inShearCompliance, float inBendCompliance, ELRAType inLRAType = ELRAType::None, float inLRAMaxDistanceMultiplier = 1.0f) : mCompliance(inCompliance), mShearCompliance(inShearCompliance), mBendCompliance(inBendCompliance), mLRAType(inLRAType), mLRAMaxDistanceMultiplier(inLRAMaxDistanceMultiplier) { }4344float mCompliance = 0.0f; ///< The compliance of the normal edges. Set to FLT_MAX to disable regular edges for any edge involving this vertex.45float mShearCompliance = 0.0f; ///< The compliance of the shear edges. Set to FLT_MAX to disable shear edges for any edge involving this vertex.46float mBendCompliance = FLT_MAX; ///< The compliance of the bend edges. Set to FLT_MAX to disable bend edges for any bend constraint involving this vertex.47ELRAType mLRAType = ELRAType::None; ///< The type of long range attachment constraint to create.48float mLRAMaxDistanceMultiplier = 1.0f; ///< Multiplier for the max distance of the LRA constraint, e.g. 1.01 means the max distance is 1% longer than the calculated distance in the rest pose.49};5051/// Automatically create constraints based on the faces of the soft body52/// @param inVertexAttributes A list of attributes for each vertex (1-on-1 with mVertices, note that if the list is smaller than mVertices the last element will be repeated). This defines the properties of the constraints that are created.53/// @param inVertexAttributesLength The length of inVertexAttributes54/// @param inBendType The type of bend constraint to create55/// @param inAngleTolerance Shear edges are created when two connected triangles form a quad (are roughly in the same plane and form a square with roughly 90 degree angles). This defines the tolerance (in radians).56void CreateConstraints(const VertexAttributes *inVertexAttributes, uint inVertexAttributesLength, EBendType inBendType = EBendType::Distance, float inAngleTolerance = DegreesToRadians(8.0f));5758/// Calculate the initial lengths of all springs of the edges of this soft body (if you use CreateConstraint, this is already done)59void CalculateEdgeLengths();6061/// Calculate the max lengths for the long range attachment constraints based on Euclidean distance (if you use CreateConstraints, this is already done)62/// @param inMaxDistanceMultiplier Multiplier for the max distance of the LRA constraint, e.g. 1.01 means the max distance is 1% longer than the calculated distance in the rest pose.63void CalculateLRALengths(float inMaxDistanceMultiplier = 1.0f);6465/// Calculate the constants for the bend constraints (if you use CreateConstraints, this is already done)66void CalculateBendConstraintConstants();6768/// Calculates the initial volume of all tetrahedra of this soft body69void CalculateVolumeConstraintVolumes();7071/// Calculate information needed to be able to calculate the skinned constraint normals at run-time72void CalculateSkinnedConstraintNormals();7374/// Information about the optimization of the soft body, the indices of certain elements may have changed.75class OptimizationResults76{77public:78Array<uint> mEdgeRemap; ///< Maps old edge index to new edge index79Array<uint> mLRARemap; ///< Maps old LRA index to new LRA index80Array<uint> mDihedralBendRemap; ///< Maps old dihedral bend index to new dihedral bend index81Array<uint> mVolumeRemap; ///< Maps old volume constraint index to new volume constraint index82Array<uint> mSkinnedRemap; ///< Maps old skinned constraint index to new skinned constraint index83};8485/// Optimize the soft body settings for simulation. This will reorder constraints so they can be executed in parallel.86void Optimize(OptimizationResults &outResults);8788/// Optimize the soft body settings without results89void Optimize() { OptimizationResults results; Optimize(results); }9091/// Clone this object92Ref<SoftBodySharedSettings> Clone() const;9394/// Saves the state of this object in binary form to inStream. Doesn't store the material list.95void SaveBinaryState(StreamOut &inStream) const;9697/// Restore the state of this object from inStream. Doesn't restore the material list.98void RestoreBinaryState(StreamIn &inStream);99100using SharedSettingsToIDMap = StreamUtils::ObjectToIDMap<SoftBodySharedSettings>;101using IDToSharedSettingsMap = StreamUtils::IDToObjectMap<SoftBodySharedSettings>;102using MaterialToIDMap = StreamUtils::ObjectToIDMap<PhysicsMaterial>;103using IDToMaterialMap = StreamUtils::IDToObjectMap<PhysicsMaterial>;104105/// Save this shared settings and its materials. Pass in an empty map ioSettingsMap / ioMaterialMap or reuse the same map while saving multiple settings objects to the same stream in order to avoid writing duplicates.106void SaveWithMaterials(StreamOut &inStream, SharedSettingsToIDMap &ioSettingsMap, MaterialToIDMap &ioMaterialMap) const;107108using SettingsResult = Result<Ref<SoftBodySharedSettings>>;109110/// Restore a shape and materials. Pass in an empty map in ioSettingsMap / ioMaterialMap or reuse the same map while reading multiple settings objects from the same stream in order to restore duplicates.111static SettingsResult sRestoreWithMaterials(StreamIn &inStream, IDToSharedSettingsMap &ioSettingsMap, IDToMaterialMap &ioMaterialMap);112113/// Create a cube. This can be used to create a simple soft body for testing purposes.114/// It will contain edge constraints, volume constraints and faces.115/// @param inGridSize Number of points along each axis116/// @param inGridSpacing Distance between points117static Ref<SoftBodySharedSettings> sCreateCube(uint inGridSize, float inGridSpacing);118119/// A vertex is a particle, the data in this structure is only used during creation of the soft body and not during simulation120struct JPH_EXPORT Vertex121{122JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Vertex)123124/// Constructor125Vertex() = default;126Vertex(const Float3 &inPosition, const Float3 &inVelocity = Float3(0, 0, 0), float inInvMass = 1.0f) : mPosition(inPosition), mVelocity(inVelocity), mInvMass(inInvMass) { }127128Float3 mPosition { 0, 0, 0 }; ///< Initial position of the vertex129Float3 mVelocity { 0, 0, 0 }; ///< Initial velocity of the vertex130float mInvMass = 1.0f; ///< Initial inverse of the mass of the vertex131};132133/// A face defines the surface of the body134struct JPH_EXPORT Face135{136JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Face)137138/// Constructor139Face() = default;140Face(uint32 inVertex1, uint32 inVertex2, uint32 inVertex3, uint32 inMaterialIndex = 0) : mVertex { inVertex1, inVertex2, inVertex3 }, mMaterialIndex(inMaterialIndex) { }141142/// Check if this is a degenerate face (a face which points to the same vertex twice)143bool IsDegenerate() const { return mVertex[0] == mVertex[1] || mVertex[0] == mVertex[2] || mVertex[1] == mVertex[2]; }144145uint32 mVertex[3]; ///< Indices of the vertices that form the face146uint32 mMaterialIndex = 0; ///< Index of the material of the face in SoftBodySharedSettings::mMaterials147};148149/// An edge keeps two vertices at a constant distance using a spring: |x1 - x2| = rest length150struct JPH_EXPORT Edge151{152JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Edge)153154/// Constructor155Edge() = default;156Edge(uint32 inVertex1, uint32 inVertex2, float inCompliance = 0.0f) : mVertex { inVertex1, inVertex2 }, mCompliance(inCompliance) { }157158/// Return the lowest vertex index of this constraint159uint32 GetMinVertexIndex() const { return min(mVertex[0], mVertex[1]); }160161uint32 mVertex[2]; ///< Indices of the vertices that form the edge162float mRestLength = 1.0f; ///< Rest length of the spring163float mCompliance = 0.0f; ///< Inverse of the stiffness of the spring164};165166/**167* A dihedral bend constraint keeps the angle between two triangles constant along their shared edge.168*169* x2170* / \171* / t0 \172* x0----x1173* \ t1 /174* \ /175* x3176*177* x0..x3 are the vertices, t0 and t1 are the triangles that share the edge x0..x1178*179* Based on:180* - "Position Based Dynamics" - Matthias Muller et al.181* - "Strain Based Dynamics" - Matthias Muller et al.182* - "Simulation of Clothing with Folds and Wrinkles" - R. Bridson et al.183*/184struct JPH_EXPORT DihedralBend185{186JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, DihedralBend)187188/// Constructor189DihedralBend() = default;190DihedralBend(uint32 inVertex1, uint32 inVertex2, uint32 inVertex3, uint32 inVertex4, float inCompliance = 0.0f) : mVertex { inVertex1, inVertex2, inVertex3, inVertex4 }, mCompliance(inCompliance) { }191192/// Return the lowest vertex index of this constraint193uint32 GetMinVertexIndex() const { return min(min(mVertex[0], mVertex[1]), min(mVertex[2], mVertex[3])); }194195uint32 mVertex[4]; ///< Indices of the vertices of the 2 triangles that share an edge (the first 2 vertices are the shared edge)196float mCompliance = 0.0f; ///< Inverse of the stiffness of the constraint197float mInitialAngle = 0.0f; ///< Initial angle between the normals of the triangles (pi - dihedral angle).198};199200/// Volume constraint, keeps the volume of a tetrahedron constant201struct JPH_EXPORT Volume202{203JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Volume)204205/// Constructor206Volume() = default;207Volume(uint32 inVertex1, uint32 inVertex2, uint32 inVertex3, uint32 inVertex4, float inCompliance = 0.0f) : mVertex { inVertex1, inVertex2, inVertex3, inVertex4 }, mCompliance(inCompliance) { }208209/// Return the lowest vertex index of this constraint210uint32 GetMinVertexIndex() const { return min(min(mVertex[0], mVertex[1]), min(mVertex[2], mVertex[3])); }211212uint32 mVertex[4]; ///< Indices of the vertices that form the tetrahedron213float mSixRestVolume = 1.0f; ///< 6 times the rest volume of the tetrahedron (calculated by CalculateVolumeConstraintVolumes())214float mCompliance = 0.0f; ///< Inverse of the stiffness of the constraint215};216217/// An inverse bind matrix take a skinned vertex from its bind pose into joint local space218class JPH_EXPORT InvBind219{220JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, InvBind)221222public:223/// Constructor224InvBind() = default;225InvBind(uint32 inJointIndex, Mat44Arg inInvBind) : mJointIndex(inJointIndex), mInvBind(inInvBind) { }226227uint32 mJointIndex = 0; ///< Joint index to which this is attached228Mat44 mInvBind = Mat44::sIdentity(); ///< The inverse bind matrix, this takes a vertex in its bind pose (Vertex::mPosition) to joint local space229};230231/// A joint and its skin weight232class JPH_EXPORT SkinWeight233{234JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, SkinWeight)235236public:237/// Constructor238SkinWeight() = default;239SkinWeight(uint32 inInvBindIndex, float inWeight) : mInvBindIndex(inInvBindIndex), mWeight(inWeight) { }240241uint32 mInvBindIndex = 0; ///< Index in mInvBindMatrices242float mWeight = 0.0f; ///< Weight with which it is skinned243};244245/// A constraint that skins a vertex to joints and limits the distance that the simulated vertex can travel from this vertex246class JPH_EXPORT Skinned247{248JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Skinned)249250public:251/// Constructor252Skinned() = default;253Skinned(uint32 inVertex, float inMaxDistance, float inBackStopDistance, float inBackStopRadius) : mVertex(inVertex), mMaxDistance(inMaxDistance), mBackStopDistance(inBackStopDistance), mBackStopRadius(inBackStopRadius) { }254255/// Normalize the weights so that they add up to 1256void NormalizeWeights()257{258// Get the total weight259float total = 0.0f;260for (const SkinWeight &w : mWeights)261total += w.mWeight;262263// Normalize264if (total > 0.0f)265for (SkinWeight &w : mWeights)266w.mWeight /= total;267}268269/// Maximum number of skin weights270static constexpr uint cMaxSkinWeights = 4;271272uint32 mVertex = 0; ///< Index in mVertices which indicates which vertex is being skinned273SkinWeight mWeights[cMaxSkinWeights]; ///< Skin weights, the bind pose of the vertex is assumed to be stored in Vertex::mPosition. The first weight that is zero indicates the end of the list. Weights should add up to 1.274float mMaxDistance = FLT_MAX; ///< Maximum distance that this vertex can reach from the skinned vertex, disabled when FLT_MAX. 0 when you want to hard skin the vertex to the skinned vertex.275float mBackStopDistance = FLT_MAX; ///< Disabled if mBackStopDistance >= mMaxDistance. The faces surrounding mVertex determine an average normal. mBackStopDistance behind the vertex in the opposite direction of this normal, the back stop sphere starts. The simulated vertex will be pushed out of this sphere and it can be used to approximate the volume of the skinned mesh behind the skinned vertex.276float mBackStopRadius = 40.0f; ///< Radius of the backstop sphere. By default this is a fairly large radius so the sphere approximates a plane.277uint32 mNormalInfo = 0; ///< Information needed to calculate the normal of this vertex, lowest 24 bit is start index in mSkinnedConstraintNormals, highest 8 bit is number of faces (generated by CalculateSkinnedConstraintNormals())278};279280/// A long range attachment constraint, this is a constraint that sets a max distance between a kinematic vertex and a dynamic vertex281/// See: "Long Range Attachments - A Method to Simulate Inextensible Clothing in Computer Games", Tae-Yong Kim, Nuttapong Chentanez and Matthias Mueller-Fischer282class JPH_EXPORT LRA283{284JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, LRA)285286public:287/// Constructor288LRA() = default;289LRA(uint32 inVertex1, uint32 inVertex2, float inMaxDistance) : mVertex { inVertex1, inVertex2 }, mMaxDistance(inMaxDistance) { }290291/// Return the lowest vertex index of this constraint292uint32 GetMinVertexIndex() const { return min(mVertex[0], mVertex[1]); }293294uint32 mVertex[2]; ///< The vertices that are connected. The first vertex should be kinematic, the 2nd dynamic.295float mMaxDistance = 0.0f; ///< The maximum distance between the vertices296};297298/// Add a face to this soft body299void AddFace(const Face &inFace) { JPH_ASSERT(!inFace.IsDegenerate()); mFaces.push_back(inFace); }300301Array<Vertex> mVertices; ///< The list of vertices or particles of the body302Array<Face> mFaces; ///< The list of faces of the body303Array<Edge> mEdgeConstraints; ///< The list of edges or springs of the body304Array<DihedralBend> mDihedralBendConstraints; ///< The list of dihedral bend constraints of the body305Array<Volume> mVolumeConstraints; ///< The list of volume constraints of the body that keep the volume of tetrahedra in the soft body constant306Array<Skinned> mSkinnedConstraints; ///< The list of vertices that are constrained to a skinned vertex307Array<InvBind> mInvBindMatrices; ///< The list of inverse bind matrices for skinning vertices308Array<LRA> mLRAConstraints; ///< The list of long range attachment constraints309PhysicsMaterialList mMaterials { PhysicsMaterial::sDefault }; ///< The materials of the faces of the body, referenced by Face::mMaterialIndex310float mVertexRadius = 0.0f; ///< How big the particles are, can be used to push the vertices a little bit away from the surface of other bodies to prevent z-fighting311312private:313friend class SoftBodyMotionProperties;314315/// Calculate the closest kinematic vertex array316void CalculateClosestKinematic();317318/// Tracks the closest kinematic vertex319struct ClosestKinematic320{321uint32 mVertex = 0xffffffff; ///< Vertex index of closest kinematic vertex322float mDistance = FLT_MAX; ///< Distance to the closest kinematic vertex323};324325/// Tracks the end indices of the various constraint groups326struct UpdateGroup327{328uint mEdgeEndIndex; ///< The end index of the edge constraints in this group329uint mLRAEndIndex; ///< The end index of the LRA constraints in this group330uint mDihedralBendEndIndex; ///< The end index of the dihedral bend constraints in this group331uint mVolumeEndIndex; ///< The end index of the volume constraints in this group332uint mSkinnedEndIndex; ///< The end index of the skinned constraints in this group333};334335Array<ClosestKinematic> mClosestKinematic; ///< The closest kinematic vertex to each vertex in mVertices336Array<UpdateGroup> mUpdateGroups; ///< The end indices for each group of constraints that can be updated in parallel337Array<uint32> mSkinnedConstraintNormals; ///< A list of indices in the mFaces array used by mSkinnedConstraints, calculated by CalculateSkinnedConstraintNormals()338};339340JPH_NAMESPACE_END341342343