PointCloudEyeDomeLighting.glsl 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #extension GL_EXT_frag_depth : enable
  2. uniform sampler2D u_pointCloud_colorGBuffer;
  3. uniform sampler2D u_pointCloud_depthGBuffer;
  4. uniform vec2 u_distanceAndEdlStrength;
  5. varying vec2 v_textureCoordinates;
  6. vec2 neighborContribution(float log2Depth, vec2 offset)
  7. {
  8. float dist = u_distanceAndEdlStrength.x;
  9. vec2 texCoordOrig = v_textureCoordinates + offset * dist;
  10. vec2 texCoord0 = v_textureCoordinates + offset * floor(dist);
  11. vec2 texCoord1 = v_textureCoordinates + offset * ceil(dist);
  12. float depthOrLogDepth0 = czm_unpackDepth(texture2D(u_pointCloud_depthGBuffer, texCoord0));
  13. float depthOrLogDepth1 = czm_unpackDepth(texture2D(u_pointCloud_depthGBuffer, texCoord1));
  14. // ignore depth values that are the clear depth
  15. if (depthOrLogDepth0 == 0.0 || depthOrLogDepth1 == 0.0) {
  16. return vec2(0.0);
  17. }
  18. // interpolate the two adjacent depth values
  19. float depthMix = mix(depthOrLogDepth0, depthOrLogDepth1, fract(dist));
  20. vec4 eyeCoordinate = czm_windowToEyeCoordinates(texCoordOrig, depthMix);
  21. return vec2(max(0.0, log2Depth - log2(-eyeCoordinate.z / eyeCoordinate.w)), 1.0);
  22. }
  23. void main()
  24. {
  25. float depthOrLogDepth = czm_unpackDepth(texture2D(u_pointCloud_depthGBuffer, v_textureCoordinates));
  26. vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, depthOrLogDepth);
  27. eyeCoordinate /= eyeCoordinate.w;
  28. float log2Depth = log2(-eyeCoordinate.z);
  29. if (depthOrLogDepth == 0.0) // 0.0 is the clear value for the gbuffer
  30. {
  31. discard;
  32. }
  33. vec4 color = texture2D(u_pointCloud_colorGBuffer, v_textureCoordinates);
  34. // sample from neighbors left, right, down, up
  35. vec2 texelSize = 1.0 / czm_viewport.zw;
  36. vec2 responseAndCount = vec2(0.0);
  37. responseAndCount += neighborContribution(log2Depth, vec2(-texelSize.x, 0.0));
  38. responseAndCount += neighborContribution(log2Depth, vec2(+texelSize.x, 0.0));
  39. responseAndCount += neighborContribution(log2Depth, vec2(0.0, -texelSize.y));
  40. responseAndCount += neighborContribution(log2Depth, vec2(0.0, +texelSize.y));
  41. float response = responseAndCount.x / responseAndCount.y;
  42. float strength = u_distanceAndEdlStrength.y;
  43. float shade = exp(-response * 300.0 * strength);
  44. color.rgb *= shade;
  45. gl_FragColor = vec4(color);
  46. #ifdef LOG_DEPTH
  47. czm_writeLogDepth(1.0 + (czm_projection * vec4(eyeCoordinate.xyz, 1.0)).w);
  48. #else
  49. gl_FragDepthEXT = czm_eyeToWindowCoordinates(vec4(eyeCoordinate.xyz, 1.0)).z;
  50. #endif
  51. }