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:



filter_none

edit
close

play_arrow

link
brightness_4
code

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 checbox 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);
}

chevron_right


Output:
toggle-noLights

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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 checbox 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);
}

chevron_right


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

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.