| 1 |
#ifdef ALPHA_HASH_USED |
| 2 |
|
| 3 |
float hash_2d(vec2 p) { |
| 4 |
return fract(1.0e4 * sin(17.0 * p.x + 0.1 * p.y) * |
| 5 |
(0.1 + abs(sin(13.0 * p.y + p.x)))); |
| 6 |
} |
| 7 |
|
| 8 |
float hash_3d(vec3 p) { |
| 9 |
return hash_2d(vec2(hash_2d(p.xy), p.z)); |
| 10 |
} |
| 11 |
|
| 12 |
half compute_alpha_hash_threshold(vec3 pos, float hash_scale) { |
| 13 |
vec3 dx = dFdx(pos); |
| 14 |
vec3 dy = dFdy(pos); |
| 15 |
|
| 16 |
float delta_max_sqr = max(length(dx), length(dy)); |
| 17 |
float pix_scale = 1.0 / (hash_scale * delta_max_sqr); |
| 18 |
|
| 19 |
vec2 pix_scales = |
| 20 |
vec2(exp2(floor(log2(pix_scale))), exp2(ceil(log2(pix_scale)))); |
| 21 |
|
| 22 |
vec2 a_thresh = vec2(hash_3d(floor(pix_scales.x * pos.xyz)), |
| 23 |
hash_3d(floor(pix_scales.y * pos.xyz))); |
| 24 |
|
| 25 |
float lerp_factor = fract(log2(pix_scale)); |
| 26 |
|
| 27 |
float a_interp = (1.0 - lerp_factor) * a_thresh.x + lerp_factor * a_thresh.y; |
| 28 |
|
| 29 |
float min_lerp = min(lerp_factor, 1.0 - lerp_factor); |
| 30 |
|
| 31 |
vec3 cases = vec3(a_interp * a_interp / (2.0 * min_lerp * (1.0 - min_lerp)), |
| 32 |
(a_interp - 0.5 * min_lerp) / (1.0 - min_lerp), |
| 33 |
1.0 - ((1.0 - a_interp) * (1.0 - a_interp) / (2.0 * min_lerp * (1.0 - min_lerp)))); |
| 34 |
|
| 35 |
float alpha_hash_threshold = |
| 36 |
(a_interp < (1.0 - min_lerp)) ? ((a_interp < min_lerp) ? cases.x : cases.y) : cases.z; |
| 37 |
|
| 38 |
return half(clamp(alpha_hash_threshold, 0.00001, 1.0)); |
| 39 |
} |
| 40 |
|
| 41 |
#endif // ALPHA_HASH_USED |
| 42 |
|
| 43 |
#ifdef ALPHA_ANTIALIASING_EDGE_USED |
| 44 |
|
| 45 |
half calc_mip_level(vec2 texture_coord) { |
| 46 |
vec2 dx = dFdx(texture_coord); |
| 47 |
vec2 dy = dFdy(texture_coord); |
| 48 |
float delta_max_sqr = max(dot(dx, dx), dot(dy, dy)); |
| 49 |
return max(half(0.0), half(0.5) * half(log2(delta_max_sqr))); |
| 50 |
} |
| 51 |
|
| 52 |
half compute_alpha_antialiasing_edge(half input_alpha, vec2 texture_coord, half alpha_edge) { |
| 53 |
input_alpha *= half(1.0) + calc_mip_level(texture_coord) * half(0.25); |
| 54 |
input_alpha = (input_alpha - alpha_edge) / max(fwidth(input_alpha), half(0.0001)) + half(0.5); |
| 55 |
return clamp(input_alpha, half(0.0), half(1.0)); |
| 56 |
} |
| 57 |
|
| 58 |
#endif // ALPHA_ANTIALIASING_USED |