beyond_the_board

Untitled

Oct 29th, 2024
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | Software | 0 0
  1. // Calculate ray direction
  2.     float3 rayDir = normalize(fragPos - camPos);
  3.  
  4.     // half extents of cube
  5.     float3 halfExtents = cubeSize * 0.5;
  6.  
  7.     // bounds of the cube
  8.     float3 cubeMin = cubeCenter - halfExtents;
  9.     float3 cubeMax = cubeCenter + halfExtents;
  10.  
  11.     // Ray-box intersection -  Slab method
  12.     float3 tMin = (cubeMin - camPos) / rayDir;
  13.     float3 tMax = (cubeMax - camPos) / rayDir;
  14.  
  15.     // Ensure tMin < tMax
  16.     float3 t1 = min(tMin, tMax);
  17.     float3 t2 = max(tMin, tMax);
  18.  
  19.     // Nuum of intersections (entry n exit points)
  20.     float tNear = max(max(t1.x, t1.y), t1.z);
  21.     float tFar = min(min(t2.x, t2.y), t2.z);
  22.  
  23.     // Check if intersection
  24.     bool intersects = (tNear <= tFar) && (tFar >= 0.0);
  25.  
  26.     float3 normal= float3(0,0,0);
  27.  
  28.     if (intersects)
  29.     {
  30.         float3 intersectPoint = camPos + tNear * rayDir;
  31.         float3 closestPoint = clamp(intersectPoint, cubeMin, cubeMax);
  32.  
  33.         // Normal at the closest point
  34.         float3 diff = closestPoint - cubeCenter;
  35.  
  36.         normal = float3(0, 0, 0);
  37.  
  38.         // Determine face of the the closest point
  39.         if (abs(diff.x) > abs(diff.y) && abs(diff.x) > abs(diff.z))
  40.             normal = float3(sign(diff.x), 0, 0);
  41.         else if (abs(diff.y) > abs(diff.x) && abs(diff.y) > abs(diff.z))
  42.             normal = float3(0, sign(diff.y), 0);
  43.         else
  44.             normal = float3(0, 0, sign(diff.z));
  45.        
  46.     }
  47.     else
  48.     {
  49.         //zero normal if no intersection
  50.         normal = float3(0, 0, 0);
  51.     }
  52.  
  53.  
  54.  
  55. float dotP= dot(otherNormal,normal);
  56. result= step(dotP,thres);
  57.  
  58. if(!invert)
  59.     result= 1-result;
Advertisement
Add Comment
Please, Sign In to add comment