Advertisement

SSR artifacts

Started by November 18, 2018 01:39 PM
0 comments, last by congard 6 years, 2 months ago

Hello! When I implemented SSR I encountered the problem of artifacts.

Screenshots here

Code:


#version 330 core

uniform sampler2D normalMap; // in world space
uniform sampler2D colorMap;
uniform sampler2D reflectionStrengthMap;
uniform sampler2D positionMap; // in world space
uniform mat4 projection, view;
uniform vec3 cameraPosition;

in vec2 texCoord;

layout (location = 0) out vec4 fragColor;

void main() {
    mat4 vp = projection * view;
    vec3 position = texture(positionMap, texCoord).xyz;
    vec3 normal = texture(normalMap, texCoord).xyz;
    vec4 coords;
    vec3 viewDir = normalize(position - cameraPosition);
    vec3 reflected = reflect(viewDir, normal);

    float L = 0.5;
    vec3 newPos;
    for (int i = 0; i < 10; i++) {
        newPos = position + reflected * L;

        coords = vp * vec4(newPos, 1.0);
        coords.xy = 0.5 + 0.5 * coords.xy / coords.w;

        newPos = texture(positionMap, coords.xy).xyz;
        L = length(position - newPos);
    }

    float fresnel = 0.0 + 2.8 * pow(1 + dot(viewDir, normal), 4);
    L = clamp(L * 0.1, 0, 1);
    float error = (1 - L);

    vec3 color = texture(colorMap, coords.xy).xyz;

    fragColor = mix(texture(colorMap, texCoord), vec4(color, 1.0), texture(reflectionStrengthMap, texCoord).r);
}

I will be grateful for help!

This topic is closed to new replies.

Advertisement