canvas_occlusion.glsl 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /* clang-format off */
  2. #[modes]
  3. mode_sdf =
  4. mode_shadow = #define MODE_SHADOW
  5. mode_shadow_RGBA = #define MODE_SHADOW \n#define USE_RGBA_SHADOWS
  6. #[specializations]
  7. #[vertex]
  8. layout(location = 0) in vec3 vertex;
  9. uniform highp mat4 projection;
  10. uniform highp vec4 modelview1;
  11. uniform highp vec4 modelview2;
  12. uniform highp vec2 direction;
  13. uniform highp float z_far;
  14. #ifdef MODE_SHADOW
  15. out float depth;
  16. #endif
  17. void main() {
  18. highp vec4 vtx = vec4(vertex, 1.0) * mat4(modelview1, modelview2, vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
  19. #ifdef MODE_SHADOW
  20. depth = dot(direction, vtx.xy);
  21. #endif
  22. gl_Position = projection * vtx;
  23. }
  24. #[fragment]
  25. uniform highp mat4 projection;
  26. uniform highp vec4 modelview1;
  27. uniform highp vec4 modelview2;
  28. uniform highp vec2 direction;
  29. uniform highp float z_far;
  30. #ifdef MODE_SHADOW
  31. in highp float depth;
  32. #endif
  33. #ifdef USE_RGBA_SHADOWS
  34. layout(location = 0) out lowp vec4 out_buf;
  35. #else
  36. layout(location = 0) out highp float out_buf;
  37. #endif
  38. void main() {
  39. float out_depth = 1.0;
  40. #ifdef MODE_SHADOW
  41. out_depth = depth / z_far;
  42. #endif
  43. #ifdef USE_RGBA_SHADOWS
  44. out_depth = clamp(out_depth, -1.0, 1.0);
  45. out_depth = out_depth * 0.5 + 0.5;
  46. highp vec4 comp = fract(out_depth * vec4(255.0 * 255.0 * 255.0, 255.0 * 255.0, 255.0, 1.0));
  47. comp -= comp.xxyz * vec4(0.0, 1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0);
  48. out_buf = comp;
  49. #else
  50. out_buf = out_depth;
  51. #endif
  52. }