Ah, I failed to notice that you are using a positively ancient version of p5.js. I highly recommend you update to version 1.4.0 which is the version where the normal()
function was added. Unfortunately in subsequent versions I’m having trouble getting textures to render in your app which is odd because the work in trivial examples.
Here’s another version of your code with custom geometry implemented: p5.js Web Editor
// Added to your Box function
// note: texturing for geometry disregards textureMode
const textureSize = 1.0;
box.geometry =
new p5.Geometry(
// detailX and detailY are not used in this implementation
1, 1,
function createGeometry() {
// helper function for making a square plane of triangles
// The last arg is a callback function to take x, y arguments and
// turn them into a 3d vector. Thus allowing the plan to be changed
// to a different orientation.
this.makePlane = function(size, detail, mk3d) {
const fSize = size / detail;
const txSize = textureSize / detail;
for (let i = 0; i < detail; i++) {
for (let j = 0; j < detail; j++) {
const [t, l, b, r] = [
fSize * i - size / 2,
fSize * j - size / 2,
fSize * (i + 1) - size / 2,
fSize * (j + 1) - size / 2,
];
const [txt, txl, txb, txr] = [
txSize * i,
txSize * j,
txSize * (i + 1),
txSize * (j + 1),
];
let vix = this.vertices.length;
// Draw ◸
this.vertices.push(mk3d(l, t));
this.vertices.push(mk3d(r, t));
this.vertices.push(mk3d(l, b));
this.uvs.push([txl, txt]);
this.uvs.push([txr, txt]);
this.uvs.push([txl, txb]);
this.faces.push([vix, vix + 1, vix + 2]);
// Draw ◿
vix = this.vertices.length;
this.vertices.push(mk3d(l, b));
this.vertices.push(mk3d(r, t));
this.vertices.push(mk3d(r, b));
this.uvs.push([txl, txb]);
this.uvs.push([txr, txt]);
this.uvs.push([txr, txb]);
this.faces.push([vix, vix + 1, vix + 2]);
}
}
}
// Note: it is important to get the signs right so that the winding
// direction will be correct. Triangle vertices need to be drawn in
// clockwise order for computeNormals to work.
// front
this.makePlane(size, 10, (x, y) => createVector(x, y, size * 1/2));
// right
this.makePlane(size, 10, (x, y) => createVector(size * 1/2, y, -1 * x));
// back
this.makePlane(size, 10, (x, y) => createVector(-1 * x, y, size * -1/2));
// left
this.makePlane(size, 10, (x, y) => createVector(size * -1/2, y, x));
// top
this.makePlane(size, 10, (x, y) => createVector(x, size * -1/2, y));
// bottom
this.makePlane(size, 10, (x, y) => createVector(x, size * 1/2, -1 * y));
this.computeNormals();
this.gid = `custom-box-${size}`;
}
);
box.render = function(tex, tintcol) {
texture(tex);
tint(tintcol);
model(this.geometry);
};