opengl-deferred/assets/shader/pbr/pbr.fs

152 lines
3.8 KiB
Forth
Raw Normal View History

2024-10-18 14:12:01 +00:00
#version 460 core
// gbuffer textures
uniform sampler2D s_albedo;
uniform sampler2D s_depth;
uniform sampler2D s_normal;
uniform sampler2D s_material;
uniform sampler2D s_position;
// lights
#define MAX_LIGHT_COUNT 100
#define POINT 0
#define SPOT 1
#define DIRECTIONAL 2
#define AMBIENT 3
struct light_t {
vec3 color;
vec3 position;
vec3 direction;
float intensity;
int type;
};
uniform light_t lights[MAX_LIGHT_COUNT];
uniform int light_count;
uniform vec3 view_pos;
// from vertex shader
in vec3 pos;
in vec2 tex;
out vec4 FragColor;
vec3 albedo;
float depth;
vec3 normal;
vec3 material; // specular, roughness, metalic
vec3 position;
// float specular;
float roughness;
float metalic;
const float PI = 3.14159265359;
float DistributionGGX(vec3 N, vec3 H, float roughness)
{
float a = roughness*roughness;
float a2 = a*a;
float NdotH = max(dot(N, H), 0.0);
float NdotH2 = NdotH*NdotH;
float nom = a2;
float denom = (NdotH2 * (a2 - 1.0) + 1.0);
denom = PI * denom * denom;
return nom / denom;
}
float GeometrySchlickGGX(float NdotV, float roughness)
{
float r = (roughness + 1.0);
float k = (r*r) / 8.0;
float nom = NdotV;
float denom = NdotV * (1.0 - k) + k;
return nom / denom;
}
float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness)
{
float NdotV = max(dot(N, V), 0.0);
float NdotL = max(dot(N, L), 0.0);
float ggx2 = GeometrySchlickGGX(NdotV, roughness);
float ggx1 = GeometrySchlickGGX(NdotL, roughness);
return ggx1 * ggx2;
}
vec3 fresnelSchlick(float cosTheta, vec3 F0)
{
return F0 + (1.0 - F0) * pow(clamp(1.0 - cosTheta, 0.0, 1.0), 5.0);
}
void main() {
albedo = pow(texture(s_albedo, tex).rgb, vec3(2.2));
depth = texture(s_depth, tex).x;
normal = normalize(texture(s_normal, tex).rgb);
material = texture(s_material, tex).xyz;
{
// specular = material.x;
roughness = 1 - material.y;
metalic = material.z;
// metalic = 0;
}
position = texture(s_position, tex).xyz;
vec3 N = normal;
vec3 V = normalize(view_pos - position);
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metalic);
vec3 Lo = vec3(0.0);
for(int i = 0; i < light_count; i++)
{
// calculate per-light radiance
vec3 L = normalize(lights[i].position - position);
vec3 H = normalize(V + L);
float distance = length(lights[i].position - position);
float attenuation = 1.0 / (distance * distance);
vec3 radiance = lights[i].color * lights[i].intensity * attenuation;
// Cook-Torrance BRDF
float NDF = DistributionGGX(N, H, roughness);
float G = GeometrySmith(N, V, L, roughness);
vec3 F = fresnelSchlick(max(dot(H, V), 0.0), F0);
vec3 numerator = NDF * G * F;
float denominator = 4.0 * max(dot(N, V), 0.0) * max(dot(N, L), 0.0) + 0.0001; // + 0.0001 to prevent divide by zero
vec3 specular = numerator / denominator;
// kS is equal to Fresnel
vec3 kS = F;
// for energy conservation, the diffuse and specular light can't
// be above 1.0 (unless the surface emits light); to preserve this
// relationship the diffuse component (kD) should equal 1.0 - kS.
vec3 kD = vec3(1.0) - kS;
// multiply kD by the inverse metalness such that only non-metals
// have diffuse lighting, or a linear blend if partly metal (pure metals
// have no diffuse light).
kD *= 1.0 - metalic;
// scale light by NdotL
float NdotL = max(dot(N, L), 0.0);
// add to outgoing radiance Lo
Lo += (kD * albedo / PI + specular) * radiance * NdotL; // note that we already multiplied the BRDF by the Fresnel (kS) so we won't multiply by kS again
}
vec3 ambient = vec3(0.03) * albedo * 1.0;
vec3 color = ambient + Lo;
color = color / (color + vec3(1.0));
color = pow(color, vec3(1.0/2.2));
FragColor = vec4(color, 1.0);
}