lm_raster.glsl 4.6 KB

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