canvas_occlusion.glsl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. layout(location = 0) in highp vec3 vertex;
  5. #ifdef POSITIONAL_SHADOW
  6. layout(push_constant, std430) uniform Constants {
  7. mat2x4 modelview;
  8. vec4 rotation;
  9. vec2 direction;
  10. float z_far;
  11. uint pad;
  12. float z_near;
  13. uint cull_mode;
  14. float pad3;
  15. float pad4;
  16. }
  17. constants;
  18. layout(set = 0, binding = 0, std430) restrict readonly buffer OccluderTransforms {
  19. mat2x4 transforms[];
  20. }
  21. occluder_transforms;
  22. #else
  23. layout(push_constant, std430) uniform Constants {
  24. mat4 projection;
  25. mat2x4 modelview;
  26. vec2 direction;
  27. float z_far;
  28. uint cull_mode;
  29. }
  30. constants;
  31. #endif
  32. #ifdef MODE_SHADOW
  33. layout(location = 0) out highp float depth;
  34. #endif
  35. void main() {
  36. #ifdef POSITIONAL_SHADOW
  37. float c = -(constants.z_far + constants.z_near) / (constants.z_far - constants.z_near);
  38. float d = -2.0 * constants.z_far * constants.z_near / (constants.z_far - constants.z_near);
  39. mat4 projection = mat4(vec4(1.0, 0.0, 0.0, 0.0),
  40. vec4(0.0, 1.0, 0.0, 0.0),
  41. vec4(0.0, 0.0, c, -1.0),
  42. vec4(0.0, 0.0, d, 0.0));
  43. // Precomputed:
  44. // Vector3 cam_target = Basis::from_euler(Vector3(0, 0, Math_TAU * ((i + 3) / 4.0))).xform(Vector3(0, 1, 0));
  45. // projection = projection * Projection(Transform3D().looking_at(cam_targets[i], Vector3(0, 0, -1)).affine_inverse());
  46. projection *= mat4(vec4(constants.rotation.x, 0.0, constants.rotation.y, 0.0), vec4(constants.rotation.z, 0.0, constants.rotation.w, 0.0), vec4(0.0, -1.0, 0.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
  47. mat4 modelview = mat4(occluder_transforms.transforms[constants.pad]) * mat4(constants.modelview);
  48. #else
  49. mat4 projection = constants.projection;
  50. mat4 modelview = mat4(constants.modelview[0], constants.modelview[1], vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
  51. #endif
  52. highp vec4 vtx = vec4(vertex, 1.0) * modelview;
  53. #ifdef MODE_SHADOW
  54. depth = dot(constants.direction, vtx.xy);
  55. #endif
  56. gl_Position = projection * vtx;
  57. }
  58. #[fragment]
  59. #version 450
  60. #VERSION_DEFINES
  61. #ifdef POSITIONAL_SHADOW
  62. layout(push_constant, std430) uniform Constants {
  63. mat2x4 modelview;
  64. vec4 rotation;
  65. vec2 direction;
  66. float z_far;
  67. uint pad;
  68. float z_near;
  69. uint cull_mode;
  70. float pad3;
  71. float pad4;
  72. }
  73. constants;
  74. #else
  75. layout(push_constant, std430) uniform Constants {
  76. mat4 projection;
  77. mat2x4 modelview;
  78. vec2 direction;
  79. float z_far;
  80. uint cull_mode;
  81. }
  82. constants;
  83. #endif
  84. #ifdef MODE_SHADOW
  85. layout(location = 0) in highp float depth;
  86. layout(location = 0) out highp float distance_buf;
  87. #else
  88. layout(location = 0) out highp float sdf_buf;
  89. #endif
  90. #define POLYGON_CULL_DISABLED 0
  91. #define POLYGON_CULL_FRONT 1
  92. #define POLYGON_CULL_BACK 2
  93. void main() {
  94. #ifdef MODE_SHADOW
  95. bool front_facing = gl_FrontFacing;
  96. if (constants.cull_mode == POLYGON_CULL_BACK && !front_facing) {
  97. discard;
  98. } else if (constants.cull_mode == POLYGON_CULL_FRONT && front_facing) {
  99. discard;
  100. }
  101. distance_buf = depth / constants.z_far;
  102. #else
  103. sdf_buf = 1.0;
  104. #endif
  105. }