【发布时间】:2017-01-27 23:02:48
【问题描述】:
我正在 GLSL 中处理 2D 照明,但它似乎有一个我无法修复的错误。 当场景中有多个灯光时,我的灯光会产生这些奇怪的“层”重叠。
我的着色器代码非常简单:
uniform vec2 lightLocation[6];
uniform vec3 lightColor[6];
uniform float lightRadius[6];
uniform sampler2D texture0;
void main() {
float distance;
float attenuation;
vec4 color;
vec4 finalColor = vec4(0, 0, 0, 1);
for (int i = 0; i < 6; ++i) {
distance = length(lightLocation[i] - gl_FragCoord.xy);
attenuation = 4.0 / (1.0 + 10.0*(distance / lightRadius[i]));
color = vec4(attenuation, attenuation, attenuation, pow(attenuation, 3)) * vec4(lightColor[i], 1);
finalColor = finalColor + color;
}
gl_FragColor = finalColor * texture(texture0, gl_TexCoord[0].st);
}
有谁知道是什么导致了这个问题?
【问题讨论】:
-
也许尝试在给定距离后钳制衰减?
-
实际上我无法重现您的问题,我尝试了something similair in shadertoy,但与您在此处显示的内容完全不同。
texture0中有什么内容?为什么你在你的颜色中使用 alpha?你在做一些混合吗? -
@Zouch 感谢您提出并尝试重现该问题。 Texture0 是我的精灵表,其中包括我的游戏的图块。我绑定 spritesheet 并每帧渲染约 200 个图块。我的颜色中的 alpha 是我的错误,我将其删除。我将您的代码编辑为与我的完全相同,并且运行完美,这让我相信这不是着色器问题。你想让我发布任何其他代码吗?非常感谢您迄今为止的帮助!编辑:禁用纹理也不能解决问题。
-
Err 是的,如果您可以添加所有用于绘图的 opengl 代码,我认为这可能是有意义的。 mcve 将是完美的,但只是相关的 opengl 代码已经有很大帮助
-
@Zouch 感谢您对这个问题感兴趣。我很高兴地说我设法解决了这个问题。我犯了一个可怕的错误——我画了两次灯而不是一次。无论如何,谢谢你的时间:)