Grafika2023/PlanetCreator/cw 6/shaders/shader_pbr_instanced.frag

79 lines
2.1 KiB
GLSL

#version 430 core
#define PI 3.14159265359
layout (location = 0) out vec4 outColor;
layout (location = 1) out vec4 BrightColor;
float AMBIENT = 0.1;
uniform vec3 albedo;
uniform float metallic;
uniform float roughness;
uniform vec3 color;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 ambientColor;
uniform sampler2D colorTexture;
in vec3 fragNormal;
in vec3 fragPosition;
in vec2 texCoords;
//out vec4 outColor;
float specularD(float NdotH, float roughness)
{
float alpha = roughness * roughness;
float alpha2 = alpha * alpha;
float cos2ThetaH = NdotH * NdotH;
float expTerm = (cos2ThetaH - 1.0) / (alpha2 * cos2ThetaH);
return exp(expTerm) / (PI * alpha2 * pow(cos2ThetaH + alpha2 - 1.0, 2.0));
}
vec3 specularF(float LdotH, vec3 F0)
{
return F0 + (vec3(1.0) - F0) * pow(1.0 - LdotH, 5.0);
}
float specularG(float NdotL, float NdotV, float roughness)
{
float k = (roughness + 1.0) * (roughness + 1.0) / 8.0;
float GL = NdotL / (NdotL * (1.0 - k) + k);
float GV = NdotV / (NdotV * (1.0 - k) + k);
return GL * GV;
}
void main()
{
vec3 normal = normalize(fragNormal);
vec3 viewDir = normalize(viewPos - fragPosition);
vec3 lightDir = normalize(lightPos - fragPosition);
vec3 F0 = vec3(0.04);
F0 = mix(F0, albedo, metallic);
float NdotL = max(dot(normal, lightDir), 0.0);
float NdotV = max(dot(normal, viewDir), 0.0);
vec3 H = normalize(lightDir + viewDir);
float NdotH = max(dot(normal, H), 0.0);
float LdotH = max(dot(lightDir, H), 0.0);
vec3 albedo = texture(colorTexture, texCoords).rgb;
albedo *= 2.0;
vec3 specular = F0 * (specularD(NdotH, roughness) * specularF(LdotH, F0) * specularG(NdotL, NdotV, roughness)) / (4.0 * NdotL * NdotV);
vec3 diffuse = (1.0 - F0) * albedo / PI;
vec3 ambient = ambientColor * AMBIENT;
vec3 finalColor = ambient + (diffuse + specular) * NdotL;
float brightness = dot(finalColor, vec3(0.2126, 0.7152, 0.0722));
if(brightness > 1.0)
BrightColor = vec4(finalColor, 1.0);
else
BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
outColor = vec4(finalColor, 1.0);
}