* Improve normal accuracy & Fix wrong SSR * One less sample count
This commit is contained in:
parent
68d9fd2138
commit
e2c245c420
@ -15,26 +15,19 @@ out vec4 Spec;
|
||||
#stk_include "utils/DiffuseIBL.frag"
|
||||
#stk_include "utils/SpecularIBL.frag"
|
||||
|
||||
|
||||
float makeLinear(float f, float n, float z)
|
||||
{
|
||||
return (2.0f * n) / (f + n - z * (f - n));
|
||||
}
|
||||
|
||||
vec3 CalcViewPositionFromDepth(in vec2 TexCoord)
|
||||
vec3 CalcViewPositionFromDepth(in vec2 uv)
|
||||
{
|
||||
// Combine UV & depth into XY & Z (NDC)
|
||||
float z = makeLinear(1000.0, 1.0, textureLod(dtex, TexCoord, 0.).x);
|
||||
vec3 rawPosition = vec3(TexCoord, z);
|
||||
float z = texture(dtex, uv).x;
|
||||
return getPosFromUVDepth(vec3(uv, z), u_inverse_projection_matrix).xyz;
|
||||
}
|
||||
|
||||
// Convert from (0, 1) range to (-1, 1)
|
||||
vec4 ScreenSpacePosition = vec4( rawPosition * 2.0 - 1.0, 1.0);
|
||||
|
||||
// Undo Perspective transformation to bring into view space
|
||||
vec4 ViewPosition = u_inverse_projection_matrix * ScreenSpacePosition;
|
||||
|
||||
// Perform perspective divide and return
|
||||
return ViewPosition.xyz / ViewPosition.w;
|
||||
vec2 CalcCoordFromPosition(in vec3 pos)
|
||||
{
|
||||
vec4 projectedCoord = u_projection_matrix * vec4(pos, 1.0);
|
||||
projectedCoord.xy /= projectedCoord.w;
|
||||
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
|
||||
return projectedCoord.xy;
|
||||
}
|
||||
|
||||
// Fade out edges of screen buffer tex
|
||||
@ -48,35 +41,33 @@ float GetEdgeFade(vec2 coords)
|
||||
return min(min(gradL, gradR), min(gradT, gradB));
|
||||
}
|
||||
|
||||
vec2 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth)
|
||||
vec2 RayCast(vec3 dir, vec3 hitCoord)
|
||||
{
|
||||
dir *= 0.25f;
|
||||
vec2 projectedCoord;
|
||||
vec3 dirstep = dir * 0.5f;
|
||||
float depth;
|
||||
hitCoord += dirstep;
|
||||
|
||||
for(int i = 0; i < 8; ++i) {
|
||||
hitCoord += dir;
|
||||
for (int i = 1; i <= 32; i++)
|
||||
{
|
||||
projectedCoord = CalcCoordFromPosition(hitCoord);
|
||||
|
||||
vec4 projectedCoord = u_projection_matrix * vec4(hitCoord, 1.0);
|
||||
projectedCoord.xy /= projectedCoord.w;
|
||||
projectedCoord.xy = projectedCoord.xy * 0.5 + 0.5;
|
||||
float depth = CalcViewPositionFromDepth(projectedCoord).z;
|
||||
|
||||
float depth = CalcViewPositionFromDepth(projectedCoord.xy).z;
|
||||
dDepth = hitCoord.z - depth;
|
||||
|
||||
if (dDepth < 0.0)
|
||||
{
|
||||
if (projectedCoord.x > 0.0 && projectedCoord.x < 1.0 &&
|
||||
projectedCoord.y > 0.0 && projectedCoord.y < 1.0)
|
||||
{
|
||||
return projectedCoord.xy;
|
||||
}
|
||||
else
|
||||
{
|
||||
return vec2(0.f);
|
||||
}
|
||||
}
|
||||
float directionSign = sign(abs(hitCoord.z) - depth);
|
||||
dirstep = dirstep * (1.0 - 0.5 * max(directionSign, 0.0));
|
||||
hitCoord += dirstep * (-directionSign);
|
||||
}
|
||||
|
||||
return vec2(0.f);
|
||||
if (projectedCoord.x > 0.0 && projectedCoord.x < 1.0 &&
|
||||
projectedCoord.y > 0.0 && projectedCoord.y < 1.0)
|
||||
{
|
||||
return projectedCoord.xy;
|
||||
}
|
||||
else
|
||||
{
|
||||
return vec2(0.f);
|
||||
}
|
||||
}
|
||||
|
||||
// Main ===================================================================
|
||||
@ -84,7 +75,7 @@ vec2 RayCast(vec3 dir, inout vec3 hitCoord, out float dDepth)
|
||||
void main(void)
|
||||
{
|
||||
vec2 uv = gl_FragCoord.xy / u_screen;
|
||||
vec3 normal = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
||||
vec3 normal = DecodeNormal(texture(ntex, uv).xy);
|
||||
|
||||
Diff = vec4(0.25 * DiffuseIBL(normal), 1.);
|
||||
|
||||
@ -110,18 +101,10 @@ void main(void)
|
||||
// otherwise just use specular IBL
|
||||
if (specval > 0.5)
|
||||
{
|
||||
vec3 View_Pos = CalcViewPositionFromDepth(uv);
|
||||
|
||||
// Reflection vector
|
||||
vec3 reflected = normalize(reflect(eyedir, normal));
|
||||
vec3 reflected = reflect(-eyedir, normal);
|
||||
|
||||
// Ray cast
|
||||
vec3 hitPos = View_Pos.xyz;
|
||||
float dDepth;
|
||||
float minRayStep = 50.0f;
|
||||
|
||||
vec2 coords = RayCast(reflected * max(minRayStep, -View_Pos.z),
|
||||
hitPos, dDepth);
|
||||
vec2 coords = RayCast(reflected, xpos.xyz);
|
||||
|
||||
if (coords.x == 0.0 && coords.y == 0.0) {
|
||||
outColor = fallback;
|
||||
|
@ -16,7 +16,7 @@ out vec4 Spec;
|
||||
void main(void)
|
||||
{
|
||||
vec2 uv = gl_FragCoord.xy / u_screen;
|
||||
vec3 normal = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
||||
vec3 normal = DecodeNormal(texture(ntex, uv).xy);
|
||||
|
||||
Diff = vec4(0.25 * DiffuseIBL(normal), 1.);
|
||||
Spec = vec4(0.031, 0.106, 0.173, 1.);
|
||||
|
@ -27,7 +27,7 @@ void main()
|
||||
{
|
||||
vec2 texc = gl_FragCoord.xy / u_screen;
|
||||
float z = texture(dtex, texc).x;
|
||||
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, texc).xy - 1.));
|
||||
vec3 norm = DecodeNormal(texture(ntex, texc).xy);
|
||||
float roughness = texture(ntex, texc).z;
|
||||
|
||||
vec4 xpos = getPosFromUVDepth(vec3(texc, z), u_inverse_projection_matrix);
|
||||
|
@ -32,7 +32,7 @@ void main(void)
|
||||
vec4 layer_2 = sampleTextureLayer2(uv);
|
||||
o_diffuse_color = vec4(col.xyz, layer_2.z);
|
||||
|
||||
o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5;
|
||||
o_normal_color.xy = EncodeNormal(normalize(normal));
|
||||
o_normal_color.zw = layer_2.xy;
|
||||
#else
|
||||
o_diffuse_color = vec4(col.xyz, 1.0);
|
||||
|
@ -20,7 +20,7 @@ void main(void)
|
||||
vec4 layer_2 = sampleTextureLayer2(uv);
|
||||
o_diffuse_color = vec4(final_color, layer_2.z);
|
||||
|
||||
o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5;
|
||||
o_normal_color.xy = EncodeNormal(normalize(normal));
|
||||
o_normal_color.zw = layer_2.xy;
|
||||
#else
|
||||
o_diffuse_color = vec4(final_color, 1.0);
|
||||
|
@ -48,7 +48,7 @@ void main()
|
||||
|
||||
vec3 world_normal = t_b_n * tangent_space_normal;
|
||||
|
||||
o_normal_color.xy = 0.5 * EncodeNormal(normalize(world_normal)) + 0.5;
|
||||
o_normal_color.xy = EncodeNormal(normalize(world_normal));
|
||||
o_normal_color.zw = layer_2.xy;
|
||||
#else
|
||||
o_diffuse_color = vec4(final_color, 1.0);
|
||||
|
@ -31,7 +31,7 @@ void main(void)
|
||||
vec4 layer_2 = sampleTextureLayer2(uv);
|
||||
o_diffuse_color = vec4(col.xyz, layer_2.z);
|
||||
|
||||
o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5;
|
||||
o_normal_color.xy = EncodeNormal(normalize(normal));
|
||||
o_normal_color.zw = layer_2.xy;
|
||||
#else
|
||||
o_diffuse_color = vec4(col.xyz, 1.0);
|
||||
|
@ -47,7 +47,7 @@ void main()
|
||||
|
||||
vec3 world_normal = t_b_n * tangent_space_normal;
|
||||
|
||||
o_normal_color.xy = 0.5 * EncodeNormal(normalize(world_normal)) + 0.5;
|
||||
o_normal_color.xy = EncodeNormal(normalize(world_normal));
|
||||
o_normal_color.zw = layer_2.xy;
|
||||
#else
|
||||
o_diffuse_color = vec4(final_color, 1.0);
|
||||
|
@ -85,7 +85,7 @@ void main()
|
||||
|
||||
vec3 world_normal = t_b_n * tangent_space_normal;
|
||||
|
||||
o_normal_color.xy = 0.5 * EncodeNormal(normalize(world_normal)) + 0.5;
|
||||
o_normal_color.xy = EncodeNormal(normalize(world_normal));
|
||||
o_normal_color.zw = layer_2.xy;
|
||||
#else
|
||||
o_diffuse_color = vec4(final_color, 1.0);
|
||||
|
@ -37,7 +37,7 @@ void main(void)
|
||||
vec4 layer_2 = sampleTextureLayer2(uv);
|
||||
o_diffuse_color = vec4(final_color, layer_2.z);
|
||||
|
||||
o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5;
|
||||
o_normal_color.xy = EncodeNormal(normalize(normal));
|
||||
o_normal_color.zw = layer_2.xy;
|
||||
#else
|
||||
o_diffuse_color = vec4(final_color, 1.0);
|
||||
|
@ -49,7 +49,7 @@ void main()
|
||||
|
||||
vec3 world_normal = t_b_n * tangent_space_normal;
|
||||
|
||||
o_normal_color.xy = 0.5 * EncodeNormal(normalize(world_normal)) + 0.5;
|
||||
o_normal_color.xy = EncodeNormal(normalize(world_normal));
|
||||
o_normal_color.zw = layer_2.xy;
|
||||
#else
|
||||
o_diffuse_color = vec4(final_color, 1.0);
|
||||
|
@ -22,7 +22,7 @@ void main(void)
|
||||
#if defined(Advanced_Lighting_Enabled)
|
||||
o_diffuse_color = vec4(final_color, 0.4);
|
||||
|
||||
o_normal_color.xy = 0.5 * EncodeNormal(normalize(normal)) + 0.5;
|
||||
o_normal_color.xy = EncodeNormal(normalize(normal));
|
||||
o_normal_color.zw = vec2(0.0);
|
||||
#else
|
||||
o_diffuse_color = vec4(final_color, 1.0);
|
||||
|
@ -55,7 +55,7 @@ void main()
|
||||
|
||||
vec3 world_normal = t_b_n * tangent_space_normal;
|
||||
|
||||
o_normal_color.xy = 0.5 * EncodeNormal(normalize(world_normal)) + 0.5;
|
||||
o_normal_color.xy = EncodeNormal(normalize(world_normal));
|
||||
o_normal_color.zw = layer_2.xy;
|
||||
#else
|
||||
o_diffuse_color = vec4(final_color, 1.0);
|
||||
|
@ -23,7 +23,7 @@ void main() {
|
||||
float z = texture(dtex, uv).x;
|
||||
vec4 xpos = getPosFromUVDepth(vec3(uv, z), u_inverse_projection_matrix);
|
||||
|
||||
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
||||
vec3 norm = DecodeNormal(texture(ntex, uv).xy);
|
||||
float roughness = texture(ntex, uv).z;
|
||||
vec3 eyedir = -normalize(xpos.xyz);
|
||||
|
||||
|
@ -88,7 +88,7 @@ void main() {
|
||||
float z = texture(dtex, uv).x;
|
||||
vec4 xpos = getPosFromUVDepth(vec3(uv, z), u_inverse_projection_matrix);
|
||||
|
||||
vec3 norm = normalize(DecodeNormal(2. * texture(ntex, uv).xy - 1.));
|
||||
vec3 norm = DecodeNormal(texture(ntex, uv).xy);
|
||||
float roughness =texture(ntex, uv).z;
|
||||
vec3 eyedir = -normalize(xpos.xyz);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user