ShadowVolumeAppearanceFS.glsl 6.5 KB

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