Just to complete. To make the lighting work correctly on P5.Geometric polygons, you need to keep track of the rotation. When passed 180 degrees, the z value of the light functions (poinLight() as well) takes/needs the same sign as basic 3D shapes.
let m;
let rotX = 0;
let rotY = 0;
function setup() {
createCanvas(400, 300, WEBGL);
background(0);
let v0 = new p5.Vector(-30, -50, 0);
let v1 = new p5.Vector(-50, 50, 0);
let v2 = new p5.Vector(50, 50, 0);
let v3 = new p5.Vector(30, -50, 0);
m = createQuadrilateral(v0, v1, v2, v3);
stroke(180, 0, 0);
fill(255, 0, 0);
rotateX(rotX);
rotateY(-rotY);
}
function draw() {
background(0);
directionalLight(255, 255, 255, 0, 0, -1);
//pointLight(255, 255, 255, 0, 0, 200);
rotateX(rotX);
rotateY(-rotY);
push();
translate(75, 0, 0);
box(50);
pop();
push();
if(rotX > -PI) directionalLight(255, 255, 255, 0, 0, 1);
//if(rotX > -PI) pointLight(255, 255, 255, 0, 0, -200);
translate(-75, 0, 0);
model(m);
pop();
}
function createQuadrilateral(v0, v1, v2, v3) {
return new p5.Geometry(1, 1, function createGeometry() {
this.vertices.push(v0, v1, v2, v3);
this.faces.push([0, 1, 2], [0, 2, 3]);
let n1 = p5.Vector.sub(v1, v0).cross(p5.Vector.sub(v2, v0));
let n2 = p5.Vector.sub(v2, v3).cross(p5.Vector.sub(v3, v0));
this.vertexNormals.push(n1, n1, n1, n2, n2, n2);
});
}
function mouseDragged() {
rotY -= (mouseX - pmouseX) * 0.01;
rotX -= (mouseY - pmouseY) * 0.01;
}