p5.js | lights() Function

The lights() function in p5.js is used to set the default ambient and directional light in the scene. The default ambient light used is ambientLight(128, 128, 128) and the directional light is directionalLight(128, 128, 128, 0, 0, -1). This function can be used to quickly add default lights in the scene.

The lights() function has to be used in the draw() function of the code to remain persistent in the scene.

Syntax:

lights()

Parameters: This function does not accept any parameters.

Below examples illustrate the lights() function in p5.js:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

let newFont;
let lightsEnable = false;
  
function preload() {
  newFont = loadFont('fonts/Montserrat.otf');
}
  
function setup() {
  createCanvas(600, 300, WEBGL);
  textFont(newFont, 18);
  
  lightsEnableCheck = createCheckbox(
      "Enable Default Lights", false);
  lightsEnableCheck.position(20, 60);
  
  // Toggle default light
  lightsEnableCheck.changed(() => {
    lightsEnable = !lightsEnable;
  });
}
  
function draw() {
  background("green");
  text("Click on the checbox to toggle the"
     + " default lights.", -285, -125);
  noStroke();
  shininess(15);
  specularMaterial(250);
  
  if (lightsEnable) {
  
    // Enable the default lights
    lights();
  }
  
  sphere(80);
}

chevron_right


Output:
lights-sphere

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

let newFont;
let lightsEnable = false;
  
function preload() {
  newFont = loadFont('fonts/Montserrat.otf');
}
  
function setup() {
  createCanvas(600, 300, WEBGL);
  textFont(newFont, 18);
  
  lightsEnableCheck = createCheckbox(
       "Enable Default Lights", false);
  
  lightsEnableCheck.position(20, 60);
  
  // Toggle default light
  lightsEnableCheck.changed(() => {
    lightsEnable = !lightsEnable;
  });
}
  
function draw() {
  background("green");
  text("Click on the checbox to toggle the"
       + " default lights.", -285, -125);
  noStroke();
  shininess(15);
  specularMaterial(250);
  
  if (lightsEnable) {
  
    // Enable the default lights
    lights();
  }
  
  rotateX(millis() / 1000);
  rotateY(millis() / 1000);
  box(100);
}

chevron_right


Output:
lights-box

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/lights

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.