p5.js | specularMaterial() Function

The specularMaterial() function in p5.js is used to create a specular material for a geometry with the given color. The specular material is a shiny reflective material. It also defines the color that the object would reflect. If the specular material is set to reflect only red, then it would reflect only the red color, else the object would not reflect any light of any other color.

Syntax:

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

OR

specularMaterial( color )

Parameters: This function accept four parameters as mentioned above and described below:

  • v1: It is a number which determines the gray value, or the red or hue value relative to the current color range.
  • v2: It is a number which determines the green or saturation value relative to the current color range. It is an optional parameter.
  • v3: It is a number which determines the blue or brightness value relative to the current color range. It is an optional parameter.
  • color: It is a p5.Color or color string which defines the color of the specular material.

Below example illustrates the directionalLight() function in p5.js:



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:
specularmat-options

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/specularMaterial

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.