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


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

p5.j​​s中的pointLight()函數用於創建具有指定顏色和場景中特定位置的點光源。一個場景一次最多可以有5個活動點光源。

用法:

pointLight( v1, v2, v3, x, y, z )

OR



pointLight( v1, v2, v3, position )

OR

pointLight( color, x, y, z )

OR

pointLight( color, position )

參數:該函數接受上述和以下所述的八個參數:

  • v1:它是一個數字,用於確定相對於當前顏色範圍的紅色或色調值。
  • v2:它是一個數字,用於確定相對於當前顏色範圍的綠色或飽和度值。
  • v3:它是一個數字,用於確定相對於當前顏色範圍的藍色或亮度值。
  • x:它是確定燈光x軸位置的數字。
  • y:它是確定燈光的y軸位置的數字。
  • z:它是確定燈光的z軸位置的數字。
  • position:它是p5.Vector,它定義了燈光的位置。
  • color:它是一個顏色字符串或p5.Color,它定義了點光源的顏色。

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

範例1:

let newFont; 
let directionalLightEnable = false; 
  
function preload() { 
  newFont = loadFont('fonts/Montserrat.otf'); 
} 
  
function setup() { 
  createCanvas(600, 300, WEBGL); 
  textFont(newFont, 16); 
  
  redSlider = createSlider(0, 255, 128, 1); 
  redSlider.position(20, 50); 
  
  greenSlider = createSlider(0, 255, 128, 1); 
  greenSlider.position(20, 80); 
  
  blueSlider = createSlider(0, 255, 128, 1); 
  blueSlider.position(20, 110); 
} 
  
function draw() { 
  background('green'); 
  text("Move the sliders to change the point light's red, "+ 
                        "green and blue values", -285, -125); 
  noStroke(); 
  
  redValue = redSlider.value(); 
  greenValue = greenSlider.value(); 
  blueValue = blueSlider.value(); 
  
  // Create a point light with the selected light color 
  pointLight(redValue, greenValue, blueValue, 100, 0, 150); 
  
  ambientMaterial(255); 
  
  // Create the sphere on which the point light will work 
  sphere(100); 
}

輸出:
pointLight-slider-color

範例2:

let newFont; 
let directionalLightEnable = false; 
  
function preload() { 
  newFont = loadFont('fonts/Montserrat.otf'); 
} 
  
function setup() { 
  createCanvas(600, 300, WEBGL); 
  textFont(newFont, 16); 
  
  xPosSlider = createSlider(-150, 150, 100, 1); 
  xPosSlider.position(20, 50); 
  
  yPosSlider = createSlider(-300, 300, 0, 1); 
  yPosSlider.position(20, 80); 
  
  zPosSlider = createSlider(0, 250, 150, 1); 
  zPosSlider.position(20, 110); 
  
} 
  
function draw() { 
  background('green'); 
  text("Change the sliders to move the point light position "+ 
                          "along x, y and z axis", -285, -125); 
  noStroke(); 
  
  xPositionValue = xPosSlider.value(); 
  yPositionValue = yPosSlider.value(); 
  zPositionValue = zPosSlider.value(); 
  
  // Create a point light at the selected location 
  pointLight(255, 0, 0, xPositionValue, yPositionValue, zPositionValue); 
  
  // Create a sphere to show the demonstrate 
  // of the point light location 
  translate(xPositionValue, yPositionValue, zPositionValue); 
  sphere(10); 
  translate(-xPositionValue, -yPositionValue, -zPositionValue); 
  
  specularMaterial(255); 
  
  // Create the sphere on which the point light will work 
  sphere(100); 
}

輸出:
pointLight-slider-position

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

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

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




相關用法


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