GLSL 着色器和 WebGL 问题

发布于 2024-10-16 17:39:56 字数 1322 浏览 7 评论 0原文

我创建了一个在 Firefox 中完美运行的着色器,但在 Chrome 中,片段和顶点着色器无法链接。它们编译得很好,但在链接部分出了问题。我已将问题定位在以下代码中:

        else if (uLightType[i] == 1) { //point light

        NdotL = dot(n, normalize(lightDir[i])); 

        if (NdotL > 0.0) {
            distance = length(lightDir[i]);
            att = (1.0 / (uLightAttenuation[i] * distance * distance));
            color += vec3(uLightColor[i]  * NdotL * uLightIntensity[i] * att);                  
        }
}

这段小代码计算从点光源反射的漫反射颜色。它是一个更大的 for 循环的一部分。正如这里所示,它根本不会链接,但如果我从计算 att 中删除 uLightAttenuation,就像这样:

att = (1.0 / (distance * distance));    

它工作得很好。如果我用任何其他制服(例如 uLightIntensity)替换它,

att = (1.0 / (uLightIntensity[i] * distance * distance));

它又将不起作用。如果我用一个简单的常量值/浮点变量替换它,奇怪的是它会编译。更奇怪的是,如果我从计算颜色中删除 att,但将制服保持在当前位置,它运行得很好:

att = (1.0 / (uLightAttenuation[i] * distance * distance));
color += vec3(uLightColor[i]  * NdotL * uLightIntensity[i]);

制服是一个浮点值,即使这是类型转换的问题,它也会失败在编译时,而不是链接时。
这是完整的着色器,也许我错过了代码中其他地方的东西。

片段着色器
顶点着色器

I have created a shader that works perfectly in Firefox, but in Chrome the fragment and vertex shader cannot be linked. They compile just fine, but at the linking part something goes wrong. I have localized the problem at the fallowing bit of code :

        else if (uLightType[i] == 1) { //point light

        NdotL = dot(n, normalize(lightDir[i])); 

        if (NdotL > 0.0) {
            distance = length(lightDir[i]);
            att = (1.0 / (uLightAttenuation[i] * distance * distance));
            color += vec3(uLightColor[i]  * NdotL * uLightIntensity[i] * att);                  
        }
}

This small piece of code calculates the diffuse color reflected from a point light. It's part of a larger for loop. As it is shown here it won't link at all, but if I remove uLightAttenuation from calculating att, like so :

att = (1.0 / (distance * distance));    

it works just fine. If I replace it with any other uniform, say uLightIntensity,

att = (1.0 / (uLightIntensity[i] * distance * distance));

again it won't work. If I replace it with a simple constant value / float variabile, strangely enough it compiles. And what is even more strange is, if I remove att from calculating color, but keep the uniform at it's current position, it runs just fine:

att = (1.0 / (uLightAttenuation[i] * distance * distance));
color += vec3(uLightColor[i]  * NdotL * uLightIntensity[i]);

The uniform is a float value, and even if it were a problem with type casting it should fail at compilation, not linking.
Here are the complete shaders, maybe I missed something elsewhere in the code.

Fragment Shader
Vertex Shader

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

友谊不毕业 2024-10-23 17:39:56

我已经成功地让它工作了,结果发现我有两个问题。一种是计算att时除以0。它可以让我在浮动制服上划分一些东西,所以我将 uLightAttenuation 和 uLightIntensity 组合成一个 vec2 制服,之后该部分起作用了。其次,在计算颜色时,我必须单独引用每个组件(颜色[0]、颜色[1]等...)并且仅使用浮点变量而不是向量。之后它在 chrome 中就可以正常工作了。

I have managed to make it to work, it turns out I had 2 problems. One is with division by 0 when calculating att. It would let me divide something over a float uniform, so I combined uLightAttenuation and uLightIntensity into a single vec2 uniform, after that that part worked. Secondly, when calculating color I had to reference every component individually (color[0], color[1] etc...) and work only with float variables and not vectors. After that it worked correctly in chrome to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文