EllipsoidFS.js 4.5 KB

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