How to Design Flat Shading Graphics using p5.js ?

Flat shading is a lighting technique used in 3D computer graphics to shade each polygon of an object based on the angle between the polygon’s surface normal and the direction of the light source, their respective colors and the intensity of the light source.

  • Color is computed once for each face of the polygon.
  • All pixels in a polygon are set to the same color
  • Works for objects made of flat faces.

Example:

Javascript




// Store the angle value in a variable
var angle = 0.1;
  
// Set the canvas size
function setup() {
    createCanvas(500, 500, WEBGL);
}
  
// Function to draw the Flat 
// Shading Graphics
function draw() {
  
    // Create a directional light with 
    // specified color and direction
    directionalLight(25, 250, 50, 101, 0, 0);
  
    // Set the background color
    background(50, 50, 200);
  
    // Set the rotation angle
    rotateX(angle);
    rotateY(angle * 0.3);
    rotateZ(angle * 1.2);
  
    // Set the fill color
    fill(0, 0, 205);
      
    noStroke();
    ambientMaterial(300);
    cone(100);
    angle += 0.03;
}


Output: