Move software dynlight stuff to lightmodel

This commit is contained in:
Magnus Norddahl 2025-05-19 18:06:50 +02:00
commit f1fba4fe98
6 changed files with 58 additions and 91 deletions

View file

@ -6,14 +6,6 @@
#include "shaders/scene/material.glsl"
#include "shaders/scene/fogball.glsl"
#ifndef SIMPLE3D
vec3 swLightContribution(DynLightInfo light, vec3 normal);
float distanceAttenuation(float dist, float radius, float strength, float linearity);
float spotLightAttenuation(vec3 lightpos, vec3 spotdir, float lightCosInnerAngle, float lightCosOuterAngle);
float traceSun(vec3 SunDir);
float shadowAttenuation(vec3 lightpos, int shadowIndex, float softShadowRadius, int flags);
#endif
vec3 PickGamePaletteColor(vec3 color)
{
ivec3 c = ivec3(clamp(color.rgb, vec3(0.0), vec3(1.0)) * 63.0 + 0.5);
@ -65,36 +57,7 @@ vec4 getLightColor(Material material)
dynlight.rgb += texture(LightMap, vLightmap).rgb;
}
vec3 normal = material.Normal;
#ifndef SIMPLE3D
#ifndef UBERSHADER
#ifdef SHADE_VERTEX
dynlight.rgb += vLightColor;
#else
if (uLightIndex >= 0)
{
ivec4 lightRange = getLightRange();
if (lightRange.z > lightRange.x)
{
// modulated lights
for(int i=lightRange.x; i<lightRange.y; i++)
{
dynlight.rgb += swLightContribution(getLights()[i], normal);
}
// subtractive lights
for(int i=lightRange.y; i<lightRange.z; i++)
{
dynlight.rgb -= swLightContribution(getLights()[i], normal);
}
}
}
#endif
dynlight = desaturate(dynlight);
#endif
#endif
dynlight.rgb += ProcessSWLight(material);
frag.rgb = PickGamePaletteColor(frag.rgb + matColor * dynlight.rgb);
return frag;
@ -238,56 +201,3 @@ vec4 ProcessLightMode(Material material)
return getLightColor(material);
}
}
#ifndef SIMPLE3D
vec3 swLightContribution(DynLightInfo light, vec3 normal)
{
float lightdistance = distance(light.pos.xyz, pixelpos.xyz);
if (light.radius < lightdistance)
return vec3(0.0); // Early out lights touching surface but not this fragment
vec3 lightdir = normalize(light.pos.xyz - pixelpos.xyz);
float dotprod;
if (!LIGHT_NONORMALS)
{
dotprod = dot(normal, lightdir);
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.
}
float attenuation = distanceAttenuation(lightdistance, light.radius, light.strength, light.linearity);
if ((light.flags & LIGHTINFO_SPOT) != 0)
{
attenuation *= spotLightAttenuation(light.pos.xyz, light.spotDir.xyz, light.spotInnerAngle, light.spotOuterAngle);
}
if (!LIGHT_NONORMALS)
{
if ((light.flags & LIGHTINFO_ATTENUATED) != 0)
{
attenuation *= clamp(dotprod, 0.0, 1.0);
}
}
if (attenuation > 0.0) // Skip shadow map test if possible
{
if((light.flags & (LIGHTINFO_SUN | LIGHTINFO_TRACE)) == (LIGHTINFO_SUN | LIGHTINFO_TRACE))
{
attenuation *= traceSun(lightdir);
}
else if((light.flags & (LIGHTINFO_SHADOWMAPPED | LIGHTINFO_SUN)) == LIGHTINFO_SHADOWMAPPED)
{
attenuation *= shadowAttenuation(light.pos.xyz, light.shadowIndex, light.softShadowRadius, light.flags);
}
return light.color.rgb * attenuation;
}
else
{
return vec3(0.0);
}
}
#endif

View file

@ -3,3 +3,8 @@ vec3 ProcessMaterialLight(Material material, vec3 color)
{
return material.Base.rgb * clamp(color + desaturate(uDynLightColor).rgb, 0.0, 1.4);
}
vec3 ProcessSWLight(Material material)
{
return vec3(0.0);
}

View file

@ -123,9 +123,50 @@
return frag;
}
vec3 ProcessSWLight(Material material)
{
vec3 normal = material.Normal;
vec3 dynlight = vec3(0.0);
#ifndef UBERSHADER
#ifdef SHADE_VERTEX
dynlight.rgb += vLightColor;
#else
if (uLightIndex >= 0)
{
ivec4 lightRange = getLightRange();
if (lightRange.z > lightRange.x)
{
// modulated lights
for(int i=lightRange.x; i<lightRange.y; i++)
{
dynlight.rgb += lightContribution(getLights()[i], normal);
}
// subtractive lights
for(int i=lightRange.y; i<lightRange.z; i++)
{
dynlight.rgb -= lightContribution(getLights()[i], normal);
}
}
}
#endif
dynlight = desaturate(vec4(dynlight, 0.0)).rgb;
#endif
return dynlight;
}
#else
vec3 ProcessMaterialLight(Material material, vec3 color)
{
return material.Base.rgb;
}
vec3 ProcessSWLight(Material material)
{
return vec3(0.0);
}
#endif

View file

@ -166,3 +166,8 @@ vec3 ProcessMaterialLight(Material material, vec3 ambientLight)
vec3 color = max(ambient + Lo, vec3(0.0));
return color;
}
vec3 ProcessSWLight(Material material)
{
return vec3(0.0);
}

View file

@ -115,3 +115,8 @@ vec3 ProcessMaterialLight(Material material, vec3 color)
return frag;
}
vec3 ProcessSWLight(Material material)
{
return vec3(0.0);
}

View file

@ -21,6 +21,7 @@ struct Material
vec4 Process(vec4 color);
void SetupMaterial(inout Material mat);
vec3 ProcessMaterialLight(Material material, vec3 color);
vec3 ProcessSWLight(Material material);
vec2 GetTexCoord();
Material CreateMaterial()