Grafika_Komputerowa_Interst.../cw 7/shaders/shader_5_tex.frag
2024-02-08 09:24:56 +01:00

73 lines
1.6 KiB
GLSL

#version 430 core
float AMBIENT = 0.4;
float roughness;
float metalic;
float MAX_DISTANCE = 12.0;
uniform vec3 color;
uniform sampler2D colorTexture;
uniform sampler2D normalSampler;
uniform sampler2D metalnessTexture;
uniform sampler2D roughnessTexture;
in vec3 worldPos;
in vec2 vecTex;
in vec3 viewDirTS;
in vec3 lightDirTS;
in float distanceToCamera;
out vec4 outColor;
void main()
{
vec3 normal = vec3(0,0,1);
vec3 L = (lightDirTS);
vec3 V = (viewDirTS);
vec3 textureColor = texture2D(colorTexture, vecTex).xyz;
float metalnessValue = texture2D(metalnessTexture, vecTex).r;
float roughnessValue = texture2D(roughnessTexture, vecTex).r;
roughness =roughnessValue;
metalic = metalnessValue;
vec3 N = texture2D(normalSampler, vecTex).xyz;
N = 2.0 * N - 1.0;
N = normalize(N);
vec3 H = normalize(L + V);
float NdotH = max(0.0, dot(N, H));
float NdotL = max(dot(N, L),0.0000001 );
float NdotV = max(0.0, dot(N, V));
float VdotH = max(0.00001, dot(V, H));
float k = pow((roughness +1),2.0)/8.0;
float D = (roughness * roughness) / (3.14159 * pow(pow(NdotH * NdotH,2.0) * (roughness * roughness - 1.0) + 1.0, 2.0));
float ggx1 = NdotV / (NdotV * (1.0 - k) + k);
float ggx2 = NdotL / (NdotL * (1.0 - k) + k);
vec3 F0 = mix(vec3(0.04), vec3(1.0), metalic);
float G = ggx1 * ggx2;
vec3 F = F0 + (1.0-F0)*pow(1-dot(V,H),5.0);
vec3 specular = (D*G*F)/(4*NdotL*NdotV+0.00001);
vec3 kD = vec3(1.0) - F;
vec3 BRDF = kD*(textureColor/3.1458493) + specular;
if (distanceToCamera > MAX_DISTANCE)
{
discard;
}
float diffuse=max(0.0001,dot(N,L));
outColor = vec4(BRDF*(AMBIENT + diffuse)*1.5, 1.0);
}