image.frag.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * Copyright (c) 2020-2022 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Alexander Rose <alexander.rose@weirdbyte.de>
  5. */
  6. export const image_frag = `
  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. uniform vec2 uImageTexDim;
  14. uniform sampler2D tImageTex;
  15. uniform sampler2D tGroupTex;
  16. uniform vec2 uMarkerTexDim;
  17. uniform sampler2D tMarker;
  18. varying vec2 vUv;
  19. varying float vInstance;
  20. #if defined(dInterpolation_catmulrom) || defined(dInterpolation_mitchell) || defined(dInterpolation_bspline)
  21. #define dInterpolation_cubic
  22. #endif
  23. #if defined(dInterpolation_cubic)
  24. #if defined(dInterpolation_catmulrom) || defined(dInterpolation_mitchell)
  25. #if defined(dInterpolation_catmulrom)
  26. const float B = 0.0;
  27. const float C = 0.5;
  28. #elif defined(dInterpolation_mitchell)
  29. const float B = 0.333;
  30. const float C = 0.333;
  31. #endif
  32. float cubicFilter(float x){
  33. float f = x;
  34. if (f < 0.0) {
  35. f = -f;
  36. }
  37. if (f < 1.0) {
  38. return ((12.0 - 9.0 * B - 6.0 * C) * (f * f * f) +
  39. (-18.0 + 12.0 * B + 6.0 * C) * (f * f) +
  40. (6.0 - 2.0 * B)) / 6.0;
  41. }else if (f >= 1.0 && f < 2.0){
  42. return ((-B - 6.0 * C) * ( f * f * f)
  43. + (6.0 * B + 30.0 * C) * (f * f) +
  44. (-(12.0 * B) - 48.0 * C) * f +
  45. 8.0 * B + 24.0 * C) / 6.0;
  46. }else{
  47. return 0.0;
  48. }
  49. }
  50. #elif defined(dInterpolation_bspline)
  51. float cubicFilter(float x) {
  52. float f = x;
  53. if (f < 0.0) {
  54. f = -f;
  55. }
  56. if (f >= 0.0 && f <= 1.0){
  57. return (2.0 / 3.0) + (0.5) * (f * f * f) - (f * f);
  58. } else if (f > 1.0 && f <= 2.0) {
  59. return 1.0 / 6.0 * pow((2.0 - f), 3.0);
  60. }
  61. return 1.0;
  62. }
  63. #endif
  64. vec4 biCubic(sampler2D tex, vec2 texCoord) {
  65. vec2 texelSize = 1.0 / uImageTexDim;
  66. texCoord -= texelSize / 2.0;
  67. vec4 nSum = vec4(0.0);
  68. float nDenom = 0.0;
  69. vec2 cell = fract(texCoord * uImageTexDim);
  70. for (float m = -1.0; m <= 2.0; ++m) {
  71. for (float n = -1.0; n <= 2.0; ++n) {
  72. vec4 vecData = texture2D(tex, texCoord + texelSize * vec2(m, n));
  73. float c = cubicFilter(m - cell.x) * cubicFilter(-n + cell.y);
  74. nSum += vecData * c;
  75. nDenom += c;
  76. }
  77. }
  78. return nSum / nDenom;
  79. }
  80. #endif
  81. void main() {
  82. #include clip_pixel
  83. #if defined(dInterpolation_cubic)
  84. vec4 imageData = biCubic(tImageTex, vUv);
  85. #else
  86. vec4 imageData = texture2D(tImageTex, vUv);
  87. #endif
  88. imageData.a = clamp(imageData.a, 0.0, 1.0);
  89. if (imageData.a > 0.9) imageData.a = 1.0;
  90. float fragmentDepth = gl_FragCoord.z;
  91. #if defined(dRenderVariant_pick)
  92. if (imageData.a < 0.3)
  93. discard;
  94. #ifdef requiredDrawBuffers
  95. gl_FragColor = vec4(packIntToRGB(float(uObjectId)), 1.0);
  96. gl_FragData[1] = vec4(packIntToRGB(vInstance), 1.0);
  97. gl_FragData[2] = vec4(texture2D(tGroupTex, vUv).rgb, 1.0);
  98. gl_FragData[3] = packDepthToRGBA(gl_FragCoord.z);
  99. #else
  100. gl_FragColor = vColor;
  101. if (uPickType == 1) {
  102. gl_FragColor = vec4(packIntToRGB(float(uObjectId)), 1.0);
  103. } else if (uPickType == 2) {
  104. gl_FragColor = vec4(packIntToRGB(vInstance), 1.0);
  105. } else {
  106. gl_FragColor = vec4(texture2D(tGroupTex, vUv).rgb, 1.0);
  107. }
  108. #endif
  109. #elif defined(dRenderVariant_depth)
  110. if (imageData.a < 0.05)
  111. discard;
  112. gl_FragColor = packDepthToRGBA(gl_FragCoord.z);
  113. #elif defined(dRenderVariant_marking)
  114. float marker = uMarker;
  115. if (uMarker == -1.0) {
  116. float group = unpackRGBToInt(texture2D(tGroupTex, vUv).rgb);
  117. marker = readFromTexture(tMarker, vInstance * float(uGroupCount) + group, uMarkerTexDim).a;
  118. marker = floor(marker * 255.0 + 0.5); // rounding required to work on some cards on win
  119. }
  120. if (uMarkingType == 1) {
  121. if (marker > 0.0 || imageData.a < 0.05)
  122. discard;
  123. gl_FragColor = packDepthToRGBA(gl_FragCoord.z);
  124. } else {
  125. if (marker == 0.0 || imageData.a < 0.05)
  126. discard;
  127. float depthTest = 1.0;
  128. if (uMarkingDepthTest) {
  129. depthTest = (fragmentDepth >= getDepthPacked(gl_FragCoord.xy / uDrawingBufferSize)) ? 1.0 : 0.0;
  130. }
  131. bool isHighlight = intMod(marker, 2.0) > 0.1;
  132. gl_FragColor = vec4(0.0, depthTest, isHighlight ? 1.0 : 0.0, 1.0);
  133. }
  134. #elif defined(dRenderVariant_color)
  135. if (imageData.a < 0.05)
  136. discard;
  137. gl_FragColor = imageData;
  138. gl_FragColor.a *= uAlpha;
  139. float marker = uMarker;
  140. if (uMarker == -1.0) {
  141. float group = unpackRGBToInt(texture2D(tGroupTex, vUv).rgb);
  142. marker = readFromTexture(tMarker, vInstance * float(uGroupCount) + group, uMarkerTexDim).a;
  143. marker = floor(marker * 255.0 + 0.5); // rounding required to work on some cards on win
  144. }
  145. #include apply_marker_color
  146. #include apply_fog
  147. #include wboit_write
  148. #endif
  149. }
  150. `;