DeferredCombine.fsh 2.99 KB
Newer Older
Dario Seyb's avatar
Dario Seyb committed
1
#version 410 core
Dario Seyb's avatar
Dario Seyb committed
2 3

uniform sampler2D uSamplerColor;
Dario Seyb's avatar
Dario Seyb committed
4
uniform sampler2D uSamplerEmissive;
Dario Seyb's avatar
Dario Seyb committed
5
uniform sampler2D uSamplerNormal;
Dario Seyb's avatar
Dario Seyb committed
6
uniform sampler2D uSamplerMotion;
Dario Seyb's avatar
Dario Seyb committed
7 8
uniform sampler2D uSamplerDepth;

Dario Seyb's avatar
Dario Seyb committed
9
uniform sampler2DShadow  uSamplerShadowMap;
Dario Seyb's avatar
Dario Seyb committed
10

Dario Seyb's avatar
Dario Seyb committed
11
uniform bool uLightHasShadow;
Dario Seyb's avatar
Dario Seyb committed
12
uniform vec2 uOneOverShadowTexSize;
Dario Seyb's avatar
Dario Seyb committed
13 14
uniform vec3 uLightPosition;
uniform vec4 uLightColor;
Dario Seyb's avatar
Dario Seyb committed
15
uniform mat4 uLightProjMatrix;
Dario Seyb's avatar
Dario Seyb committed
16 17
uniform bool uLightIsDirectional;
uniform vec3 uLightDir;
Dario Seyb's avatar
Dario Seyb committed
18 19 20 21

uniform float uNear;
uniform float uFar;

Dario Seyb's avatar
Dario Seyb committed
22
uniform vec3 cameraPosition;
Dario Seyb's avatar
Dario Seyb committed
23 24 25 26 27 28
uniform mat4 uViewProjectionInverseMatrix;

in vec2 vTexCoord;
out vec4 oColor;

vec3 unpackNormal(vec4 norm) {
Dario Seyb's avatar
Dario Seyb committed
29
  return norm.xyz; //(norm.xyz * 2) - vec3(1, 1, 1);
Dario Seyb's avatar
Dario Seyb committed
30 31 32 33 34 35
}

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

Dario Seyb's avatar
Dario Seyb committed
36 37 38 39
float unpackDepth(float depth) {
  return depth;
}

Dario Seyb's avatar
Dario Seyb committed
40 41
vec3 unpackWorldPosition(float depth) {
    vec4 clipSpaceLocation;
Dario Seyb's avatar
Dario Seyb committed
42 43 44
    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
45 46 47 48
    vec4 homogenousLocation = uViewProjectionInverseMatrix * clipSpaceLocation;
    return homogenousLocation.xyz / homogenousLocation.w;
}

Dario Seyb's avatar
Dario Seyb committed
49 50 51 52 53 54 55
vec3 projShadowCoord(vec3 worldPosition) {
  vec4 shadowCoord =  uLightProjMatrix * vec4(worldPosition, 1); 
  vec3 clipShadowCoord = shadowCoord.xyz/shadowCoord.w;
  return clipShadowCoord;
}

float shadowFactor(vec3 worldPosition) {
Dario Seyb's avatar
Dario Seyb committed
56 57
  if(!uLightHasShadow) return 1;
  
Dario Seyb's avatar
Dario Seyb committed
58
  vec3 shadowCoord = projShadowCoord(worldPosition.xyz);
Dario Seyb's avatar
Dario Seyb committed
59 60 61 62 63 64 65 66 67 68 69
  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
70 71
}

Dario Seyb's avatar
Dario Seyb committed
72 73 74
void main()
{
    vec4 color  = texture(uSamplerColor, vTexCoord);
Dario Seyb's avatar
Dario Seyb committed
75
    vec4 emissive  = texture(uSamplerEmissive, vTexCoord);
Dario Seyb's avatar
Dario Seyb committed
76 77
    vec3 normal = unpackNormal(texture(uSamplerNormal, vTexCoord));
    
Dario Seyb's avatar
Dario Seyb committed
78
    float depth = unpackDepth(texture(uSamplerDepth, vTexCoord).r);
Dario Seyb's avatar
Dario Seyb committed
79 80
    
    if(depth == 1) discard;
Dario Seyb's avatar
Dario Seyb committed
81
	
Dario Seyb's avatar
Dario Seyb committed
82 83
    vec3 worldPosition = unpackWorldPosition(depth);
    
Dario Seyb's avatar
Dario Seyb committed
84 85 86 87 88 89 90 91 92 93 94
	vec3 lightDir = vec3(0); 
	float lightDistance = 1;
	
	if(uLightIsDirectional) {
		lightDir = uLightDir;
	} else {
		lightDir = uLightPosition - worldPosition;
		lightDistance = length(lightDir);
		lightDir /= lightDistance;	
	}
	
Dario Seyb's avatar
Dario Seyb committed
95
    float diffuseFactor = dot(lightDir, normal);
Dario Seyb's avatar
Dario Seyb committed
96
	
Dario Seyb's avatar
Dario Seyb committed
97 98
    if(diffuseFactor < 0) diffuseFactor = 0;
    
Dario Seyb's avatar
Dario Seyb committed
99
    vec3 diffuseColor =  diffuseFactor * uLightColor.rgb * color.rgb;
Dario Seyb's avatar
Dario Seyb committed
100
    
Dario Seyb's avatar
Dario Seyb committed
101
    vec3 viewDir = normalize(cameraPosition - worldPosition);
Dario Seyb's avatar
Dario Seyb committed
102
	
Dario Seyb's avatar
Dario Seyb committed
103 104 105
    float specularFactor = dot( reflect(lightDir, -normal), -viewDir); 
    if(specularFactor < 0) specularFactor = 0;
    
106
    vec3 specularColor = pow(specularFactor, 210) * uLightColor.rgb;
Dario Seyb's avatar
Dario Seyb committed
107 108
    
    float attenuation = 1.0/(lightDistance * lightDistance);
Dario Seyb's avatar
Dario Seyb committed
109

Dario Seyb's avatar
Dario Seyb committed
110
    oColor = vec4((diffuseColor + specularColor) * color.rgb * uLightColor.a * shadowFactor(worldPosition) * attenuation + emissive.rgb, 1);
Dario Seyb's avatar
Dario Seyb committed
111
}