image.frag.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. uniform vec2 uImageTexDim;
  13. uniform sampler2D tImageTex;
  14. uniform sampler2D tGroupTex;
  15. uniform vec2 uMarkerTexDim;
  16. uniform sampler2D tMarker;
  17. varying vec2 vUv;
  18. varying float vInstance;
  19. #if defined(dInterpolation_catmulrom) || defined(dInterpolation_mitchell) || defined(dInterpolation_bspline)
  20. #define dInterpolation_cubic
  21. #endif
  22. #if defined(dInterpolation_cubic)
  23. #if defined(dInterpolation_catmulrom) || defined(dInterpolation_mitchell)
  24. #if defined(dInterpolation_catmulrom)
  25. const float B = 0.0;
  26. const float C = 0.5;
  27. #elif defined(dInterpolation_mitchell)
  28. const float B = 0.333;
  29. const float C = 0.333;
  30. #endif
  31. float cubicFilter(float x){
  32. float f = x;
  33. if (f < 0.0) {
  34. f = -f;
  35. }
  36. if (f < 1.0) {
  37. return ((12.0 - 9.0 * B - 6.0 * C) * (f * f * f) +
  38. (-18.0 + 12.0 * B + 6.0 * C) * (f * f) +
  39. (6.0 - 2.0 * B)) / 6.0;
  40. }else if (f >= 1.0 && f < 2.0){
  41. return ((-B - 6.0 * C) * ( f * f * f)
  42. + (6.0 * B + 30.0 * C) * (f * f) +
  43. (-(12.0 * B) - 48.0 * C) * f +
  44. 8.0 * B + 24.0 * C) / 6.0;
  45. }else{
  46. return 0.0;
  47. }
  48. }
  49. #elif defined(dInterpolation_bspline)
  50. float cubicFilter(float x) {
  51. float f = x;
  52. if (f < 0.0) {
  53. f = -f;
  54. }
  55. if (f >= 0.0 && f <= 1.0){
  56. return (2.0 / 3.0) + (0.5) * (f * f * f) - (f * f);
  57. } else if (f > 1.0 && f <= 2.0) {
  58. return 1.0 / 6.0 * pow((2.0 - f), 3.0);
  59. }
  60. return 1.0;
  61. }
  62. #endif
  63. vec4 biCubic(sampler2D tex, vec2 texCoord) {
  64. vec2 texelSize = 1.0 / uImageTexDim;
  65. texCoord -= texelSize / 2.0;
  66. vec4 nSum = vec4(0.0);
  67. float nDenom = 0.0;
  68. vec2 cell = fract(texCoord * uImageTexDim);
  69. for (float m = -1.0; m <= 2.0; ++m) {
  70. for (float n = -1.0; n <= 2.0; ++n) {
  71. vec4 vecData = texture2D(tex, texCoord + texelSize * vec2(m, n));
  72. float c = cubicFilter(m - cell.x) * cubicFilter(-n + cell.y);
  73. nSum += vecData * c;
  74. nDenom += c;
  75. }
  76. }
  77. return nSum / nDenom;
  78. }
  79. #endif
  80. void main() {
  81. #if defined(dInterpolation_cubic)
  82. vec4 imageData = biCubic(tImageTex, vUv);
  83. #else
  84. vec4 imageData = texture2D(tImageTex, vUv);
  85. #endif
  86. imageData.a = clamp(imageData.a, 0.0, 1.0);
  87. if (imageData.a > 0.9) imageData.a = 1.0;
  88. #if defined(dRenderVariant_pick)
  89. if (imageData.a < 0.3)
  90. discard;
  91. #if defined(dRenderVariant_pickObject)
  92. gl_FragColor = vec4(encodeFloatRGB(float(uObjectId)), 1.0);
  93. #elif defined(dRenderVariant_pickInstance)
  94. gl_FragColor = vec4(encodeFloatRGB(vInstance), 1.0);
  95. #elif defined(dRenderVariant_pickGroup)
  96. float group = texture2D(tGroupTex, vUv).a;
  97. gl_FragColor = vec4(encodeFloatRGB(group), 1.0);
  98. #endif
  99. #elif defined(dRenderVariant_depth)
  100. if (imageData.a < 0.05)
  101. discard;
  102. gl_FragColor = packDepthToRGBA(gl_FragCoord.z);
  103. #elif defined(dRenderVariant_color)
  104. if (imageData.a < 0.05)
  105. discard;
  106. gl_FragColor = imageData;
  107. gl_FragColor.a *= uAlpha;
  108. float group = texture2D(tGroupTex, vUv).a;
  109. float vMarker = readFromTexture(tMarker, vInstance * float(uGroupCount) + group, uMarkerTexDim).a;
  110. #include apply_marker_color
  111. #include apply_fog
  112. #endif
  113. }
  114. `;