edl.fs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #extension GL_EXT_frag_depth : enable
  2. //
  3. // adapted from the EDL shader code from Christian Boucheny in cloud compare:
  4. // https://github.com/cloudcompare/trunk/tree/master/plugins/qEDL/shaders/EDL
  5. //
  6. precision mediump float;
  7. precision mediump int;
  8. uniform float screenWidth;
  9. uniform float screenHeight;
  10. uniform vec2 neighbours[NEIGHBOUR_COUNT];
  11. uniform float edlStrength;
  12. uniform float radius;
  13. uniform float opacity;
  14. uniform float uNear;
  15. uniform float uFar;
  16. uniform mat4 uProj;
  17. uniform sampler2D uEDLColor;
  18. uniform sampler2D uEDLDepth;
  19. varying vec2 vUv;
  20. float response(float depth){
  21. vec2 uvRadius = radius / vec2(screenWidth, screenHeight);
  22. float sum = 0.0;
  23. for(int i = 0; i < NEIGHBOUR_COUNT; i++){
  24. vec2 uvNeighbor = vUv + uvRadius * neighbours[i];
  25. float neighbourDepth = texture2D(uEDLColor, uvNeighbor).a;
  26. neighbourDepth = (neighbourDepth == 1.0) ? 0.0 : neighbourDepth;
  27. if(neighbourDepth != 0.0){
  28. if(depth == 0.0){
  29. sum += 100.0;
  30. }else{
  31. sum += max(0.0, depth - neighbourDepth);
  32. }
  33. }
  34. }
  35. return sum / float(NEIGHBOUR_COUNT);
  36. }
  37. void main(){
  38. vec4 cEDL = texture2D(uEDLColor, vUv);
  39. float depth = cEDL.a;
  40. depth = (depth == 1.0) ? 0.0 : depth;
  41. float res = response(depth);
  42. float shade = exp(-res * 300.0 * edlStrength);
  43. gl_FragColor = vec4(cEDL.rgb * shade, opacity);
  44. { // write regular hyperbolic depth values to depth buffer
  45. float dl = pow(2.0, depth);
  46. vec4 dp = uProj * vec4(0.0, 0.0, -dl, 1.0);
  47. float pz = dp.z / dp.w;
  48. float fragDepth = (pz + 1.0) / 2.0;
  49. gl_FragDepthEXT = fragDepth;
  50. }
  51. if(depth == 0.0){
  52. discard;
  53. }
  54. }