ShadowVolumeAppearanceFS.glsl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifdef GL_EXT_frag_depth
  2. #extension GL_EXT_frag_depth : enable
  3. #endif
  4. #ifdef TEXTURE_COORDINATES
  5. #ifdef SPHERICAL
  6. varying vec4 v_sphericalExtents;
  7. #else // SPHERICAL
  8. varying vec2 v_inversePlaneExtents;
  9. varying vec4 v_westPlane;
  10. varying vec4 v_southPlane;
  11. #endif // SPHERICAL
  12. varying vec3 v_uvMinAndSphericalLongitudeRotation;
  13. varying vec3 v_uMaxAndInverseDistance;
  14. varying vec3 v_vMaxAndInverseDistance;
  15. #endif // TEXTURE_COORDINATES
  16. #ifdef PER_INSTANCE_COLOR
  17. varying vec4 v_color;
  18. #endif
  19. #ifdef NORMAL_EC
  20. vec3 getEyeCoordinate3FromWindowCoordinate(vec2 fragCoord, float logDepthOrDepth) {
  21. vec4 eyeCoordinate = czm_windowToEyeCoordinates(fragCoord, logDepthOrDepth);
  22. return eyeCoordinate.xyz / eyeCoordinate.w;
  23. }
  24. vec3 vectorFromOffset(vec4 eyeCoordinate, vec2 positiveOffset) {
  25. vec2 glFragCoordXY = gl_FragCoord.xy;
  26. // Sample depths at both offset and negative offset
  27. float upOrRightLogDepth = czm_unpackDepth(texture2D(czm_globeDepthTexture, (glFragCoordXY + positiveOffset) / czm_viewport.zw));
  28. float downOrLeftLogDepth = czm_unpackDepth(texture2D(czm_globeDepthTexture, (glFragCoordXY - positiveOffset) / czm_viewport.zw));
  29. // Explicitly evaluate both paths
  30. // Necessary for multifrustum and for edges of the screen
  31. bvec2 upOrRightInBounds = lessThan(glFragCoordXY + positiveOffset, czm_viewport.zw);
  32. float useUpOrRight = float(upOrRightLogDepth > 0.0 && upOrRightInBounds.x && upOrRightInBounds.y);
  33. float useDownOrLeft = float(useUpOrRight == 0.0);
  34. vec3 upOrRightEC = getEyeCoordinate3FromWindowCoordinate(glFragCoordXY + positiveOffset, upOrRightLogDepth);
  35. vec3 downOrLeftEC = getEyeCoordinate3FromWindowCoordinate(glFragCoordXY - positiveOffset, downOrLeftLogDepth);
  36. return (upOrRightEC - (eyeCoordinate.xyz / eyeCoordinate.w)) * useUpOrRight + ((eyeCoordinate.xyz / eyeCoordinate.w) - downOrLeftEC) * useDownOrLeft;
  37. }
  38. #endif // NORMAL_EC
  39. void main(void)
  40. {
  41. #ifdef REQUIRES_EC
  42. float logDepthOrDepth = czm_unpackDepth(texture2D(czm_globeDepthTexture, gl_FragCoord.xy / czm_viewport.zw));
  43. vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, logDepthOrDepth);
  44. #endif
  45. #ifdef REQUIRES_WC
  46. vec4 worldCoordinate4 = czm_inverseView * eyeCoordinate;
  47. vec3 worldCoordinate = worldCoordinate4.xyz / worldCoordinate4.w;
  48. #endif
  49. #ifdef TEXTURE_COORDINATES
  50. vec2 uv;
  51. #ifdef SPHERICAL
  52. // Treat world coords as a sphere normal for spherical coordinates
  53. vec2 sphericalLatLong = czm_approximateSphericalCoordinates(worldCoordinate);
  54. sphericalLatLong.y += v_uvMinAndSphericalLongitudeRotation.z;
  55. sphericalLatLong.y = czm_branchFreeTernary(sphericalLatLong.y < czm_pi, sphericalLatLong.y, sphericalLatLong.y - czm_twoPi);
  56. uv.x = (sphericalLatLong.y - v_sphericalExtents.y) * v_sphericalExtents.w;
  57. uv.y = (sphericalLatLong.x - v_sphericalExtents.x) * v_sphericalExtents.z;
  58. #else // SPHERICAL
  59. // Unpack planes and transform to eye space
  60. uv.x = czm_planeDistance(v_westPlane, eyeCoordinate.xyz / eyeCoordinate.w) * v_inversePlaneExtents.x;
  61. uv.y = czm_planeDistance(v_southPlane, eyeCoordinate.xyz / eyeCoordinate.w) * v_inversePlaneExtents.y;
  62. #endif // SPHERICAL
  63. #endif // TEXTURE_COORDINATES
  64. #ifdef PICK
  65. #ifdef CULL_FRAGMENTS
  66. if (0.0 <= uv.x && uv.x <= 1.0 && 0.0 <= uv.y && uv.y <= 1.0) {
  67. gl_FragColor.a = 1.0; // 0.0 alpha leads to discard from ShaderSource.createPickFragmentShaderSource
  68. czm_writeDepthClamp();
  69. }
  70. #else // CULL_FRAGMENTS
  71. gl_FragColor.a = 1.0;
  72. #endif // CULL_FRAGMENTS
  73. #else // PICK
  74. #ifdef CULL_FRAGMENTS
  75. if (uv.x <= 0.0 || 1.0 <= uv.x || uv.y <= 0.0 || 1.0 <= uv.y) {
  76. discard;
  77. }
  78. #endif
  79. #ifdef NORMAL_EC
  80. // Compute normal by sampling adjacent pixels in 2x2 block in screen space
  81. vec3 downUp = vectorFromOffset(eyeCoordinate, vec2(0.0, 1.0));
  82. vec3 leftRight = vectorFromOffset(eyeCoordinate, vec2(1.0, 0.0));
  83. vec3 normalEC = normalize(cross(leftRight, downUp));
  84. #endif
  85. #ifdef PER_INSTANCE_COLOR
  86. vec4 color = czm_gammaCorrect(v_color);
  87. #ifdef FLAT
  88. gl_FragColor = color;
  89. #else // FLAT
  90. czm_materialInput materialInput;
  91. materialInput.normalEC = normalEC;
  92. materialInput.positionToEyeEC = -eyeCoordinate.xyz;
  93. czm_material material = czm_getDefaultMaterial(materialInput);
  94. material.diffuse = color.rgb;
  95. material.alpha = color.a;
  96. gl_FragColor = czm_phong(normalize(-eyeCoordinate.xyz), material, czm_lightDirectionEC);
  97. #endif // FLAT
  98. // Premultiply alpha. Required for classification primitives on translucent globe.
  99. gl_FragColor.rgb *= gl_FragColor.a;
  100. #else // PER_INSTANCE_COLOR
  101. // Material support.
  102. // USES_ is distinct from REQUIRES_, because some things are dependencies of each other or
  103. // dependencies for culling but might not actually be used by the material.
  104. czm_materialInput materialInput;
  105. #ifdef USES_NORMAL_EC
  106. materialInput.normalEC = normalEC;
  107. #endif
  108. #ifdef USES_POSITION_TO_EYE_EC
  109. materialInput.positionToEyeEC = -eyeCoordinate.xyz;
  110. #endif
  111. #ifdef USES_TANGENT_TO_EYE
  112. materialInput.tangentToEyeMatrix = czm_eastNorthUpToEyeCoordinates(worldCoordinate, normalEC);
  113. #endif
  114. #ifdef USES_ST
  115. // Remap texture coordinates from computed (approximately aligned with cartographic space) to the desired
  116. // texture coordinate system, which typically forms a tight oriented bounding box around the geometry.
  117. // Shader is provided a set of reference points for remapping.
  118. materialInput.st.x = czm_lineDistance(v_uvMinAndSphericalLongitudeRotation.xy, v_uMaxAndInverseDistance.xy, uv) * v_uMaxAndInverseDistance.z;
  119. materialInput.st.y = czm_lineDistance(v_uvMinAndSphericalLongitudeRotation.xy, v_vMaxAndInverseDistance.xy, uv) * v_vMaxAndInverseDistance.z;
  120. #endif
  121. czm_material material = czm_getMaterial(materialInput);
  122. #ifdef FLAT
  123. gl_FragColor = vec4(material.diffuse + material.emission, material.alpha);
  124. #else // FLAT
  125. gl_FragColor = czm_phong(normalize(-eyeCoordinate.xyz), material, czm_lightDirectionEC);
  126. #endif // FLAT
  127. // Premultiply alpha. Required for classification primitives on translucent globe.
  128. gl_FragColor.rgb *= gl_FragColor.a;
  129. #endif // PER_INSTANCE_COLOR
  130. czm_writeDepthClamp();
  131. #endif // PICK
  132. }