raySphereIntersectionInterval.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "/**\n\
  3. * Compute the intersection interval of a ray with a sphere.\n\
  4. *\n\
  5. * @name czm_raySphereIntersectionInterval\n\
  6. * @glslFunction\n\
  7. *\n\
  8. * @param {czm_ray} ray The ray.\n\
  9. * @param {vec3} center The center of the sphere.\n\
  10. * @param {float} radius The radius of the sphere.\n\
  11. * @return {czm_raySegment} The intersection interval of the ray with the sphere.\n\
  12. */\n\
  13. czm_raySegment czm_raySphereIntersectionInterval(czm_ray ray, vec3 center, float radius)\n\
  14. {\n\
  15. vec3 o = ray.origin;\n\
  16. vec3 d = ray.direction;\n\
  17. \n\
  18. vec3 oc = o - center;\n\
  19. \n\
  20. float a = dot(d, d);\n\
  21. float b = 2.0 * dot(d, oc);\n\
  22. float c = dot(oc, oc) - (radius * radius);\n\
  23. \n\
  24. float det = (b * b) - (4.0 * a * c);\n\
  25. \n\
  26. if (det < 0.0) {\n\
  27. return czm_emptyRaySegment;\n\
  28. }\n\
  29. \n\
  30. float sqrtDet = sqrt(det);\n\
  31. \n\
  32. float t0 = (-b - sqrtDet) / (2.0 * a);\n\
  33. float t1 = (-b + sqrtDet) / (2.0 * a);\n\
  34. \n\
  35. czm_raySegment result = czm_raySegment(t0, t1);\n\
  36. return result;\n\
  37. }\n\
  38. ";