depthBasic.fs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. varying vec2 vUv;
  2. uniform float opacity;
  3. uniform float mapScale;
  4. uniform vec3 baseColor;
  5. #if defined use_map
  6. uniform sampler2D map;
  7. vec4 getMapColor(vec4 color){
  8. vec2 uv = (vUv - 0.5) / mapScale + 0.5;
  9. if(uv.x > 1.0 || uv.y > 1.0 || uv.x < 0.0 || uv.y < 0.0){
  10. //color = vec4(0.0,0.0,0.0,0.0);
  11. discard;
  12. }else{
  13. color = texture2D(map, uv) * color;
  14. }
  15. return color;
  16. }
  17. #endif
  18. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  19. //似乎通过gl.getExtension('EXT_frag_depth')得到的GL_EXT_frag_depth
  20. uniform sampler2D depthTexture;
  21. uniform float nearPlane;
  22. uniform float farPlane;
  23. uniform vec2 resolution;
  24. uniform vec2 viewportOffset; // viewportOffset 范围从0-整个画布的像素
  25. uniform vec3 backColor;
  26. uniform float occlusionDistance;
  27. uniform float clipDistance;
  28. uniform float maxClipFactor;
  29. uniform float maxOcclusionFactor;
  30. //uniform bool uUseOrthographicCamera;
  31. float convertToLinear(float zValue)
  32. {
  33. //if(uUseOrthographicCamera){
  34. // return zValue*(farPlane-nearPlane)+nearPlane;
  35. //}else{
  36. float z = zValue * 2.0 - 1.0;
  37. return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - z * (farPlane - nearPlane));
  38. //}
  39. }
  40. #endif
  41. void main() {
  42. vec4 color = vec4(baseColor, opacity);
  43. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  44. // mixFactor and clipFactor define the color mixing proportion between the states of
  45. // full visibility and occluded visibility
  46. // and
  47. // full visibility and total invisibility
  48. float mixFactor = 0.0;
  49. float clipFactor = 0.0;
  50. // The linear depth value of the current fragment
  51. float fragDepth = convertToLinear(gl_FragCoord.z);
  52. // The coordinates of the current fragment in the depth texture
  53. vec2 depthTxtCoords = vec2(gl_FragCoord.x-viewportOffset.x, gl_FragCoord.y - viewportOffset.y) / resolution;
  54. //gl_FragCoord大小为 viewport client大小
  55. // The linear depth value of the pixel occupied by this fragment in the depth buffer
  56. float textureDepth = convertToLinear(texture2D(depthTexture, depthTxtCoords).r);
  57. // The difference between the two depths
  58. float delta = fragDepth - textureDepth;
  59. if (delta > 0.0)//差距
  60. {
  61. // occlusionDistance and clipDistance define the width of the respective zones and
  62. // mixFactor and clipFactor express the interpolation between the two colors depending on the position
  63. // of the current fragment withing those zones.
  64. mixFactor = clamp(delta / occlusionDistance, 0.0, maxOcclusionFactor);
  65. clipFactor = clamp(delta / clipDistance, 0.0, maxClipFactor);
  66. }
  67. // If the fragment is totally transparent, don't bother drawing it
  68. if (clipFactor == 1.0)
  69. {
  70. discard;
  71. }else{
  72. #if defined use_map
  73. color = getMapColor(color);
  74. #endif
  75. color = vec4(mix(color.rgb, backColor, mixFactor), color.a * (1.0 - clipFactor));
  76. }
  77. #else
  78. #if defined use_map
  79. color = getMapColor(color);
  80. #endif
  81. #endif
  82. gl_FragColor = color;
  83. }