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


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

p5.j​​s中的lights()函數用於設置場景中的默認環境光和定向光。使用的默認環境光是ambientLight(128、128、128),定向光是directionalLight(128、128、128、0、0,-1)。此函數可用於在場景中快速添加默認燈光。

必須在代碼的draw()函數中使用lights()函數,以保持場景中的持久性。

用法:



lights()

參數:該函數不接受任何參數。

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

範例1:

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

輸出:
lights-sphere

範例2:

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

輸出:
lights-box

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

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

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




相關用法


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