Using shaders to apply color in WebGL

Having created a square plane in the previous demonstration, the next obvious step is to add a splash of color to it. We can do this by revising the shaders.

Applying color to the vertices

In WebGL objects are built using sets of vertices, each of which has a position and a color. By default, all other pixels' colors (and all its other attributes, including position) are computed using interpolation, automatically creating smooth gradients. Previously, our vertex shader didn't apply any specific colors to the vertices. Between this and the fragment shader assigning the fixed color of white to each pixel, the entire square was rendered as solid white.

Let's say we want to render a gradient in which each corner of the square is a different color: red, blue, green, and white. The first thing to do is to establish these colors for the four vertices. To do this, we first need to create an array of vertex colors, then store it into a WebGL buffer.

Note: Add the following function to your init-buffers.js module:

function initColorBuffer(gl) {
  const colors = [
    1.0, // white
    1.0, // red
    1.0, // green
    1.0, // blue

  const colorBuffer = gl.createBuffer();
  gl.bindBuffer(gl.ARRAY_BUFFER, colorBuffer);
  gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);

  return colorBuffer;

This code starts by creating a JavaScript array containing four 4-value vectors, one for each vertex color. Then a new WebGL buffer is allocated to store these colors, and the array is converted into floats and stored into the buffer.

Of course, we also need to call this new function from initBuffers(), and return the new buffer it creates.

Note: At the end of your initBuffers() function, add the following code, replacing the existing return statement:

const colorBuffer = initColorBuffer(gl);

return {
  position: positionBuffer,
  color: colorBuffer,

To use these colors, the vertex shader needs to be updated to pull the appropriate color from the color buffer.

Note: Update the vsSource declaration in your main() function like this:

// Vertex shader program

const vsSource = `
    attribute vec4 aVertexPosition;
    attribute vec4 aVertexColor;

    uniform mat4 uModelViewMatrix;
    uniform mat4 uProjectionMatrix;

    varying lowp vec4 vColor;

    void main(void) {
      gl_Position = uProjectionMatrix * uModelViewMatrix * aVertexPosition;
      vColor = aVertexColor;

The key difference here is that for each vertex, we pass its color using a varying to the fragment shader.

Coloring the fragments

In order to pick up the interpolated color for each pixel, we need to change the fragment shader to fetch the value from the vColor varying.

Note: Update the fsSource declaration in your main() function like this:

// Fragment shader program

const fsSource = `
    varying lowp vec4 vColor;

    void main(void) {
      gl_FragColor = vColor;

Each fragment receives the interpolated color based on its position relative to the vertex positions instead of a fixed value.

Drawing using the colors

Next, you need to add code to look up the attribute location for the colors and set up that attribute for the shader program.

Note: Update the programInfo declaration in your main() function like this:

// Collect all the info needed to use the shader program.
// Look up which attributes our shader program is using
// for aVertexPosition, aVertexColor and also
// look up uniform locations.
const programInfo = {
  program: shaderProgram,
  attribLocations: {
    vertexPosition: gl.getAttribLocation(shaderProgram, "aVertexPosition"),
    vertexColor: gl.getAttribLocation(shaderProgram, "aVertexColor"),
  uniformLocations: {
    projectionMatrix: gl.getUniformLocation(shaderProgram, "uProjectionMatrix"),
    modelViewMatrix: gl.getUniformLocation(shaderProgram, "uModelViewMatrix"),

Next, drawScene() needs to use these colors when drawing the square.

Note: Add the following function to your draw-scene.js module:

// Tell WebGL how to pull out the colors from the color buffer
// into the vertexColor attribute.
function setColorAttribute(gl, buffers, programInfo) {
  const numComponents = 4;
  const type = gl.FLOAT;
  const normalize = false;
  const stride = 0;
  const offset = 0;
  gl.bindBuffer(gl.ARRAY_BUFFER, buffers.color);

Note: Call the setColorAttribute() function from drawScene(), right before the gl.useProgram() call:

setColorAttribute(gl, buffers, programInfo);

The result should now look like this:

View the complete code | Open this demo on a new page