diff --git a/data/shaders/IBL.frag b/data/shaders/IBL.frag index 8f8202e68..d1e3b2964 100644 --- a/data/shaders/IBL.frag +++ b/data/shaders/IBL.frag @@ -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; diff --git a/data/shaders/degraded_ibl.frag b/data/shaders/degraded_ibl.frag index ec049f242..af53a3a85 100644 --- a/data/shaders/degraded_ibl.frag +++ b/data/shaders/degraded_ibl.frag @@ -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.); diff --git a/data/shaders/pointlight.frag b/data/shaders/pointlight.frag index 611fcf2d8..3fc1c063f 100644 --- a/data/shaders/pointlight.frag +++ b/data/shaders/pointlight.frag @@ -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); diff --git a/data/shaders/sp_alpha_test.frag b/data/shaders/sp_alpha_test.frag index 22dcd404e..1eb62ba5f 100644 --- a/data/shaders/sp_alpha_test.frag +++ b/data/shaders/sp_alpha_test.frag @@ -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); diff --git a/data/shaders/sp_decal.frag b/data/shaders/sp_decal.frag index a0e2636de..a9bfd0a52 100644 --- a/data/shaders/sp_decal.frag +++ b/data/shaders/sp_decal.frag @@ -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); diff --git a/data/shaders/sp_dynamic_night_bloom.frag b/data/shaders/sp_dynamic_night_bloom.frag index 27b006fc1..b006c51ca 100644 --- a/data/shaders/sp_dynamic_night_bloom.frag +++ b/data/shaders/sp_dynamic_night_bloom.frag @@ -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); diff --git a/data/shaders/sp_grass.frag b/data/shaders/sp_grass.frag index 6605979fd..013151221 100644 --- a/data/shaders/sp_grass.frag +++ b/data/shaders/sp_grass.frag @@ -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); diff --git a/data/shaders/sp_normal_map.frag b/data/shaders/sp_normal_map.frag index 60a7ddfaa..98cc78b4e 100644 --- a/data/shaders/sp_normal_map.frag +++ b/data/shaders/sp_normal_map.frag @@ -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); diff --git a/data/shaders/sp_road_blending.frag b/data/shaders/sp_road_blending.frag index 3a5eb7ce9..3e9d8e042 100644 --- a/data/shaders/sp_road_blending.frag +++ b/data/shaders/sp_road_blending.frag @@ -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); diff --git a/data/shaders/sp_solid.frag b/data/shaders/sp_solid.frag index 9a68416ed..1a48a81db 100644 --- a/data/shaders/sp_solid.frag +++ b/data/shaders/sp_solid.frag @@ -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); diff --git a/data/shaders/sp_tilling_mitigation.frag b/data/shaders/sp_tilling_mitigation.frag index 8a2f46d10..b1166cc92 100644 --- a/data/shaders/sp_tilling_mitigation.frag +++ b/data/shaders/sp_tilling_mitigation.frag @@ -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); diff --git a/data/shaders/sp_unlit.frag b/data/shaders/sp_unlit.frag index 5a82f58f7..8012ea7fa 100644 --- a/data/shaders/sp_unlit.frag +++ b/data/shaders/sp_unlit.frag @@ -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); diff --git a/data/shaders/sp_vertical_mapping.frag b/data/shaders/sp_vertical_mapping.frag index a2a1db36c..d0b6e79de 100644 --- a/data/shaders/sp_vertical_mapping.frag +++ b/data/shaders/sp_vertical_mapping.frag @@ -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); diff --git a/data/shaders/sunlight.frag b/data/shaders/sunlight.frag index 0bde1e617..8ad88c32a 100644 --- a/data/shaders/sunlight.frag +++ b/data/shaders/sunlight.frag @@ -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); diff --git a/data/shaders/sunlightshadow.frag b/data/shaders/sunlightshadow.frag index e7a5692ec..6641cb25f 100644 --- a/data/shaders/sunlightshadow.frag +++ b/data/shaders/sunlightshadow.frag @@ -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);