Thursday, September 4, 2025
HomeLanguagesJavascriptHow to Design Flat Shading Graphics using p5.js ?

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:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6717 POSTS0 COMMENTS