Jolt Physics
A multi core friendly Game Physics Engine
Loading...
Searching...
No Matches
HairIntegrate.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
5float DeltaDensity(uint inIndex)
6{
7 return gVelocityAndDensity[inIndex].w - gNeutralDensity[inIndex];
8}
9
10void ApplyGrid(JPH_IN(JPH_HairPosition) inPos, JPH_IN_OUT(JPH_HairVelocity) ioVel, JPH_IN(JPH_HairMaterial) inMaterial, float inStrandFraction)
11{
12 if (!inMaterial.mEnableGrid)
13 return;
14
15 // Convert position to grid index and fraction
16 uint3 index;
17 float3 ma;
18 GridPositionToIndexAndFraction(inPos.mPosition, index, ma);
19 float3 a = float3(1, 1, 1) - ma;
20
21 // Get average velocity at the vertex position (trilinear sample)
22 float3 velocity;
23 uint3 stride = cGridStride;
24 uint adr_000 = GridIndexToBufferIndex(index);
25 uint adr_100 = adr_000 + 1;
26 uint adr_010 = adr_000 + stride.y;
27 uint adr_110 = adr_010 + 1;
28 velocity = gVelocityAndDensity[adr_000].xyz * ( a.x * a.y * a.z);
29 velocity += gVelocityAndDensity[adr_100].xyz * (ma.x * a.y * a.z);
30 velocity += gVelocityAndDensity[adr_010].xyz * ( a.x * ma.y * a.z);
31 velocity += gVelocityAndDensity[adr_110].xyz * (ma.x * ma.y * a.z);
32 velocity += gVelocityAndDensity[adr_000 + stride.z].xyz * ( a.x * a.y * ma.z);
33 velocity += gVelocityAndDensity[adr_100 + stride.z].xyz * (ma.x * a.y * ma.z);
34 velocity += gVelocityAndDensity[adr_010 + stride.z].xyz * ( a.x * ma.y * ma.z);
35 velocity += gVelocityAndDensity[adr_110 + stride.z].xyz * (ma.x * ma.y * ma.z);
36
37 // Drive towards the average velocity of the cell
38 ioVel.mVelocity += GradientSamplerSample(inMaterial.mGridVelocityFactor, inStrandFraction) * (velocity - ioVel.mVelocity);
39
40 // Calculate force to go towards neutral density
41 // Based on eq 3 of Volumetric Methods for Simulation and Rendering of Hair - Lena Petrovic, Mark Henne and John Anderson
42 float dd000 = DeltaDensity(adr_000);
43 float dd100 = DeltaDensity(adr_100);
44 float dd010 = DeltaDensity(adr_010);
45 float dd110 = DeltaDensity(adr_110);
46 float dd001 = DeltaDensity(adr_000 + stride.z);
47 float dd101 = DeltaDensity(adr_100 + stride.z);
48 float dd011 = DeltaDensity(adr_010 + stride.z);
49 float dd111 = DeltaDensity(adr_110 + stride.z);
50
51 float3 force = float3(
52 a.y * a.z * (dd000 - dd100)
53 + ma.y * a.z * (dd010 - dd110)
54 + a.y * ma.z * (dd001 - dd101)
55 + ma.y * ma.z * (dd011 - dd111),
56
57 a.x * a.z * (dd000 - dd010)
58 + ma.x * a.z * (dd100 - dd110)
59 + a.x * ma.z * (dd001 - dd011)
60 + ma.x * ma.z * (dd101 - dd111),
61
62 a.x * a.y * (dd000 - dd001)
63 + ma.x * a.y * (dd100 - dd101)
64 + a.x * ma.y * (dd010 - dd011)
65 + ma.x * ma.y * (dd110 - dd111));
66
67 ioVel.mVelocity += inMaterial.mGridDensityForceFactor * force * cDeltaTime; // / mass, but mass is 1
68}
69
70void Integrate(JPH_IN_OUT(JPH_HairPosition) ioPos, JPH_IN(JPH_HairVelocity) inVel, JPH_IN(JPH_HairMaterial) inMaterial, float inStrandFraction)
71{
72 JPH_HairVelocity vel = inVel;
73
74 // Gravity
75 vel.mVelocity += cSubStepGravity * GradientSamplerSample(inMaterial.mGravityFactor, inStrandFraction);
76
77 // Damping
78 vel.mVelocity *= inMaterial.mExpLinearDampingDeltaTime;
79 vel.mAngularVelocity *= inMaterial.mExpAngularDampingDeltaTime;
80
81 // Integrate position
82 ioPos.mPosition += vel.mVelocity * cDeltaTime;
83
84 // Integrate rotation
85 JPH_Quat rotation = ioPos.mRotation;
86 JPH_Quat delta_rotation = cHalfDeltaTime * JPH_QuatImaginaryMulQuat(vel.mAngularVelocity, rotation);
87 ioPos.mRotation = normalize(rotation + delta_rotation);
88}
unsigned int uint
Definition Core.h:500
float GradientSamplerSample(float4 inSampler, float inStrandFraction)
Definition HairCommon.h:40
uint GridIndexToBufferIndex(uint3 inIndex)
Definition HairCommon.h:53
void GridPositionToIndexAndFraction(float3 inPosition, JPH_OUT(uint3) outIndex, JPH_OUT(float3) outFraction)
Definition HairCommon.h:45
void ApplyGrid(JPH_IN(JPH_HairPosition) inPos, JPH_IN_OUT(JPH_HairVelocity) ioVel, JPH_IN(JPH_HairMaterial) inMaterial, float inStrandFraction)
Definition HairIntegrate.h:10
float DeltaDensity(uint inIndex)
Definition HairIntegrate.h:5
void Integrate(JPH_IN_OUT(JPH_HairPosition) ioPos, JPH_IN(JPH_HairVelocity) inVel, JPH_IN(JPH_HairMaterial) inMaterial, float inStrandFraction)
Definition HairIntegrate.h:70
#define JPH_IN_OUT(type)
Definition ShaderCore.h:83
#define JPH_IN(type)
Definition ShaderCore.h:81
float4 JPH_Quat
Definition ShaderCore.h:50
JPH_Quat JPH_QuatImaginaryMulQuat(float3 inLHS, JPH_Quat inRHS)
Definition ShaderQuat.h:24