lm_raster.glsl 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #[vertex]
  2. #version 450
  3. #VERSION_DEFINES
  4. #include "lm_common_inc.glsl"
  5. layout(location = 0) out vec3 vertex_interp;
  6. layout(location = 1) out vec3 normal_interp;
  7. layout(location = 2) out vec2 uv_interp;
  8. layout(location = 3) out vec3 barycentric;
  9. layout(location = 4) flat out uvec3 vertex_indices;
  10. layout(location = 5) flat out vec3 face_normal;
  11. layout(location = 6) flat out uint fragment_action;
  12. layout(push_constant, std430) uniform Params {
  13. vec2 atlas_size;
  14. vec2 uv_offset;
  15. vec3 to_cell_size;
  16. uint base_triangle;
  17. vec3 to_cell_offset;
  18. float bias;
  19. ivec3 grid_size;
  20. uint pad2;
  21. }
  22. params;
  23. void main() {
  24. uint triangle_idx = params.base_triangle + gl_VertexIndex / 3;
  25. uint triangle_subidx = gl_VertexIndex % 3;
  26. vertex_indices = triangles.data[triangle_idx].indices;
  27. uint vertex_idx;
  28. if (triangle_subidx == 0) {
  29. vertex_idx = vertex_indices.x;
  30. barycentric = vec3(1, 0, 0);
  31. } else if (triangle_subidx == 1) {
  32. vertex_idx = vertex_indices.y;
  33. barycentric = vec3(0, 1, 0);
  34. } else {
  35. vertex_idx = vertex_indices.z;
  36. barycentric = vec3(0, 0, 1);
  37. }
  38. vertex_interp = vertices.data[vertex_idx].position;
  39. uv_interp = vertices.data[vertex_idx].uv;
  40. normal_interp = vec3(vertices.data[vertex_idx].normal_xy, vertices.data[vertex_idx].normal_z);
  41. face_normal = -normalize(cross((vertices.data[vertex_indices.x].position - vertices.data[vertex_indices.y].position), (vertices.data[vertex_indices.x].position - vertices.data[vertex_indices.z].position)));
  42. {
  43. const float FLAT_THRESHOLD = 0.99;
  44. const vec3 norm_a = vec3(vertices.data[vertex_indices.x].normal_xy, vertices.data[vertex_indices.x].normal_z);
  45. const vec3 norm_b = vec3(vertices.data[vertex_indices.y].normal_xy, vertices.data[vertex_indices.y].normal_z);
  46. const vec3 norm_c = vec3(vertices.data[vertex_indices.z].normal_xy, vertices.data[vertex_indices.z].normal_z);
  47. fragment_action = (dot(norm_a, norm_b) < FLAT_THRESHOLD || dot(norm_a, norm_c) < FLAT_THRESHOLD || dot(norm_b, norm_c) < FLAT_THRESHOLD) ? FA_SMOOTHEN_POSITION : FA_NONE;
  48. }
  49. gl_Position = vec4((uv_interp + params.uv_offset) * 2.0 - 1.0, 0.0001, 1.0);
  50. }
  51. #[fragment]
  52. #version 450
  53. #VERSION_DEFINES
  54. #include "lm_common_inc.glsl"
  55. layout(push_constant, std430) uniform Params {
  56. vec2 atlas_size;
  57. vec2 uv_offset;
  58. vec3 to_cell_size;
  59. uint base_triangle;
  60. vec3 to_cell_offset;
  61. float bias;
  62. ivec3 grid_size;
  63. uint pad2;
  64. }
  65. params;
  66. layout(location = 0) in vec3 vertex_interp;
  67. layout(location = 1) in vec3 normal_interp;
  68. layout(location = 2) in vec2 uv_interp;
  69. layout(location = 3) in vec3 barycentric;
  70. layout(location = 4) in flat uvec3 vertex_indices;
  71. layout(location = 5) in flat vec3 face_normal;
  72. layout(location = 6) in flat uint fragment_action;
  73. layout(location = 0) out vec4 position;
  74. layout(location = 1) out vec4 normal;
  75. layout(location = 2) out vec4 unocclude;
  76. void main() {
  77. vec3 vertex_pos = vertex_interp;
  78. if (fragment_action == FA_SMOOTHEN_POSITION) {
  79. // smooth out vertex position by interpolating its projection in the 3 normal planes (normal plane is created by vertex pos and normal)
  80. // because we don't want to interpolate inwards, normals found pointing inwards are pushed out.
  81. vec3 pos_a = vertices.data[vertex_indices.x].position;
  82. vec3 pos_b = vertices.data[vertex_indices.y].position;
  83. vec3 pos_c = vertices.data[vertex_indices.z].position;
  84. vec3 center = (pos_a + pos_b + pos_c) * 0.3333333;
  85. vec3 norm_a = vec3(vertices.data[vertex_indices.x].normal_xy, vertices.data[vertex_indices.x].normal_z);
  86. vec3 norm_b = vec3(vertices.data[vertex_indices.y].normal_xy, vertices.data[vertex_indices.y].normal_z);
  87. vec3 norm_c = vec3(vertices.data[vertex_indices.z].normal_xy, vertices.data[vertex_indices.z].normal_z);
  88. {
  89. vec3 dir_a = normalize(pos_a - center);
  90. float d_a = dot(dir_a, norm_a);
  91. if (d_a < 0) {
  92. //pointing inwards
  93. norm_a = normalize(norm_a - dir_a * d_a);
  94. }
  95. }
  96. {
  97. vec3 dir_b = normalize(pos_b - center);
  98. float d_b = dot(dir_b, norm_b);
  99. if (d_b < 0) {
  100. //pointing inwards
  101. norm_b = normalize(norm_b - dir_b * d_b);
  102. }
  103. }
  104. {
  105. vec3 dir_c = normalize(pos_c - center);
  106. float d_c = dot(dir_c, norm_c);
  107. if (d_c < 0) {
  108. //pointing inwards
  109. norm_c = normalize(norm_c - dir_c * d_c);
  110. }
  111. }
  112. float d_a = dot(norm_a, pos_a);
  113. float d_b = dot(norm_b, pos_b);
  114. float d_c = dot(norm_c, pos_c);
  115. vec3 proj_a = vertex_pos - norm_a * (dot(norm_a, vertex_pos) - d_a);
  116. vec3 proj_b = vertex_pos - norm_b * (dot(norm_b, vertex_pos) - d_b);
  117. vec3 proj_c = vertex_pos - norm_c * (dot(norm_c, vertex_pos) - d_c);
  118. vec3 smooth_position = proj_a * barycentric.x + proj_b * barycentric.y + proj_c * barycentric.z;
  119. if (dot(face_normal, smooth_position) > dot(face_normal, vertex_pos)) { //only project outwards
  120. vertex_pos = smooth_position;
  121. }
  122. }
  123. {
  124. // unocclusion technique based on:
  125. // https://ndotl.wordpress.com/2018/08/29/baking-artifact-free-lightmaps/
  126. /* compute texel size */
  127. vec3 delta_uv = max(abs(dFdx(vertex_interp)), abs(dFdy(vertex_interp)));
  128. float texel_size = max(delta_uv.x, max(delta_uv.y, delta_uv.z));
  129. texel_size *= sqrt(2.0); //expand to unit box edge length (again, worst case)
  130. unocclude.xyz = face_normal;
  131. unocclude.w = texel_size;
  132. //continued on lm_compute.glsl
  133. }
  134. position = vec4(vertex_pos, 1.0);
  135. normal = vec4(normalize(normal_interp), 1.0);
  136. }