Intersection.glsl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Main intersection function for Voxel scenes.
  2. // See IntersectBox.glsl, IntersectCylinder.glsl, or IntersectEllipsoid.glsl
  3. // for the definition of intersectShape. The appropriate function is selected
  4. // based on the VoxelPrimitive shape type, and added to the shader in
  5. // Scene/VoxelRenderResources.js.
  6. // See also IntersectClippingPlane.glsl and IntersectDepth.glsl.
  7. // See IntersectionUtils.glsl for the definitions of Ray, NO_HIT,
  8. // getFirstIntersection, initializeIntersections, nextIntersection.
  9. /* Intersection defines (set in Scene/VoxelRenderResources.js)
  10. #define INTERSECTION_COUNT ###
  11. */
  12. RayShapeIntersection intersectScene(in vec2 screenCoord, in Ray ray, out Intersections ix) {
  13. // Do a ray-shape intersection to find the exact starting and ending points.
  14. intersectShape(ray, ix);
  15. // Exit early if the positive shape was completely missed or behind the ray.
  16. RayShapeIntersection intersection = getFirstIntersection(ix);
  17. if (intersection.entry.w == NO_HIT) {
  18. // Positive shape was completely missed - so exit early.
  19. return intersection;
  20. }
  21. // Clipping planes
  22. #if defined(CLIPPING_PLANES)
  23. intersectClippingPlanes(ray, ix);
  24. #endif
  25. // Depth
  26. #if defined(DEPTH_TEST)
  27. intersectDepth(screenCoord, ray, ix);
  28. #endif
  29. // Find the first intersection that's in front of the ray
  30. #if (INTERSECTION_COUNT > 1)
  31. initializeIntersections(ix);
  32. for (int i = 0; i < INTERSECTION_COUNT; ++i) {
  33. intersection = nextIntersection(ix);
  34. if (intersection.exit.w > 0.0) {
  35. // Set start to 0.0 when ray is inside the shape.
  36. intersection.entry.w = max(intersection.entry.w, 0.0);
  37. break;
  38. }
  39. }
  40. #else
  41. // Set start to 0.0 when ray is inside the shape.
  42. intersection.entry.w = max(intersection.entry.w, 0.0);
  43. #endif
  44. return intersection;
  45. }