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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

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.