9 uniform vec3 lightPos;
\r
10 uniform vec3 lightColor;
\r
11 uniform vec3 objectColor;
\r
12 uniform vec3 viewPos;
\r
13 uniform sampler2D irradianceTexture;
\r
14 uniform int screenWidth;
\r
15 uniform int screenHeight;
\r
16 uniform int renderState;
\r
17 uniform vec2 samplePositions[13];
\r
18 uniform vec3 sampleWeights[13];
\r
19 uniform float transmittanceScale;
\r
24 vec3 norm = normalize(Normal);
\r
25 vec3 lightDir = normalize(lightPos - FragPos);
\r
27 float diff = max(dot(norm, lightDir), 0.0);
\r
28 vec3 diffuse = diff * lightColor;
\r
30 float ambientStrength = 0.1;
\r
31 vec3 ambient = ambientStrength * lightColor;
\r
33 float specularStrength = 0.5;
\r
34 vec3 viewDir = normalize(viewPos - FragPos);
\r
35 vec3 reflectDir = reflect(-lightDir, norm);
\r
36 float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32);
\r
37 vec3 specular = specularStrength * spec * lightColor;
\r
39 vec3 result = vec3((ambient + diffuse + specular) * objectColor);
\r
41 // sample irradiance as distance to light combined with angle to the light
\r
42 // with a Gaussian kernel
\r
43 vec3 result2 = vec3(0, 0, 0);
\r
44 for (int i = 0; i < 13; i++) {
\r
45 vec2 sampleCoords = UV + samplePositions[i] * vec2(1.0/screenWidth, 1.0/screenHeight);
\r
46 //vec4 sample = texture(irradianceTexture, sampleCoords)
\r
47 // * texture(shadowmapTexture, sampleCoords);
\r
48 vec3 sample = vec3(texture(irradianceTexture, sampleCoords)) * diff;
\r
49 vec3 weight = sampleWeights[i];
\r
50 result2 += sample * weight;
\r
53 // multiply to apply irradiance, sqrt to get values between 0 and 1
\r
54 result = sqrt(result * result2);
\r
56 // sample texture to get distance from light
\r
57 vec4 t = texture(irradianceTexture, UV);
\r
58 float BacksideIrradiance = t.r; //*100 + t.g + t.b/100;
\r
59 // and calculate world pos
\r
60 vec3 Backside = (lightPos + (normalize(FragPos - lightPos) * BacksideIrradiance));
\r
62 // add translucency by amplifying color inverse to the thickness
\r
63 // (1 - diff) is part of the irradiance term,
\r
64 // if the light hits the object straight at 90°
\r
65 // most light is received
\r
66 float distanceToBackside = length(FragPos - Backside);
\r
67 if (distanceToBackside != 0)
\r
68 result += objectColor * exp(2 / pow(distanceToBackside, 0.6)) * transmittanceScale * (1 - diff);
\r
70 FragColor = vec4(result, 1);
\r