ShadowVolumeAppearanceFS.glsl 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. // When classifying translucent geometry, logDepthOrDepth == 0.0
  67. // indicates a region that should not be classified, possibly due to there
  68. // being opaque pixels there in another buffer.
  69. // Check for logDepthOrDepth != 0.0 to make sure this should be classified.
  70. if (0.0 <= uv.x && uv.x <= 1.0 && 0.0 <= uv.y && uv.y <= 1.0 || logDepthOrDepth != 0.0) {
  71. gl_FragColor.a = 1.0; // 0.0 alpha leads to discard from ShaderSource.createPickFragmentShaderSource
  72. czm_writeDepthClamp();
  73. }
  74. #else // CULL_FRAGMENTS
  75. gl_FragColor.a = 1.0;
  76. #endif // CULL_FRAGMENTS
  77. #else // PICK
  78. #ifdef CULL_FRAGMENTS
  79. // When classifying translucent geometry, logDepthOrDepth == 0.0
  80. // indicates a region that should not be classified, possibly due to there
  81. // being opaque pixels there in another buffer.
  82. if (uv.x <= 0.0 || 1.0 <= uv.x || uv.y <= 0.0 || 1.0 <= uv.y || logDepthOrDepth == 0.0) {
  83. discard;
  84. }
  85. #endif
  86. #ifdef NORMAL_EC
  87. // Compute normal by sampling adjacent pixels in 2x2 block in screen space
  88. vec3 downUp = vectorFromOffset(eyeCoordinate, vec2(0.0, 1.0));
  89. vec3 leftRight = vectorFromOffset(eyeCoordinate, vec2(1.0, 0.0));
  90. vec3 normalEC = normalize(cross(leftRight, downUp));
  91. #endif
  92. #ifdef PER_INSTANCE_COLOR
  93. vec4 color = czm_gammaCorrect(v_color);
  94. #ifdef FLAT
  95. gl_FragColor = color;
  96. #else // FLAT
  97. czm_materialInput materialInput;
  98. materialInput.normalEC = normalEC;
  99. materialInput.positionToEyeEC = -eyeCoordinate.xyz;
  100. czm_material material = czm_getDefaultMaterial(materialInput);
  101. material.diffuse = color.rgb;
  102. material.alpha = color.a;
  103. gl_FragColor = czm_phong(normalize(-eyeCoordinate.xyz), material, czm_lightDirectionEC);
  104. #endif // FLAT
  105. // Premultiply alpha. Required for classification primitives on translucent globe.
  106. gl_FragColor.rgb *= gl_FragColor.a;
  107. #else // PER_INSTANCE_COLOR
  108. // Material support.
  109. // USES_ is distinct from REQUIRES_, because some things are dependencies of each other or
  110. // dependencies for culling but might not actually be used by the material.
  111. czm_materialInput materialInput;
  112. #ifdef USES_NORMAL_EC
  113. materialInput.normalEC = normalEC;
  114. #endif
  115. #ifdef USES_POSITION_TO_EYE_EC
  116. materialInput.positionToEyeEC = -eyeCoordinate.xyz;
  117. #endif
  118. #ifdef USES_TANGENT_TO_EYE
  119. materialInput.tangentToEyeMatrix = czm_eastNorthUpToEyeCoordinates(worldCoordinate, normalEC);
  120. #endif
  121. #ifdef USES_ST
  122. // Remap texture coordinates from computed (approximately aligned with cartographic space) to the desired
  123. // texture coordinate system, which typically forms a tight oriented bounding box around the geometry.
  124. // Shader is provided a set of reference points for remapping.
  125. materialInput.st.x = czm_lineDistance(v_uvMinAndSphericalLongitudeRotation.xy, v_uMaxAndInverseDistance.xy, uv) * v_uMaxAndInverseDistance.z;
  126. materialInput.st.y = czm_lineDistance(v_uvMinAndSphericalLongitudeRotation.xy, v_vMaxAndInverseDistance.xy, uv) * v_vMaxAndInverseDistance.z;
  127. #endif
  128. czm_material material = czm_getMaterial(materialInput);
  129. #ifdef FLAT
  130. gl_FragColor = vec4(material.diffuse + material.emission, material.alpha);
  131. #else // FLAT
  132. gl_FragColor = czm_phong(normalize(-eyeCoordinate.xyz), material, czm_lightDirectionEC);
  133. #endif // FLAT
  134. // Premultiply alpha. Required for classification primitives on translucent globe.
  135. gl_FragColor.rgb *= gl_FragColor.a;
  136. #endif // PER_INSTANCE_COLOR
  137. czm_writeDepthClamp();
  138. #endif // PICK
  139. }