Skip to content

Commit ca70eab

Browse files
committed
fix: spot light attenuation issues
1 parent f920bf6 commit ca70eab

File tree

3 files changed

+24
-20
lines changed

3 files changed

+24
-20
lines changed

extensions/pl_renderer_ext.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3282,7 +3282,7 @@ pl_renderer_debug_draw_lights(plView* ptView, const plLightComponent* sbtLights,
32823282
else if(sbtLights[i].tType == PL_LIGHT_TYPE_SPOT)
32833283
{
32843284
plCone tCone0 = {
3285-
.fRadius = tanf(sbtLights[i].fOuterConeAngle) * sbtLights[i].fRange,
3285+
.fRadius = tanf(sbtLights[i].fOuterConeAngle * 0.5f) * sbtLights[i].fRange,
32863286
.tTipPos = sbtLights[i].tPosition,
32873287
.tBasePos = pl_add_vec3(sbtLights[i].tPosition, pl_mul_vec3_scalarf(sbtLights[i].tDirection, sbtLights[i].fRange))
32883288
};

shaders/lighting.glsl

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,19 @@ filterPCF(vec4 sc, vec2 offset, int textureIndex)
113113
float dy = scale * 1.0 / (float(texDim.y));
114114

115115
float shadowFactor = 0.0;
116-
int count = 0;
116+
// int count = 0;
117117
int range = 1;
118118

119-
for (int x = -range; x <= range; x++) {
120-
for (int y = -range; y <= range; y++) {
119+
for (int x = -range; x <= range; x++)
120+
{
121+
for (int y = -range; y <= range; y++)
122+
{
121123
shadowFactor += textureProj(sc, vec2(dx*x, dy*y) + offset, textureIndex);
122-
count++;
124+
// count++;
123125
}
124126
}
125-
return shadowFactor / count;
127+
// return shadowFactor / count;
128+
return shadowFactor / 4.0;
126129
}
127130

128131
float
@@ -134,16 +137,19 @@ filterPCF2(vec4 sc, vec2 offset, int textureIndex)
134137
float dy = scale * 1.0 / (float(texDim.y));
135138

136139
float shadowFactor = 0.0;
137-
int count = 0;
140+
// int count = 0;
138141
int range = 1;
139142

140-
for (int x = -range; x <= range; x++) {
141-
for (int y = -range; y <= range; y++) {
143+
for (int x = -range; x <= range; x++)
144+
{
145+
for (int y = -range; y <= range; y++)
146+
{
142147
shadowFactor += textureProj2(sc, vec2(dx*x, dy*y) + offset, textureIndex);
143-
count++;
148+
// count++;
144149
}
145150
}
146-
return shadowFactor / count;
151+
// return shadowFactor / count;
152+
return shadowFactor / 4.0;
147153
}
148154

149155
#endif // LIGHTING_GLSL

shaders/lights.glsl

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,14 @@
44
#include "pl_shader_interop_renderer.h"
55

66
float
7-
getRangeAttenuation(float range2, float dist2)
7+
getRangeAttenuation(float range, float distance)
88
{
9-
if (range2 <= 0.0)
9+
if (range <= 0.0)
1010
{
1111
// negative range means unlimited
12-
return 1.0 / dist2;
12+
return 1.0 / pow(distance, 2.0);
1313
}
14-
float dist_per_range = dist2 / range2;
15-
dist_per_range *= dist_per_range;
16-
return max(min(1.0 - dist_per_range, 1.0), 0.0) / dist2;
14+
return max(min(1.0 - pow(distance / range, 4.0), 1.0), 0.0) / pow(distance, 2.0);
1715
}
1816

1917
// https://github.com/KhronosGroup/glTF/blob/master/extensions/2.0/Khronos/KHR_lights_punctual/README.md#inner-and-outer-cone-angles
@@ -39,11 +37,11 @@ getLightIntensity(plGpuLight light, vec3 pointToLight)
3937
float rangeAttenuation = 1.0;
4038
float spotAttenuation = 1.0;
4139

42-
if (light.iType != 0)
40+
if (light.iType != PL_LIGHT_TYPE_DIRECTIONAL)
4341
{
44-
rangeAttenuation = getRangeAttenuation(light.fRange * light.fRange, pointToLight.x * pointToLight.x + pointToLight.y * pointToLight.y + pointToLight.z * pointToLight.z);
42+
rangeAttenuation = getRangeAttenuation(light.fRange, length(pointToLight));
4543
}
46-
if (light.iType == 2)
44+
if (light.iType == PL_LIGHT_TYPE_SPOT)
4745
{
4846
spotAttenuation = getSpotAttenuation(pointToLight, light.tDirection, light.fOuterConeCos, light.fInnerConeCos);
4947
}

0 commit comments

Comments
 (0)