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