2016-02-16 15:07:25 +01:00
|
|
|
#version 450
|
|
|
|
|
|
|
|
|
|
layout (binding = 1) uniform sampler2D samplerposition;
|
|
|
|
|
layout (binding = 2) uniform sampler2D samplerNormal;
|
|
|
|
|
layout (binding = 3) uniform sampler2D samplerAlbedo;
|
|
|
|
|
|
|
|
|
|
layout (location = 0) in vec2 inUV;
|
|
|
|
|
|
|
|
|
|
layout (location = 0) out vec4 outFragcolor;
|
|
|
|
|
|
|
|
|
|
struct Light {
|
2016-07-03 21:32:35 +02:00
|
|
|
vec4 position;
|
|
|
|
|
vec3 color;
|
2016-02-16 15:07:25 +01:00
|
|
|
float radius;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
layout (binding = 4) uniform UBO
|
|
|
|
|
{
|
|
|
|
|
Light lights[6];
|
|
|
|
|
vec4 viewPos;
|
|
|
|
|
} ubo;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void main()
|
|
|
|
|
{
|
2016-07-03 21:32:35 +02:00
|
|
|
// Get G-Buffer values
|
|
|
|
|
vec3 fragPos = texture(samplerposition, inUV).rgb;
|
|
|
|
|
vec3 normal = texture(samplerNormal, inUV).rgb;
|
|
|
|
|
vec4 albedo = texture(samplerAlbedo, inUV);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
#define lightCount 6
|
|
|
|
|
#define ambient 0.0
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
// Ambient part
|
|
|
|
|
vec3 fragcolor = albedo.rgb * ambient;
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
for(int i = 0; i < lightCount; ++i)
|
|
|
|
|
{
|
|
|
|
|
// Vector to light
|
|
|
|
|
vec3 L = ubo.lights[i].position.xyz - fragPos;
|
|
|
|
|
// Distance from light to fragment position
|
|
|
|
|
float dist = length(L);
|
|
|
|
|
|
|
|
|
|
// Viewer to fragment
|
|
|
|
|
vec3 V = ubo.viewPos.xyz - fragPos;
|
|
|
|
|
V = normalize(V);
|
2016-02-16 15:07:25 +01:00
|
|
|
|
2016-07-03 21:32:35 +02:00
|
|
|
//if(dist < ubo.lights[i].radius)
|
|
|
|
|
{
|
|
|
|
|
// Light to fragment
|
|
|
|
|
L = normalize(L);
|
|
|
|
|
|
|
|
|
|
// Attenuation
|
|
|
|
|
float atten = ubo.lights[i].radius / (pow(dist, 2.0) + 1.0);
|
|
|
|
|
|
|
|
|
|
// Diffuse part
|
|
|
|
|
vec3 N = normalize(normal);
|
|
|
|
|
float NdotL = max(0.0, dot(N, L));
|
|
|
|
|
vec3 diff = ubo.lights[i].color * albedo.rgb * NdotL * atten;
|
|
|
|
|
|
|
|
|
|
// Specular part
|
|
|
|
|
// Specular map values are stored in alpha of albedo mrt
|
|
|
|
|
vec3 R = reflect(-L, N);
|
|
|
|
|
float NdotR = max(0.0, dot(R, V));
|
|
|
|
|
vec3 spec = ubo.lights[i].color * albedo.a * pow(NdotR, 16.0) * atten;
|
|
|
|
|
|
|
|
|
|
fragcolor += diff + spec;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-02-16 15:07:25 +01:00
|
|
|
|
|
|
|
|
outFragcolor = vec4(fragcolor, 1.0);
|
|
|
|
|
}
|