p5.js | noLights()函数
p5.js 中的noLights()函数用于移除草图中的所有灯光,以便在此函数之后渲染材质。在此之后对灯光功能的任何调用都将再次重新启用草图中的灯光。
句法:
noLights()
参数:此函数不接受任何参数。
以下示例说明了 p5.js 中的noLights()函数:
示例 1:
javascript
let newFont;
let nolightsEnable = false;
function preload() {
newFont = loadFont('fonts/Montserrat.otf');
}
function setup() {
createCanvas(600, 300, WEBGL);
textFont(newFont, 18);
nolightsEnableCheck = createCheckbox(
"Enable noLights", false);
nolightsEnableCheck.position(20, 60);
// Toggle default light
nolightsEnableCheck.changed(() => {
nolightsEnable = !nolightsEnable;
});
}
function draw() {
background("green");
text("Click on the checkbox to toggle the "
+ "noLights() function.", -285, -125);
noStroke();
// Ambient light with red color
ambientLight('red');
// First sphere in the sketch
translate(-100, 0, 0);
sphere(50);
translate(100, 0, 0);
// If checkbox is enabled
if (nolightsEnable) {
// Disable all lights after this
noLights();
text("Lights disabled for second"
+ " sphere", -285, 125);
}
else {
text("Lights enabled for second"
+ " sphere", -285, 125);
}
// Second sphere in the sketch
translate(100, 0, 0);
sphere(50);
}
javascript
let newFont;
let nolightsEnable = false;
function preload() {
newFont = loadFont('fonts/Montserrat.otf');
}
function setup() {
createCanvas(600, 300, WEBGL);
textFont(newFont, 18);
nolightsEnableCheck = createCheckbox(
"Enable noLights", false);
nolightsEnableCheck.position(20, 60);
// Toggle default light
nolightsEnableCheck.changed(() => {
nolightsEnable = !nolightsEnable;
});
}
function draw() {
background("green");
text("Click on the checkbox to toggle the"
+ " noLights() function.", -285, -125);
noStroke();
// Ambient light with red color
ambientLight('red');
// First sphere in the sketch
translate(-100, 0, 0);
sphere(50);
translate(100, 0, 0);
// If checkbox is enabled
if (nolightsEnable) {
// Disable all lights after this
noLights();
text("Red ambient light disabled for"
+ " second sphere", -285, 125);
}
else {
text("Red ambient light enabled for"
+ " second sphere", -285, 125);
}
ambientLight('blue');
// Second sphere in the sketch
translate(100, 0, 0);
sphere(50);
}
输出:
示例 2:
javascript
let newFont;
let nolightsEnable = false;
function preload() {
newFont = loadFont('fonts/Montserrat.otf');
}
function setup() {
createCanvas(600, 300, WEBGL);
textFont(newFont, 18);
nolightsEnableCheck = createCheckbox(
"Enable noLights", false);
nolightsEnableCheck.position(20, 60);
// Toggle default light
nolightsEnableCheck.changed(() => {
nolightsEnable = !nolightsEnable;
});
}
function draw() {
background("green");
text("Click on the checkbox to toggle the"
+ " noLights() function.", -285, -125);
noStroke();
// Ambient light with red color
ambientLight('red');
// First sphere in the sketch
translate(-100, 0, 0);
sphere(50);
translate(100, 0, 0);
// If checkbox is enabled
if (nolightsEnable) {
// Disable all lights after this
noLights();
text("Red ambient light disabled for"
+ " second sphere", -285, 125);
}
else {
text("Red ambient light enabled for"
+ " second sphere", -285, 125);
}
ambientLight('blue');
// Second sphere in the sketch
translate(100, 0, 0);
sphere(50);
}
输出:
在线编辑器: https://editor.p5js.org/
环境设置: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/
参考: https://p5js.org/reference/#/p5/noLights