Saturday, November 16, 2024
Google search engine
HomeLanguagesJavascriptp5.js emissiveMaterial() Function

p5.js emissiveMaterial() Function

The emissiveMaterial() function in p5.js is used to create a emissive material for a geometry with the given color. The emissivity of an object gives it the appearance that the object is glowing. An emissive material would still display in full strength even if no light exists in the scene, unlike ambient or specular materials.

Syntax:

emissiveMaterial( v1, [v2], [v3], [a] )

OR

emissiveMaterial( 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.
  • a: It is a number which denotes the opacity of the material. It is an optional parameter.
  • color: It is a p5.Color or color string which defines the color of the material.

The example below illustrates the emissiveMaterial() function in p5.js:

Example: The example below shows that the emissive material always shows regardless of the ambient light present.




let newFont;
let hasAmbientLight = true;
let currentEmissiveColor = "red";
  
function preload() {
  newFont = loadFont('fonts/Montserrat.otf');
}
  
function setup() {
  createCanvas(600, 300, WEBGL);
  textFont(newFont, 16);
  
  // Create checkbox to enable/disable ambient light
  redCheckbox = createCheckbox('Enable Ambient Light', true);
  redCheckbox.position(30, 250);
  redCheckbox.changed(() => hasAmbientLight = !hasAmbientLight);
  
  // Create a selector for selecting the directional light color
  lightColorSel = createSelect();
  lightColorSel.position(30, 80);
  lightColorSel.option('red');
  lightColorSel.option('green');
  lightColorSel.option('blue');
  lightColorSel.changed(() => {
    currentEmissiveColor = lightColorSel.value();
  });
}
  
function draw() {
  background('white');
  fill('black');
  
  text("Select an option below to set the emissive material color",
                                                        -285, -125);
  text("Select emissive material color", -285, -100);
  noStroke();
  
  // Enable ambient light if the checkbox is enabled
  if (hasAmbientLight)
    ambientLight(0, 0, 255);
  
  // Draw sphere which uses emissive material
  emissiveMaterial(currentEmissiveColor);
  translate(-100, 0, 0);
  sphere(50);
  translate(100, 0, 0);
  
  // Draw sphere which uses ambient material
  ambientMaterial(255);
  translate(100, 0, 0);
  sphere(50);
  translate(-100, 0, 0);
}


Output:
emissive-toggle

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

RELATED ARTICLES

Most Popular

Recent Comments