CollisionFunctions.hlsl 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright (C) 2009-2023, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Shaders/Common.hlsl>
  7. /// https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/moller-trumbore-ray-triangle-intersection
  8. Bool testRayTriangle(Vec3 rayOrigin, Vec3 rayDir, Vec3 v0, Vec3 v1, Vec3 v2, Bool backfaceCulling, out F32 t, out F32 u,
  9. out F32 v)
  10. {
  11. const Vec3 v0v1 = v1 - v0;
  12. const Vec3 v0v2 = v2 - v0;
  13. const Vec3 pvec = cross(rayDir, v0v2);
  14. const F32 det = dot(v0v1, pvec);
  15. if((backfaceCulling && det < kEpsilonF32) || abs(det) < kEpsilonF32)
  16. {
  17. return false;
  18. }
  19. const F32 invDet = 1.0 / det;
  20. const Vec3 tvec = rayOrigin - v0;
  21. u = dot(tvec, pvec) * invDet;
  22. if(u < 0.0 || u > 1.0)
  23. {
  24. return false;
  25. }
  26. const Vec3 qvec = cross(tvec, v0v1);
  27. v = dot(rayDir, qvec) * invDet;
  28. if(v < 0.0 || u + v > 1.0)
  29. {
  30. return false;
  31. }
  32. t = dot(v0v2, qvec) * invDet;
  33. if(t <= kEpsilonF32)
  34. {
  35. // This is an addition to the original code. Can't have rays that don't touch the triangle
  36. return false;
  37. }
  38. return true;
  39. }
  40. /// Return true if to AABBs overlap.
  41. Bool testAabbAabb(Vec3 aMin, Vec3 aMax, Vec3 bMin, Vec3 bMax)
  42. {
  43. return all(aMin < bMax) && all(bMin < aMax);
  44. }
  45. /// Intersect a ray against an AABB. The ray is inside the AABB. The function returns the distance 'a' where the
  46. /// intersection point is rayOrigin + rayDir * a
  47. /// https://community.arm.com/graphics/b/blog/posts/reflections-based-on-local-cubemaps-in-unity
  48. F32 testRayAabbInside(Vec3 rayOrigin, Vec3 rayDir, Vec3 aabbMin, Vec3 aabbMax)
  49. {
  50. const Vec3 intersectMaxPointPlanes = (aabbMax - rayOrigin) / rayDir;
  51. const Vec3 intersectMinPointPlanes = (aabbMin - rayOrigin) / rayDir;
  52. const Vec3 largestParams = max(intersectMaxPointPlanes, intersectMinPointPlanes);
  53. const F32 distToIntersect = min(min(largestParams.x, largestParams.y), largestParams.z);
  54. return distToIntersect;
  55. }
  56. /// Ray box intersection by Simon Green
  57. Bool testRayAabb(Vec3 rayOrigin, Vec3 rayDir, Vec3 aabbMin, Vec3 aabbMax, out F32 t0, out F32 t1)
  58. {
  59. const Vec3 invR = 1.0 / rayDir;
  60. const Vec3 tbot = invR * (aabbMin - rayOrigin);
  61. const Vec3 ttop = invR * (aabbMax - rayOrigin);
  62. const Vec3 tmin = min(ttop, tbot);
  63. const Vec3 tmax = max(ttop, tbot);
  64. t0 = max(tmin.x, max(tmin.y, tmin.z));
  65. t1 = min(tmax.x, min(tmax.y, tmax.z));
  66. return t0 < t1 && t1 > kEpsilonF32;
  67. }
  68. Bool testRayObb(Vec3 rayOrigin, Vec3 rayDir, Vec3 obbExtend, Mat4 obbTransformInv, out F32 t0, out F32 t1)
  69. {
  70. // Transform ray to OBB space
  71. const Vec3 rayOriginS = mul(obbTransformInv, Vec4(rayOrigin, 1.0)).xyz;
  72. const Vec3 rayDirS = mul(obbTransformInv, Vec4(rayDir, 0.0)).xyz;
  73. // Test as AABB
  74. return testRayAabb(rayOriginS, rayDirS, -obbExtend, obbExtend, t0, t1);
  75. }
  76. /// https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection
  77. Bool testRaySphere(Vec3 rayOrigin, Vec3 rayDir, Vec3 sphereCenter, F32 sphereRadius, out F32 t0, out F32 t1)
  78. {
  79. const Vec3 L = sphereCenter - rayOrigin;
  80. const F32 tca = dot(L, rayDir);
  81. const F32 d2 = dot(L, L) - tca * tca;
  82. const F32 radius2 = sphereRadius * sphereRadius;
  83. const F32 diff = radius2 - d2;
  84. if(diff < 0.0)
  85. {
  86. return false;
  87. }
  88. const F32 thc = sqrt(diff);
  89. t0 = tca - thc;
  90. t1 = tca + thc;
  91. if(t0 < 0.0 && t1 < 0.0)
  92. {
  93. return false;
  94. }
  95. // Swap
  96. if(t0 > t1)
  97. {
  98. const F32 tmp = t0;
  99. t0 = t1;
  100. t1 = tmp;
  101. }
  102. t0 = max(0.0, t0);
  103. return true;
  104. }
  105. F32 testPlanePoint(Vec3 planeNormal, F32 planeOffset, Vec3 point3d)
  106. {
  107. return dot(planeNormal, point3d) - planeOffset;
  108. }
  109. F32 testPlaneAabb(Vec3 planeNormal, F32 planeOffset, Vec3 aabbMin, Vec3 aabbMax)
  110. {
  111. const bool3 ge = planeNormal >= 0.0;
  112. const Vec3 diagMin = select(aabbMin, aabbMax, ge);
  113. const Vec3 diagMax = select(aabbMax, aabbMin, ge);
  114. F32 test = testPlanePoint(planeNormal, planeOffset, diagMin);
  115. if(test > 0.0)
  116. {
  117. return test;
  118. }
  119. test = testPlanePoint(planeNormal, planeOffset, diagMax);
  120. return (test >= 0.0) ? 0.0 : test;
  121. }