ShadowVolumeAppearanceFS.js 7.2 KB

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