2023-02-25 19:03:39 +00:00
#ifndef LIB_LIGHT_LIGHTING
#define LIB_LIGHT_LIGHTING
2021-04-08 19:42:11 +00:00
#include "lighting_util.glsl"
2016-02-17 22:29:26 +00:00
2023-12-15 06:17:24 +00:00
float calcLambert(vec3 viewNormal, vec3 lightDir, vec3 viewDir)
2021-02-21 18:38:15 +00:00
{
2023-12-15 06:17:24 +00:00
float lambert = dot(viewNormal, lightDir);
2020-01-12 07:42:47 +00:00
#ifndef GROUNDCOVER
lambert = max(lambert, 0.0);
#else
2023-12-15 06:17:24 +00:00
float eyeCosine = dot(viewNormal, viewDir);
2021-02-09 21:56:21 +00:00
if (lambert < 0.0)
2020-01-12 07:42:47 +00:00
{
2021-02-09 21:56:21 +00:00
lambert = -lambert;
eyeCosine = -eyeCosine;
2020-01-12 07:42:47 +00:00
}
2021-02-09 21:56:21 +00:00
lambert *= clamp(-8.0 * (1.0 - 0.3) * eyeCosine + 1.0, 0.3, 1.0);
2020-01-12 07:42:47 +00:00
#endif
2023-12-15 06:17:24 +00:00
return lambert;
2017-09-20 23:25:48 +00:00
}
2023-12-15 07:23:10 +00:00
float calcSpecIntensity(vec3 viewNormal, vec3 viewDir, float shininess, vec3 lightDir)
{
if (dot(viewNormal, lightDir) > 0.0)
{
vec3 halfVec = normalize(lightDir - viewDir);
float NdotH = max(dot(viewNormal, halfVec), 0.0);
return pow(NdotH, shininess);
}
return 0.0;
}
2017-10-07 23:44:35 +00:00
#if PER_PIXEL_LIGHTING
2023-12-15 07:23:10 +00:00
void doLighting(vec3 viewPos, vec3 viewNormal, float shininess, float shadowing, out vec3 diffuseLight, out vec3 ambientLight, out vec3 specularLight)
2017-09-20 23:25:48 +00:00
#else
2023-12-15 07:23:10 +00:00
void doLighting(vec3 viewPos, vec3 viewNormal, float shininess, out vec3 diffuseLight, out vec3 ambientLight, out vec3 specularLight, out vec3 shadowDiffuse, out vec3 shadowSpecular)
2017-09-20 23:25:48 +00:00
#endif
{
2023-12-15 06:17:24 +00:00
vec3 viewDir = normalize(viewPos);
2023-12-15 07:23:10 +00:00
shininess = max(shininess, 1e-4);
2021-02-21 18:38:15 +00:00
2023-12-15 07:23:10 +00:00
vec3 sunDir = normalize(lcalcPosition(0));
diffuseLight = lcalcDiffuse(0) * calcLambert(viewNormal, sunDir, viewDir);
2021-04-12 08:08:45 +00:00
ambientLight = gl_LightModel.ambient.xyz;
2023-12-15 07:23:10 +00:00
specularLight = lcalcSpecular(0).xyz * calcSpecIntensity(viewNormal, viewDir, shininess, sunDir);
2017-10-08 00:34:54 +00:00
#if PER_PIXEL_LIGHTING
2023-12-15 06:17:24 +00:00
diffuseLight *= shadowing;
2023-12-15 07:23:10 +00:00
specularLight *= shadowing;
2017-09-20 23:25:48 +00:00
#else
2023-12-15 06:17:24 +00:00
shadowDiffuse = diffuseLight;
2023-12-15 07:23:10 +00:00
shadowSpecular = specularLight;
2021-04-13 18:00:48 +00:00
diffuseLight = vec3(0.0);
2023-12-15 07:23:10 +00:00
specularLight = vec3(0.0);
2017-09-20 23:25:48 +00:00
#endif
2021-03-15 04:42:34 +00:00
2021-04-08 19:42:11 +00:00
for (int i = @startLight; i < @endLight; ++i)
2021-03-15 04:42:34 +00:00
{
2021-04-08 19:42:11 +00:00
#if @lightingMethodUBO
2023-12-15 06:17:24 +00:00
int lightIndex = PointLightIndex[i];
2021-04-08 19:42:11 +00:00
#else
2023-12-15 06:17:24 +00:00
int lightIndex = i;
2021-04-08 19:42:11 +00:00
#endif
2023-12-15 06:17:24 +00:00
vec3 lightPos = lcalcPosition(lightIndex) - viewPos;
float lightDistance = length(lightPos);
// cull non-FFP point lighting by radius, light is guaranteed to not fall outside this bound with our cutoff
#if !@lightingMethodFFP
if (lightDistance > lcalcRadius(lightIndex) * 2.0)
continue;
#endif
vec3 lightDir = lightPos / lightDistance;
float illumination = lcalcIllumination(lightIndex, lightDistance);
diffuseLight += lcalcDiffuse(lightIndex) * calcLambert(viewNormal, lightDir, viewDir) * illumination;
2023-12-15 07:23:10 +00:00
ambientLight += lcalcAmbient(lightIndex) * illumination;
specularLight += lcalcSpecular(lightIndex).xyz * calcSpecIntensity(viewNormal, viewDir, shininess, lightDir) * illumination;
2016-02-17 22:29:26 +00:00
}
}
2016-02-20 18:02:11 +00:00
2023-02-25 19:03:39 +00:00
#endif