CollisionFunctions.glsl 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // Copyright (C) 2009-2021, 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. /// https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/moller-trumbore-ray-triangle-intersection
  7. Bool testRayTriangle(Vec3 rayOrigin, Vec3 rayDir, Vec3 v0, Vec3 v1, Vec3 v2, Bool backfaceCulling, out F32 t, out F32 u,
  8. 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. if((backfaceCulling && det < EPSILON) || abs(det) < EPSILON)
  15. {
  16. return false;
  17. }
  18. const F32 invDet = 1.0 / det;
  19. const Vec3 tvec = rayOrigin - v0;
  20. u = dot(tvec, pvec) * invDet;
  21. if(u < 0.0 || u > 1.0)
  22. {
  23. return false;
  24. }
  25. const Vec3 qvec = cross(tvec, v0v1);
  26. v = dot(rayDir, qvec) * invDet;
  27. if(v < 0.0 || u + v > 1.0)
  28. {
  29. return false;
  30. }
  31. t = dot(v0v2, qvec) * invDet;
  32. if(t <= EPSILON)
  33. {
  34. // This is an addition to the original code. Can't have rays that don't touch the triangle
  35. return false;
  36. }
  37. return true;
  38. }
  39. /// Return true if to AABBs overlap.
  40. Bool testAabbAabb(Vec3 aMin, Vec3 aMax, Vec3 bMin, Vec3 bMax)
  41. {
  42. return all(lessThan(aMin, bMax)) && all(lessThan(bMin, aMax));
  43. }
  44. /// Intersect a ray against an AABB. The ray is inside the AABB. The function returns the distance 'a' where the
  45. /// intersection point is rayOrigin + rayDir * a
  46. /// https://community.arm.com/graphics/b/blog/posts/reflections-based-on-local-cubemaps-in-unity
  47. F32 testRayAabbInside(Vec3 rayOrigin, Vec3 rayDir, Vec3 aabbMin, Vec3 aabbMax)
  48. {
  49. const Vec3 intersectMaxPointPlanes = (aabbMax - rayOrigin) / rayDir;
  50. const Vec3 intersectMinPointPlanes = (aabbMin - rayOrigin) / rayDir;
  51. const Vec3 largestParams = max(intersectMaxPointPlanes, intersectMinPointPlanes);
  52. const F32 distToIntersect = min(min(largestParams.x, largestParams.y), largestParams.z);
  53. return distToIntersect;
  54. }
  55. /// Ray box intersection by Simon Green
  56. Bool testRayAabb(Vec3 rayOrigin, Vec3 rayDir, Vec3 aabbMin, Vec3 aabbMax, out F32 t0, out F32 t1)
  57. {
  58. const Vec3 invR = 1.0 / rayDir;
  59. const Vec3 tbot = invR * (aabbMin - rayOrigin);
  60. const Vec3 ttop = invR * (aabbMax - rayOrigin);
  61. const Vec3 tmin = min(ttop, tbot);
  62. const Vec3 tmax = max(ttop, tbot);
  63. t0 = max(tmin.x, max(tmin.y, tmin.z));
  64. t1 = min(tmax.x, min(tmax.y, tmax.z));
  65. return t0 < t1 && t1 > EPSILON;
  66. }
  67. Bool testRayObb(Vec3 rayOrigin, Vec3 rayDir, Vec3 obbExtend, Mat4 obbTransformInv, out F32 t0, out F32 t1)
  68. {
  69. // Transform ray to OBB space
  70. const Vec3 rayOriginS = (obbTransformInv * Vec4(rayOrigin, 1.0)).xyz;
  71. const Vec3 rayDirS = (obbTransformInv * Vec4(rayDir, 0.0)).xyz;
  72. // Test as AABB
  73. return testRayAabb(rayOriginS, rayDirS, -obbExtend, obbExtend, t0, t1);
  74. }
  75. /// https://www.scratchapixel.com/lessons/3d-basic-rendering/minimal-ray-tracer-rendering-simple-shapes/ray-sphere-intersection
  76. Bool testRaySphere(Vec3 rayOrigin, Vec3 rayDir, Vec3 sphereCenter, F32 sphereRadius, out F32 t0, out F32 t1)
  77. {
  78. const Vec3 L = sphereCenter - rayOrigin;
  79. const F32 tca = dot(L, rayDir);
  80. const F32 d2 = dot(L, L) - tca * tca;
  81. const F32 radius2 = sphereRadius * sphereRadius;
  82. const F32 diff = radius2 - d2;
  83. if(diff < 0.0)
  84. {
  85. return false;
  86. }
  87. const F32 thc = sqrt(diff);
  88. t0 = tca - thc;
  89. t1 = tca + thc;
  90. if(t0 < 0.0 && t1 < 0.0)
  91. {
  92. return false;
  93. }
  94. // Swap
  95. if(t0 > t1)
  96. {
  97. const F32 tmp = t0;
  98. t0 = t1;
  99. t1 = tmp;
  100. }
  101. t0 = max(0.0, t0);
  102. return true;
  103. }
  104. F32 testPlanePoint(Vec3 planeNormal, F32 planeOffset, Vec3 point)
  105. {
  106. return dot(planeNormal, point) - planeOffset;
  107. }