當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


p5.js lightFalloff()用法及代碼示例

p5.j​​s中的lightFalloff()函數用於設置場景中點光源的衰減。光衰減意味著隨著物體距點光源的距離的減少,照明度降低。它僅影響在其後創建的元素。以下公式用於計算衰減:

falloff = 1 / (CONSTANT + d * LINEAR + ( d * d ) * QUADRATIC)

其中d是從光照位置到頂點位置的距離。該函數的默認值為lightFalloff(1.0,0.0,0.0)。

用法:



lightFalloff( constant, linear, quadratic )

參數:此函數接受上述和以下所述的三個參數:

  • constant:它是一個數字,表示衰減方程中的常數。
  • linear:它是一個數字,表示衰減方程中的線性值。
  • quadratic:它是一個數字,表示衰減方程中的二次值。

以下示例說明了p5.js中的lightFalloff()函數:

例:

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

輸出:
sliders-falloff

在線編輯: https://editor.p5js.org/

環境設置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

參考: https://p5js.org/reference/#/p5/lightFalloff




相關用法


注:本文由純淨天空篩選整理自sayantanm19大神的英文原創作品 p5.js | lightFalloff() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。