The provided solution in the previous section to add lights to our scene is not a scalable solution because if we wanted to add four or more lights, we would have had to add more uniforms to our shader. WebGL limits the amount of storage for uniforms. If we exceed the limit, we would get a compile-time or a runtime error. You can query the number of allowed uniforms using the following functions:
gl.getParameter(gl.MAX_VERTEX_UNIFORM_VECTORS); gl.getParameter(gl. MAX_FRAGMENT_UNIFORM_VECTORS);
Also, the ambient color from all lights need not be calculated in the shaders as the ambient component is independent of distance and orientation of the object. We can sum the ambient color from all lights and pass the sum to the shader. Even if we use a single ambient color uniform in our shader for all lights, we would need two to three uniforms (position/direction, diffuseColor
, specularColor
) per light.