Open In App

p5.js noLights() Function

The noLights() function in p5.js is used to remove all lights in the sketch for the materials that would be rendered after this function. Any calls to light functions made after this would again re-enable the lights in the sketch.
Syntax: 
 

noLights()

Parameters: This function does not accept any parameters.
Below examples illustrates the noLights() function in p5.js:
Example 1: 
 






let newFont;
let nolightsEnable = false;
  
function preload() {
  newFont = loadFont('fonts/Montserrat.otf');
}
  
function setup() {
  createCanvas(600, 300, WEBGL);
  textFont(newFont, 18);
  
  nolightsEnableCheck = createCheckbox(
        "Enable noLights", false);
  
  nolightsEnableCheck.position(20, 60);
  
  // Toggle default light
  nolightsEnableCheck.changed(() => {
    nolightsEnable = !nolightsEnable;
  });
}
  
function draw() {
  background("green");
  text("Click on the checkbox to toggle the "
       + "noLights() function.", -285, -125);
  noStroke();
  
  // Ambient light with red color
  ambientLight('red');
  
  // First sphere in the sketch
  translate(-100, 0, 0);
  sphere(50);
  
  translate(100, 0, 0);
  
  // If checkbox is enabled
  if (nolightsEnable) {
  
    // Disable all lights after this
    noLights();
  
    text("Lights disabled for second"
          + " sphere", -285, 125);
  }
  else {
    text("Lights enabled for second"
          + " sphere", -285, 125);
  }
  
  // Second sphere in the sketch
  translate(100, 0, 0);
  sphere(50);
}

Output: 
 



Example 2: 
 




let newFont;
let nolightsEnable = false;
  
function preload() {
  newFont = loadFont('fonts/Montserrat.otf');
}
  
function setup() {
  createCanvas(600, 300, WEBGL);
  textFont(newFont, 18);
  
  nolightsEnableCheck = createCheckbox(
           "Enable noLights", false);
  
  nolightsEnableCheck.position(20, 60);
  
  // Toggle default light
  nolightsEnableCheck.changed(() => {
    nolightsEnable = !nolightsEnable;
  });
}
  
function draw() {
  background("green");
  text("Click on the checkbox to toggle the"
    + " noLights() function.", -285, -125);
  noStroke();
  
  // Ambient light with red color
  ambientLight('red');
  
  // First sphere in the sketch
  translate(-100, 0, 0);
  sphere(50);
  
  translate(100, 0, 0);
  
  // If checkbox is enabled
  if (nolightsEnable) {
  
    // Disable all lights after this
    noLights();
  
    text("Red ambient light disabled for"
        + " second sphere", -285, 125);
  }
  else {
    text("Red ambient light enabled for"
        + " second sphere", -285, 125);
  }
  
  ambientLight('blue');
  
  // Second sphere in the sketch
  translate(100, 0, 0);
  sphere(50);
}

Output: 
 

Online editor: https://editor.p5js.org/
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/amp/
Reference: https://p5js.org/reference/#/p5/noLights
 


Article Tags :