113 lines
3.0 KiB
GLSL
113 lines
3.0 KiB
GLSL
// G-BUFFER
|
|
|
|
#ifndef UNLIT
|
|
// - Position in view space
|
|
uniform sampler2DRect positionBuffer;
|
|
// - Albedo + Roughness
|
|
uniform sampler2DRect albedoBuffer;
|
|
// - Normal buffer
|
|
uniform sampler2DRect normalBuffer;
|
|
#endif
|
|
// - Emission + Metallic
|
|
uniform sampler2DRect emissionBuffer;
|
|
|
|
// LIGHT ATTRIBUTES
|
|
#ifdef AMBIENT_LIGHT
|
|
uniform samplerCube ambientMap;
|
|
uniform samplerCube reflectMap;
|
|
uniform sampler2D brdfLUT;
|
|
uniform mat3 inverseViewMatrix;
|
|
#elif !defined UNLIT
|
|
uniform vec3 lightColor;
|
|
#endif
|
|
|
|
#ifdef SHADOWMAP
|
|
#ifdef POINT_LIGHT
|
|
uniform samplerCube shadowMap;
|
|
uniform mat3 inverseViewMatrix;
|
|
#else
|
|
uniform sampler2DShadow shadowMap;
|
|
uniform mat4 viewToLightMatrix;
|
|
#endif
|
|
#endif
|
|
|
|
#if defined POINT_LIGHT
|
|
uniform vec3 pointLight;
|
|
uniform float range;
|
|
#elif defined DIRECTIONNAL_LIGHT
|
|
uniform vec3 dirLight;
|
|
#elif defined SPOT_LIGHT
|
|
uniform vec3 pointLight;
|
|
uniform float attenuation;
|
|
uniform vec3 dirLight;
|
|
uniform float cutoff;
|
|
#endif
|
|
|
|
// FRAGMENT POSITIONNING
|
|
|
|
in vec2 screenPos;
|
|
|
|
uniform mat4 inverseProjectionMatrix;
|
|
|
|
// OUTPUT LIGHT
|
|
|
|
layout(location = 0)out vec4 outColor;
|
|
|
|
// MAIN PROGRAM
|
|
|
|
void main(void) {
|
|
// get fragment information from the G-Buffer
|
|
ivec2 texCoord = ivec2(gl_FragCoord.xy);
|
|
#ifdef UNLIT
|
|
outColor = vec4(texelFetch(emissionBuffer, texCoord).rgb, 1);
|
|
#else
|
|
vec3 normal = texelFetch(normalBuffer, texCoord).xyz;
|
|
vec4 albedoTexel = texelFetch(albedoBuffer, texCoord);
|
|
vec3 albedo = albedoTexel.rgb;
|
|
float roughness = albedoTexel.a;
|
|
vec4 emissionTexel = texelFetch(emissionBuffer, texCoord);
|
|
vec3 emission = emissionTexel.rgb;
|
|
float metallic = emissionTexel.a;
|
|
vec4 fragPos = texelFetch(positionBuffer, texCoord);
|
|
|
|
// compute shadow
|
|
#ifdef SHADOWMAP
|
|
#ifdef POINT_LIGHT
|
|
float shadow = 1;
|
|
#else
|
|
vec4 fragInLightSpace = viewToLightMatrix * fragPos;
|
|
fragInLightSpace.z = fragInLightSpace.z - 0.002;
|
|
float shadow = texture(shadowMap, fragInLightSpace.xyz/fragInLightSpace.w);
|
|
#endif
|
|
#else
|
|
float shadow = 1;
|
|
#endif
|
|
|
|
float att = 1;
|
|
#ifdef POINT_LIGHT
|
|
vec3 dirLight = pointLight - fragPos.xyz;
|
|
//vec4 pointShadowParam = vec4(inverseViewMatrix * dirLight, length(dirLight));
|
|
//att = texture(shadowMap, pointShadowParam);
|
|
//outColor = vec4(vec3(texture(shadowMap, vec4(inverseViewMatrix * dirLight, length(dirLight)/range))), 1);
|
|
//outColor = vec4(vec3(texture(shadowMap, vec3(inverseViewMatrix * dirLight)).r), 1);
|
|
//outColor = vec4(vec3(length(dirLight)/range), 1);
|
|
//return;
|
|
float dist = length(dirLight);
|
|
att = clamp(1 - dist/range, 0, 1);
|
|
dirLight = normalize(dirLight);
|
|
#endif
|
|
|
|
vec3 viewDir = normalize(-fragPos.xyz);
|
|
#ifdef AMBIENT_LIGHT
|
|
vec3 ambientLight = ambientGGX(albedo, metallic, roughness, normal, viewDir, inverseViewMatrix, ambientMap, reflectMap, brdfLUT);
|
|
outColor = vec4(emission + ambientLight, 1);
|
|
#else
|
|
vec3 halfVec = normalize(viewDir + dirLight);
|
|
vec3 light = GGX(albedo, metallic, roughness, lightColor, normal, dirLight, halfVec, viewDir);
|
|
outColor = vec4(mix(vec3(0.0), light*shadow, att*att), 1);
|
|
#endif
|
|
|
|
#endif // UNLIT
|
|
}
|
|
|