edl_new.fs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 resolution;
  11. uniform vec2 neighbours[NEIGHBOUR_COUNT];
  12. uniform float edlStrength;
  13. uniform float radius;
  14. uniform float opacity;
  15. //uniform float uNear;
  16. //uniform float uFar;
  17. uniform mat4 uProj;
  18. uniform sampler2D uEDLColor;
  19. uniform sampler2D uEDLDepth;
  20. varying vec2 vUv;
  21. uniform int useEDL;
  22. float response(float depth){
  23. vec2 uvRadius = radius / resolution; //vec2(screenWidth, screenHeight);
  24. float sum = 0.0;
  25. for(int i = 0; i < NEIGHBOUR_COUNT; i++){
  26. vec2 uvNeighbor = vUv + uvRadius * neighbours[i];
  27. //获取周围八个格子的值
  28. float neighbourDepth = texture2D(uEDLColor, uvNeighbor).a;
  29. neighbourDepth = (neighbourDepth == 1.0) ? 0.0 : neighbourDepth;
  30. if(neighbourDepth != 0.0){
  31. //if(depth == 0.0){
  32. // sum += 100.0;
  33. //}else{
  34. sum += max(0.0, depth - neighbourDepth); //获取差值
  35. //}
  36. }
  37. }
  38. return sum / float(NEIGHBOUR_COUNT);
  39. }
  40. void main(){
  41. vec4 cEDL = texture2D(uEDLColor, vUv);
  42. float depth = cEDL.a;
  43. depth = (depth == 1.0) ? 0.0 : depth;
  44. if(depth == 0.0){ //去掉这句就能在无点云像素的地方渲染outline,但会遮住其他mesh
  45. discard;
  46. }
  47. if(useEDL == 1){
  48. float res = response(depth);
  49. float shade = exp(-res * 300.0 * edlStrength); //自然常数e为底的指数函数
  50. const float min = 0.2; //add 使边缘色变浅 范围从0-1 变为min-1
  51. shade = shade * (1.0-min) + min;
  52. gl_FragColor = vec4(cEDL.rgb * shade, opacity);
  53. }else{//加 不改颜色的情况
  54. gl_FragColor = vec4(cEDL.rgb, opacity);
  55. }
  56. //注意,edlStrength越强,在相同depth的叠放的点云间的轮廓线颜色越深
  57. //作用:勾勒边缘,强化深度差异,模拟阴影,加强结构立体感
  58. { // write regular hyperbolic depth values to depth buffer 修改深度
  59. float dl = pow(2.0, depth);
  60. vec4 dp = uProj * vec4(0.0, 0.0, -dl, 1.0);
  61. float pz = dp.z / dp.w;
  62. float fragDepth = (pz + 1.0) / 2.0;
  63. gl_FragDepthEXT = fragDepth;
  64. }
  65. }