HairApplyDeltaTransform.hlsl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2026 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "HairApplyDeltaTransformBindings.h"
  5. #include "HairCommon.h"
  6. JPH_SHADER_FUNCTION_BEGIN(void, main, cHairPerVertexBatch, 1, 1)
  7. JPH_SHADER_PARAM_THREAD_ID(tid)
  8. JPH_SHADER_FUNCTION_END
  9. {
  10. // Check if this is a valid vertex
  11. uint vtx = tid.x + cNumStrands; // Skip the root of each strand, it's fixed
  12. if (vtx >= cNumVertices)
  13. return;
  14. if (IsVertexFixed(gVerticesFixed, vtx))
  15. return;
  16. // Load the material
  17. uint strand_idx = vtx % cNumStrands;
  18. JPH_HairMaterial material = gMaterials[GetStrandMaterialIndex(gStrandMaterialIndex, strand_idx)];
  19. // Load the vertex
  20. float strand_fraction = GetVertexStrandFraction(gStrandFractions, vtx);
  21. JPH_HairPosition pos = gPositions[vtx];
  22. JPH_HairVelocity vel = gVelocities[vtx];
  23. // Transform the position so that it stays in the same place in world space (if influence is 1)
  24. float influence = GradientSamplerSample(material.mWorldTransformInfluence, strand_fraction);
  25. pos.mPosition += influence * (JPH_Mat44Mul3x4Vec3(cDeltaTransform, pos.mPosition) - pos.mPosition);
  26. // Linear interpolate the rotation based on the influence
  27. pos.mRotation = normalize(JPH_QuatMulQuat(influence * cDeltaTransformQuat + float4(0, 0, 0, 1.0f - influence), pos.mRotation));
  28. // Transform velocities
  29. vel.mVelocity = JPH_Mat44Mul3x3Vec3(cDeltaTransform, vel.mVelocity);
  30. vel.mAngularVelocity = JPH_Mat44Mul3x3Vec3(cDeltaTransform, vel.mAngularVelocity);
  31. // Write back vertex
  32. gPositions[vtx] = pos;
  33. gVelocities[vtx] = vel;
  34. }