image.frag.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * Copyright (c) 2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. export default `
  7. precision highp float;
  8. precision highp int;
  9. #include common
  10. #include read_from_texture
  11. #include common_frag_params
  12. #include common_clip
  13. #include wboit_params
  14. uniform vec2 uImageTexDim;
  15. uniform sampler2D tImageTex;
  16. uniform sampler2D tGroupTex;
  17. uniform vec2 uMarkerTexDim;
  18. uniform sampler2D tMarker;
  19. varying vec2 vUv;
  20. varying float vInstance;
  21. #if defined(dInterpolation_catmulrom) || defined(dInterpolation_mitchell) || defined(dInterpolation_bspline)
  22. #define dInterpolation_cubic
  23. #endif
  24. #if defined(dInterpolation_cubic)
  25. #if defined(dInterpolation_catmulrom) || defined(dInterpolation_mitchell)
  26. #if defined(dInterpolation_catmulrom)
  27. const float B = 0.0;
  28. const float C = 0.5;
  29. #elif defined(dInterpolation_mitchell)
  30. const float B = 0.333;
  31. const float C = 0.333;
  32. #endif
  33. float cubicFilter(float x){
  34. float f = x;
  35. if (f < 0.0) {
  36. f = -f;
  37. }
  38. if (f < 1.0) {
  39. return ((12.0 - 9.0 * B - 6.0 * C) * (f * f * f) +
  40. (-18.0 + 12.0 * B + 6.0 * C) * (f * f) +
  41. (6.0 - 2.0 * B)) / 6.0;
  42. }else if (f >= 1.0 && f < 2.0){
  43. return ((-B - 6.0 * C) * ( f * f * f)
  44. + (6.0 * B + 30.0 * C) * (f * f) +
  45. (-(12.0 * B) - 48.0 * C) * f +
  46. 8.0 * B + 24.0 * C) / 6.0;
  47. }else{
  48. return 0.0;
  49. }
  50. }
  51. #elif defined(dInterpolation_bspline)
  52. float cubicFilter(float x) {
  53. float f = x;
  54. if (f < 0.0) {
  55. f = -f;
  56. }
  57. if (f >= 0.0 && f <= 1.0){
  58. return (2.0 / 3.0) + (0.5) * (f * f * f) - (f * f);
  59. } else if (f > 1.0 && f <= 2.0) {
  60. return 1.0 / 6.0 * pow((2.0 - f), 3.0);
  61. }
  62. return 1.0;
  63. }
  64. #endif
  65. vec4 biCubic(sampler2D tex, vec2 texCoord) {
  66. vec2 texelSize = 1.0 / uImageTexDim;
  67. texCoord -= texelSize / 2.0;
  68. vec4 nSum = vec4(0.0);
  69. float nDenom = 0.0;
  70. vec2 cell = fract(texCoord * uImageTexDim);
  71. for (float m = -1.0; m <= 2.0; ++m) {
  72. for (float n = -1.0; n <= 2.0; ++n) {
  73. vec4 vecData = texture2D(tex, texCoord + texelSize * vec2(m, n));
  74. float c = cubicFilter(m - cell.x) * cubicFilter(-n + cell.y);
  75. nSum += vecData * c;
  76. nDenom += c;
  77. }
  78. }
  79. return nSum / nDenom;
  80. }
  81. #endif
  82. void main() {
  83. #include clip_pixel
  84. #if defined(dInterpolation_cubic)
  85. vec4 imageData = biCubic(tImageTex, vUv);
  86. #else
  87. vec4 imageData = texture2D(tImageTex, vUv);
  88. #endif
  89. imageData.a = clamp(imageData.a, 0.0, 1.0);
  90. if (imageData.a > 0.9) imageData.a = 1.0;
  91. float fragmentDepth = gl_FragCoord.z;
  92. bool interior = false;
  93. #if defined(dRenderVariant_pick)
  94. if (imageData.a < 0.3)
  95. discard;
  96. #if defined(dRenderVariant_pickObject)
  97. gl_FragColor = vec4(encodeFloatRGB(float(uObjectId)), 1.0);
  98. #elif defined(dRenderVariant_pickInstance)
  99. gl_FragColor = vec4(encodeFloatRGB(vInstance), 1.0);
  100. #elif defined(dRenderVariant_pickGroup)
  101. gl_FragColor = vec4(texture2D(tGroupTex, vUv).rgb, 1.0);
  102. #endif
  103. #elif defined(dRenderVariant_depth)
  104. if (imageData.a < 0.05)
  105. discard;
  106. gl_FragColor = packDepthToRGBA(gl_FragCoord.z);
  107. #elif defined(dRenderVariant_color)
  108. if (imageData.a < 0.05)
  109. discard;
  110. gl_FragColor = imageData;
  111. gl_FragColor.a *= uAlpha;
  112. float group = decodeFloatRGB(texture2D(tGroupTex, vUv).rgb);
  113. float vMarker = readFromTexture(tMarker, vInstance * float(uGroupCount) + group, uMarkerTexDim).a;
  114. #include apply_marker_color
  115. #include apply_fog
  116. #include wboit_write
  117. #endif
  118. }
  119. `;