HairCalculateCollisionPlanes.hlsl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2026 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "HairCalculateCollisionPlanesBindings.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. // Load the vertex
  15. float3 pos = gPositions[vtx].mPosition;
  16. // Start with a plane that is far away (i.e. no collision)
  17. JPH_HairCollisionPlane collision_plane;
  18. collision_plane.mPlane = float4(1, 0, 0, 1.0e6f);
  19. collision_plane.mShapeIndex = 0;
  20. float largest_penetration = -1.0e6f;
  21. // Loop over all shapes
  22. uint current_idx = 0;
  23. uint current_plane = 0;
  24. for (uint current_shape_idx = 0;; ++current_shape_idx)
  25. {
  26. // Find most facing plane
  27. float max_distance = -1.0e6f;
  28. float3 max_plane_normal = float3(0, 0, 0);
  29. uint max_plane_face_info = 0;
  30. // Get number of faces in this shape
  31. uint nf = gShapeIndices[current_idx++];
  32. if (nf == 0)
  33. break;
  34. for (uint f = 0; f < nf; ++f)
  35. {
  36. // Get the plane
  37. JPH_Plane plane = gShapePlanes[current_plane++];
  38. float distance = JPH_PlaneSignedDistance(plane, pos);
  39. if (distance > max_distance)
  40. {
  41. max_distance = distance;
  42. max_plane_normal = JPH_PlaneGetNormal(plane);
  43. max_plane_face_info = current_idx;
  44. }
  45. // Skip over vertex start and end
  46. current_idx += 2;
  47. }
  48. // Project point onto that plane, in local space to the vertex
  49. float3 closest_point = -max_distance * max_plane_normal;
  50. // Check edges if we're outside the hull (when inside we know the closest face is also the closest point to the surface)
  51. bool is_outside = max_distance > 0.0f;
  52. if (is_outside)
  53. {
  54. // Loop over edges
  55. float closest_point_dist_sq = 1.0e12f;
  56. uint vi = gShapeIndices[max_plane_face_info];
  57. uint vi_end = gShapeIndices[max_plane_face_info + 1];
  58. float3 p1 = gShapeVertices[gShapeIndices[vi_end - 1]];
  59. for (; vi < vi_end; ++vi)
  60. {
  61. // Get edge points
  62. float3 p2 = gShapeVertices[gShapeIndices[vi]];
  63. // Check if the position is outside the edge (if not, the face will be closer)
  64. float3 p1_p2 = p2 - p1;
  65. float3 p1_pos = p1 - pos;
  66. float3 edge_normal = cross(p1_p2, max_plane_normal);
  67. if (dot(edge_normal, p1_pos) <= 0.0f)
  68. {
  69. // Get closest point on edge
  70. float3 closest = JPH_GetClosestPointOnLine(p1_pos, p1_p2);
  71. float distance_sq = dot(closest, closest);
  72. if (distance_sq < closest_point_dist_sq)
  73. {
  74. closest_point_dist_sq = distance_sq;
  75. closest_point = closest;
  76. }
  77. }
  78. // Cycle vertex
  79. p1 = p2;
  80. }
  81. }
  82. // Check if this is the largest penetration
  83. float3 normal = -closest_point;
  84. float normal_length = length(normal);
  85. float penetration = normal_length;
  86. if (is_outside)
  87. penetration = -penetration;
  88. else
  89. normal = -normal;
  90. if (penetration > largest_penetration)
  91. {
  92. // Calculate contact plane
  93. normal = normal_length > 0.0f? normal / normal_length : max_plane_normal;
  94. collision_plane.mPlane = JPH_PlaneFromPointAndNormal(pos + closest_point, normal);
  95. collision_plane.mShapeIndex = current_shape_idx;
  96. largest_penetration = penetration;
  97. }
  98. }
  99. gCollisionPlanes[vtx] = collision_plane;
  100. }