Harbor

branch main
showing the latest snapshot on main
pbr_fragment.glsl-opengl 1.4 KB · Plain text
shader/tests/golden/pbr_fragment.glsl-opengl 0644 Raw
#version 450

layout(std140, binding = 0) uniform material_Block {
	vec3 albedo;
	float metallic;
	float roughness;
} material;
layout(std140, binding = 1) uniform light_dir_Block {
	vec3 light_dir;
};
layout(std140, binding = 2) uniform camera_pos_Block {
	vec3 camera_pos;
};
layout(binding = 3) uniform sampler2D albedo_tex;

vec3 fresnel_schlick(float cos_theta, vec3 f0) {
	return (f0 + ((vec3(1.0, 1.0, 1.0) - f0) * pow((1.0 - cos_theta), 5.0)));
}

vec3 compute_lighting(vec3 normal, vec3 view_dir, vec3 albedo) {
	vec3 n = normalize(normal);
	vec3 l = normalize(light_dir);
	vec3 h = normalize((l + view_dir));
	float ndotl = max(dot(n, l), 0.0);
	float ndoth = max(dot(n, h), 0.0);
	vec3 f0 = mix(vec3(0.04, 0.04, 0.04), albedo, material.metallic);
	vec3 f = fresnel_schlick(max(dot(h, view_dir), 0.0), f0);
	vec3 specular = (f * pow(ndoth, ((1.0 - material.roughness) * 256.0)));
	vec3 diffuse = ((albedo * ndotl) * (vec3(1.0, 1.0, 1.0) - f));
	return (diffuse + specular);
}

layout(location = 0) in vec2 in_uv;
layout(location = 1) in vec3 in_normal;
layout(location = 2) in vec3 in_world_pos;
layout(location = 0) out vec4 out_color;

void main() {
	vec4 tex_color = texture(albedo_tex, in_uv);
	vec3 albedo = (tex_color.rgb * material.albedo);
	vec3 view_dir = normalize((camera_pos - in_world_pos));
	vec3 color = compute_lighting(in_normal, view_dir, albedo);
	out_color = vec4(color, tex_color.a);
}