DeferredCombine.fsh 2.51 KB
Newer Older
Dario Seyb's avatar
Dario Seyb committed
1
#version 330 core
Dario Seyb's avatar
Dario Seyb committed
2 3 4 5 6

uniform sampler2D uSamplerColor;
uniform sampler2D uSamplerNormal;
uniform sampler2D uSamplerDepth;

Dario Seyb's avatar
Dario Seyb committed
7
uniform sampler2DShadow  uSamplerShadowMap;
Dario Seyb's avatar
Dario Seyb committed
8

Dario Seyb's avatar
Dario Seyb committed
9
uniform vec2 uOneOverShadowTexSize;
Dario Seyb's avatar
Dario Seyb committed
10 11
uniform vec3 uLightPosition;
uniform vec4 uLightColor;
Dario Seyb's avatar
Dario Seyb committed
12
uniform mat4 uLightProjMatrix;
Dario Seyb's avatar
Dario Seyb committed
13 14 15 16

uniform float uNear;
uniform float uFar;

Dario Seyb's avatar
Dario Seyb committed
17
uniform vec3 cameraPosition;
Dario Seyb's avatar
Dario Seyb committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
uniform mat4 uViewProjectionInverseMatrix;

in vec2 vTexCoord;
out vec4 oColor;

vec3 unpackNormal(vec4 norm) {
  return (norm.xyz * 2) - vec3(1, 1, 1);
}

float linearizeDepth(float depth) {
  return  (2.0 * uNear) / (uFar + uNear - depth * (uFar - uNear));  // convert to linear values
}

vec3 unpackWorldPosition(float depth) {
    vec4 clipSpaceLocation;
Dario Seyb's avatar
Dario Seyb committed
33 34 35
    clipSpaceLocation.xy = vTexCoord * 2.0 - 1.0;
    clipSpaceLocation.z = depth * 2.0 - 1.0;
    clipSpaceLocation.w = 1.0;
Dario Seyb's avatar
Dario Seyb committed
36 37 38 39
    vec4 homogenousLocation = uViewProjectionInverseMatrix * clipSpaceLocation;
    return homogenousLocation.xyz / homogenousLocation.w;
}

Dario Seyb's avatar
Dario Seyb committed
40 41 42 43 44 45 46 47
vec3 projShadowCoord(vec3 worldPosition) {
  vec4 shadowCoord =  uLightProjMatrix * vec4(worldPosition, 1); 
  vec3 clipShadowCoord = shadowCoord.xyz/shadowCoord.w;
  return clipShadowCoord;
}

float shadowFactor(vec3 worldPosition) {
  vec3 shadowCoord = projShadowCoord(worldPosition.xyz);
Dario Seyb's avatar
Dario Seyb committed
48 49 50 51 52 53 54 55 56 57 58
  float x,y;
  float shadow = 0;
  for (y = -1.5 ; y <=1.5 ; y+=1.0) {
    for (x = -1.5 ; x <=1.5 ; x+=1.0) {
      shadow += texture(uSamplerShadowMap, shadowCoord + vec3(x * uOneOverShadowTexSize.x , y * uOneOverShadowTexSize.y, -0.005));
    }
  }

  shadow /= 16.0 ;

  return shadow; 
Dario Seyb's avatar
Dario Seyb committed
59 60
}

Dario Seyb's avatar
Dario Seyb committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
void main()
{
    vec4 color  = texture(uSamplerColor, vTexCoord);
    vec3 normal = unpackNormal(texture(uSamplerNormal, vTexCoord));
    
    float depth = texture(uSamplerDepth, vTexCoord).r;
    
    if(depth == 1) discard;
    
    vec3 worldPosition = unpackWorldPosition(depth);
    
    vec3 lightDir = uLightPosition - worldPosition;
    float lightDistance = length(lightDir);
    lightDir /= lightDistance;
    
Dario Seyb's avatar
Dario Seyb committed
76
    float diffuseFactor = dot(lightDir, normal);
Dario Seyb's avatar
Dario Seyb committed
77 78
    if(diffuseFactor < 0) diffuseFactor = 0;
    
Dario Seyb's avatar
Dario Seyb committed
79
    vec3 diffuseColor =  diffuseFactor * uLightColor.rgb * color.rgb;
Dario Seyb's avatar
Dario Seyb committed
80
    
Dario Seyb's avatar
Dario Seyb committed
81 82 83 84 85
    vec3 viewDir = normalize(cameraPosition - worldPosition);
    
    float specularFactor = dot( reflect(lightDir, -normal), -viewDir); 
    if(specularFactor < 0) specularFactor = 0;
    
86
    vec3 specularColor = pow(specularFactor, 210) * uLightColor.rgb;
Dario Seyb's avatar
Dario Seyb committed
87

88
    oColor = vec4((diffuseColor + specularColor) * color.rgb * uLightColor.a/(lightDistance*lightDistance) * shadowFactor(worldPosition), 1);
Dario Seyb's avatar
Dario Seyb committed
89
}