Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Calculate ray direction
- float3 rayDir = normalize(fragPos - camPos);
- // half extents of cube
- float3 halfExtents = cubeSize * 0.5;
- // bounds of the cube
- float3 cubeMin = cubeCenter - halfExtents;
- float3 cubeMax = cubeCenter + halfExtents;
- // Ray-box intersection - Slab method
- float3 tMin = (cubeMin - camPos) / rayDir;
- float3 tMax = (cubeMax - camPos) / rayDir;
- // Ensure tMin < tMax
- float3 t1 = min(tMin, tMax);
- float3 t2 = max(tMin, tMax);
- // Nuum of intersections (entry n exit points)
- float tNear = max(max(t1.x, t1.y), t1.z);
- float tFar = min(min(t2.x, t2.y), t2.z);
- // Check if intersection
- bool intersects = (tNear <= tFar) && (tFar >= 0.0);
- float3 normal= float3(0,0,0);
- if (intersects)
- {
- float3 intersectPoint = camPos + tNear * rayDir;
- float3 closestPoint = clamp(intersectPoint, cubeMin, cubeMax);
- // Normal at the closest point
- float3 diff = closestPoint - cubeCenter;
- normal = float3(0, 0, 0);
- // Determine face of the the closest point
- if (abs(diff.x) > abs(diff.y) && abs(diff.x) > abs(diff.z))
- normal = float3(sign(diff.x), 0, 0);
- else if (abs(diff.y) > abs(diff.x) && abs(diff.y) > abs(diff.z))
- normal = float3(0, sign(diff.y), 0);
- else
- normal = float3(0, 0, sign(diff.z));
- }
- else
- {
- //zero normal if no intersection
- normal = float3(0, 0, 0);
- }
- float dotP= dot(otherNormal,normal);
- result= step(dotP,thres);
- if(!invert)
- result= 1-result;
Advertisement
Add Comment
Please, Sign In to add comment