Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4574 #5080

Merged
merged 2 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 36 additions & 53 deletions data/shaders/IBL.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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)
vec3 CalcViewPositionFromDepth(in vec2 uv)
{
return (2.0f * n) / (f + n - z * (f - n));
// Combine UV & depth into XY & Z (NDC)
float z = texture(dtex, uv).x;
return getPosFromUVDepth(vec3(uv, z), u_inverse_projection_matrix).xyz;
}

vec3 CalcViewPositionFromDepth(in vec2 TexCoord)
vec2 CalcCoordFromPosition(in vec3 pos)
{
// Combine UV & depth into XY & Z (NDC)
float z = makeLinear(1000.0, 1.0, textureLod(dtex, TexCoord, 0.).x);
vec3 rawPosition = vec3(TexCoord, z);

// 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;
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
Expand All @@ -48,43 +41,41 @@ 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;

for(int i = 0; i < 8; ++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)
{
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);
}
}
vec2 projectedCoord;
vec3 dirstep = dir * 0.5f;
float depth;
hitCoord += dirstep;

for (int i = 1; i <= 32; i++)
{
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);
}

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 ===================================================================

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.);

Expand All @@ -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));

// Ray cast
vec3 hitPos = View_Pos.xyz;
float dDepth;
float minRayStep = 50.0f;
vec3 reflected = reflect(-eyedir, normal);

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;
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/degraded_ibl.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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.);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/pointlight.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sp_alpha_test.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sp_decal.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sp_dynamic_night_bloom.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sp_grass.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sp_normal_map.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sp_road_blending.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sp_solid.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sp_tilling_mitigation.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sp_unlit.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sp_vertical_mapping.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sunlight.frag
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion data/shaders/sunlightshadow.frag
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,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);

Expand Down
Loading