DeferredCombine.fsh 3.4 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;
5
uniform sampler2D uSamplerNormalMotion;
Dario Seyb's avatar
Dario Seyb committed
6
uniform sampler2D uSamplerMotion;
Dario Seyb's avatar
Dario Seyb committed
7
uniform sampler2D uSamplerDepth;
Dario Seyb's avatar
Dario Seyb committed
8
uniform sampler2D uSamplerSpecularSmoothness;
Dario Seyb's avatar
Dario Seyb committed
9

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

Dario Seyb's avatar
Dario Seyb committed
12 13 14
uniform sampler2D uSamplerSSAO;
uniform bool uHasSSAO;

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

23 24
uniform float uOneOverLightCount;

Dario Seyb's avatar
Dario Seyb committed
25 26 27
uniform float uNear;
uniform float uFar;

Dario Seyb's avatar
Dario Seyb committed
28
uniform vec3 cameraPosition;
Dario Seyb's avatar
Dario Seyb committed
29 30 31 32 33
uniform mat4 uViewProjectionInverseMatrix;

in vec2 vTexCoord;
out vec4 oColor;

34
#pragma import "Utils.glsl"
Dario Seyb's avatar
Dario Seyb committed
35
#pragma import "Lighting.glsl"
Dario Seyb's avatar
Dario Seyb committed
36 37 38 39 40

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

Dario Seyb's avatar
Dario Seyb committed
41 42 43 44
float unpackDepth(float depth) {
  return depth;
}

Dario Seyb's avatar
Dario Seyb committed
45 46
vec3 unpackWorldPosition(float depth) {
    vec4 clipSpaceLocation;
Dario Seyb's avatar
Dario Seyb committed
47 48 49
    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
50 51 52 53
    vec4 homogenousLocation = uViewProjectionInverseMatrix * clipSpaceLocation;
    return homogenousLocation.xyz / homogenousLocation.w;
}

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

Dario Seyb's avatar
Dario Seyb committed
75
  return shadow;
Dario Seyb's avatar
Dario Seyb committed
76 77
}

Dario Seyb's avatar
Dario Seyb committed
78 79 80 81 82
float ssaoFactor() {
  if(!uHasSSAO) return 1;
  return texture(uSamplerSSAO, vTexCoord).r;
}

Dario Seyb's avatar
Dario Seyb committed
83 84
void main()
{
Dario Seyb's avatar
Dario Seyb committed
85 86
    vec4 albedo  = texture(uSamplerColor, vTexCoord);
    vec4 specularSmoothness  = texture(uSamplerSpecularSmoothness, vTexCoord);
Dario Seyb's avatar
Dario Seyb committed
87
    vec4 emissive  = texture(uSamplerEmissive, vTexCoord);
88
    vec3 normal = unpackNormal(texture(uSamplerNormalMotion, vTexCoord));
Dario Seyb's avatar
Dario Seyb committed
89
    
Dario Seyb's avatar
Dario Seyb committed
90
 
Dario Seyb's avatar
Dario Seyb committed
91
    float depth = unpackDepth(texture(uSamplerDepth, vTexCoord).r);
Dario Seyb's avatar
Dario Seyb committed
92 93
    
    if(depth == 1) discard;
Dario Seyb's avatar
Dario Seyb committed
94
	
Dario Seyb's avatar
Dario Seyb committed
95 96
    vec3 worldPosition = unpackWorldPosition(depth);
    
Dario Seyb's avatar
Dario Seyb committed
97 98
    vec3 lightDir = vec3(0); 
    float lightDistance = 1;
Dario Seyb's avatar
Dario Seyb committed
99
    float attenuation = 1;
Dario Seyb's avatar
Dario Seyb committed
100
	
Dario Seyb's avatar
Dario Seyb committed
101 102 103 104 105 106
    if(uLightIsDirectional) {
      lightDir = uLightDir;
    } else {
      lightDir = uLightPosition - worldPosition;
      lightDistance = length(lightDir);
      lightDir /= lightDistance;	
Dario Seyb's avatar
Dario Seyb committed
107
      attenuation = max(1, 1.0/pow(lightDistance, 2));
Dario Seyb's avatar
Dario Seyb committed
108 109
    }

Dario Seyb's avatar
Dario Seyb committed
110
    vec3 viewDir = normalize(cameraPosition - worldPosition);
Dario Seyb's avatar
Dario Seyb committed
111
    
Dario Seyb's avatar
Dario Seyb committed
112 113
    float oneMinusReflectivity;
    albedo.rgb = EnergyConservationBetweenDiffuseAndSpecular (albedo.rgb, specularSmoothness.rgb, /*out*/ oneMinusReflectivity);
Dario Seyb's avatar
Dario Seyb committed
114

Dario Seyb's avatar
Dario Seyb committed
115
    float lightNdotL = max(0, dot(lightDir, normal));
Dario Seyb's avatar
Dario Seyb committed
116
    vec4 lighting =  BRDF (albedo.rgb, specularSmoothness.rgb, oneMinusReflectivity, specularSmoothness.a, normal, viewDir, lightDir, lightNdotL, uLightColor.rgb * uLightColor.a);
Dario Seyb's avatar
Dario Seyb committed
117 118 119
    
    float ssao = ssaoFactor();
    
Dario Seyb's avatar
Dario Seyb committed
120 121 122
    
    
    oColor = vec4(lighting.rgb * ssao * shadowFactor(worldPosition) * attenuation  + emissive.rgb, uOneOverLightCount * albedo.a);
Dario Seyb's avatar
Dario Seyb committed
123
}