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