CollisionFunctions.hlsl 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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, out F32 v)
  9. {
  10. const Vec3 v0v1 = v1 - v0;
  11. const Vec3 v0v2 = v2 - v0;
  12. const Vec3 pvec = cross(rayDir, v0v2);
  13. const F32 det = dot(v0v1, pvec);
  14. t = 0.0f;
  15. u = 0.0f;
  16. v = 0.0f;
  17. if((backfaceCulling && det < kEpsilonF32) || abs(det) < kEpsilonF32)
  18. {
  19. return false;
  20. }
  21. const F32 invDet = 1.0 / det;
  22. const Vec3 tvec = rayOrigin - v0;
  23. u = dot(tvec, pvec) * invDet;
  24. if(u < 0.0 || u > 1.0)
  25. {
  26. return false;
  27. }
  28. const Vec3 qvec = cross(tvec, v0v1);
  29. v = dot(rayDir, qvec) * invDet;
  30. if(v < 0.0 || u + v > 1.0)
  31. {
  32. return false;
  33. }
  34. t = dot(v0v2, qvec) * invDet;
  35. if(t <= kEpsilonF32)
  36. {
  37. // This is an addition to the original code. Can't have rays that don't touch the triangle
  38. return false;
  39. }
  40. return true;
  41. }
  42. /// Return true if to AABBs overlap.
  43. Bool testAabbAabb(Vec3 aMin, Vec3 aMax, Vec3 bMin, Vec3 bMax)
  44. {
  45. return all(aMin < bMax) && all(bMin < aMax);
  46. }
  47. /// Intersect a ray against an AABB. The ray is inside the AABB. The function returns the distance 'a' where the
  48. /// intersection point is rayOrigin + rayDir * a
  49. /// https://community.arm.com/graphics/b/blog/posts/reflections-based-on-local-cubemaps-in-unity
  50. F32 testRayAabbInside(Vec3 rayOrigin, Vec3 rayDir, Vec3 aabbMin, Vec3 aabbMax)
  51. {
  52. const Vec3 intersectMaxPointPlanes = (aabbMax - rayOrigin) / rayDir;
  53. const Vec3 intersectMinPointPlanes = (aabbMin - rayOrigin) / rayDir;
  54. const Vec3 largestParams = max(intersectMaxPointPlanes, intersectMinPointPlanes);
  55. const F32 distToIntersect = min(min(largestParams.x, largestParams.y), largestParams.z);
  56. return distToIntersect;
  57. }
  58. /// Ray box intersection by Simon Green
  59. Bool testRayAabb(Vec3 rayOrigin, Vec3 rayDir, Vec3 aabbMin, Vec3 aabbMax, out F32 t0, out F32 t1)
  60. {
  61. const Vec3 invR = 1.0 / rayDir;
  62. const Vec3 tbot = invR * (aabbMin - rayOrigin);
  63. const Vec3 ttop = invR * (aabbMax - rayOrigin);
  64. const Vec3 tmin = min(ttop, tbot);
  65. const Vec3 tmax = max(ttop, tbot);
  66. t0 = max(tmin.x, max(tmin.y, tmin.z));
  67. t1 = min(tmax.x, min(tmax.y, tmax.z));
  68. return t0 < t1 && t1 > kEpsilonF32;
  69. }
  70. Bool testRayObb(Vec3 rayOrigin, Vec3 rayDir, Vec3 obbExtend, Mat4 obbTransformInv, out F32 t0, out F32 t1)
  71. {
  72. // Transform ray to OBB space
  73. const Vec3 rayOriginS = mul(obbTransformInv, Vec4(rayOrigin, 1.0)).xyz;
  74. const Vec3 rayDirS = mul(obbTransformInv, Vec4(rayDir, 0.0)).xyz;
  75. // Test as AABB
  76. return testRayAabb(rayOriginS, rayDirS, -obbExtend, obbExtend, t0, t1);
  77. }
  78. /// https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection
  79. Bool testRaySphere(Vec3 rayOrigin, Vec3 rayDir, Vec3 sphereCenter, F32 sphereRadius, out F32 t0, out F32 t1)
  80. {
  81. t0 = 0.0f;
  82. t1 = 0.0f;
  83. const Vec3 L = sphereCenter - rayOrigin;
  84. const F32 tca = dot(L, rayDir);
  85. const F32 d2 = dot(L, L) - tca * tca;
  86. const F32 radius2 = sphereRadius * sphereRadius;
  87. const F32 diff = radius2 - d2;
  88. if(diff < 0.0)
  89. {
  90. return false;
  91. }
  92. const F32 thc = sqrt(diff);
  93. t0 = tca - thc;
  94. t1 = tca + thc;
  95. if(t0 < 0.0 && t1 < 0.0)
  96. {
  97. return false;
  98. }
  99. // Swap
  100. if(t0 > t1)
  101. {
  102. const F32 tmp = t0;
  103. t0 = t1;
  104. t1 = tmp;
  105. }
  106. t0 = max(0.0, t0);
  107. return true;
  108. }
  109. F32 testPlanePoint(Vec3 planeNormal, F32 planeOffset, Vec3 point3d)
  110. {
  111. return dot(planeNormal, point3d) - planeOffset;
  112. }
  113. F32 testPlaneAabb(Vec3 planeNormal, F32 planeOffset, Vec3 aabbMin, Vec3 aabbMax)
  114. {
  115. const bool3 ge = planeNormal >= 0.0;
  116. const Vec3 diagMin = select(aabbMin, aabbMax, ge);
  117. const Vec3 diagMax = select(aabbMax, aabbMin, ge);
  118. F32 test = testPlanePoint(planeNormal, planeOffset, diagMin);
  119. if(test > 0.0)
  120. {
  121. return test;
  122. }
  123. test = testPlanePoint(planeNormal, planeOffset, diagMax);
  124. return (test >= 0.0) ? 0.0 : test;
  125. }
  126. F32 testPlaneSphere(Vec3 planeNormal, F32 planeOffset, Vec3 sphereCenter, F32 sphereRadius)
  127. {
  128. const F32 centerDist = testPlanePoint(planeNormal, planeOffset, sphereCenter);
  129. F32 dist = centerDist - sphereRadius;
  130. if(dist >= 0.0f)
  131. {
  132. return dist;
  133. }
  134. dist = centerDist + sphereRadius;
  135. return (dist < 0.0f) ? dist : 0.0f;
  136. }
  137. Bool frustumTest(Vec4 frustumPlanes[6], Vec3 sphereCenter, F32 sphereRadius)
  138. {
  139. F32 minPlaneDistance = testPlanePoint(frustumPlanes[0].xyz, frustumPlanes[0].w, sphereCenter);
  140. [unroll] for(U32 i = 1; i < 6; ++i)
  141. {
  142. const F32 d = testPlanePoint(frustumPlanes[i].xyz, frustumPlanes[i].w, sphereCenter);
  143. minPlaneDistance = min(minPlaneDistance, d);
  144. }
  145. return minPlaneDistance > -sphereRadius;
  146. }