85 vec2 dx = dFdx(texCoords);
86 vec2 dy = dFdy(texCoords);
87 const float minLayers = 2.0;
88 const float maxLayers = 16.0;
90 float tx_width = float(textureSize(normal_map, 0).x);
92 vec2 dx2 = max(dx * dx * tx_width * tx_width, dy * dy * tx_width * tx_width);
93 float mip_level = 0.5 * log2(max(dx2.
x, dx2.
y));
95 float fade =
clamp(1.0 - (mip_level / 3.0), 0.0, 1.0);
98 float current_max_layers = mix(minLayers, maxLayers, fade);
101 float numLayers = mix(current_max_layers, minLayers, abs(dot(
vec3(0.0, 0.0, 1.0), viewDir)));
103 float layerDepth = 1.0 / numLayers;
104 float currentLayerDepth = 0.0;
108 float uv_scale = length(fwidth(texCoords));
111 vec2 deltaTexCoords =
vec2(P.
x, -P.
y) / numLayers;
113 vec2 currentTexCoords = texCoords;
115 float currentDepthMapValue = textureGrad(normal_map, currentTexCoords, dx, dy).a;
119 while(currentLayerDepth < currentDepthMapValue && safety > cur)
121 currentTexCoords -= deltaTexCoords;
124 currentDepthMapValue = textureGrad(normal_map, currentTexCoords, dx, dy).a;
125 currentLayerDepth += layerDepth;
129 vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
130 float afterDepth = currentLayerDepth - currentDepthMapValue;
131 float current_depth = textureGrad(normal_map, currentTexCoords, dx, dy).a;
132 float prev_depth = textureGrad(normal_map, prevTexCoords, dx, dy).a;
133 float beforeDepth = (prev_depth) - currentLayerDepth + layerDepth;
135 float weight = afterDepth / (afterDepth + beforeDepth);
136 vec2 finalTexCoords = mix(currentTexCoords, prevTexCoords, weight);
137 final_depth = mix(current_depth, prev_depth, weight);
139 return finalTexCoords;
148 float final_depth = 0.0;
158 float ndc_depth = clip_space_pos.
z / clip_space_pos.
w;
160 float ndc_screen_depth = ndc_depth * 0.5 + 0.5;
162 gPomDepth = ndc_screen_depth;
173 vec3 N = normalize(
TBN * map_normal);
187 vec2 current_uv = current_ndc * 0.5 + 0.5;
188 vec2 previous_uv = previous_ndc * 0.5 + 0.5;
190 gVel = (current_uv - previous_uv);