Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
Hair.h
Go to the documentation of this file.
1// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2// SPDX-FileCopyrightText: 2026 Jorrit Rouwe
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
12
14
15class PhysicsSystem;
16#ifdef JPH_DEBUG_RENDERER
17class DebugRenderer;
18#endif
19class HairShaders;
20
35{
36public:
38 Hair(const HairSettings *inSettings, RVec3Arg inPosition, QuatArg inRotation, ObjectLayer inLayer);
39 ~Hair();
40
42 void Init(ComputeSystem *inComputeSystem);
43
45 void SetPosition(RVec3Arg inPosition) { mPosition = inPosition; }
46 void SetRotation(QuatArg inRotation) { mRotation = inRotation; }
47 RMat44 GetWorldTransform() const { return RMat44::sRotationTranslation(mRotation, mPosition); }
48
50 const HairSettings * GetHairSettings() const { return mSettings; }
51
53 void OnTeleported() { mTeleported = true; }
54
56 void SetScalpVerticesCB(ComputeBuffer *inBuffer) { mScalpVerticesCB = inBuffer; }
57
59 void SetScalpTrianglesCB(ComputeBuffer *inBuffer) { mScalpTrianglesCB = inBuffer; }
60
62 void SetScalpToHead(Mat44Arg inMat) { mScalpToHead = inMat; }
63
65 using RenderPositionsToFloat3 = std::function<void(ComputeBuffer *, Float3 *, uint)>;
66
68 void OverrideRenderPositionsCB(const RenderPositionsToFloat3 &inRenderPositionsToFloat3) { JPH_ASSERT(mRenderPositionsCB == nullptr, "Must be called before Init"); mRenderPositionsOverridden = true; mRenderPositionsToFloat3 = inRenderPositionsToFloat3; }
69
71 void SetRenderPositionsCB(ComputeBuffer *inBuffer) { JPH_ASSERT(mRenderPositionsOverridden, "Must call OverrideRenderPositionsCB first"); mRenderPositionsCB = inBuffer; }
72
81 void Update(float inDeltaTime, Mat44Arg inJointToHair, const Mat44 *inJointMatrices, const PhysicsSystem &inSystem, const HairShaders &inShaders, ComputeSystem *inComputeSystem, ComputeQueue *inComputeQueue);
82
84 ComputeBuffer * GetScalpVerticesCB() const { return mScalpVerticesCB; }
85 ComputeBuffer * GetScalpTrianglesCB() const { return mScalpTrianglesCB; }
86 ComputeBuffer * GetPositionsCB() const { return mPositionsCB; }
87 ComputeBuffer * GetVelocitiesCB() const { return mVelocitiesCB; }
88 ComputeBuffer * GetVelocityAndDensityCB() const { return mVelocityAndDensityCB; }
89 ComputeBuffer * GetRenderPositionsCB() const { return mRenderPositionsCB; }
90
92 void ReadBackGPUState(ComputeQueue *inComputeQueue);
93
95 void LockReadBackBuffers();
96 void UnlockReadBackBuffers();
97
99 const Float3 * GetScalpVertices() const { return mScalpVertices; }
100 const Float3 * GetPositions() const { return mPositions; }
101 const Quat * GetRotations() const { return mRotations; }
102 StridedPtr<const Float3> GetVelocities() const { return { (const Float3 *)&mVelocities->mVelocity, sizeof(JPH_HairVelocity) }; }
103 StridedPtr<const Float3> GetAngularVelocities() const { return { (const Float3 *)&mVelocities->mAngularVelocity, sizeof(JPH_HairVelocity) }; }
104 const Float4 * GetGridVelocityAndDensity() const { return mVelocityAndDensity; }
105 const Float3 * GetRenderPositions() const { return mRenderPositions; }
106
107#ifdef JPH_DEBUG_RENDERER
109 {
110 PerRenderStrand,
111 PerSimulatedStrand,
112 GravityFactor,
113 WorldTransformInfluence,
114 GridVelocityFactor,
115 GlobalPose,
116 SkinGlobalPose,
117 };
118
120 {
122 uint mSimulationStrandBegin = 0;
123 uint mSimulationStrandEnd = UINT_MAX;
124
125 bool mDrawRods = true;
126 bool mDrawUnloadedRods = false;
127 bool mDrawVertexVelocity = false;
128 bool mDrawAngularVelocity = false;
129 bool mDrawOrientations = false;
130 bool mDrawNeutralDensity = false;
131 bool mDrawGridDensity = false;
132 bool mDrawGridVelocity = false;
133 bool mDrawSkinPoints = false;
134 bool mDrawRenderStrands = false;
135 bool mDrawInitialGravity = true;
136 ERenderStrandColor mRenderStrandColor = ERenderStrandColor::PerSimulatedStrand;
137 };
138
140 void Draw(const DrawSettings &inSettings, DebugRenderer *inRenderer);
141#endif // JPH_DEBUG_RENDERER
142
143protected:
146
147 // Information about a colliding shape. Is always a leaf shape, compound shapes are expanded.
149 {
150 LeafShape() = default;
151 LeafShape(Mat44Arg inTransform, Vec3Arg inScale, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, const Shape *inShape) : mTransform(inTransform), mScale(inScale), mLinearVelocity(inLinearVelocity), mAngularVelocity(inAngularVelocity), mShape(inShape) { }
152
158 };
159
160 // Internal context used during a simulation step
162 {
163 Mat44 mDeltaTransform; // Transforms positions from the old hair transform to the new
164 Quat mDeltaTransformQuat; // Rotation part of mDeltaTransform
165 uint mNumIterations; // Number of iterations to run the solver for
166 bool mNeedsCollision; // If collision detection should be performed
167 bool mNeedsGrid; // If the grid should be calculated
168 bool mGlobalPoseOnly; // If no simulation is needed and only the global pose needs to be applied
169 bool mHasTransformChanged; // If the world transform has changed
170 float mDeltaTime; // Delta time for a sub step
171 float mHalfDeltaTime; // 0.5 * mDeltaTime
172 float mInvDeltaTimeSq; // 1 / mDeltaTime^2
173 float mTwoDivDeltaTime; // 2 / mDeltaTime
174 float mTimeRatio; // Ratio between sub step delta time and default sub step delta time
175 Vec3 mSubStepGravity; // Gravity to apply in a sub step
176 Array<LeafShape> mShapes; // List of colliding shapes
177 };
178
179 // Calculate the UpdateContext parameters
180 void InitializeContext(UpdateContext &outCtx, float inDeltaTime, const PhysicsSystem &inSystem);
181
182 RefConst<HairSettings> mSettings; // Shared hair settings, must be kept alive during the lifetime of this hair instance
183
184 RVec3 mPrevPosition; // Position at the start of the last time step
185 RVec3 mPosition; // Current position in world space
186 Quat mPrevRotation; // Rotation at the start of the last time step
187 Quat mRotation; // Current rotation in world space
188 bool mTeleported = true; // If the hair got teleported and should be set to the default pose
189 ObjectLayer mLayer; // Layer for the hair to collide with
190
191 Mat44 mScalpToHead = Mat44::sIdentity(); // When skipping skinning, this allow specifying a transform that transforms the scalp mesh into head space
192
193 bool mRenderPositionsOverridden = false; // Indicates that the render positions buffer is provided externally
194 RenderPositionsToFloat3 mRenderPositionsToFloat3; // Function that transforms the render positions buffer to Float3 vertices for debugging purposes
195
199 Ref<ComputeBuffer> mTargetPositionsCB; // Target root positions determined by skinning (where we're interpolating to, eventually written to mPositionsCB)
200 Ref<ComputeBuffer> mTargetGlobalPoseTransformsCB; // Target global pose transforms determined by skinning (where we're interpolating to, eventually written to mGlobalPoseTransformsCB)
201 Ref<ComputeBuffer> mGlobalPoseTransformsCB; // Current global pose transforms used for skinning the hairs
215
216 // Only valid after ReadBackGPUState has been called
222 const Float3 * mScalpVertices = nullptr;
223 Float3 * mPositions = nullptr;
224 Quat * mRotations = nullptr;
225 JPH_HairVelocity * mVelocities = nullptr;
226 const Float4 * mVelocityAndDensity = nullptr;
227 const Float3 * mRenderPositions = nullptr;
228};
229
#define JPH_EXPORT
Definition Core.h:275
unsigned int uint
Definition Core.h:500
#define JPH_NAMESPACE_END
Definition Core.h:425
#define JPH_NAMESPACE_BEGIN
Definition Core.h:419
#define JPH_ASSERT(...)
Definition IssueReporting.h:33
uint16 ObjectLayer
Definition ObjectLayer.h:16
Definition Array.h:36
Buffer that can be read from / written to by a compute shader.
Definition ComputeBuffer.h:18
Definition ComputeQueue.h:20
Interface to run a workload on the GPU.
Definition ComputeSystem.h:16
Definition DebugRenderer.h:47
Class that holds 3 floats. Used as a storage class. Convert to Vec3 for calculations.
Definition Float3.h:13
Class that holds 4 float values. Convert to Vec4 to perform calculations.
Definition Float4.h:11
Definition Hair.h:35
Ref< ComputeBuffer > mConstantsCB
Definition Hair.h:212
Ref< ComputeBuffer > mVelocityAndDensityReadBackCB
Definition Hair.h:220
void SetScalpVerticesCB(ComputeBuffer *inBuffer)
Ability to externally provide the scalp vertices buffer. This allows skipping skinning the scalp duri...
Definition Hair.h:56
Quat mPrevRotation
Definition Hair.h:186
StridedPtr< const Float3 > GetAngularVelocities() const
Definition Hair.h:103
Ref< ComputeBuffer > mShapeVerticesCB
Definition Hair.h:203
Ref< ComputeBuffer > mScalpVerticesCB
Definition Hair.h:197
void SetRenderPositionsCB(ComputeBuffer *inBuffer)
Allow setting the render vertices buffer externally in case it has special requirements for the calli...
Definition Hair.h:71
RenderPositionsToFloat3 mRenderPositionsToFloat3
Definition Hair.h:194
Ref< ComputeBuffer > mShapeIndicesCB
Definition Hair.h:204
Ref< ComputeBuffer > mScalpJointMatricesCB
Definition Hair.h:196
void SetRotation(QuatArg inRotation)
Definition Hair.h:46
ComputeBuffer * GetScalpTrianglesCB() const
Skinned scalp triangle indices.
Definition Hair.h:85
ERenderStrandColor
Definition Hair.h:109
Quat mRotation
Definition Hair.h:187
Ref< ComputeBuffer > mTargetPositionsCB
Definition Hair.h:199
ComputeBuffer * GetRenderPositionsCB() const
Render positions of the hair strands (see HairSettings::mRenderStrands to see where each strand start...
Definition Hair.h:89
const Float3 * GetPositions() const
Definition Hair.h:100
std::function< void(ComputeBuffer *, Float3 *, uint)> RenderPositionsToFloat3
Function that converts the render positions buffer to Float3 vertices for debugging purposes....
Definition Hair.h:65
Ref< ComputeBuffer > mShapePlanesCB
Definition Hair.h:202
RefConst< HairSettings > mSettings
Definition Hair.h:182
Ref< ComputeBuffer > mRenderPositionsReadBackCB
Definition Hair.h:221
Array< Ref< ComputeBuffer > > mIterationConstantsCB
Definition Hair.h:213
Ref< ComputeBuffer > mPreviousPositionsCB
Definition Hair.h:208
Ref< ComputeBuffer > mVelocitiesCB
Definition Hair.h:210
void OnTeleported()
The hair will be initialized in its default pose with zero velocity at the new position and rotation ...
Definition Hair.h:53
Ref< ComputeBuffer > mTargetGlobalPoseTransformsCB
Definition Hair.h:200
StridedPtr< const Float3 > GetVelocities() const
Definition Hair.h:102
Ref< ComputeBuffer > mCollisionShapesCB
Definition Hair.h:206
RMat44 GetWorldTransform() const
Definition Hair.h:47
ComputeBuffer * GetPositionsCB() const
Note transposed for better memory access.
Definition Hair.h:86
Ref< ComputeBuffer > mGlobalPoseTransformsCB
Definition Hair.h:201
const HairSettings * GetHairSettings() const
Access to the hair settings object which contains the configuration of the hair.
Definition Hair.h:50
ComputeBuffer * GetVelocityAndDensityCB() const
Velocity grid.
Definition Hair.h:88
const Float3 * GetScalpVertices() const
Access to the resulting simulation data (only valid when ReadBackGPUState has been called and the buf...
Definition Hair.h:99
Ref< ComputeBuffer > mVelocitiesReadBackCB
Definition Hair.h:219
ComputeBuffer * GetScalpVerticesCB() const
Access to the resulting simulation data.
Definition Hair.h:84
const Float4 * GetGridVelocityAndDensity() const
Definition Hair.h:104
const Float3 * GetRenderPositions() const
Definition Hair.h:105
Ref< ComputeBuffer > mMaterialsCB
Definition Hair.h:207
void OverrideRenderPositionsCB(const RenderPositionsToFloat3 &inRenderPositionsToFloat3)
Enable externally set render vertices buffer (with potentially different vertex layout)....
Definition Hair.h:68
Ref< ComputeBuffer > mCollisionPlanesCB
Definition Hair.h:205
void SetPosition(RVec3Arg inPosition)
Position and rotation of the hair in world space.
Definition Hair.h:45
Ref< ComputeBuffer > mRenderPositionsCB
Definition Hair.h:214
Ref< ComputeBuffer > mPositionsCB
Definition Hair.h:209
Ref< ComputeBuffer > mPositionsReadBackCB
Definition Hair.h:218
ComputeBuffer * GetVelocitiesCB() const
Note transposed for better memory access.
Definition Hair.h:87
void SetScalpToHead(Mat44Arg inMat)
When skipping skinning, this allow specifying a transform that transforms the scalp mesh into head sp...
Definition Hair.h:62
ObjectLayer mLayer
Definition Hair.h:189
RVec3 mPosition
Definition Hair.h:185
Ref< ComputeBuffer > mVelocityAndDensityCB
Definition Hair.h:211
RVec3 mPrevPosition
Definition Hair.h:184
void SetScalpTrianglesCB(ComputeBuffer *inBuffer)
Ability to externally provide the scalp triangle indices buffer. This allows skipping skinning the sc...
Definition Hair.h:59
const Quat * GetRotations() const
Definition Hair.h:101
Ref< ComputeBuffer > mScalpTrianglesCB
Definition Hair.h:198
Ref< ComputeBuffer > mScalpVerticesReadBackCB
Definition Hair.h:217
Gradient along a hair strand of a value, e.g. compliance, friction, etc.
Definition HairSettings.h:110
Definition HairSettings.h:150
This class defines the setup of a hair groom, it can be shared between multiple hair instances.
Definition HairSettings.h:22
This class loads the shaders used by the hair system. This can be shared among all hair instances.
Definition HairShaders.h:14
Holds a 4x4 matrix of floats, but supports also operations on the 3x3 upper left part of the matrix.
Definition Mat44.h:13
static JPH_INLINE Mat44 sIdentity()
Identity matrix.
Definition Mat44.inl:35
static JPH_INLINE Mat44 sRotationTranslation(QuatArg inR, Vec3Arg inT)
Get matrix that rotates and translates.
Definition Mat44.inl:149
Class that makes another class non-copyable. Usage: Inherit from NonCopyable.
Definition NonCopyable.h:11
Definition PhysicsSystem.h:30
Definition Quat.h:33
Definition Reference.h:163
Definition Reference.h:107
Base class for all shapes (collision volume of a body). Defines a virtual interface for collision det...
Definition Shape.h:185
Definition StridedPtr.h:14
Definition Vec3.h:17
Definition Hair.h:120
Definition Hair.h:149
RefConst< Shape > mShape
Definition Hair.h:157
Vec3 mLinearVelocity
Definition Hair.h:155
LeafShape()=default
Mat44 mTransform
Definition Hair.h:153
Vec3 mAngularVelocity
Definition Hair.h:156
Vec3 mScale
Definition Hair.h:154
LeafShape(Mat44Arg inTransform, Vec3Arg inScale, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, const Shape *inShape)
Definition Hair.h:151
Definition Hair.h:162
Array< LeafShape > mShapes
Definition Hair.h:176
bool mNeedsCollision
Definition Hair.h:166
float mTwoDivDeltaTime
Definition Hair.h:173
float mInvDeltaTimeSq
Definition Hair.h:172
float mHalfDeltaTime
Definition Hair.h:171
bool mNeedsGrid
Definition Hair.h:167
Quat mDeltaTransformQuat
Definition Hair.h:164
Vec3 mSubStepGravity
Definition Hair.h:175
float mDeltaTime
Definition Hair.h:170
uint mNumIterations
Definition Hair.h:165
bool mGlobalPoseOnly
Definition Hair.h:168
Mat44 mDeltaTransform
Definition Hair.h:163
bool mHasTransformChanged
Definition Hair.h:169
float mTimeRatio
Definition Hair.h:174