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


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

p5.j​​s中的specularMaterial()函數用於為具有給定顏色的幾何圖形創建鏡麵材質。鏡麵反射材料是發亮的反射材料。它還定義了對象將反射的顏色。如果將鏡麵材質設置為僅反射紅色,則它將僅反射紅色,否則對象將不反射任何其他顏色的光。

用法:

specularMaterial( v1, [v2], [v3] )

OR



specularMaterial( color )

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

  • v1:它是一個數字,用於確定相對於當前顏色範圍的灰度值或紅色或色調值。
  • v2:它是一個數字,用於確定相對於當前顏色範圍的綠色或飽和度值。它是一個可選參數。
  • v3:它是一個數字,用於確定相對於當前顏色範圍的藍色或亮度值。它是一個可選參數。
  • color:它是一個p5.Color或顏色字符串,用於定義鏡麵材質的顏色。

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

例:

let newFont; 
let currentLightColor = "red"; 
let currentSpecularColor = "red"; 
  
function preload() { 
  newFont = loadFont('fonts/Montserrat.otf'); 
} 
  
function setup() { 
  createCanvas(600, 300, WEBGL); 
  textFont(newFont, 16); 
  
  materialColorSel = createSelect(); 
  materialColorSel.position(30, 70); 
  materialColorSel.option('red'); 
  materialColorSel.option('green'); 
  materialColorSel.option('blue'); 
  materialColorSel.changed(() => { 
    currentSpecularColor = materialColorSel.value(); 
  }); 
  
  lightColorSel = createSelect(); 
  lightColorSel.position(30, 120); 
  lightColorSel.option('red'); 
  lightColorSel.option('green'); 
  lightColorSel.option('blue'); 
  lightColorSel.changed(() => { 
    currentLightColor = lightColorSel.value(); 
  }); 
} 
  
function draw() { 
  background('white'); 
  fill('black'); 
  
  text("Select an option below to set the light"
     + " and specular material color", -285, -125); 
  text("Select directional light color", -285, -100); 
  text("Select specular material color", -285, -50); 
  shininess(10); 
  noStroke(); 
  
  // Set the specular material to the selected color 
  specularMaterial(currentSpecularColor); 
  
  // Set the directional to the selected color 
  directionalLight(color(currentLightColor),  
               height / 2, width / 2, -250); 
  
  // Draw the sphere 
  translate(100, 0, 0); 
  sphere(100); 
  translate(-100, 0, 0); 
}

輸出:
specularmat-options

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

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

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




相關用法


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