Skip to content
Related Articles

Related Articles

p5.js | noLights() Function

Improve Article
Save Article
  • Last Updated : 13 Jan, 2022
Improve Article
Save Article

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: 
 

javascript




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: 
 

toggle-noLights

Example 2: 
 

javascript




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: 
 

toggle-two-ambient-noLights

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


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!