IntersectClippingPlanes.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "// See IntersectionUtils.glsl for the definitions of Ray, Intersections, INF_HIT,\n\
  3. // NO_HIT, setIntersectionPair\n\
  4. \n\
  5. /* Clipping plane defines (set in Scene/VoxelRenderResources.js)\n\
  6. #define CLIPPING_PLANES_UNION\n\
  7. #define CLIPPING_PLANES_COUNT\n\
  8. #define CLIPPING_PLANES_INTERSECTION_INDEX\n\
  9. */\n\
  10. \n\
  11. uniform sampler2D u_clippingPlanesTexture;\n\
  12. uniform mat4 u_clippingPlanesMatrix;\n\
  13. \n\
  14. // Plane is in Hessian Normal Form\n\
  15. vec4 intersectPlane(in Ray ray, in vec4 plane) {\n\
  16. vec3 n = plane.xyz; // normal\n\
  17. float w = plane.w; // -dot(pointOnPlane, normal)\n\
  18. \n\
  19. float a = dot(ray.pos, n);\n\
  20. float b = dot(ray.dir, n);\n\
  21. float t = -(w + a) / b;\n\
  22. \n\
  23. return vec4(n, t);\n\
  24. }\n\
  25. \n\
  26. void intersectClippingPlanes(in Ray ray, inout Intersections ix) {\n\
  27. vec4 backSide = vec4(-ray.dir, -INF_HIT);\n\
  28. vec4 farSide = vec4(ray.dir, +INF_HIT);\n\
  29. RayShapeIntersection clippingVolume;\n\
  30. \n\
  31. #if (CLIPPING_PLANES_COUNT == 1)\n\
  32. // Union and intersection are the same when there's one clipping plane, and the code\n\
  33. // is more simplified.\n\
  34. vec4 planeUv = getClippingPlane(u_clippingPlanesTexture, 0, u_clippingPlanesMatrix);\n\
  35. vec4 intersection = intersectPlane(ray, planeUv);\n\
  36. bool reflects = dot(ray.dir, intersection.xyz) < 0.0;\n\
  37. clippingVolume.entry = reflects ? backSide : intersection;\n\
  38. clippingVolume.exit = reflects ? intersection : farSide;\n\
  39. setShapeIntersection(ix, CLIPPING_PLANES_INTERSECTION_INDEX, clippingVolume);\n\
  40. #elif defined(CLIPPING_PLANES_UNION)\n\
  41. vec4 firstTransmission = vec4(ray.dir, +INF_HIT);\n\
  42. vec4 lastReflection = vec4(-ray.dir, -INF_HIT);\n\
  43. for (int i = 0; i < CLIPPING_PLANES_COUNT; i++) {\n\
  44. vec4 planeUv = getClippingPlane(u_clippingPlanesTexture, i, u_clippingPlanesMatrix);\n\
  45. vec4 intersection = intersectPlane(ray, planeUv);\n\
  46. if (dot(ray.dir, planeUv.xyz) > 0.0) {\n\
  47. firstTransmission = intersection.w <= firstTransmission.w ? intersection : firstTransmission;\n\
  48. } else {\n\
  49. lastReflection = intersection.w >= lastReflection.w ? intersection : lastReflection;\n\
  50. }\n\
  51. }\n\
  52. clippingVolume.entry = backSide;\n\
  53. clippingVolume.exit = lastReflection;\n\
  54. setShapeIntersection(ix, CLIPPING_PLANES_INTERSECTION_INDEX + 0, clippingVolume);\n\
  55. clippingVolume.entry = firstTransmission;\n\
  56. clippingVolume.exit = farSide;\n\
  57. setShapeIntersection(ix, CLIPPING_PLANES_INTERSECTION_INDEX + 1, clippingVolume);\n\
  58. #else // intersection\n\
  59. vec4 lastTransmission = vec4(ray.dir, -INF_HIT);\n\
  60. vec4 firstReflection = vec4(-ray.dir, +INF_HIT);\n\
  61. for (int i = 0; i < CLIPPING_PLANES_COUNT; i++) {\n\
  62. vec4 planeUv = getClippingPlane(u_clippingPlanesTexture, i, u_clippingPlanesMatrix);\n\
  63. vec4 intersection = intersectPlane(ray, planeUv);\n\
  64. if (dot(ray.dir, planeUv.xyz) > 0.0) {\n\
  65. lastTransmission = intersection.w > lastTransmission.w ? intersection : lastTransmission;\n\
  66. } else {\n\
  67. firstReflection = intersection.w < firstReflection.w ? intersection: firstReflection;\n\
  68. }\n\
  69. }\n\
  70. if (lastTransmission.w < firstReflection.w) {\n\
  71. clippingVolume.entry = lastTransmission;\n\
  72. clippingVolume.exit = firstReflection;\n\
  73. } else {\n\
  74. clippingVolume.entry = vec4(-ray.dir, NO_HIT);\n\
  75. clippingVolume.exit = vec4(ray.dir, NO_HIT);\n\
  76. }\n\
  77. setShapeIntersection(ix, CLIPPING_PLANES_INTERSECTION_INDEX, clippingVolume);\n\
  78. #endif\n\
  79. }\n\
  80. ";