2021-02-22 23:28:58 +00:00
vec3 lightContribution(int i, vec3 normal)
{
vec4 lightpos = lights[i];
vec4 lightcolor = lights[i+1];
vec4 lightspot1 = lights[i+2];
vec4 lightspot2 = lights[i+3];
float lightdistance = distance(lightpos.xyz, pixelpos.xyz);
2021-03-09 18:14:52 +00:00
//if (lightpos.w < lightdistance)
// return vec3(0.0); // Early out lights touching surface but not this fragment
2021-02-22 23:28:58 +00:00
vec3 lightdir = normalize(lightpos.xyz - pixelpos.xyz);
float dotprod = dot(normal, lightdir);
2021-03-09 18:14:52 +00:00
//if (dotprod < -0.0001) return vec3(0.0); // light hits from the backside. This can happen with full sector light lists and must be rejected for all cases. Note that this can cause precision issues.
2021-02-22 23:28:58 +00:00
float attenuation = clamp((lightpos.w - lightdistance) / lightpos.w, 0.0, 1.0);
2021-03-09 18:14:52 +00:00
// NOTE, all spot light stuff gone
return lightcolor.rgb * attenuation;
/*
2021-02-22 23:28:58 +00:00
if (lightspot1.w == 1.0)
attenuation *= spotLightAttenuation(lightpos, lightspot1.xyz, lightspot2.x, lightspot2.y);
if (lightcolor.a < 0.0) // Sign bit is the attenuated light flag
{
attenuation *= clamp(dotprod, 0.0, 1.0);
}
2021-03-08 20:23:12 +00:00
2021-02-22 23:28:58 +00:00
if (attenuation > 0.0) // Skip shadow map test if possible
{
attenuation *= shadowAttenuation(lightpos, lightcolor.a);
return lightcolor.rgb * attenuation;
}
else
{
return vec3(0.0);
}
2021-03-09 18:14:52 +00:00
*/
2021-02-22 23:28:58 +00:00
}
vec3 ProcessMaterialLight(Material material, vec3 color)
{
vec4 dynlight = uDynLightColor;
vec3 normal = material.Normal;
2021-03-09 17:54:37 +00:00
#if (DEF_DYNAMIC_LIGHTS_MOD == 1)
// modulated lights
2021-03-20 15:38:20 +00:00
for(int i=uLightRange.x; i<uLightRange.y; i+=4)
2021-02-22 23:28:58 +00:00
{
2021-03-09 17:54:37 +00:00
dynlight.rgb += lightContribution(i, normal);
}
#endif
2021-02-22 23:28:58 +00:00
2021-03-09 17:54:37 +00:00
#if (DEF_DYNAMIC_LIGHTS_SUB == 1)
// subtractive lights
2021-03-20 15:38:20 +00:00
for(int i=uLightRange.y; i<uLightRange.z; i+=4)
2021-03-09 17:54:37 +00:00
{
dynlight.rgb -= lightContribution(i, normal);
2021-02-22 23:28:58 +00:00
}
2021-03-09 17:54:37 +00:00
#endif
2021-03-08 21:18:01 +00:00
2021-02-22 23:28:58 +00:00
vec3 frag = material.Base.rgb * clamp(color + desaturate(dynlight).rgb, 0.0, 1.4);
2021-03-08 21:18:01 +00:00
2021-03-09 17:54:37 +00:00
#if (DEF_DYNAMIC_LIGHTS_ADD == 1)
2021-03-08 21:18:01 +00:00
vec4 addlight = vec4(0.0,0.0,0.0,0.0);
2021-02-22 23:28:58 +00:00
2021-03-08 21:18:01 +00:00
// additive lights
2021-03-20 15:38:20 +00:00
for(int i=uLightRange.z; i<uLightRange.w; i+=4)
2021-03-08 21:18:01 +00:00
{
addlight.rgb += lightContribution(i, normal);
2021-02-22 23:28:58 +00:00
}
2021-03-08 21:18:01 +00:00
frag = clamp(frag + desaturate(addlight).rgb, 0.0, 1.0);
2021-03-09 17:54:37 +00:00
#endif
2021-03-08 20:23:12 +00:00
2021-02-22 23:28:58 +00:00
return frag;
}