ShadowVolumeAppearanceFS.glsl 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. {
  22. vec4 eyeCoordinate = czm_windowToEyeCoordinates(fragCoord, logDepthOrDepth);
  23. return eyeCoordinate.xyz / eyeCoordinate.w;
  24. }
  25. vec3 vectorFromOffset(vec4 eyeCoordinate, vec2 positiveOffset)
  26. {
  27. vec2 glFragCoordXY = gl_FragCoord.xy;
  28. // Sample depths at both offset and negative offset
  29. float upOrRightLogDepth = czm_unpackDepth(texture2D(czm_globeDepthTexture, (glFragCoordXY + positiveOffset) / czm_viewport.zw));
  30. float downOrLeftLogDepth = czm_unpackDepth(texture2D(czm_globeDepthTexture, (glFragCoordXY - positiveOffset) / czm_viewport.zw));
  31. // Explicitly evaluate both paths
  32. // Necessary for multifrustum and for edges of the screen
  33. bvec2 upOrRightInBounds = lessThan(glFragCoordXY + positiveOffset, czm_viewport.zw);
  34. float useUpOrRight = float(upOrRightLogDepth > 0.0 && upOrRightInBounds.x && upOrRightInBounds.y);
  35. float useDownOrLeft = float(useUpOrRight == 0.0);
  36. vec3 upOrRightEC = getEyeCoordinate3FromWindowCoordinate(glFragCoordXY + positiveOffset, upOrRightLogDepth);
  37. vec3 downOrLeftEC = getEyeCoordinate3FromWindowCoordinate(glFragCoordXY - positiveOffset, downOrLeftLogDepth);
  38. return (upOrRightEC - (eyeCoordinate.xyz / eyeCoordinate.w)) * useUpOrRight + ((eyeCoordinate.xyz / eyeCoordinate.w) - downOrLeftEC) * useDownOrLeft;
  39. }
  40. #endif // NORMAL_EC
  41. void main(void)
  42. {
  43. #ifdef REQUIRES_EC
  44. float logDepthOrDepth = czm_unpackDepth(texture2D(czm_globeDepthTexture, gl_FragCoord.xy / czm_viewport.zw));
  45. vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, logDepthOrDepth);
  46. #endif
  47. #ifdef REQUIRES_WC
  48. vec4 worldCoordinate4 = czm_inverseView * eyeCoordinate;
  49. vec3 worldCoordinate = worldCoordinate4.xyz / worldCoordinate4.w;
  50. #endif
  51. #ifdef TEXTURE_COORDINATES
  52. vec2 uv;
  53. #ifdef SPHERICAL
  54. // Treat world coords as a sphere normal for spherical coordinates
  55. vec2 sphericalLatLong = czm_approximateSphericalCoordinates(worldCoordinate);
  56. sphericalLatLong.y += v_uvMinAndSphericalLongitudeRotation.z;
  57. sphericalLatLong.y = czm_branchFreeTernary(sphericalLatLong.y < czm_pi, sphericalLatLong.y, sphericalLatLong.y - czm_twoPi);
  58. uv.x = (sphericalLatLong.y - v_sphericalExtents.y) * v_sphericalExtents.w;
  59. uv.y = (sphericalLatLong.x - v_sphericalExtents.x) * v_sphericalExtents.z;
  60. #else // SPHERICAL
  61. // Unpack planes and transform to eye space
  62. uv.x = czm_planeDistance(v_westPlane, eyeCoordinate.xyz / eyeCoordinate.w) * v_inversePlaneExtents.x;
  63. uv.y = czm_planeDistance(v_southPlane, eyeCoordinate.xyz / eyeCoordinate.w) * v_inversePlaneExtents.y;
  64. #endif // SPHERICAL
  65. #endif // TEXTURE_COORDINATES
  66. #ifdef PICK
  67. #ifdef CULL_FRAGMENTS
  68. if (0.0 <= uv.x && uv.x <= 1.0 && 0.0 <= uv.y && uv.y <= 1.0)
  69. {
  70. gl_FragColor.a = 1.0; // 0.0 alpha leads to discard from ShaderSource.createPickFragmentShaderSource
  71. czm_writeDepthClampedToFarPlane();
  72. }
  73. #else // CULL_FRAGMENTS
  74. gl_FragColor.a = 1.0;
  75. #endif // CULL_FRAGMENTS
  76. #else // PICK
  77. #ifdef CULL_FRAGMENTS
  78. if (uv.x <= 0.0 || 1.0 <= uv.x || uv.y <= 0.0 || 1.0 <= uv.y)
  79. {
  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. gl_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. gl_FragColor = czm_phong(normalize(-eyeCoordinate.xyz), material);
  101. #endif // FLAT
  102. #else // PER_INSTANCE_COLOR
  103. // Material support.
  104. // USES_ is distinct from REQUIRES_, because some things are dependencies of each other or
  105. // dependencies for culling but might not actually be used by the material.
  106. czm_materialInput materialInput;
  107. #ifdef USES_NORMAL_EC
  108. materialInput.normalEC = normalEC;
  109. #endif
  110. #ifdef USES_POSITION_TO_EYE_EC
  111. materialInput.positionToEyeEC = -eyeCoordinate.xyz;
  112. #endif
  113. #ifdef USES_TANGENT_TO_EYE
  114. materialInput.tangentToEyeMatrix = czm_eastNorthUpToEyeCoordinates(worldCoordinate, normalEC);
  115. #endif
  116. #ifdef USES_ST
  117. // Remap texture coordinates from computed (approximately aligned with cartographic space) to the desired
  118. // texture coordinate system, which typically forms a tight oriented bounding box around the geometry.
  119. // Shader is provided a set of reference points for remapping.
  120. materialInput.st.x = czm_lineDistance(v_uvMinAndSphericalLongitudeRotation.xy, v_uMaxAndInverseDistance.xy, uv) * v_uMaxAndInverseDistance.z;
  121. materialInput.st.y = czm_lineDistance(v_uvMinAndSphericalLongitudeRotation.xy, v_vMaxAndInverseDistance.xy, uv) * v_vMaxAndInverseDistance.z;
  122. #endif
  123. czm_material material = czm_getMaterial(materialInput);
  124. #ifdef FLAT
  125. gl_FragColor = vec4(material.diffuse + material.emission, material.alpha);
  126. #else // FLAT
  127. gl_FragColor = czm_phong(normalize(-eyeCoordinate.xyz), material);
  128. #endif // FLAT
  129. #endif // PER_INSTANCE_COLOR
  130. czm_writeDepthClampedToFarPlane();
  131. #endif // PICK
  132. }