depthBasic.fs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. uniform float opacity;
  2. //uniform float mapScale;
  3. uniform vec3 baseColor;
  4. #if defined use_map
  5. varying vec2 vUv;
  6. uniform sampler2D map;
  7. uniform vec3 mapColor;
  8. vec4 applyMapColor(vec4 color){
  9. vec2 uv = vUv;
  10. /*if(mapScale != 1.0){
  11. uv = (vUv - 0.5) / mapScale + 0.5;
  12. if(uv.x > 1.0 || uv.y > 1.0 || uv.x < 0.0 || uv.y < 0.0){
  13. //color = vec4(0.0,0.0,0.0,0.0);
  14. discard;
  15. }
  16. }*/
  17. vec4 colorFromMap = texture2D(map, uv);
  18. #if defined mapOverlay
  19. //贴图叠加在基础色上,而非相乘
  20. colorFromMap.rgb *= mapColor;
  21. color = color * (1.0-colorFromMap.a) + colorFromMap;
  22. #else
  23. color *= colorFromMap;
  24. #endif
  25. return color;
  26. }
  27. #endif
  28. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  29. //似乎通过gl.getExtension('EXT_frag_depth')得到的GL_EXT_frag_depth
  30. uniform sampler2D depthTexture;
  31. uniform vec2 resolution;
  32. uniform vec2 viewportOffset; // viewportOffset 范围从0-整个画布的像素
  33. uniform vec3 backColor;
  34. uniform float occlusionDistance;
  35. uniform float clipDistance;
  36. uniform float startClipDis;
  37. uniform float startOcclusDis;
  38. uniform float maxClipFactor;
  39. uniform float maxOcclusionFactor;
  40. //uniform bool uUseOrthographicCamera;
  41. #endif
  42. #if defined(GL_EXT_frag_depth) && defined(useDepth) || defined(FadeFar)
  43. uniform float nearPlane;
  44. uniform float farPlane;
  45. float convertToLinear(float zValue){
  46. //if(uUseOrthographicCamera){
  47. // return zValue*(farPlane-nearPlane)+nearPlane;
  48. //}else{
  49. float z = zValue * 2.0 - 1.0;
  50. return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - z * (farPlane - nearPlane));
  51. //}
  52. }
  53. #endif
  54. #if defined(FadeFar)
  55. uniform float fadeFar;
  56. #endif
  57. //#include <clipping_planes_pars_fragment>
  58. void main() {
  59. //#include <clipping_planes_fragment>
  60. vec4 color = vec4(baseColor, opacity);
  61. #if defined(GL_EXT_frag_depth) && defined(useDepth) || defined(FadeFar)
  62. float fragDepth = convertToLinear(gl_FragCoord.z);
  63. #if defined(FadeFar)
  64. float fadeOutFar = fadeFar * 1.3; //完全消失距离
  65. if(fragDepth > fadeOutFar){
  66. discard;
  67. }else if(fragDepth > fadeFar){
  68. color.a *= (fadeOutFar - fragDepth) / (fadeOutFar - fadeFar);
  69. }
  70. #endif
  71. float mixFactor = 0.0;
  72. float clipFactor = 0.0;
  73. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  74. vec2 depthTxtCoords = vec2(gl_FragCoord.x-viewportOffset.x, gl_FragCoord.y - viewportOffset.y) / resolution;
  75. //gl_FragCoord大小为 viewport client大小
  76. float textureDepth = convertToLinear(texture2D(depthTexture, depthTxtCoords).r);
  77. float delta = fragDepth - textureDepth;
  78. if (delta > 0.0)//差距
  79. {
  80. // occlusionDistance and clipDistance define the width of the respective zones and
  81. // mixFactor and clipFactor express the interpolation between the two colors depending on the position
  82. // of the current fragment withing those zones.
  83. mixFactor = clamp((delta - startOcclusDis) / (occlusionDistance - startOcclusDis), 0.0, maxOcclusionFactor);
  84. clipFactor = clamp((delta - startClipDis) / (clipDistance - startClipDis), 0.0, maxClipFactor);
  85. }
  86. #endif
  87. // If the fragment is totally transparent, don't bother drawing it
  88. if (clipFactor == 1.0)
  89. {
  90. discard;
  91. }else{
  92. #if defined use_map
  93. color = applyMapColor(color);
  94. #endif
  95. #if defined(GL_EXT_frag_depth) && defined(useDepth)
  96. float r = pow(1.0 - clipFactor, 3.0); //更透明些
  97. color = vec4(mix(color.rgb, backColor, mixFactor), color.a * r);
  98. #endif
  99. }
  100. #else
  101. #if defined use_map
  102. color = applyMapColor(color);
  103. #endif
  104. #endif
  105. gl_FragColor = color;
  106. }