The lightFalloff() function in p5.js is used to set the falloff for point lights in the scene. Light falloff means the reduction of illumination with the distance of the object from the point light. It only affects elements that are created after it. The following equation is used to calculate the falloff:
falloff = 1 / (CONSTANT + d * LINEAR + ( d * d ) * QUADRATIC)
Where d is the distance from the light position to the vertex position. The default value of the function is lightFalloff(1.0, 0.0, 0.0).
Syntax:
lightFalloff( constant, linear, quadratic )
Parameters: This function accept three parameters as mentioned above and described below:
- constant: It is a number which denotes the constant value in the falloff equation.
- linear: It is a number which denotes the linear value in the falloff equation.
- quadratic: It is a number which denotes the quadratic value in the falloff equation.
Below example illustrates the lightFalloff() function in p5.js:
Example:
let newFont; function preload() { newFont = loadFont( 'fonts/Montserrat.otf' ); } function setup() { createCanvas(600, 300, WEBGL); textFont(newFont, 15); constantSlider = createSlider(0.1, 1, 0.1, 0.1); constantSlider.position(20, 50); linearSlider = createSlider(0, 0.01, 0, 0.0001); linearSlider.position(20, 80); quadraticSlider = createSlider(0, 0.0001, 0, 0.00001); quadraticSlider.position(20, 110); } function draw() { background( 'green' ); text( "Move the sliders to change the CONSTANT, LINEAR" + " and QUADRATIC values" , -285, -125); noStroke(); shininess(15); constantValue = constantSlider.value(); linearValue = linearSlider.value(); quadraticValue = quadraticSlider.value(); lightFalloff(constantValue, linearValue, quadraticValue); pointLight(0, 128, 255, -width / 2, -height / 2, 250); specularMaterial(250); sphere(100); text( "falloff = 1 / (" + constantValue + " + d * " + linearValue + " + ( d * d ) * " + quadraticValue + " )" , -285, 125); } |
Output:
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/lightFalloff