开发者

GLSL shaders and WebGL problem

开发者 https://www.devze.com 2023-02-09 20:30 出处:网络
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 loc

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 la开发者_运维知识库rger 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消