IntersectBox.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "// See IntersectionUtils.glsl for the definitions of Ray and NO_HIT\n\
  3. // See convertUvToBox.glsl for the definition of convertShapeUvToUvSpace\n\
  4. \n\
  5. /* Box defines (set in Scene/VoxelBoxShape.js)\n\
  6. #define BOX_INTERSECTION_INDEX ### // always 0\n\
  7. */\n\
  8. \n\
  9. uniform vec3 u_renderMinBounds;\n\
  10. uniform vec3 u_renderMaxBounds;\n\
  11. \n\
  12. struct Box {\n\
  13. vec3 p0;\n\
  14. vec3 p1;\n\
  15. };\n\
  16. \n\
  17. Box constructVoxelBox(in ivec4 octreeCoords, in vec3 tileUv)\n\
  18. {\n\
  19. // Find the min/max cornerpoints of the voxel in tile coordinates\n\
  20. vec3 tileOrigin = vec3(octreeCoords.xyz);\n\
  21. vec3 numSamples = vec3(u_dimensions);\n\
  22. vec3 voxelSize = 1.0 / numSamples;\n\
  23. vec3 coordP0 = floor(tileUv * numSamples) * voxelSize + tileOrigin;\n\
  24. vec3 coordP1 = coordP0 + voxelSize;\n\
  25. \n\
  26. // Transform to the UV coordinates of the scaled tileset\n\
  27. float tileSize = 1.0 / pow(2.0, float(octreeCoords.w));\n\
  28. vec3 p0 = convertShapeUvToUvSpace(coordP0 * tileSize);\n\
  29. vec3 p1 = convertShapeUvToUvSpace(coordP1 * tileSize);\n\
  30. \n\
  31. return Box(p0, p1);\n\
  32. }\n\
  33. \n\
  34. vec3 getBoxNormal(in Box box, in Ray ray, in float t)\n\
  35. {\n\
  36. vec3 hitPoint = ray.pos + t * ray.dir;\n\
  37. vec3 lower = step(hitPoint, box.p0);\n\
  38. vec3 upper = step(box.p1, hitPoint);\n\
  39. return normalize(upper - lower);\n\
  40. }\n\
  41. \n\
  42. // Find the distances along a ray at which the ray intersects an axis-aligned box\n\
  43. // See https://tavianator.com/2011/ray_box.html\n\
  44. RayShapeIntersection intersectBox(in Ray ray, in Box box)\n\
  45. {\n\
  46. // Consider the box as the intersection of the space between 3 pairs of parallel planes\n\
  47. // Compute the distance along the ray to each plane\n\
  48. vec3 t0 = (box.p0 - ray.pos) * ray.dInv;\n\
  49. vec3 t1 = (box.p1 - ray.pos) * ray.dInv;\n\
  50. \n\
  51. // Identify candidate entries/exits based on distance from ray.pos\n\
  52. vec3 entries = min(t0, t1);\n\
  53. vec3 exits = max(t0, t1);\n\
  54. \n\
  55. // The actual box intersection points are the furthest entry and the closest exit\n\
  56. float entryT = max(max(entries.x, entries.y), entries.z);\n\
  57. float exitT = min(min(exits.x, exits.y), exits.z);\n\
  58. \n\
  59. vec3 entryNormal = getBoxNormal(box, ray, entryT - RAY_SHIFT);\n\
  60. vec3 exitNormal = getBoxNormal(box, ray, exitT + RAY_SHIFT);\n\
  61. \n\
  62. if (entryT > exitT) {\n\
  63. entryT = NO_HIT;\n\
  64. exitT = NO_HIT;\n\
  65. }\n\
  66. \n\
  67. return RayShapeIntersection(vec4(entryNormal, entryT), vec4(exitNormal, exitT));\n\
  68. }\n\
  69. \n\
  70. void intersectShape(in Ray ray, inout Intersections ix)\n\
  71. {\n\
  72. RayShapeIntersection intersection = intersectBox(ray, Box(u_renderMinBounds, u_renderMaxBounds));\n\
  73. setShapeIntersection(ix, BOX_INTERSECTION_INDEX, intersection);\n\
  74. }\n\
  75. ";