HairSkinRoots.hlsl 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2026 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "HairSkinRootsBindings.h"
  5. #include "HairCommon.h"
  6. JPH_SHADER_FUNCTION_BEGIN(void, main, cHairPerStrandBatch, 1, 1)
  7. JPH_SHADER_PARAM_THREAD_ID(tid)
  8. JPH_SHADER_FUNCTION_END
  9. {
  10. // Check if this is a valid strand
  11. uint strand_idx = tid.x;
  12. if (strand_idx >= cNumStrands)
  13. return;
  14. JPH_HairSkinPoint sp = gSkinPoints[strand_idx];
  15. // Get the vertices of the attached triangle
  16. uint tri_idx = sp.mTriangleIndex * 3;
  17. float3 v0 = JPH_Mat44Mul3x3Vec3(cScalpToHead, gScalpVertices[gScalpTriangles[tri_idx + 0]]);
  18. float3 v1 = JPH_Mat44Mul3x3Vec3(cScalpToHead, gScalpVertices[gScalpTriangles[tri_idx + 1]]);
  19. float3 v2 = JPH_Mat44Mul3x3Vec3(cScalpToHead, gScalpVertices[gScalpTriangles[tri_idx + 2]]);
  20. JPH_HairPosition root;
  21. // Set the position of the root
  22. root.mPosition = sp.mU * v0 + sp.mV * v1 + (1.0f - sp.mU - sp.mV) * v2 + cScalpToHead[3].xyz;
  23. // Get tangent vector
  24. float3 tangent = normalize(v1 - v0);
  25. // Get normal of the triangle
  26. float3 normal = normalize(cross(tangent, v2 - v0));
  27. // Calculate basis for the triangle
  28. float3 binormal = cross(tangent, normal);
  29. JPH_Quat triangle_basis = JPH_QuatFromMat33(normal, binormal, tangent);
  30. // Calculate the new Bishop frame of the root
  31. root.mRotation = JPH_QuatMulQuat(triangle_basis, JPH_QuatDecompress(sp.mToBishop));
  32. gPositions[strand_idx] = root;
  33. // Calculate the transform that transforms the stored global pose to the space of the skinned root of the strand
  34. JPH_HairGlobalPoseTransform transform;
  35. transform.mRotation = JPH_QuatMulQuat(root.mRotation, JPH_QuatConjugate(JPH_QuatDecompress(gInitialBishops[strand_idx])));
  36. transform.mPosition = root.mPosition - JPH_QuatMulVec3(transform.mRotation, gInitialPositions[strand_idx]);
  37. gGlobalPoseTransforms[strand_idx] = transform;
  38. }