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 void OnTeleported() { mTeleported = true; }
51
53 void SetScalpVerticesCB(ComputeBuffer *inBuffer) { mScalpVerticesCB = inBuffer; }
54
56 void SetScalpTrianglesCB(ComputeBuffer *inBuffer) { mScalpTrianglesCB = inBuffer; }
57
59 void SetScalpToHead(Mat44Arg inMat) { mScalpToHead = inMat; }
60
62 using RenderPositionsToFloat3 = std::function<void(ComputeBuffer *, Float3 *, uint)>;
63
65 void OverrideRenderPositionsCB(const RenderPositionsToFloat3 &inRenderPositionsToFloat3) { JPH_ASSERT(mRenderPositionsCB == nullptr, "Must be called before Init"); mRenderPositionsOverridden = true; mRenderPositionsToFloat3 = inRenderPositionsToFloat3; }
66
68 void SetRenderPositionsCB(ComputeBuffer *inBuffer) { JPH_ASSERT(mRenderPositionsOverridden, "Must call OverrideRenderPositionsCB first"); mRenderPositionsCB = inBuffer; }
69
78 void Update(float inDeltaTime, Mat44Arg inJointToHair, const Mat44 *inJointMatrices, const PhysicsSystem &inSystem, const HairShaders &inShaders, ComputeSystem *inComputeSystem, ComputeQueue *inComputeQueue);
79
81 ComputeBuffer * GetScalpVerticesCB() const { return mScalpVerticesCB; }
82 ComputeBuffer * GetScalpTrianglesCB() const { return mScalpTrianglesCB; }
83 ComputeBuffer * GetPositionsCB() const { return mPositionsCB; }
84 ComputeBuffer * GetVelocitiesCB() const { return mVelocitiesCB; }
85 ComputeBuffer * GetVelocityAndDensityCB() const { return mVelocityAndDensityCB; }
86 ComputeBuffer * GetRenderPositionsCB() const { return mRenderPositionsCB; }
87
89 void ReadBackGPUState(ComputeQueue *inComputeQueue);
90
92 void LockReadBackBuffers();
93 void UnlockReadBackBuffers();
94
96 const Float3 * GetScalpVertices() const { return mScalpVertices; }
97 const Float3 * GetPositions() const { return mPositions; }
98 const Quat * GetRotations() const { return mRotations; }
99 StridedPtr<const Float3> GetVelocities() const { return { &mVelocities->mVelocity, sizeof(JPH_HairVelocity) }; }
100 StridedPtr<const Float3> GetAngularVelocities() const { return { &mVelocities->mAngularVelocity, sizeof(JPH_HairVelocity) }; }
101 const Float4 * GetGridVelocityAndDensity() const { return mVelocityAndDensity; }
102 const Float3 * GetRenderPositions() const { return mRenderPositions; }
103
104#ifdef JPH_DEBUG_RENDERER
106 {
107 PerRenderStrand,
108 PerSimulatedStrand,
109 GravityFactor,
110 WorldTransformInfluence,
111 GridVelocityFactor,
112 GlobalPose,
113 SkinGlobalPose,
114 };
115
117 {
119 uint mSimulationStrandBegin = 0;
120 uint mSimulationStrandEnd = UINT_MAX;
121
122 bool mDrawRods = true;
123 bool mDrawUnloadedRods = false;
124 bool mDrawVertexVelocity = false;
125 bool mDrawAngularVelocity = false;
126 bool mDrawOrientations = false;
127 bool mDrawNeutralDensity = false;
128 bool mDrawGridDensity = false;
129 bool mDrawGridVelocity = false;
130 bool mDrawSkinPoints = false;
131 bool mDrawRenderStrands = false;
132 bool mDrawInitialGravity = true;
133 ERenderStrandColor mRenderStrandColor = ERenderStrandColor::PerSimulatedStrand;
134 };
135
137 void Draw(const DrawSettings &inSettings, DebugRenderer *inRenderer);
138#endif // JPH_DEBUG_RENDERER
139
140protected:
143
144 // Information about a colliding shape. Is always a leaf shape, compound shapes are expanded.
146 {
147 LeafShape() = default;
148 LeafShape(Mat44Arg inTransform, Vec3Arg inScale, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, const Shape *inShape) : mTransform(inTransform), mScale(inScale), mLinearVelocity(inLinearVelocity), mAngularVelocity(inAngularVelocity), mShape(inShape) { }
149
155 };
156
157 // Internal context used during a simulation step
159 {
160 Mat44 mDeltaTransform; // Transforms positions from the old hair transform to the new
161 Quat mDeltaTransformQuat; // Rotation part of mDeltaTransform
162 uint mNumIterations; // Number of iterations to run the solver for
163 bool mNeedsCollision; // If collision detection should be performed
164 bool mNeedsGrid; // If the grid should be calculated
165 bool mGlobalPoseOnly; // If no simulation is needed and only the global pose needs to be applied
166 bool mHasTransformChanged; // If the world transform has changed
167 float mDeltaTime; // Delta time for a sub step
168 float mHalfDeltaTime; // 0.5 * mDeltaTime
169 float mInvDeltaTimeSq; // 1 / mDeltaTime^2
170 float mTwoDivDeltaTime; // 2 / mDeltaTime
171 float mTimeRatio; // Ratio between sub step delta time and default sub step delta time
172 Vec3 mSubStepGravity; // Gravity to apply in a sub step
173 Array<LeafShape> mShapes; // List of colliding shapes
174 };
175
176 // Calculate the UpdateContext parameters
177 void InitializeContext(UpdateContext &outCtx, float inDeltaTime, const PhysicsSystem &inSystem);
178
179 RefConst<HairSettings> mSettings; // Shared hair settings, must be kept alive during the lifetime of this hair instance
180
181 RVec3 mPrevPosition; // Position at the start of the last time step
182 RVec3 mPosition; // Current position in world space
183 Quat mPrevRotation; // Rotation at the start of the last time step
184 Quat mRotation; // Current rotation in world space
185 bool mTeleported = true; // If the hair got teleported and should be set to the default pose
186 ObjectLayer mLayer; // Layer for the hair to collide with
187
188 Mat44 mScalpToHead = Mat44::sIdentity(); // When skipping skinning, this allow specifying a transform that transforms the scalp mesh into head space
189
190 bool mRenderPositionsOverridden = false; // Indicates that the render positions buffer is provided externally
191 RenderPositionsToFloat3 mRenderPositionsToFloat3; // Function that transforms the render positions buffer to Float3 vertices for debugging purposes
192
196 Ref<ComputeBuffer> mTargetPositionsCB; // Target root positions determined by skinning (where we're interpolating to, eventually written to mPositionsCB)
197 Ref<ComputeBuffer> mTargetGlobalPoseTransformsCB; // Target global pose transforms determined by skinning (where we're interpolating to, eventually written to mGlobalPoseTransformsCB)
198 Ref<ComputeBuffer> mGlobalPoseTransformsCB; // Current global pose transforms used for skinning the hairs
212
213 // Only valid after ReadBackGPUState has been called
219 const Float3 * mScalpVertices = nullptr;
220 Float3 * mPositions = nullptr;
221 Quat * mRotations = nullptr;
222 JPH_HairVelocity * mVelocities = nullptr;
223 const Float4 * mVelocityAndDensity = nullptr;
224 const Float3 * mRenderPositions = nullptr;
225};
226
#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:15
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:209
Ref< ComputeBuffer > mVelocityAndDensityReadBackCB
Definition Hair.h:217
void SetScalpVerticesCB(ComputeBuffer *inBuffer)
Ability to externally provide the scalp vertices buffer. This allows skipping skinning the scalp duri...
Definition Hair.h:53
Quat mPrevRotation
Definition Hair.h:183
StridedPtr< const Float3 > GetAngularVelocities() const
Definition Hair.h:100
Ref< ComputeBuffer > mShapeVerticesCB
Definition Hair.h:200
Ref< ComputeBuffer > mScalpVerticesCB
Definition Hair.h:194
void SetRenderPositionsCB(ComputeBuffer *inBuffer)
Allow setting the render vertices buffer externally in case it has special requirements for the calli...
Definition Hair.h:68
RenderPositionsToFloat3 mRenderPositionsToFloat3
Definition Hair.h:191
Ref< ComputeBuffer > mShapeIndicesCB
Definition Hair.h:201
Ref< ComputeBuffer > mScalpJointMatricesCB
Definition Hair.h:193
void SetRotation(QuatArg inRotation)
Definition Hair.h:46
ComputeBuffer * GetScalpTrianglesCB() const
Skinned scalp triangle indices.
Definition Hair.h:82
ERenderStrandColor
Definition Hair.h:106
Quat mRotation
Definition Hair.h:184
Ref< ComputeBuffer > mTargetPositionsCB
Definition Hair.h:196
ComputeBuffer * GetRenderPositionsCB() const
Render positions of the hair strands (see HairSettings::mRenderStrands to see where each strand start...
Definition Hair.h:86
const Float3 * GetPositions() const
Definition Hair.h:97
std::function< void(ComputeBuffer *, Float3 *, uint)> RenderPositionsToFloat3
Function that converts the render positions buffer to Float3 vertices for debugging purposes....
Definition Hair.h:62
Ref< ComputeBuffer > mShapePlanesCB
Definition Hair.h:199
RefConst< HairSettings > mSettings
Definition Hair.h:179
Ref< ComputeBuffer > mRenderPositionsReadBackCB
Definition Hair.h:218
Array< Ref< ComputeBuffer > > mIterationConstantsCB
Definition Hair.h:210
Ref< ComputeBuffer > mPreviousPositionsCB
Definition Hair.h:205
Ref< ComputeBuffer > mVelocitiesCB
Definition Hair.h:207
void OnTeleported()
The hair will be initialized in its default pose with zero velocity at the new position and rotation ...
Definition Hair.h:50
Ref< ComputeBuffer > mTargetGlobalPoseTransformsCB
Definition Hair.h:197
StridedPtr< const Float3 > GetVelocities() const
Definition Hair.h:99
Ref< ComputeBuffer > mCollisionShapesCB
Definition Hair.h:203
RMat44 GetWorldTransform() const
Definition Hair.h:47
ComputeBuffer * GetPositionsCB() const
Note transposed for better memory access.
Definition Hair.h:83
Ref< ComputeBuffer > mGlobalPoseTransformsCB
Definition Hair.h:198
ComputeBuffer * GetVelocityAndDensityCB() const
Velocity grid.
Definition Hair.h:85
const Float3 * GetScalpVertices() const
Access to the resulting simulation data (only valid when ReadBackGPUState has been called and the buf...
Definition Hair.h:96
Ref< ComputeBuffer > mVelocitiesReadBackCB
Definition Hair.h:216
ComputeBuffer * GetScalpVerticesCB() const
Access to the resulting simulation data.
Definition Hair.h:81
const Float4 * GetGridVelocityAndDensity() const
Definition Hair.h:101
const Float3 * GetRenderPositions() const
Definition Hair.h:102
Ref< ComputeBuffer > mMaterialsCB
Definition Hair.h:204
void OverrideRenderPositionsCB(const RenderPositionsToFloat3 &inRenderPositionsToFloat3)
Enable externally set render vertices buffer (with potentially different vertex layout)....
Definition Hair.h:65
Ref< ComputeBuffer > mCollisionPlanesCB
Definition Hair.h:202
void SetPosition(RVec3Arg inPosition)
Position and rotation of the hair in world space.
Definition Hair.h:45
Ref< ComputeBuffer > mRenderPositionsCB
Definition Hair.h:211
Ref< ComputeBuffer > mPositionsCB
Definition Hair.h:206
Ref< ComputeBuffer > mPositionsReadBackCB
Definition Hair.h:215
ComputeBuffer * GetVelocitiesCB() const
Note transposed for better memory access.
Definition Hair.h:84
void SetScalpToHead(Mat44Arg inMat)
When skipping skinning, this allow specifying a transform that transforms the scalp mesh into head sp...
Definition Hair.h:59
ObjectLayer mLayer
Definition Hair.h:186
RVec3 mPosition
Definition Hair.h:182
Ref< ComputeBuffer > mVelocityAndDensityCB
Definition Hair.h:208
RVec3 mPrevPosition
Definition Hair.h:181
void SetScalpTrianglesCB(ComputeBuffer *inBuffer)
Ability to externally provide the scalp triangle indices buffer. This allows skipping skinning the sc...
Definition Hair.h:56
const Quat * GetRotations() const
Definition Hair.h:98
Ref< ComputeBuffer > mScalpTrianglesCB
Definition Hair.h:195
Ref< ComputeBuffer > mScalpVerticesReadBackCB
Definition Hair.h:214
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:117
Definition Hair.h:146
RefConst< Shape > mShape
Definition Hair.h:154
Vec3 mLinearVelocity
Definition Hair.h:152
LeafShape()=default
Mat44 mTransform
Definition Hair.h:150
Vec3 mAngularVelocity
Definition Hair.h:153
Vec3 mScale
Definition Hair.h:151
LeafShape(Mat44Arg inTransform, Vec3Arg inScale, Vec3Arg inLinearVelocity, Vec3Arg inAngularVelocity, const Shape *inShape)
Definition Hair.h:148
Definition Hair.h:159
Array< LeafShape > mShapes
Definition Hair.h:173
bool mNeedsCollision
Definition Hair.h:163
float mTwoDivDeltaTime
Definition Hair.h:170
float mInvDeltaTimeSq
Definition Hair.h:169
float mHalfDeltaTime
Definition Hair.h:168
bool mNeedsGrid
Definition Hair.h:164
Quat mDeltaTransformQuat
Definition Hair.h:161
Vec3 mSubStepGravity
Definition Hair.h:172
float mDeltaTime
Definition Hair.h:167
uint mNumIterations
Definition Hair.h:162
bool mGlobalPoseOnly
Definition Hair.h:165
Mat44 mDeltaTransform
Definition Hair.h:160
bool mHasTransformChanged
Definition Hair.h:166
float mTimeRatio
Definition Hair.h:171