I have a lighting problem in which some triangles seem to exhibit poor shading artifacts in which the shading isn't smooth across the overall surface of the entire polygon (a wall, for instance). That is, each triangle that makes up the polygon seems to be shading slightly darker or lighter than it's neighbor.
I am trying to achieve a simple directional lighting. Here is what it looks like:
Here is what the vertex shader code:
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition.xyz, 1.0);
vNormal = vec3(uNMatrix * vec4(aVertexNormal.xyz, 1.0));
vColor = aVertexMaterialColor;
And here is the fragment shader code:
vec3 light = normalize(vec3( 0.5, 0.2, 1.0));
float amount = max(dot(vNormal, light), 0.0);
vec4 finalColor = vColor;
finalColor.rgb *= amount;
The problem is that the wall with the windows exhibits various banding where individual triangles appear darker than others. I can't control the geometry I'm getting, but I do believe the triangles are defined ok, as are the normals.
uPMatrix is the projection matrix, uMVMatrix is the model view matrix, and uNMatrix is the normal matrix. Here is how the normal matrix is created (gl-matrix):
var mvMatrix = this.getModelViewMatrix();
var nMatrix = this.nMatrix;
mat4.copy(nMatrix, mvMatrix);
mat4.invert(nMatrix, nMatrix);
mat4.transpose(nMatrix, nMatrix);
Any ideas what I might be doing wrong, or how I can make the entire wall smoothly shaded?