diff --git a/wadsrc/static/shaders/glsl/main.fp b/wadsrc/static/shaders/glsl/main.fp index d1ae8f50f..d7c06eb1c 100644 --- a/wadsrc/static/shaders/glsl/main.fp +++ b/wadsrc/static/shaders/glsl/main.fp @@ -384,6 +384,158 @@ float spotLightAttenuation(vec4 lightpos, vec3 spotdir, float lightCosInnerAngle return smoothstep(lightCosOuterAngle, lightCosInnerAngle, cosDir); } +#if defined(PBR) + +const float PI = 3.14159265359; + +float DistributionGGX(vec3 N, vec3 H, float roughness) +{ + float a = roughness * roughness; + float a2 = a * a; + float NdotH = max(dot(N, H), 0.0); + float NdotH2 = NdotH*NdotH; + + float nom = a2; + float denom = (NdotH2 * (a2 - 1.0) + 1.0); + denom = PI * denom * denom; + + return nom / denom; +} + +float GeometrySchlickGGX(float NdotV, float roughness) +{ + float r = (roughness + 1.0); + float k = (r * r) / 8.0; + + float nom = NdotV; + float denom = NdotV * (1.0 - k) + k; + + return nom / denom; +} + +float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) +{ + float NdotV = max(dot(N, V), 0.0); + float NdotL = max(dot(N, L), 0.0); + float ggx2 = GeometrySchlickGGX(NdotV, roughness); + float ggx1 = GeometrySchlickGGX(NdotL, roughness); + return ggx1 * ggx2; +} + +vec3 fresnelSchlick(float cosTheta, vec3 F0) +{ + return F0 + (1.0 - F0) * pow(1.0 - cosTheta, 5.0); +} + +vec3 applyLight(vec3 albedo, vec3 ambientLight) +{ + vec3 worldpos = pixelpos.xyz; + + albedo = pow(albedo, vec3(2.2)); // sRGB to linear + ambientLight = pow(ambientLight, vec3(2.2)); + + vec3 normal = ApplyNormalMap(); + float metallic = texture(metallictexture, vTexCoord.st).r; + float roughness = texture(roughnesstexture, vTexCoord.st).r; + float ao = texture(aotexture, vTexCoord.st).r; + + vec3 N = normalize(normal); + vec3 V = normalize(uCameraPos.xyz - worldpos); + + vec3 F0 = mix(vec3(0.04), albedo, metallic); + + vec3 Lo = uDynLightColor.rgb; + +#if defined NUM_UBO_LIGHTS || defined SHADER_STORAGE_LIGHTS + if (uLightIndex >= 0) + { + ivec4 lightRange = ivec4(lights[uLightIndex]) + ivec4(uLightIndex + 1); + if (lightRange.z > lightRange.x) + { + // + // modulated lights + // + for(int i=lightRange.x; i