Grafika2023/PlanetCreator/cw 6/shaders/shader_pbr_instanced.frag
2024-03-18 16:51:38 +01:00

82 lines
2.2 KiB
GLSL

#version 430 core
#define PI 3.14159265359
layout (location = 0) out vec4 outColor;
layout (location = 1) out vec4 BrightColor;
in vec3 fragNormal;
in vec3 fragPosition;
in vec2 texCoords;
uniform sampler2D textures[4];
uniform vec3 albedo;
uniform float metallic;
uniform float roughness;
uniform vec3 lightPos;
uniform vec3 viewPos;
uniform vec3 ambientColor;
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 = mix(vec3(0.04), 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);
int texIndex = 0;
vec3 albedoTex = texture(textures[texIndex], texCoords).rgb;
albedoTex = vec3(smoothstep(0.0, 1.0, albedoTex));
float luminance = dot(albedoTex, vec3(0.2126, 0.7152, 0.0722));
albedoTex *= 4.0;
float saturationFactor = 4.0;
albedoTex = mix(vec3(luminance), albedoTex, saturationFactor);
vec3 specular = F0 * (specularD(NdotH, roughness) * specularF(LdotH, F0) * specularG(NdotL, NdotV, roughness));
vec3 diffuse = (1.0 - F0) * albedoTex / PI;
vec3 ambient = ambientColor * 0.02;
vec3 finalColor = ambient + (diffuse + specular) * NdotL;
if (dot(finalColor, vec3(0.2126, 0.7152, 0.0722)) > 1.0)
BrightColor = vec4(finalColor, 1.0);
else
BrightColor = vec4(0.0, 0.0, 0.0, 1.0);
outColor = vec4(finalColor, 1.0);
}