outlines.frag.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Copyright (c) 2019-2020 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Áron Samuel Kovács <aron.kovacs@mail.muni.cz>
  5. */
  6. export const outlines_frag = `
  7. precision highp float;
  8. precision highp int;
  9. precision highp sampler2D;
  10. uniform sampler2D tDepth;
  11. uniform vec2 uTexSize;
  12. uniform float uNear;
  13. uniform float uFar;
  14. uniform float uMaxPossibleViewZDiff;
  15. #include common
  16. float getViewZ(const in float depth) {
  17. #if dOrthographic == 1
  18. return orthographicDepthToViewZ(depth, uNear, uFar);
  19. #else
  20. return perspectiveDepthToViewZ(depth, uNear, uFar);
  21. #endif
  22. }
  23. float getDepth(const in vec2 coords) {
  24. return unpackRGBAToDepth(texture2D(tDepth, coords));
  25. }
  26. bool isBackground(const in float depth) {
  27. return depth == 1.0;
  28. }
  29. void main(void) {
  30. float backgroundViewZ = uFar + 3.0 * uMaxPossibleViewZDiff;
  31. vec2 coords = gl_FragCoord.xy / uTexSize;
  32. vec2 invTexSize = 1.0 / uTexSize;
  33. float selfDepth = getDepth(coords);
  34. float selfViewZ = isBackground(selfDepth) ? backgroundViewZ : getViewZ(getDepth(coords));
  35. float outline = 1.0;
  36. float bestDepth = 1.0;
  37. for (int y = -1; y <= 1; y++) {
  38. for (int x = -1; x <= 1; x++) {
  39. vec2 sampleCoords = coords + vec2(float(x), float(y)) * invTexSize;
  40. float sampleDepth = getDepth(sampleCoords);
  41. float sampleViewZ = isBackground(sampleDepth) ? backgroundViewZ : getViewZ(sampleDepth);
  42. if (abs(selfViewZ - sampleViewZ) > uMaxPossibleViewZDiff && selfDepth > sampleDepth && sampleDepth <= bestDepth) {
  43. outline = 0.0;
  44. bestDepth = sampleDepth;
  45. }
  46. }
  47. }
  48. gl_FragColor = vec4(outline, packUnitIntervalToRG(bestDepth), 0.0);
  49. }
  50. `;