2024-01-31 00:12:14 +11:00
|
|
|
|
|
|
|
#version 460 core
|
|
|
|
#extension GL_ARB_bindless_texture : require
|
|
|
|
|
2024-02-29 15:30:50 +11:00
|
|
|
const float PI = 3.141592f;
|
2024-02-25 21:54:51 +11:00
|
|
|
|
2024-02-23 19:23:18 +11:00
|
|
|
in VS_OUT {
|
|
|
|
vec3 normal;
|
|
|
|
vec4 colour;
|
|
|
|
vec3 pos;
|
|
|
|
vec2 tex_pos;
|
|
|
|
vec3 material;
|
|
|
|
} vin;
|
|
|
|
|
|
|
|
struct Light
|
|
|
|
{
|
|
|
|
vec4 pos;
|
|
|
|
vec4 colour;
|
|
|
|
};
|
|
|
|
|
2024-03-03 01:17:15 +11:00
|
|
|
layout(std140, binding = 1) readonly buffer LightBuffer
|
2024-02-23 19:23:18 +11:00
|
|
|
{
|
|
|
|
Light lights[];
|
|
|
|
};
|
|
|
|
|
2024-03-03 01:17:15 +11:00
|
|
|
layout(std430, binding = 2) readonly buffer ShadowMapBuffer
|
2024-02-25 21:54:51 +11:00
|
|
|
{
|
|
|
|
samplerCube shadow_maps[];
|
|
|
|
};
|
|
|
|
|
2024-02-23 19:23:18 +11:00
|
|
|
in flat sampler2D frag_tex;
|
|
|
|
out vec4 frag_colour;
|
2024-01-31 00:12:14 +11:00
|
|
|
|
2024-02-20 16:48:01 +11:00
|
|
|
uniform vec3 brightness;
|
2024-02-23 19:23:18 +11:00
|
|
|
uniform vec3 camera_pos;
|
|
|
|
uniform int lights_count;
|
2024-02-25 21:54:51 +11:00
|
|
|
uniform float far_plane;
|
|
|
|
uniform bool shadows_enabled;
|
2024-02-23 19:23:18 +11:00
|
|
|
|
2024-02-24 19:22:02 +11:00
|
|
|
vec3 FresnelSchlick(float cosTheta, vec3 F0)
|
2024-02-23 19:23:18 +11:00
|
|
|
{
|
2024-02-29 15:30:50 +11:00
|
|
|
return F0 + (1.f - F0) * pow(clamp(1.f - cosTheta, 0.f, 1.f), 5.f);
|
2024-02-23 19:23:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
float DistributionGGX(vec3 N, vec3 H, float roughness)
|
|
|
|
{
|
2024-02-25 21:54:51 +11:00
|
|
|
float a = roughness*roughness;
|
|
|
|
float a2 = a*a;
|
2024-02-29 15:30:50 +11:00
|
|
|
float NdotH = max(dot(N, H), 0.f);
|
2024-02-25 21:54:51 +11:00
|
|
|
float NdotH2 = NdotH*NdotH;
|
2024-02-23 19:23:18 +11:00
|
|
|
|
2024-02-25 21:54:51 +11:00
|
|
|
float num = a2;
|
2024-02-29 15:30:50 +11:00
|
|
|
float denom = (NdotH2 * (a2 - 1.f) + 1.f);
|
2024-02-25 21:54:51 +11:00
|
|
|
denom = PI * denom * denom;
|
2024-02-23 19:23:18 +11:00
|
|
|
|
2024-02-25 21:54:51 +11:00
|
|
|
return num / denom;
|
2024-02-23 19:23:18 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
float GeometrySchlickGGX(float NdotV, float roughness)
|
|
|
|
{
|
2024-02-29 15:30:50 +11:00
|
|
|
float r = (roughness + 1.f);
|
|
|
|
float k = (r*r) / 8.f;
|
2024-02-23 19:23:18 +11:00
|
|
|
|
2024-02-25 21:54:51 +11:00
|
|
|
float num = NdotV;
|
2024-02-29 15:30:50 +11:00
|
|
|
float denom = NdotV * (1.f - k) + k;
|
2024-02-23 19:23:18 +11:00
|
|
|
|
2024-02-25 21:54:51 +11:00
|
|
|
return num / denom;
|
2024-02-23 19:23:18 +11:00
|
|
|
}
|
2024-02-24 19:22:02 +11:00
|
|
|
|
2024-02-23 19:23:18 +11:00
|
|
|
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
|
|
|
|
{
|
2024-02-29 15:30:50 +11:00
|
|
|
float NdotV = max(dot(N, V), 0.f);
|
|
|
|
float NdotL = max(dot(N, L), 0.f);
|
2024-02-25 21:54:51 +11:00
|
|
|
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
|
|
|
|
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
|
2024-02-23 19:23:18 +11:00
|
|
|
|
2024-02-25 21:54:51 +11:00
|
|
|
return ggx1 * ggx2;
|
2024-02-23 19:23:18 +11:00
|
|
|
}
|
2024-01-31 00:12:14 +11:00
|
|
|
|
2024-02-24 19:22:02 +11:00
|
|
|
vec3 LinRGB_To_sRGB(vec3 c)
|
|
|
|
{
|
2024-02-24 20:36:27 +11:00
|
|
|
bvec3 th = lessThan(c, vec3(0.0031308f));
|
|
|
|
vec3 high = pow(c, vec3(1.0f / 2.4f)) * vec3(1.055f) - vec3(0.055f);
|
|
|
|
vec3 low = c * vec3(12.92f);
|
2024-02-24 19:22:02 +11:00
|
|
|
|
2024-02-24 20:36:27 +11:00
|
|
|
return mix(high, low, th);
|
2024-02-24 19:22:02 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
vec3 sRGB_To_LinRGB(vec3 c)
|
|
|
|
{
|
2024-02-24 20:36:27 +11:00
|
|
|
bvec3 th = lessThan(c, vec3(0.04045f));
|
|
|
|
vec3 high = pow((c + vec3(0.055f)) * vec3(1.0f / 1.055f), vec3(2.4f));
|
|
|
|
vec3 low = c * vec3(1.0f / 12.92f);
|
|
|
|
|
|
|
|
return mix(high, low, th);
|
2024-02-24 19:22:02 +11:00
|
|
|
}
|
|
|
|
|
2024-01-31 00:12:14 +11:00
|
|
|
void main()
|
|
|
|
{
|
2024-03-03 01:17:15 +11:00
|
|
|
vec4 albedo = texture2D(frag_tex, vin.tex_pos);
|
2024-02-29 15:30:50 +11:00
|
|
|
if(albedo.a == 0.f) discard;
|
|
|
|
|
2024-03-03 01:17:15 +11:00
|
|
|
vec3 albedo_lin = sRGB_To_LinRGB(albedo.rgb) * vin.colour.rgb;
|
|
|
|
albedo *= vin.colour;
|
2024-02-23 19:23:18 +11:00
|
|
|
|
|
|
|
float roughness = vin.material[0];
|
|
|
|
float metalness = vin.material[1];
|
|
|
|
float luminance = min(vin.material[2], 1.f);
|
|
|
|
|
|
|
|
vec3 N = normalize(vin.normal);
|
|
|
|
vec3 V = normalize(camera_pos - vin.pos.xyz);
|
|
|
|
|
2024-02-29 15:30:50 +11:00
|
|
|
vec3 F0 = vec3(0.04f);
|
2024-02-24 19:22:02 +11:00
|
|
|
F0 = mix(F0, albedo_lin, metalness);
|
2024-02-23 19:23:18 +11:00
|
|
|
|
2024-02-29 15:30:50 +11:00
|
|
|
vec3 Lo = vec3(0.0f);
|
2024-02-23 19:23:18 +11:00
|
|
|
for(int i = 0; i < lights_count; i++)
|
|
|
|
{
|
|
|
|
Light l = lights[i];
|
|
|
|
|
|
|
|
vec3 L = normalize(l.pos.xyz - vin.pos);
|
|
|
|
vec3 H = normalize(V + L);
|
|
|
|
|
|
|
|
float d = length(vin.pos - l.pos.xyz);
|
|
|
|
float atten = 1.f / (d*d);
|
|
|
|
vec3 radiance = l.colour.rgb * atten;
|
|
|
|
|
|
|
|
// cook-torrance brdf
|
2024-02-25 21:54:51 +11:00
|
|
|
float NDF = DistributionGGX(N, H, roughness);
|
|
|
|
float G = GeometrySmith(N, V, L, roughness);
|
2024-02-29 15:30:50 +11:00
|
|
|
vec3 F = FresnelSchlick(max(dot(H, V), 0.f), F0);
|
2024-02-25 21:54:51 +11:00
|
|
|
|
|
|
|
vec3 kS = F;
|
2024-02-29 15:30:50 +11:00
|
|
|
vec3 kD = vec3(1.f) - kS;
|
|
|
|
kD *= 1.f - metalness;
|
2024-02-25 21:54:51 +11:00
|
|
|
|
|
|
|
vec3 numerator = NDF * G * F;
|
2024-02-29 15:30:50 +11:00
|
|
|
float denominator = 4.f * max(dot(N, V), 0.f) * max(dot(N, L), 0.f) + 1e-4f;
|
2024-02-25 21:54:51 +11:00
|
|
|
vec3 specular = numerator / denominator;
|
|
|
|
|
2024-02-29 15:30:50 +11:00
|
|
|
float light_m;
|
|
|
|
float spec_m;
|
2024-02-25 21:54:51 +11:00
|
|
|
|
|
|
|
if(shadows_enabled)
|
|
|
|
{
|
2024-02-29 15:30:50 +11:00
|
|
|
float max_d = texture(shadow_maps[i], -L).r * far_plane + 1e-2f;
|
|
|
|
spec_m = max_d > d ? 1.f : 0.f;
|
|
|
|
light_m = spec_m * 0.25f + 0.75f;
|
2024-02-25 21:54:51 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
else
|
|
|
|
{
|
2024-02-29 15:30:50 +11:00
|
|
|
light_m = 1.f;
|
|
|
|
spec_m = 1.f;
|
2024-02-25 21:54:51 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
// add to outgoing radiance Lo
|
2024-02-29 15:30:50 +11:00
|
|
|
float NdotL = max(dot(N, L), 0.f);
|
|
|
|
Lo += (kD * albedo_lin / PI + specular * spec_m) * radiance * NdotL * light_m;
|
2024-02-23 19:23:18 +11:00
|
|
|
}
|
|
|
|
|
2024-02-24 19:22:02 +11:00
|
|
|
vec3 ambient = vec3(0.03f) * albedo_lin * brightness;
|
|
|
|
vec3 light = LinRGB_To_sRGB(ambient + Lo);
|
2024-02-23 19:23:18 +11:00
|
|
|
|
2024-02-24 20:36:27 +11:00
|
|
|
light = mix(light, albedo.rgb, luminance);
|
2024-02-23 19:23:18 +11:00
|
|
|
frag_colour = vec4(light, albedo.a);
|
2024-01-31 00:12:14 +11:00
|
|
|
}
|
|
|
|
|