EllipsoidFS.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. //This file is automatically rebuilt by the Cesium build process.
  2. export default "uniform vec3 u_radii;\n\
  3. uniform vec3 u_oneOverEllipsoidRadiiSquared;\n\
  4. \n\
  5. in vec3 v_positionEC;\n\
  6. \n\
  7. vec4 computeEllipsoidColor(czm_ray ray, float intersection, float side)\n\
  8. {\n\
  9. vec3 positionEC = czm_pointAlongRay(ray, intersection);\n\
  10. vec3 positionMC = (czm_inverseModelView * vec4(positionEC, 1.0)).xyz;\n\
  11. vec3 geodeticNormal = normalize(czm_geodeticSurfaceNormal(positionMC, vec3(0.0), u_oneOverEllipsoidRadiiSquared));\n\
  12. vec3 sphericalNormal = normalize(positionMC / u_radii);\n\
  13. vec3 normalMC = geodeticNormal * side; // normalized surface normal (always facing the viewer) in model coordinates\n\
  14. vec3 normalEC = normalize(czm_normal * normalMC); // normalized surface normal in eye coordiantes\n\
  15. \n\
  16. vec2 st = czm_ellipsoidWgs84TextureCoordinates(sphericalNormal);\n\
  17. vec3 positionToEyeEC = -positionEC;\n\
  18. \n\
  19. czm_materialInput materialInput;\n\
  20. materialInput.s = st.s;\n\
  21. materialInput.st = st;\n\
  22. materialInput.str = (positionMC + u_radii) / u_radii;\n\
  23. materialInput.normalEC = normalEC;\n\
  24. materialInput.tangentToEyeMatrix = czm_eastNorthUpToEyeCoordinates(positionMC, normalEC);\n\
  25. materialInput.positionToEyeEC = positionToEyeEC;\n\
  26. czm_material material = czm_getMaterial(materialInput);\n\
  27. \n\
  28. #ifdef ONLY_SUN_LIGHTING\n\
  29. return czm_private_phong(normalize(positionToEyeEC), material, czm_sunDirectionEC);\n\
  30. #else\n\
  31. return czm_phong(normalize(positionToEyeEC), material, czm_lightDirectionEC);\n\
  32. #endif\n\
  33. }\n\
  34. \n\
  35. void main()\n\
  36. {\n\
  37. // PERFORMANCE_TODO: When dynamic branching is available, compute ratio of maximum and minimum radii\n\
  38. // in the vertex shader. Only when it is larger than some constant, march along the ray.\n\
  39. // Otherwise perform one intersection test which will be the common case.\n\
  40. \n\
  41. // Test if the ray intersects a sphere with the ellipsoid's maximum radius.\n\
  42. // For very oblate ellipsoids, using the ellipsoid's radii for an intersection test\n\
  43. // may cause false negatives. This will discard fragments before marching the ray forward.\n\
  44. float maxRadius = max(u_radii.x, max(u_radii.y, u_radii.z)) * 1.5;\n\
  45. vec3 direction = normalize(v_positionEC);\n\
  46. vec3 ellipsoidCenter = czm_modelView[3].xyz;\n\
  47. \n\
  48. float t1 = -1.0;\n\
  49. float t2 = -1.0;\n\
  50. \n\
  51. float b = -2.0 * dot(direction, ellipsoidCenter);\n\
  52. float c = dot(ellipsoidCenter, ellipsoidCenter) - maxRadius * maxRadius;\n\
  53. \n\
  54. float discriminant = b * b - 4.0 * c;\n\
  55. if (discriminant >= 0.0) {\n\
  56. t1 = (-b - sqrt(discriminant)) * 0.5;\n\
  57. t2 = (-b + sqrt(discriminant)) * 0.5;\n\
  58. }\n\
  59. \n\
  60. if (t1 < 0.0 && t2 < 0.0) {\n\
  61. discard;\n\
  62. }\n\
  63. \n\
  64. float t = min(t1, t2);\n\
  65. if (t < 0.0) {\n\
  66. t = 0.0;\n\
  67. }\n\
  68. \n\
  69. // March ray forward to intersection with larger sphere and find\n\
  70. czm_ray ray = czm_ray(t * direction, direction);\n\
  71. \n\
  72. vec3 ellipsoid_inverseRadii = vec3(1.0 / u_radii.x, 1.0 / u_radii.y, 1.0 / u_radii.z);\n\
  73. \n\
  74. czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoidCenter, ellipsoid_inverseRadii);\n\
  75. \n\
  76. if (czm_isEmpty(intersection))\n\
  77. {\n\
  78. discard;\n\
  79. }\n\
  80. \n\
  81. // If the viewer is outside, compute outsideFaceColor, with normals facing outward.\n\
  82. vec4 outsideFaceColor = (intersection.start != 0.0) ? computeEllipsoidColor(ray, intersection.start, 1.0) : vec4(0.0);\n\
  83. \n\
  84. // If the viewer either is inside or can see inside, compute insideFaceColor, with normals facing inward.\n\
  85. vec4 insideFaceColor = (outsideFaceColor.a < 1.0) ? computeEllipsoidColor(ray, intersection.stop, -1.0) : vec4(0.0);\n\
  86. \n\
  87. out_FragColor = mix(insideFaceColor, outsideFaceColor, outsideFaceColor.a);\n\
  88. out_FragColor.a = 1.0 - (1.0 - insideFaceColor.a) * (1.0 - outsideFaceColor.a);\n\
  89. \n\
  90. #if (defined(WRITE_DEPTH) && (__VERSION__ == 300 || defined(GL_EXT_frag_depth)))\n\
  91. t = (intersection.start != 0.0) ? intersection.start : intersection.stop;\n\
  92. vec3 positionEC = czm_pointAlongRay(ray, t);\n\
  93. vec4 positionCC = czm_projection * vec4(positionEC, 1.0);\n\
  94. #ifdef LOG_DEPTH\n\
  95. czm_writeLogDepth(1.0 + positionCC.w);\n\
  96. #else\n\
  97. float z = positionCC.z / positionCC.w;\n\
  98. \n\
  99. float n = czm_depthRange.near;\n\
  100. float f = czm_depthRange.far;\n\
  101. \n\
  102. gl_FragDepth = (z * (f - n) + f + n) * 0.5;\n\
  103. #endif\n\
  104. #endif\n\
  105. }\n\
  106. ";