I'm trying to make a simple WebGL2 scene with low-poly terrain. Everything is working as expected when I let the shader interpolate between the three vertex colors. But as soon as I add the flat
type the whole thing breaks and every triangle starts "flashing".
Vertex Shader
#version 300 es
precision mediump float;
in vec3 in_vertexPosition;
in vec3 in_color;
uniform mat4 u_projectionMatrix;
uniform mat4 u_viewMatrix;
flat out vec3 pass_fragColor;
void main() {
gl_Position = u_projectionMatrix * u_viewMatrix * vec4(in_vertexPosition, 1.0);
pass_fragColor = in_color;
}
Fragment Shader
#version 300 es
precision mediump float;
flat in vec3 pass_fragColor;
out vec4 out_fragColor;
void main() {
out_fragColor = vec4(pass_fragColor, 1.0);
}
Vertices, indices and colors:
const vertices = [
0, 0, 0, // bottom left
1, 0, 0, // bottom right
1, 1, 0, // top right
0, 1, 0 // top left
];
const colors = [
0.3, 0.3, 0.8, // bottom left
0.3, 0.3, 0.6, // bottom right
0.3, 0.3, 0.4, // top right
0.3, 0.3, 0.2 // top left
];
const indices = [
0, 1, 3, // bl, br, tl
3, 1, 2 // tl, br, tr
];
I've tried different ways using indices and other types of vertices but nothing seems to work. Using VBOs for better structure.
I do notice that when all the three vertices in the triangle has the same color the flashing obviously stops. What I've read is that the flat
type makes all three vertices in a triangle use the color of the provoking vertex (last vertex in triangle by default). Unfortunately that makes the triangles "flash" and I do not understand why.
Using my NVIDIA GTX 1060 graphics card.
GIF showing the "flashing": https://gyazo.com/7d3ba42afe1ba1458cd2820729490e47