The ambientMaterial() function in p5.js is used to create a ambient material for a geometry with the given color. The ambient material is used to define the color that the object reflects under any light. If the ambient material is set to reflect only red and the lights reflect only blue, then the object would not reflect any light.
Syntax:
ambientMaterial( v1, [v2], [v3] )
OR
ambientMaterial( 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 ambient material.
The examples below illustrates the ambientMaterial() function in p5.js:
Example 1:
let newFont; let currentLightColor = "red"; let currentAmbientColor = "red"; function preload() { newFont = loadFont('fonts/Montserrat.otf'); } function setup() { createCanvas(600, 300, WEBGL); textFont(newFont, 16); // Create a selector for selecting // the ambient material color materialColorSel = createSelect(); materialColorSel.position(30, 70); materialColorSel.option('red'); materialColorSel.option('green'); materialColorSel.option('blue'); materialColorSel.changed(() => { currentLightColor = materialColorSel.value(); }); // Create a selector for selecting // the directional light color lightColorSel = createSelect(); lightColorSel.position(30, 120); lightColorSel.option('red'); lightColorSel.option('green'); lightColorSel.option('blue'); lightColorSel.changed(() => { currentAmbientColor = lightColorSel.value(); }); } function draw() { background('white'); fill('black'); text("Select an option below to set the light " + "and ambient material color", -285, -125); text("Select directional light color", -285, -100); text("Select ambient material color", -285, -50); shininess(10); noStroke(); // Set the ambient material to the selected color ambientMaterial(currentAmbientColor); // 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); } |
Output:

Example 2:
let newFont; let currentLightColor = "yellow"; let hasRed = false; let hasGreen = false; let hasBlue = false; function preload() { newFont = loadFont('fonts/Montserrat.otf'); } function setup() { createCanvas(600, 300, WEBGL); textFont(newFont, 16); // Create 3 checkboxes for mixing 3 colors redCheckbox = createCheckbox('Red', false); redCheckbox.position(30, 70); redCheckbox.changed(() => hasRed = !hasRed); greenCheckbox = createCheckbox('Green', false); greenCheckbox.position(90, 70); greenCheckbox.changed(() => hasGreen = !hasGreen); blueCheckbox = createCheckbox('Blue', false); blueCheckbox.position(170, 70); blueCheckbox.changed(() => hasBlue = !hasBlue); // Create a selector for selecting // the directional light color lightColorSel = createSelect(); lightColorSel.position(30, 120); lightColorSel.option('yellow'); lightColorSel.option('magenta'); lightColorSel.option('cyan'); lightColorSel.changed(() => { currentLightColor = lightColorSel.value(); }); } function draw() { background('white'); fill('black'); text("Select an option below to set the light " + "and ambient material color", -285, -125); text("Select directional light color", -285, -100); text("Select ambient material color", -285, -50); shininess(10); noStroke(); // Define the color based on the checkboxes let currentAmbientColor = color(hasRed ? 255 : 0, hasGreen ? 255 : 0, hasBlue ? 255 : 0); // Set the ambient material to the defined color ambientMaterial(currentAmbientColor); // 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); } |
Output:

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/ambientMaterial
Recommended Posts:
- How to get the function name from within that function using JavaScript ?
- How to call a function that return another function in JavaScript ?
- How to get the function name inside a function in PHP ?
- How to Check a Function is a Generator Function or not using JavaScript ?
- PHP | imagecreatetruecolor() Function
- p5.js | year() function
- D3.js | d3.utcTuesdays() Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- D3.js | d3.map.set() Function
- D3.js easePolyOut Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- D3.js | d3.bisectLeft() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
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.


